721 views
2 2 votes
What will be the output of the C program?

 #include int main() {

int size = sizeof(volatile) + sizeof(const);

 printf("%d",++size);

 return 0; }

 (a)8 (b)10 (c)9 (d)Compilation Error

2 Answers

1 1 vote

Compilation error because preprocessor directive is invalid. 

Also note that sizeof operator (not a function!) takes only  

  1.  type names like int, char, float, char *, int *, etc, or other user defined types like struct, union or enums 
  2. or objects eg instaces of types like 1, 'a', 2.4, struct tag, enum tag, pointers (p in char *p), array names (a in a[]).

Refrence: https://en.cppreference.com/w/cpp/language/sizeof.html

In the question `sizeof(volatile)` and `sizeof(const)` don't specify the type name. 

Prior to C99, 'int' is implicity assumed to be type name in such cases. 

Hence they are equivalent to `sizeof(volatile int)` and `sizeof(const int)` and their sum is 8.

Then in `printf("%d",++size);` size is preincremeted so the output, if we fix the directive error, should be 9.

• edited by
Position:
Show:

Related questions

12 12 votes
2 2 answers
409
409 views
GO Classes asked Jun 3
409 views
What is the output of the following code?#include <stdio.h int main() { int x = 3, y = 4, z; z = x++ + ++y; printf("%d %d %d", x, y, z); return 0; }$\texttt{4\ 5\ 8}$ $\t...
8 8 votes
3 3 answers
557
557 views
GO Classes asked Jun 2
557 views
What is the output of the following code?#include <stdio.h int main() { int x = 5; printf("%d", x++); printf("%d", ++x); return 0; }$57$ $67$ Compilation error Undefined ...
8 8 votes
2 2 answers
452
452 views
GO Classes asked Jun 2
452 views
What is the output of the following code?#include <stdio.h int main() { int a = 5, b = 10; printf("%d", a < b && b 0); return 0; }
12 12 votes
2 2 answers
471
471 views
GO Classes asked Jun 2
471 views
What is the output of the following code?#include <stdio.h int main() { int a = 5, b = 10; printf("%d", a b || b 0); return 0; }