Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ 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 16-Jan-13 23:19pm
Edited 16-Jan-13 23:33pm
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Did you compile and run it?
And the result is not as expected?
It is as expected when you perform the preprocessor tasks yourself:
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.
  Permalink  
Comments
mvigsnhwar at 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 at 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 at 17-Jan-13 7:40am
   
Thank u sir :)
H.Brydon at 17-Jan-13 13:45pm
   
Correct ... +5 for this point.
Sergey Alexandrovich Kryukov at 17-Jan-13 7:33am
   
Right, a 5.
—SA
mvigsnhwar at 17-Jan-13 8:43am
   
in this case semicolon permitted ah?
 
#include
#define JOIN(s1, s2) printf("%s=%s %s=%s \n", #s1, s1, #s2, s2);
int main()
{
char *s1="Hello";
char *s2="World";
JOIN(str1, str2);
return 0;
}
mvigsnhwar at 17-Jan-13 8:46am
   
for the above #define JOIN program when it gets replaced it is replaced as printf("%s=%s %s=%s \n", #s1, s1, #s2, s2);; in the place of printf("%s=%s %s=%s \n", #s1, s1, #s2, s2);
Jochen Arndt at 17-Jan-13 9:07am
   
Semicolon is always permitted. If there is one at the end of the define macro definition, it is inserted. If there is also one behind the used macro, you will have two at the end. This is no problem for the compiler (the second one is treated as the end of an empty instruction which is finally removed).
 
In general such macros should not be used anymore. A better solution is the usage of inline functions (here for your swap example):
inline void swap(int x, int y) { int t = x; y = x; y = t; }
 
With C++ you can define more with the same name with other parameter types:
inline void swap(double x, double y) { double t = x; y = x; y = t; }
 
For constants you may use const variables rather than definitions:
const int size = 10;
const int fudge = size - 2;
The 'const' keyword specifies that the content of the variable cant be changed (e.g. using 'size = 20;' later will throw a compiler error) and the compiler will usually replace it with the literal value.
 
The advantages of inline functions and const variables are type checking and avoiding all the side effects of definition macros.
 
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  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.
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 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
0 Sergey Alexandrovich Kryukov 410
1 Maciej Los 300
2 OriginalGriff 288
3 CHill60 190
4 Sinisa Hajnal 180


Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 17 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100