C

C Objective Questions And Answers
More interview questions and answers

What is output of following program when we compile and run it? void main() { int const * p=5; printf("%d",++(*p)); }

What is output of followingvoid main(){char ch;for(ch=0;ch<=127;ch )printf("%c %d \n", ch, ch);}

Is this code legal? int *ptr; ptr = (int *) 0x400;

What is output of following? main() { char s[ ]="man"; int i; for(i=0;s[ i ];i++) printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]); }

What is output of following?? main() { float f1 = 1.0; double f2 = 1.0; if(f1==f2) printf("I love U"); else printf("I hate U"); }

What is output of following main() { char a[4]="HELLO"; printf("%s",a); }

What is output of following? main() { char a[4]="HELL"; printf("%s",a); }

What is output of following? main() { int a=10,*j; void *k; j=k=&a; j++; k++; printf("\n %u %u ",j,k); }

What values are printed when we run following? main() { static int var = 5; printf("%d ",var--); if(var) main(); }

main() { int c[ ]={2.8,3.4,4,6.7,5}; int j,*p=c,*q=c; for(j=0;j<5;j++) { printf(" %d ",*c); ++q; } for(j=0;j<5;j++){ printf(" %d ",*p); ++p; } }

main() { extern int i; i=20; printf("%d",i); }

main() { int i=-1,j=-1,k=0,l=2,a; a=i++&&j++&&k++||l++; printf("%d %d %d %d %d",i,j,k,l,a); }

main() { int c=- -2; printf("c=%d",c); }

#define int char main() { int i=65; printf("sizeof(i)=%d",sizeof(i)); }

main() { int a=10; a=!a>14; Printf ("a=%d",a); }

#include main() { char s[]={'a','b','c','\n','c','\0'}; char *p,*str,*str1; p=&s[3]; str=p; str1=s; printf("%d",++*p + ++*str1-32); }

#include main() { int a[2][2][2] = {{10,2,3,4},{5,6,7,8}}; int *p,*q; p=&a[2][2][2]; *q=***a; printf("%d----%d",*p,*q); }

#include main() { struct xx { int x=3; char name[]="vivek"; }; struct xx *s; printf("%d",s->x); printf("%s",s->name); }

#include main() { struct xx { int x; struct yy { char s; struct xx *p; }; struct yy *q; }; }

main() { printf("\nab"); printf("\bsi"); printf("\rha"); }

main() { int i=5; printf("%d%d%d%d%d%d",i++,i--,++i,--i,i); }

#define square(x) x*x main() { int i; i = 125/square(5); printf("%d",i); }

#define square(x) x*x main() { int i; i = 125/square(5); printf("%d",i); }

main() { char *p="hey friends",*p1; p1=p; while(*p!='\0') ++*p++; printf("%s %s",p,p1); }

main() { char *p="hai friends",*p1; p1=p; while(*p!='\0') ++*p++; printf("%s %s",p,p1); }

#include #define i 10 main() { #define i 50 printf("%d",i); }

#define clrscr() 100 main() { clrscr(); printf("%d\n",clrscr()); }

main() { printf("%p",main); }

main() { clrscr(); } clrscr();

enum colors {BLACK,BLUE,GREEN} main() { printf("%d..%d..%d",BLACK,BLUE,GREEN); return(1); }

void main() { char far *farther,*farthest; printf("%d..%d",sizeof(farther),sizeof(farthest)); }

main() { int i=400,j=300; printf("%d..%d"); }

main() { char *p; p="java"; printf("%c\n",*&*p); }

main() { int i=1; while (i<=5) { printf("%d",i); if (i>2) goto here; i++; } } fun() { here: printf("PP"); }

main() { static char names[5][20]={"pascal","ada","cobol","fortran","perl"}; int i; char *t; t=names[3]; names[3]=names[4]; names[4]=t; for (i=0;i<=4;i++) printf("%s

void main() { int a=5; printf("%d",a++ + ++a); }

void main() { int i=5; printf("%d",i+++++i); }

#include main() { int i=1,j=2; switch(i) { case 1: printf("GOOD"); break; case j: printf("BAD"); break; } }

main() { int i; printf("%d",scanf("%d",&i)); // value 10 is given as input here }

#define f(g,g2) g##g2 main() { int var12=100; printf("%d",f(var,12)); }

main() { int i=0; for(;i++;printf("%d",i)) ; printf("%d",i); }

#include main() { char s[]={'a','b','c','\n','c','\0'}; char *p,*str,*str1; p=&s[3]; str=p; str1=s; printf("%d",++*p + ++*str1-32); }

#include main() { struct xx { int x=3; char name[]="hello"; }; struct xx *s=malloc(sizeof(struct xx)); printf("%d",s->x); printf("%s",s->name); }

#include main() { struct xx { int x; struct yy { char s; struct xx *p; }; struct yy *q; }; }

main() { extern int a; a=20; printf("%d",sizeof(a)); }

main() { printf("%d", out); } int out=100;

main() { printf("%d", out); } int out=100;

main() { extern out; printf("%d", out); } int out=100;

#include main() { struct xx { int x=3; char name[]="hello"; }; struct xx *s=malloc(sizeof(struct xx)); printf("%d",s->x); printf("%s",s->na

#include main() { struct xx { int x=3; char name[]="hello"; }; struct xx *s=malloc(sizeof(struct xx)); printf("%d",s->x); printf("%s",s->name); }

#include main() { struct xx { int x; struct yy { char s; struct xx *p; }; stru

#include main() { struct xx { int x; struct yy { char s; struct xx *p; }; stru

main() { extern int i; i=20; printf("%d",sizeof(i)); }

main() { printf("%d", out); } int out=100;

main() { show(); } void show() { printf("I'm the greatest"); }

main( ) { int m[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}}; printf("%u %u %u %d \n",m,*m,**m,***m); printf("%u %u %u %d \n",m+1,*m+1,**m+1,***m+1); }

main( ) { int a[ ] = {10,20,30,40,50},j,*p; for(j=0; j<5; j++) { printf(�%d� ,*a); a++; } p = a; for(j=0; j<5; j++) {

main( ) { static int a[ ] = {0,1,2,3,4}; int *p[ ] = {a,a+1,a+2,a+3,a+4}; int **ptr = p; ptr++; printf(�\n %d %d %d�, ptr-p, *ptr-a, **ptr); *ptr++; printf(

main( ){ static int a[ ] = {0,1,2,3,4}; int *p[ ] = {a,a+1,a+2,a+3,a+4}; int **ptr = p; ptr++; printf(�\n %d %d %d�, ptr-p, *ptr-a, **ptr); *ptr++; printf(�\n %d %d %d�, ptr-p

main( ) { static int a[ ] = {0,1,2,3,4}; int *p[ ] = {a,a+1,a+2,a+3,a+4}; int **ptr = p; ptr++; printf("\n %d %d %d", ptr-p, *ptr-a, **ptr); *ptr++; printf("\n %d %d %d", ptr-p, *ptr-a, **ptr); *++ptr; printf("\n %d %d %d", ptr-p, *ptr-a, **ptr); ++*ptr; printf("\n %d %d %d", ptr-p, *ptr-a, **ptr); }

main( ) { char *q; int j; for (j=0; j<3; j++) scanf("%s" ,(q+j)); for (j=0; j<3; j++) printf("%c" ,*(q+j)); for (j=0; j<3; j++) printf("%s" ,(q+j)); }

main( ) { void *vp; char ch = "g", *cp = "goofy"; int j = 20; vp = &ch; printf("%c", *(char *)vp); vp = &j; printf("%d",*(int *)vp); vp = cp; printf("%s",(char *)vp + 3); }

main ( ) { static char *s[ ] = {"black", "white", "yellow", "violet"}; char **ptr[ ] = {s+3, s+2, s+1, s}, ***p; p = ptr; **++p; printf("%s",*--*++p + 3); }

main() { int i, n; char *x = "girl"; n = strlen(x); *x = x[n]; for(i=0; i

int i,j; for(i=0;i<=10;i++) { j+=5; assert(i<5); }

main() { int i=-1; +i; printf("i = %d, +i = %d \n",i,+i); }

What are the files which are automatically opened when a C file is executed?

what will be the position of the file marker? a: fseek(ptr,0,SEEK_SET); b: fseek(ptr,0,SEEK_CUR);

main() { char name[10],s[12]; scanf(" \"%[^\"]\"",s); } How scanf will execute?

What is the problem with the following code segment? while ((fgets(receiving array,50,file_ptr)) != EOF)

main() { main(); }

main() { char *cptr,c; void *vptr,v; c=10; v=0; cptr=&c; vptr=&v; printf("%c%v",c,v); }

main() { char *str1="abcd"; char str2[]="abcd"; printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd")); }

main() { char not; not=!2; printf("%d",not); }

#define FALSE -1 #define TRUE 1 #define NULL 0 main() { if(NULL) puts("NULL"); else if(FALSE) puts("TRUE"); else puts("FALSE"); }

main() { int k=1; printf("%d==1 is ""%s",k,k==1?"TRUE":"FALSE"); }

main() { int y; scanf("%d",&y); // input given is 2000 if( (y%4==0 && y%100 != 0) || y%100 == 0 ) printf("%d is a leap year"); else printf("%d is not a leap year"); }

#define max 5 #define int arr1[max] main() { typedef char arr2[max]; arr1 list={0,1,2,3,4}; arr2 name="name"; printf("%d %s",list[0],name); }

int i=10; main() { extern int i; { int i=20; { const volatile unsigned i=30; printf("%d",i); } printf("%d",i); } printf("%d",i); }

main() { int *j; { int i=10; j=&i; } printf("%d",*j); }

main() { int i=-1; -i; printf("i = %d, -i = %d \n",i,-i); }

#include main() { const int i=4; float j; j = ++i; printf("%d %f", i,++j); }

#include main() { int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} }; int *p,*q; p=&a[2][2][2]; *q=***a; printf("%d..%d",*p,*q); }

#include main() { register i=5; char j[]= "hello"; printf("%s %d",j,i); }

main() { int i=5,j=6,z; printf("%d",i+++j); }

struct aaa{ struct aaa *prev; int i; struct aaa *next; }; main() { struct aaa abc,def,ghi,jkl; int x=100; abc.i=0;abc.prev=&jkl; abc.next=&def; def.i=1;def.prev=&abc;def.next=&ghi; ghi.i=2;ghi.prev=&def; ghi.next=&jkl; jkl.i=3;jkl.prev=&ghi;jkl.next=&abc; x=abc.next->next->prev->next->i; printf("%d",x); }

struct point { int x; int y; }; struct point origin,*pp; main() { pp=&origin; printf("origin is(%d%d)\n",(*pp).x,(*pp).y); printf("origin is (%d%d)\n",pp->x,pp->y); }

main() { int i=_l_abc(10); printf("%d\n",--i); } int _l_abc(int i) { return(i++); }

main() { char *p; int *q; long *r; p=q=r=0; p++; q++; r++; printf("%p...%p...%p",p,q,r); }

main() { char c=' ',x,convert(z); getc(c); if((c>='a') && (c<='z')) x=convert(c); printf("%c",x); } convert(z) { return z-32; }

main(int argc, char **argv) { printf("enter the character"); getchar(); sum(argv[1],argv[2]); } sum(num1,num2) int num1,num2; { return num1+num2; }

# include int one_d[]={1,2,3}; main() { int *ptr; ptr=one_d; ptr+=3; printf("%d",*ptr); }

# include aaa() { printf("hi"); } bbb() { printf("hello"); } ccc() { printf("bye"); } main() { int (*ptr[3])(); ptr[0]=aaa; ptr[1]=bbb; ptr[2]=ccc; ptr[2](); }

#include main() { FILE *ptr; char i; ptr=fopen("zzz.c","r"); while((i=fgetch(ptr))!=EOF) printf("%c",i); }

main() { int i =0;j=0; if(i && j++) printf("%d..%d",i++,j); printf("%d..%d,i,j); }

main() { int i; i = abc(); printf("%d",i); } abc() { _AX = 1000; }

int i; main() { int t; for ( t=4;scanf("%d",&i)-t;printf("%d\n",i)) printf("%d--",t--); } // If the inputs are 0,1,2,3 find the o/p

main() { int a= 0;int b = 20;char x =1;char y =10; if(a,b,x,y) printf("hello"); }

main() { unsigned int i; for(i=1;i>-2;i--) printf("c aptitude"); }

In the following pgm add a stmt in the function fun such that the address of 'a' gets stored in 'j'. main() { int * j; void fun(int **); fun(&j); } void fun(int **k) { int a =0; /* add a stmt here*/ }

What are the following notations of defining functions known as? int abc(int a,float b) { /* some code */ } ii. int abc(a,b) int a; float b; { /* some code*/ }

main() { char *p; p="%d\n"; p++; p++; printf(p-2,300); }

main() { char a[100]; a[0]='a';a[1]]='b';a[2]='c';a[4]='d'; abc(a); } abc(char a[]) { a++; printf("%c",*a); a++; printf("%c",*a); }

func(a,b) int a,b; { return( a= (a==b) ); } main() { int process(),func(); printf("The value of process is %d !\n ",process(func,3,6)); } process(pf,val1,val2) int (*pf) (); int val1,val2; { return((*pf) (val1,val2)); }

void main() { static int i=5; if(--i) { main(); printf("%d ",i); } }

void main() { int k=ret(sizeof(float)); printf("\n here value is %d",++k); } int ret(int ret) { ret += 2.5; return(ret); }

void main() { char a[]="12345\0"; int i=strlen(a); printf("here in 3 %d\n",++i); }

void main() { unsigned giveit=-1; int gotit; printf("%u ",++giveit); printf("%u \n",gotit=--giveit); }

void main() { int i; char a[]="\0"; if(printf("%s\n",a)) printf("Ok here \n"); else printf("Forget it\n"); }

void main() { void *v; int integer=2; int *i=&integer; v=i; printf("%d",(int*)*v); }

void main() { int i=i++,j=j++,k=k++; printf("%d%d%d",i,j,k); }

void main() { static int i=i++, j=j++, k=k++; printf("i = %d j = %d k = %d", i, j, k); }

void main() { while(1){ if(printf("%d",printf("%d"))) break; else continue; } }

main() { unsigned int i=10; while(i-->=0) printf("%u ",i); }

#include main() { int x,y=2,z,a; if(x=y%2) z=2; a=2; printf("%d %d ",z,x); }

main() { int a[10]; printf("%d",*a+1-*a+3); }

#define prod(a,b) a*b main() { int x=3,y=4; printf("%d",prod(x+2,y-1)); }

main() { unsigned int i=65000; while(i++!=0); printf("%d",i); }

main() { int i=0; while(+(+i--)!=0) i-=i++; printf("%d",i); }

main() { float f=5,g=10; enum{i=10,j=20,k=50}; printf("%d\n",++k); printf("%f\n",f<<2); printf("%lf\n",f%g); printf("%lf\n",fmod(f,g)); }

main() { int i=10; void pascal f(int,int,int); f(i++,i++,i++); printf(" %d",i); } void pascal f(integer :i,integer:j,integer :k) { write(i,j,k); }

void pascal f(int i,int j,int k) { printf("%d %d %d",i, j, k); } void cdecl f(int i,int j,int k) { printf("%d %d %d",i, j, k); } main() { int i=10; f(i++,i++,i++); printf(" %d\n",i); i=10; f(i++,i++,i++); printf(" %d",i); }

main() { signed char i=0; for(;i>=0;i++) ; printf("%d\n",i); }

main() { unsigned char i=0; for(;i>=0;i++) ; printf("%d\n",i); }

main() { char i=0; for(;i>=0;i++) ; printf("%d\n",i); }

Is the following statement a declaration/definition. Find what does it mean? int (*x)[10];

What is the output for the program given below typedef enum errorType{warning, error, exception,}error; main() { error g1; g1=1; printf("%d",g1); }

typedef struct error{int warning, error, exception;}error; main() { error g1; g1.error =1; printf("%d",g1.error); }

#ifdef something int some=0; #endif main() { int thing = 0; printf("%d %d\n", some ,thing); }

#if something == 0 int some=0; #endif main() { int thing = 0; printf("%d %d\n", some ,thing); }

What is the output for the following program main() { int arr2D[3][3]; printf("%d\n", ((arr2D==* arr2D)&&(* arr2D == arr2D[0])) ); }

void main() { if(~0 == (unsigned int)-1) printf("You can answer this if you know how values are represented in memory"); }

int swap(int *a,int *b) { *a=*a+*b;*b=*a-*b;*a=*a-*b; } main() { int x=10,y=20; swap(&x,&y); printf("x= %d y = %d\n",x,y); }

main() { char *p = "ayqm"; printf("%c",++*(p++)); }

main() { int i=5; printf("%d",++i++); }

main() { char *p = "ayqm"; char c; c = ++*p++; printf("%c",c); }

int aaa() {printf("Hi");} int bbb(){printf("hello");} iny ccc(){printf("bye");} main() { int ( * ptr[3]) (); ptr[0] = aaa; ptr[1] = bbb; ptr[2] =ccc; ptr[2](); }

main() { int i=5; printf("%d",i=++i ==6); }

main() { char p[ ]="%d\n"; p[1] = 'c'; printf(p,65); }

void ( * abc( int, void ( *def) () ) ) ();

main() { while (strcmp("some","some\0")) printf("Strings are not equal\n"); }

main() { char str1[] = {"s","o","m","e"}; char str2[] = {"s","o","m","e","\0"}; while (strcmp(str1,str2)) printf("Strings are not equal\n"); }

main() { int i = 3; for (;i++=0;) printf("%d",i); }

void main() { int *mptr, *cptr; mptr = (int*)malloc(sizeof(int)); printf("%d",*mptr); int *cptr = (int*)calloc(sizeof(int),1); printf("%d",*cptr); }

void main() { static int i; while(i<=10) (i>2)?i++:i--; printf("%d", i); }

main() { int i=10,j=20; j = i, j?(i,j)?i:j:j; printf("%d %d",i,j); }

1. const char *a; 2. char* const a; 3. char const *a; -Differentiate the above declarations.

main() { int i=5,j=10; i=i&=j&&10; printf("%d %d",i,j); }

main() { int i=4,j=7; j = j || i++ && printf("YOU CAN"); printf("%d %d", i, j); }

main() { register int a=2; printf("Address of a = %d",&a); printf("Value of a = %d",a); }

main() { float i=1.5; switch(i) { case 1: printf("1"); case 2: printf("2"); default : printf("0"); } }

main() { extern i; printf("%d\n",i); { int i=20; printf("%d\n",i); } }

main() { int a=2,*f1,*f2; f1=f2=&a; *f2+=*f2+=a+=2.5; printf("\n%d %d %d",a,*f1,*f2); }

main() { char *p="GOOD"; char a[ ]="GOOD"; printf("\n sizeof(p) = %d, sizeof(*p) = %d, strlen(p) = %d", sizeof(p), sizeof(*p), strlen(p)); printf("\n sizeof(a) = %d, strlen(a) = %d", sizeof(a), strlen(a)); }

#define DIM( array, type) sizeof(array)/sizeof(type) main() { int arr[10]; printf("The dimension of the array is %d", DIM(arr, int)); }

int DIM(int array[]) { return sizeof(array)/sizeof(int ); } main() { int arr[10]; printf("The dimension of the array is %d", DIM(arr)); }

main() { static int a[3][3]={1,2,3,4,5,6,7,8,9}; int i,j; static *p[]={a,a+1,a+2}; for(i=0;i<3;i++) { for(j=0;j<3;j++) printf("%d\t%d\t%d\t%d\n",*(*(p+i)+j),*(*(j+p)+i),*(*(i+p)+j), *(*(p+j)+i)); } }

main() { void swap(); int x=10,y=8; swap(&x,&y); printf("x=%d y=%d",x,y); } void swap(int *a, int *b) { *a ^= *b, *b ^= *a, *a ^= *b; }

main() { int i = 257; int *iPtr = &i; printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) ); }

main() { int i = 258; int *iPtr = &i; printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) ); }

main() { int i=300; char *ptr = &i; *++ptr=2; printf("%d",i); }

#include main() { char * str = "hello"; char * ptr = str; char least = 127; while (*ptr++) least = (*ptr

Declare an array of N pointers to functions returning pointers to functions returning pointers to characters?

main() { struct student { char name[30]; struct date dob; }stud; struct date { int day,month,year; }; scanf("%s%d%d%d", stud.rollno, &student.dob.day, &student.dob.month, &student.dob.year); }

main() { struct date; struct student { char name[30]; struct date dob; }stud; struct date { int day,month,year; }; scanf("%s%d%d%d", stud.rollno, &student.dob.day, &student.dob.month, &student.dob.year); }

There were 10 records stored in "somefile.dat" but the following program printed 11 names. What went wrong? void main() { struct student { char name[30], rollno[6]; }stud; FILE *fp = fopen("somefile.dat","r"); while(!feof(fp)) { fread(&stud, sizeof(stud), 1 , fp); puts(stud.name); } }

Is there any difference between the two declarations, 1. int foo(int *arr[]) and 2. int foo(int *arr[2])

What is the subtle error in the following code segment? void fun(int n, int arr[]) { int *p=0; int i=0; while(i++

What is wrong with the following code? int *foo() { int *s = malloc(sizeof(int)100); assert(s != NULL); return s; }

What is the hidden bug with the following statement?

What is the hidden bug with the following statement? assert(val++ != 0);

void main() { int *i = 0x400; // i points to the address 400 *i = 0; // set the value of memory location pointed by i; }

#define assert(cond) if(!(cond)) \(fprintf(stderr, "assertion failed: %s, file %s, line %d \n",#cond,\__FILE__,__LINE__), abort()) void main() { int i = 10; if(i==0) assert(i < 100); else printf("This statement becomes else for if in assert macro"); }

Is the following code legal? struct a { int x; struct a b; }

Is the following code legal? struct a { int x; struct a *b; }

Is the following code legal? typedef struct a { int x; aType *b; }aType

Is the following code legal? typedef struct a aType; struct a { int x; aType *b; };

Is the following code legal? void main() { typedef struct a aType; aType someVariable; struct a { int x; aType *b; }; }

void main() { printf("sizeof (void *) = %d \n", sizeof( void *)); printf("sizeof (int *) = %d \n", sizeof(int *)); printf("sizeof (double *) = %d \n", sizeof(double *)); printf("sizeof(struct unknown *) = %d \n", sizeof(struct unknown *)); }

char inputString[100] = {0}; To get string input from the keyboard which one of the following is better? 1) gets(inputString) 2) fgets(inputString, sizeof(inputString), fp)

Which version do you prefer of the following two, 1) printf("%s",str); // or the more curt one 2) printf(str)

void main() { int i=10, j=2; int *ip= &i, *jp = &j; int k = *ip/*jp; printf("%d",k); }

{ char ch; for(ch=0;ch<=127;ch++) printf("%c %d \n", ch, ch); }

Is this code legal? int *ptr; ptr = (int *) 0x400;

main() { char a[4]="HELLO"; printf("%s",a); }

main() { char a[4]="HELL"; printf("%s",a); }

main() { int a=10,*j; void *k; j=k=&a; j++; k++; printf("\n %u %u ",j,k); }

main() { extern int i; { int i=20; { const volatile unsigned i=30; printf("%d",i); } printf("%d",i); } printf("%d",i); } int i;

char *someFun() { char *temp = "string constant"; return temp; } int main() { puts(someFun()); }

char *someFun1() { char temp[ ] = "string"; return temp; } char *someFun2() { char temp[ ] = {"s", "t","r","i","n","g"}; return temp; } int main() { puts(someFun1()); puts(someFun2()); }

What is the difference b/n the Heap and the Stack