C Programming Questions and Answers -
int main() { char *g = NULL; char *h = 0; if (g) printf(" g "); else printf("nullg"); if (h) printf("h\n"); else printf(" nullh\n"); }
a) nullg nullh
b) Depends on the compiler
c) g nullh
d) g h
void main () { int a=2; if(a==2) { a=~a+2<<1; printf("%d",a); } else { a=~a; } }
a) 6
b) -2
c) 0
d) None of above
int main() { static int i; int j; for(j=0;j<=5;j+=2) switch(j) { case 1: i++;break; case 2: i+=2; case 4: i%=2;j=-1;continue; default: --i; continue; } printf ("%d", i); return 0; }
a) Different address is printed
b) Same address is printed
c) Run time error
d) Nothing
int main() { int a=8,b=2,c; c=a<<b; printf("%d",c); return 0; }
a) 32
b) 16
c) 8
d) 4
int main() { char c='8'; printf("%d",c); return 0; }
a) 56
b) 16
c) 8
d) 4
int main() { int x = 100; printf("decimal = %d; octal = %o; hex = %x\n", x, x, x); printf("decimal = %d; octal = %#o; hex = %#x\n", x, x, x); return 0; }
a) decimal = 100; octal = 414; hex = 46
b) decimal = 100; octal = 414; hex = 64
c) decimal = 100; octal = 144; hex = 64
d) decimal = 100; octal = 144; hex = 46
struct student { }; int main() { struct student s; printf("%d", sizeof(s)); }
a) 1
b) 0
c) Compilation Error
d) Runtime Error
int main(){ int a=10; switch(0) printf("%d",a); switch(1) printf("%d",a); return 0; }
a) 10 10
b) 10
c) Compilation Error
d) No output
int main() { printf("%d", max); } #define max 23
a) 23
b) 0
c) Compilation Error
d) Runtime Error
int main() { int arri[] = {1, 2 ,3}; int *ptri = arri; char arrc[] = {1, 2 ,3}; char *ptrc = arrc; printf("sizeof arri[] = %d ", sizeof(arri)); printf("sizeof ptri = %d ", sizeof(ptri)); printf("sizeof arrc[] = %d ", sizeof(arrc)); printf("sizeof ptrc = %d ", sizeof(ptrc)); return 0; }
a) sizeof arri[] = 12 sizeof ptri = 8 sizeof arrc[] = 12 sizeof ptrc = 8
b) sizeof arri[] = 12 sizeof ptri = 8 sizeof arrc[] = 3 sizeof ptrc = 8 ANS
c) sizeof arri[] = 12 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 8
d) sizeof arri[] = 12 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 8
Hello,
justification for the #3 is wrong, altough the result is correct. Here is the one I get after checking assembly output.
/*
* j=0, i=0 -> j=0, i=-1
* j=2, i=-1 -> j=-1, i=1
* j=1, i=1 -> j=1, i=2
* j=3, i=2 -> j=3, i=1
* j=5, i=1 -> j=5, i=0
* j=7, i=0
*/
Hi, Can anyone give the proper justification for the question #3 …
whatever justification here given is not understandable .
#10 ans
sizeof arri[] = 12 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 4
explanation: pointer value is an integer size of integer is 4bytes so two arrays pointer value will be same
Rakesh Thota, the sizeof a pointer depends on the architecture of that computer (32 or 64 bytes). On a 32 bytes computer, the sizeof a pointer will always be 4 and on a 64 bytes computer, the sizeof a pointer will always be 8. Answer b) it’s correct
Give some example
#10 and is 8 2 4 2
#2 is wrong, left-shift for signed type is Undefined Behavior in C and C++. So it could output `-2`, but there is no guarantee
#9 will give compilation error, so ans is c