473,395 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Macro Preprocessor, what is actually happening in here?

Hi,

I don't understand why the first C (c, not c++) program compiles and the second one does not compile.

Any idea what is happening with the macro pre-processor in here, why StateStart(0) can be used in the printf statement, but not in the #define?

TIA & Regards ...

First version: compiles
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. #define FIRST 4
  4.  
  5. #define StateStart(n) (STATE##n##_START)
  6.  
  7. #define STATE0_START (FIRST)
  8. #define STATE1_START (STATE0_START+5)
  9.  
  10. main()
  11. {
  12.     printf("State 0 start: %d\n", STATE0_START);
  13.     printf("State 1 start: %d\n", STATE1_START);
  14.     printf("State 0 start: %d\n", StateStart(0));
  15.     printf("State 1 start: %d\n", StateStart(1));
  16. }
  17.  
Second Version: Does not compile

Error: warning C4013: 'StateStart' undefined; assuming extern returning int
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. #define FIRST 4
  4.  
  5. #define StateStart(n) (STATE##n##_START)
  6.  
  7. #define STATE0_START (FIRST)
  8. #define STATE1_START (StateStart(0)+5)
  9.  
  10. main()
  11. {
  12.     printf("State 0 start: %d\n", STATE0_START);
  13.     printf("State 1 start: %d\n", STATE1_START);
  14.     printf("State 0 start: %d\n", StateStart(0));
  15.     printf("State 1 start: %d\n", StateStart(1));
  16. }
Feb 26 '08 #1
1 1329
weaknessforcats
9,208 Expert Mod 8TB
This is the translation unit I got from Visual Studio.NET 2005:
Expand|Select|Wrap|Line Numbers
  1. main()
  2. {
  3.     printf("State 0 start: %d\n", (4));
  4.     printf("State 1 start: %d\n", (((4))+5));
  5.     printf("State 0 start: %d\n", ((4)));
  6.     printf("State 1 start: %d\n", ((StateStart(0)+5)));
  7. }
  8.  
A StateStart(1) generates the token STATE1_START and that symbol is defined as (StateStart(0)+5). So that's what you get.

There is no rescan after a macro expansion unless a ## operator or a #define was involved and here none were so StateStart(0) was not seen as a macro.

You now die in the link with an unresolved external reference to StateStart.
Feb 26 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: qazmlp | last post by:
I hope comp.lang.c will not find the following question as a complete off-topic. I would like to remove ie.comment out the 'cout' statements during compilation(actually preprocessing) time. ...
1
by: John H. Krueckeberg | last post by:
How does the macro operator ## work? For example, given a macro like this #define REGISTER_CLASS(_class, _symname) _class::MetaClass instance##_class(_symname); What would...
3
by: John | last post by:
Please, consider this macro: #define mymacro(arg1, arg2) arg1 and arg2 Then it is used: mymacro(boys, girls) How is its expansion?
5
by: Eric Lilja | last post by:
Using a macro, can I change what type an object is being cast to? I know, the initial respone to question might be an instinctive "ugly, don't even think about it!" or "don't use macros at all",...
13
by: Shi Jin | last post by:
Hi there, While I was trying to disassemble a simple C program for my OS course, I found that the call to function longjmp is actually called to siglongjmp under linux. And similarly, under...
3
by: Charlie Zender | last post by:
Hi, I want to have a CPP macro that tests the value of a token and returns the string "No" if the token is undefined (or 0) and returns "Yes" if the token is defined (non-zero). Then I can...
3
by: Chris Saunders | last post by:
Hope this question is appropriate here. I'm writing an interface to some C code for the language Eiffel. I have come across this macro and am having difficuly interpreting what it returns: ...
17
by: ethan | last post by:
Hi All, How to write a macro with variable length of argument? For example, if i need a macro to check return value of printf. #define PRINTF_ESC(x, ...) if(printf(x, ...) == 10) goto end; ...
1
by: todWulff | last post by:
Good day folks. Let me open with the statement that I am not a C++/C programmer. The environment that I am programming in is ARMbasic, an embedded BASIC targeted toward ARM-based...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.