473,385 Members | 1,944 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,385 software developers and data experts.

C preprocessor

2
I was told C preprocessor "#define arg1 agr2" would do a text replacement - arg1 string with arg2....

I wrote a few lines of code as follows:

#include "stdio.h"
#define PRNT(v,x) printf("x = %v\n", x)

main() {
int i = 5;
PRNT(d, i);
return 0;
}

I expected that after the preprocessing step, the program would become

main() {
int i = 5;
printf("i = %d\n", i);
return 0;
}

and it should print
i = 5

but the preprocessor #define did not work as expected! - the program printed

i = %v

???? Why does C behave that way? What didn't I understand? Please help
Thanks
EE02IU
P.S. I used Borland C++ 4.52
Oct 2 '06 #1
1 2725
Banfa
9,065 Expert Mod 8TB
I was told C preprocessor "#define arg1 agr2" would do a text replacement - arg1 string with arg2....

I wrote a few lines of code as follows:

#include "stdio.h"
#define PRNT(v,x) printf("x = %v\n", x)

main() {
int i = 5;
PRNT(d, i);
return 0;
}
a #defined symbol (PRNT in your case) is used for a text replacement...

except inside strings, the contents of strings are not parsed by the pre-processor for the replacement of #defined symbols

However you can achieve what you want like this

Expand|Select|Wrap|Line Numbers
  1. #define PRNT(v,x)   printf("x = %" #v "\n", x)
  2.  
This makes use of the following 2 features onf the C language

1. Separate strings seperated only by white space are concatinated by the C compiler, for instance

Expand|Select|Wrap|Line Numbers
  1.   printf("H"
  2.       "ell"
  3.       "o Wor"
  4.       "ld\n");
  5.  
outputs

Hello World

This is useful if you have very long strings as you don't have to fit them on one line, or for laying out multiline messages as you can put different output lines on different source lines

2. The preprocessor stringizing token #. #v is iterpreted by the preprocessor as a string containing whatever is in the parameter v
Oct 2 '06 #2

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

Similar topics

205
by: Jeremy Siek | last post by:
CALL FOR PAPERS/PARTICIPATION C++, Boost, and the Future of C++ Libraries Workshop at OOPSLA October 24-28, 2004 Vancouver, British Columbia, Canada http://tinyurl.com/4n5pf Submissions
24
by: Nudge | last post by:
I have an array, and an unrolled loop which looks like this: do_something(A); do_something(A); .... do_something(A); I thought: why should I type so much? I should write a macro. So I was...
16
by: Trying_Harder | last post by:
Is it possible to redefine a macro with global scope after undefining it in a function? If yes, could someone explain how? /If/ my question above isn't very clear you can refer to the...
13
by: seemanta dutta | last post by:
Greetings C gurus, I have used preprocessor directives since a very long time. But whenever I see some professional piece of C code, the linux kernel for example, I get literally confused by the...
18
by: /* frank */ | last post by:
My teacher said that array in C is managed by preprocessor. Preprocesser replace all array occurences (i.e. int a ) with something that I don't understand/remember well. What's exactly happens...
13
by: Chris Croughton | last post by:
Is the following code standard-compliant, and if so what should it do? And where in the standard defines the behaviour? #include <stdio.h> #define DEF defined XXX int main(void) { int...
9
by: Walter Roberson | last post by:
I have run into a peculiarity with SGI's C compiler (7.3.1.2m). I have been reading carefully over the ANSI X3.159-1989 specification, but I cannot seem to find a justification for the behaviour....
2
by: Paolo | last post by:
I imported a VC++6.0 project into VC++7.1. The conversion operation makes a mess with Preprocessor Definitions, adding a "$(NoInherit)" for each file. For example: I had a DLL project in VC++6.0...
32
by: spibou | last post by:
Is the output of the C preprocessor deterministic ? What I mean by that is , given 2 compilers which conform to the same standard, will their preprocessors produce identical output given as input...
31
by: Sam of California | last post by:
Is it accurate to say that "the preprocessor is just a pass in the parsing of the source file"? I responded to that comment by saying that the preprocessor is not just a pass. It processes...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.