473,372 Members | 1,111 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,372 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 1326
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.