Monday, April 16, 2012

C Program without main() function

Use token paste opeator (##) to replace the code using macros. Internally has the main() but not directly visible.

#include<stdio.h>
#define decode(s,t,u,m,p,e,d) m##s##u##t
#define begin decode(a,n,i,m,a,t,e)
void begin()
{
     printf("hello");
     getch();
}

Thursday, October 20, 2011

Write to program to check whether a number is even or odd without using any operator

struct test
{ unsigned n:1;
};

main()
{
 int num;
 printf("Enter a number : "); scanf("%d",&num);
 test t;
 t.n=num;
 if(t.n)
  printf("%d is odd",num);
 else
  printf("%d is even",num);
 getch();
}

Sunday, October 16, 2011

What is the difference between rb+ and wb+?

rb+ is used to open a file in read/write mode if the file exists. If file does not exist, it returns NULL.

wb+ is used a open a file in write/read mode. If file exists, it overwrites all its previous contents and if not found creates new one.
How to open a file in C for read write operation in binary  mode?

Use built-in structure  FILE provided in stdio.h

FILE *fp=fopen("filename","rb");
if(fp==NULL)
  fp=fopen("filename","wb+");



Friday, October 7, 2011

Test your C Skills? Set A
1
  1. Why void is a primary data type even if, it cannot occupy any memory space?
  2. What is the meaning of returning int in int main()?
  3. Differentiate between '\n' and '\r'
  4. What we call format specifiers as format specifiers?
  5. What is real usage of getch()?
  6. What is the meaning and usage of following syntax of entry point
                void main(int argc, char *argv[], char *env[])
  1. What is the difference among const int *ptr, int const *ptr and int * const ptr?
  2. Can we get address of a function? If yes, how to use function pointers?
  3. What is the difference between call by address and call by reference?
  4. What is the use of suppression character?
  5. What is Stringize Operator?
  6. What is Characterized Operator?
  7. What is Token Paste Operator?
  8. What is meaning and purpose of __FILE__ and __LINE__?
  9. In C, five files are always open, name those files.
  10. Can we store data in bits rather than bytes? If yes, how?
  11. Can we compile part of the program code? If yes, how?
  12. How we can print a report on a printer?
  13. Can we use unions in place of structures in linked list?
  14. What is the difference between rb+ and wb+?
  15. How to write self destructive program?
  16. Can we create an array that can have different elements in different rows? If yes, how?
  17. What is static functions?
  18. What is the difference between NULL  and NUL?
  19. How to DOS Shell just like DOS Shell command in Turbo C using C Program?
How to run calculator, notepad and command prompt from a program

Solution

#include<stdlib.h>
void main()
{
    int choice;
    for(;;)
    {
        clrscr();
        printf(">>> Main Main <<<\n");
        printf("1 : DOS Shell\n");
        printf("2 : Calculator\n");
        printf("3 : Notepad\n");
        printf("4 : Exit\n\n");
        printf("Enter choice : ");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1: system("command");break;
            case 2: system("calc");break;
            case 3: system("notepad"); break;
            case 4: exit(0);
        }
    }
}

Wednesday, September 14, 2011

What is the output of following?

#include<stdio.h>
double i;
int main()
{
    (int)(float)(char) i;
    printf("%d",sizeof((int)(float)(char)i));
    getch();
}

Output
4

Reason : typecasting used