Click here to Skip to main content
12,754,391 members (30,660 online)
Rate this:
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 17-Jan-13 0:19am
Updated 17-Jan-13 0:33am
Jochen Arndt166.4K
Rate this: bad
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.
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)
Right, a 5.
mvigsnhwar 17-Jan-13 7:40am
Thank u sir :)
mvigsnhwar 17-Jan-13 8:43am
in this case semicolon permitted ah?

#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 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 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.

H.Brydon 17-Jan-13 13:45pm
Correct ... +5 for this point.
Rate this: bad
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.

Right, a 5.

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

Advertise | Privacy | Mobile
Web02 | 2.8.170217.1 | Last Updated 17 Jan 2013
Copyright © CodeProject, 1999-2017
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