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); } a) 11 b) 22 c) 77 d) 66
p is pointing to character \'\\n\'. str1 is pointing to character \'a\' ++*p. \"p is pointing to \'\\n\' and that is incremented by one.\" the ASCII value of \'\\n\' is 10, which is then incremented to 11. The value of ++*p is 11. ++*str1, str1 is pointing to \'a\' that is incremented by 1 and it becomes \'b\'. ASCII value of \'b\' is 98. Now performing (11 + 98 32), we get 77(\"M\"); So we get the output 77 :: \"M\" (Ascii is 77).
FAQ-C. What will be the output of the following statement ?
/* /* printf("hello"); */ */
a) hello b) no output c) error d) "hello"
Nested comments are not allowed in 'C'. Notes : Comments should be written in /* -------------- */ without any nesting in between.
FAQ-C. What will be the output of the following arithmetic expression ?
FAQ-C. What will be the output of the following statement ?
printf("%i",35,2+8*5%10-2);
a) error b) 0 c) 35 d) 350
printf( "%i ", 35, 2+8*5%10-2 ); As no format specifier is mentioned for the expression " 2+8*5%10-2 " , hence only "35" will get printed on the screen.
FAQ-C. What will be the output of the following statement ?
int a=10; printf("%d &i",a,10);
a) error b) 10 c) 10 10 d) none of these
int a = 10; printf(" %d &i ",a,10); Format specifier for "a" is mentioned , hence value of "a" will get printed on screen . No format specifier is mentioned for "10" hence it will not get printed on screen . So final output will be "10 &i".
FAQ-C. What will be the output of the following statement ?
printf("%X%x%ci%x",11,10,'s',12);
a) error b) basc c) Bas94c d) none of these
printf("%X%x%ci%x",11,10,'s',12); 11,10,12 will get converted into equivalent hexadecimal entities . 11 B 10 a 12 c hence final output will be "Basic".
FAQ-C. What will be the output of the following statements ?
int a = printf("00"); printf("%d",a);
a) 0 b) 00 c) 002 d) garbage value
FAQ-C. What will be the output of the following statements ?
int a = 4, b = 7,c; c = a = = b; printf("%i",c);
a) 0 b) error c) 1 d) garbage value
int a = printf("00"); printf("%d",a); printf() function returns the number of characters being printed on screen. Hence "2" gets stored in variable "a". Therefore the final output is "002".
int a = 4, b = 7,c; c = a = = b; printf("%i",c); The condition (a = = b) is false . Hence "0" gets stored in variable "c" which eventually gets printed out.
FAQ-C. What will be the output of the following program ?
#include<stdio.h> void main() { extern int x; printf("%d",x); }int x = 10;
a) error b) 0 c) garbage value d) 10
ANSWER : D Explanation : #include<stdio.h> void main() { extern int x; // declaration not definition printf("%d",x); }int x = 10; // definition As declaration of external variable is already done in main() , hence definition of external variable can be done outside main().
FAQ-C. What will be the output of the following program ?
#include<stdio.h> void main() { struct p { int a,c ; float b; }d = {1}; printf("%d%d%f",d.a,d.c,d.b); }
a) garbage value b) 100.0 c) error d) 000.0
ANSWER : B Explanation : void main() { struct p { int a,c ; float b; }d = {1}; // partially initialised printf("%d%d%f",d.a,d.c,d.b); } Here the structure (automatic storage class) is partially initialised. Hence the remaining elements of structure are initialised to "0"
FAQ-C. What will be the output of the following statements ?
float k = 3.84; printf("%d",(int)k);
a) 3.8 b) 3.0 c) 3 d) 4
ANSWER : C Explanation : float k = 3.84; printf("%d",(int)k); Typecasting is being done i.e float is being converted into int. 3.84 gets truncated to 3.
FAQ-C. What will be the output of the following statements ?
float c = 1.3; printf("%d%d",sizeof(c),sizeof(1.3));
a) 44 b) 48 c) 42 d) 24
ANSWER : B Explanation : Type Size Format Specifier float 4 bytes %f double 8 bytes %lf long double 10 bytes %Lf "c" is of type "float" hence "4" gets printed on screen. By default a real number is treated as a "double" hence "8" gets printed after it.
FAQ-C. Which of them can't be checked in a "switch-case" statement?
a) enum b) int c) char d) float
ANSWER : D Explanation : "float" cannot be checked in a "switch - case" statement.
FAQ-C. What will be the output of the following statement ?
printf( 3 + "goodbye");
a) goodbye b) odbye c) bye d) dbye
ANSWER : D Explanation : printf("goodbye"); Here address of 'g' is being passed to the printf() function. printf( 3 + "goodbye"); Here 3 + address of 'g' is being passed to printf() function. i.e address of 'd' is being passed, hence "dbye" gets printed on the screen.
FAQ-C. What will be the output of the following statement ?
printf("hello""""world");
a) error b) hello""""world c) hello d) helloworld
ANSWER : D Explanation : "helloworld" gets printed on the screen.
FAQ-C Find output? #include<stdio.h> 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); } Answer: Some Garbage Value---1 Explanation: p=&a[2][2][2] you declare only two 2D arrays, but you are trying to access the third 2D(which you are not declared) it will print garbage values. *q=***a starting address of a is assigned integer pointer. Now q is pointing to starting address of a. If you print *q, it will print first element of 3D array. FAQ-C. What will be the output main() { int i=5; printf("%d%d%d%d%d%d",i++,i--,++i,--i,i); } Answer:45545 Explanation: The arguments in a function call are pushed into the stack from left to right. The evaluation is by popping out from the stack. and the evaluation is from right to left, hence the result.
FAQ-C What is the output main() { Int i=4; Printf(%d,i,i++); } 6 FAQ-C What is the output main() { Int i=5; Printf(%d %d %d %d %d,i++,i--,++i,--i,i ); }
45545 void main() { char far *farther,*farthest; printf("%d..%d",sizeof(farther),sizeof(farthest) ); } Answer: 4..2 Explanation:the second pointer is of char type and not a far pointer main() { int i=400,j=300,m=900; printf("%d..%d"); } Answer: 900..300
main() { char *p; p="Hello"; printf("%c\n",*&*p); } Answer: H Explanation: * is a dereference operator & is a reference operator. They can be applied any number of times provided it is meaningful. Here p points to the first character in the string "Hello". *p dereferences it and so its value is H. Again & references it to an address and * dereferences it to the value H. main() { int i=1; while (i<=5) { printf("%d",i); if (i>2) goto here; i++; } } fun() { here: printf("PP"); } Answer: Compiler error: Undefined label 'here' in function main Explanation: Labels have functions scope, in other words The scope of the labels is limited to functions . The label 'here' is available in function fun() Hence it is not visible in function main. 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",names[i]); } Answer: Compiler error: Lvalue required in function main Explanation: Array names are pointer constants. So it cannot be modified. void main() { int i=5; printf("%d",i++ + ++i); }
Answer:
main() { int i=0; for(;i++;printf("%d",i)) ; printf("%d",i); } Answer: 1 Explanation: before entering into the for loop the checking condition is "evaluated". Here it evaluates to 0 (false) and comes out of the loop, and i is incremented (note the semicolon after the for loop). main( ) { int a[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}}; printf(%u %u %u %d \n,a,*a,**a,***a); printf(%u %u %u %d \n,a+1,*a+1,**a+1,***a+1); } Answer: 100, 100, 100, 2 114, 104, 102, 3 Explanation: The given array is a 3-D one. It can also be viewed as a 1-D array.
thus, for the first printf statement a, *a, **a give address of first element . since the indirection ***a gives the value. Hence, the first line of the output. for the second printf a+1 increases in the third dimension thus points to value at 114, *a+1 increments in second dimension thus points to 104, **a +1 increments the first dimension thus points to 102 and ***a+1 first gets the value at first location and then increments it by 1. Hence, the output. 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++) { printf(%d ,*p); p++; } } Answer: Compiler error: lvalue required. Explanation: Error is in line with statement a++. The operand must be an lvalue and may be of any of scalar type for the any operator, array name only when subscripted is an lvalue. Simply array name is a non-modifiable lvalue. 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); } Answer: 111 222 333 344 Explanation: Let us consider the array and the two pointers with some address a 0 1 2 3 4 100 102 104 106 108 p 100 102 104 106 108 1000 1002 1004 1006 1008 ptr 1000 2000 After execution of the instruction ptr++ value in ptr becomes 1002, if scaling factor for integer is 2 bytes. Now ptr p is value in ptr starting location of array p, (1002 1000) / (scaling factor) = 1, *ptr a = value at address pointed by ptr starting value of array a, 1002 has a value 102 so the value is (102 100)/(scaling factor) = 1, **ptr is the value stored in the location pointed by the pointer of ptr = value pointed by value pointed by 1002 = value pointed by 102 = 1. Hence the output of the firs printf is 1, 1, 1. After execution of *ptr++ increments value of the value in ptr by scaling factor, so it becomes1004. Hence, the outputs for the second printf are ptr p = 2, *ptr a = 2, **ptr = 2. After execution of *++ptr increments value of the value in ptr by scaling factor, so it becomes1004. Hence, the outputs for the third printf are ptr p = 3, *ptr a = 3, **ptr = 3. After execution of ++*ptr value in ptr remains the same, the value pointed by the value is incremented by the scaling factor. So the value in array p at location 1006 changes from 106 10 108,. Hence, the outputs for the fourth printf are ptr p = 1006 1000 = 3, *ptr a = 108 100 = 4, **ptr = 4. 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)); }
Inputs are psgi,psit,psat 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); } Answer: g20fy Explanation: Since a void pointer is used it can be type casted to any other type pointer. vp = &ch stores address of char ch and the next statement prints the value stored in vp after type casting it to the proper data type pointer. the output is g. Similarly the output from second printf is 20. The third printf statement type casts it to print the string from the 4th value hence the output is fy. 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); } Explanation: In this problem we have an array of char pointers pointing to start of 4 strings. Then we have ptr which is a pointer to a pointer of type char and a variable p which is a pointer to a pointer to a pointer of type char. p hold the initial value of ptr, i.e. p = s+3. The next statement increment value in p by 1 , thus now value of p = s+2. In the printf statement the expression is evaluated *++p causes gets value s+1 then the pre decrement is executed and we get s+1 1 = s . the indirection operator now gets the value from the array of s and adds 3 to the starting address. The string is printed starting from this position. Thus, the output is ck. main() { int i, n; char *x = girl; n = strlen(x); *x = x[n]; for(i=0; i<n; ++i) { printf(%s\n,x); x++; } } Answer: (blank space) irl rl l Explanation: Here a string (a pointer to char) is initialized with a value girl. The strlen function returns the length of the string, thus n has a value 4. The next statement assigns value at the nth location (\0) to the first location. Now the string becomes \0irl . Now the printf statement prints the string after each iteration it increments it starting position. Loop starts from 0 to 4. The first time x*0+ = \0 hence it prints nothing and pointer value is incremented. The second time it prints from x*1+ i.e irl and the third time it prints rl and the last time it prints l and the loop terminates. main() { char *str1="abcd"; char str2[]="abcd"; printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd")); } Answer: 2 5 5 Explanation: In first sizeof, str1 is a character pointer so it gives you the size of the pointer variable. In second sizeof the name str2 indicates the name of the array whose size is 5 (including the '\0' termination character). The third sizeof is similar to the second one. #define FALSE -1 #define TRUE 1 #define NULL 0 main() { if(NULL) puts("NULL"); else if(FALSE) puts("TRUE"); else puts("FALSE"); } Answer:TRUE Explanation:The input program to the compiler after processing by the preprocessor is, main(){ if(0) puts("NULL"); else if(-1) puts("TRUE"); else puts("FALSE"); } Preprocessor doesn't replace the values given inside the double quotes. The check by if condition is boolean value false so it goes to else. In second if -1 is boolean value true hence "TRUE" is printed. main() { int k=1; printf("%d==1 is ""%s",k,k==1?"TRUE":"FALSE"); } Answer: 1==1 is TRUE Explanation: When two strings are placed together (or separated by white-space) they are concatenated (this is called as "stringization" operation). So the string is as if it is given as "%d==1 is %s". The conditional operator( ?: ) evaluates to "TRUE". main() { int i=5,j=6,z; printf("%d",i+++j); }
Answer: 11 Explanation: the expression i+++j is treated as (i++ + j) main() { char *p; int *q; long *r; p=q=r=0; p++; q++; r++; printf("%p...%p...%p",p,q,r); }
Answer: 0001...0002...0004 Explanation:++ operator when applied to pointers increments address according to their corresponding data-types. # include<stdio.h> aaa() { printf("hi"); } bbb(){ printf("hello"); } ccc(){ printf("bye"); } main() { int (*ptr[3])(); ptr[0]=aaa; ptr[1]=bbb; ptr[2]=ccc; ptr[2](); } Answer: bye Explanation: ptr is array of pointers to functions of return type int.ptr[0] is assigned to address of the function aaa. Similarly ptr[1] and ptr[2] for bbb and ccc respectively. ptr[2]() is in effect of writing ccc(), since ptr[2] points to ccc. 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 Answer: 4--0 3--1 2--2 Explanation:Let us assume some x= scanf("%d",&i)-t the values during execution will be, t i x 4 0 -4 3 1 -2 2 2 0 #define int char main() { int i=65; printf("sizeof(i)=%d",sizeof(i)); } Answer: sizeof(i)=1 Explanation: Since the #define replaces the string int by the macro char main() { int i=10; i=!i>14; Printf ("i=%d",i); } Answer: i=0 main() { printf("\nab"); printf("\bsi"); printf("\rha"); } hai Explanation: \n - newline \b - backspace \r - linefeed #define clrscr() 100 main() { clrscr(); printf("%d\n",clrscr()); } Answer: 100 main() { printf("%p",main); } Answer: Some address will be printed. Explanation: Function names are just addresses (just like array names are addresses). main() is also a function. So the address of function main will be printed. %p in printf specifies that the argument is an address. They are printed as hexadecimal numbers. main() { int i; printf("%d",scanf("%d",&i)); // value 10 is given as input here } Answer: 1 #define f(g,g2) g##g2 main() { int var12=100; printf("%d",f(var,12)); } #define f(g,g2) g##g2 main() { int var12=100; printf("%d",f(var,12)); } 100 main() { float me = 1.1; double you = 1.1; if(me==you) printf(TRUE"); else printf(FALSE"); } FALSE Explanation: For floating point numbers (float, double, long double) the values cannot be predicted exactly. Depending on the number of bytes, the precession with of the value represented varies. main() { static int var = 5; printf("%d ",var--); if(var) main(); } Answer: 5 4 3 2 1
Answer Infinite loop main() { int a[10]; printf("%d",*a+1-*a+3); }
Answer: 4 Explanation: *a and -*a cancels out. The result is as simple as 1 + 3 = 4 !
#define prod(a,b) a*b main() { int x=3,y=4; printf("%d",prod(x+2,y-1)); } Answer: 10 Explanation: The macro expands and evaluates to as: x+2*y-1 => x+(2*y)-1 => 10
main() { unsigned int i=65000; while(i++!=0); printf("%d",i); }
Answer: 1
main() { int i=0; while(+(+i--)!=0) i-=i++; printf("%d",i); } Answer: -1 Explanation: Unary + is the only dummy operator in C. So it has no effect on the expression and now the while loop is, while(i--!=0) which is false and so breaks out of while loop. The value 1 is printed due to the post-decrement operator.
typedef enum errorType{warning, error, exception,}error; main() { error g1; g1=1; printf("%d",g1); } Answer Compiler error: Multiple declaration for error Explanation The name error is used in the two meanings. One means that it is a enumerator constant with value 1. The another use is that it is a type name (due to typedef) for enum errorType. Given a situation the compiler cannot distinguish the meaning of error to know in what sense the error is used:
typedef struct error{int warning, error, exception;}error; main() { error g1; g1.error =1; printf("%d",g1.error); } Answer 1 Explanation The three usages of name errors can be distinguishable by the compiler at any instance, so valid (they are in different namespaces).
Answer 1 Explanation The three usages of name errors can be distinguishable by the compiler at any instance, so valid (they are in different namespaces).
Answer 1
main() { int i=5; printf(%d,i=++i ==6); } Answer: 1 Explanation: The expression can be treated as i = (++i==6), because == is of higher precedence than = operator. In the inner expression, ++i is equal to 6 yielding true(1). Hence the result.
main() { char p[ ]="%d\n"; p[1] = 'c'; printf(p,65); } Answer: A Explanation: Due to the assignment p*1+ = c the string becomes, %c\n. Since this string becomes the format string for printf and ASCII value of 65 is A, the same gets printed.
main() { while (strcmp(some,some\0)) printf(Strings are not equal\n); }
Answer: No output Explanation: Ending the string constant with \0 explicitly makes no difference. So some and some\0 are equivalent. So, strcmp returns 0 (false) hence breaking out of the while loop. main() { char str1*+ = ,s,o,m,e-; char str2*+ = ,s,o,m,e,\0-; while (strcmp(str1,str2)) printf(Strings are not equal\n); } Answer: Strings are not equal Strings are not equal . Explanation: If a string constant is initialized explicitly with characters, \0 is not appended automatically to the string. Since str1 doesnt have null termination, it treats whatever the values that are in the following positions as part of the string until it randomly reaches a \0. So str1 and str2 are not the same, hence the result.
main() { int i = 3; for (;i++=0;) printf(%d,i); } Explanation: As we know that increment operators return rvalues and hence it cannot appear on the left hand side of an assignment operation. void main() { int *mptr, *cptr; mptr = (int*)malloc(sizeof(int)); printf(%d,*mptr); int *cptr = (int*)calloc(sizeof(int),1); printf(%d,*cptr); } Answer: garbage-value 0 Explanation: The memory space allocated by malloc is uninitialized, whereas calloc returns the allocated memory space initialized to zeros.
void main() { static int i; while(i<=10) (i>2)?i++:i--; printf(%d, i); } Answer: 32767 Explanation: Since i is static it is initialized to 0. Inside the while loop the conditional operator evaluates to false, executing i--. This continues till the integer value rotates to positive value (32767). The while condition becomes false and hence, comes out of the while loop, printing the i value.
#include<stdio.h> int main(){ int a = 320; char *ptr; ptr =( char *)&a; printf("%d ",*ptr); return 0; }
64 we know int is two byte data byte while char is one byte data byte. char pointer can keep the address one byte at time.
Binary value of 320 is 00000001 01000000 (In 16 bit)
Memory representation of int a = 320 is:
01000000 00000001 int main(){ void (*p)(); int (*q)(); int (*r)(); p = clrscr; q = getch; r = puts; (*p)(); (*r)(PSIT"); (*q)(); return 0; }
PSIT int main(){ register a = 25; int far *p; p=&a; printf("%d ",*p); return 0; }
Register data type stores in CPU. So it has not any memory address. Hence we cannot write &a. int main(){ int a = 10; void *p = &a; int *ptr = p; printf("%u",*ptr); return 0; }
10 int main(){ int register a; a=25; scanf("%d",&a); printf("%d",a); return 0; }
Register data type stores in CPU. So it has not any memory address. Hence we cannot write &a. int main(){ char arr[10]; arr = "world"; printf("%s",arr); return 0; }
Array name is constant pointer and we cannot assign any value in constant data type after declaration.
int main(){ int a,b,c,d; char *p = ( char *)0; int *q = ( int *q)0; float *r = ( float *)0; double *s = 0; a = (int)(p+1); b = (int)(q+1); c = (int)(r+1); d = (int)(s+1); printf("%d %d %d %d",a,b,c,d); return 0; }
1 2 4 8 int main(){ int a = 5,b = 10,c; int *p = &a,*q = &b; c = p - q; printf("%d" , c); return 0; }
1 Difference of two same type of pointer is always one.
unsigned long int (* avg())[3]{ static unsigned long int arr[3] = {1,2,3}; return &arr; } int main(){ unsigned long int (*ptr)[3]; ptr = avg(); printf("%d" , *(*ptr+2)); return 0; }
3 int main(){ int i = 5 , j; int *p , *q; p = &i; q = &j; j = 5; printf("%d %d",*p,*q); return 0; }
5 garbage int main(){ int i = 5; int *p; p = &i; printf(" %u %u", *&p , &*p); return 0; }
Address Address What is meaning of following pointer declaration? int(*(*ptr1)())[2];
ptr is pointer to such function which return type is pointer to an array. int main(){ int a=5,b=10,c=15; int *arr[]={&a,&b,&c}; printf("%d",*arr[1]); return 0;
}
Array element cannot be address of auto variable. It can be address of static or extern variables.
int main(){ int a[2][4]={3,6,9,12,15,18,21,24}; printf("%d %d %d",*(a[1]+2),*(*(a+1)+2),2[1[a]]); return 0; }
21 21 21 int main(){ const int x=25; int * const p=&x; *p=2*x; printf("%d",x); return 0; }
50 const keyword in c doesnt make any variable as constant but it only makes the variable as read only. With the help of pointer we can modify the const variable. In this example pointer p is pointing to address of variable x. In the following line: int * const p=&x; p is constant pointer while content of p i.e. *p is not constant. *p=2*x put the value 50 at the memory location of variable x.
Here ptr is pointer to array of pointer to string. P1, p2, p3 are pointers to array of string. array[3] is array which contain pointer to array of string.
Pictorial representation:
upper part of box represent content and lower part represent memory address. We have assumed arbitrary address. As we know p[i]=*(p+i) (***ptr[0])[2]=(*(***ptr+0))[2]=(***ptr)[2] =(***(&array))[2] //ptr=&array =(**array)[2] //From rule *&p=p =(**(&p1))[2] //array=&p1 =(*p1)[2] =(*&s)[2] //p1=&s =s*2+=che
int display(); int(*array[3])(); int(*(*ptr)[3])(); int main(){ array[0]=display; array[1]=getch; ptr=&array; printf("%d",(**ptr)()); (*(*ptr+1))(); return 0; }
int display(){ int x=5; return x++; }
array []: It is array of pointer to such function which parameter is void and return type is int data type. ptr: It is pointer to array which contents are pointer to such function which parameter is void and return type is int type data.
8 12 In c three continuous dots is known as ellipsis which is variable number of arguments of function. In this example ptr is generic pointer which is pointing to first element of variable number of argument. After incrementing it will point third element.