Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include <stdio.h>
#define SIZE 10;
#define FUDGE SIZE -2;
int main()
{
    int size;/* size to really use */
    size = FUDGE;
    printf("Size is %d\n", size);
    return (0);
}

what is the value of size in o/p for the following program?
Posted
Updated 16-Jan-13 23:33pm
v3

Did you compile and run it?
And the result is not as expected?
It is as expected when you perform the preprocessor tasks yourself:
C++
size = FUDGE;
// replace FUDGE = SIZE -2;
size = SIZE -2;;
// replace SIZE = 10;
size = 10; -2;;

Did you see your mistake? #define statements need no trailing semicolon. If there is one, it is also inserted.
 
Share this answer
 
Comments
mvigsnhwar 17-Jan-13 5:56am    
yes sir i have compiled it without removing the semicolons the o/p is "Size is 10"
with removing the semicolons the o/p is "Size is 8"
Jochen Arndt 17-Jan-13 6:15am    
Thank you for your feedback and accepting the answer.

I have another note which may be helpful for you and others reading this question:

Using #define statements can sometimes result in not expected behaviour. Another example is not using parenthesis when the definition is not a single literal. Consider your example '#define FUDGE SIZE -2' when using it like this:
size = 3 * FUDGE;
This will be 3 * 10 - 2 = 28 rather than the expected 3 * (10 - 2) = 24. To avoid this always use parentheses:
#define FUDGE (SIZE - 2)
mvigsnhwar 17-Jan-13 7:40am    
Thank u sir :)
H.Brydon 17-Jan-13 13:45pm    
Correct ... +5 for this point.
Sergey Alexandrovich Kryukov 17-Jan-13 7:33am    
Right, a 5.
—SA

  1. You should not put semi-colons at the end of #define statements, they may have unwanted side effects in your code.
  2. When using expressions in #defines you should put them in parentheses, for the same reason.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Jan-13 7:33am    
Right, a 5.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900