I expected output for the below program as 10 20 but it is 10 10.
#include <stdio.h>
#define i 10
int main()
{
printf("%d\t",i);
fun();
printf("%d",i);
return 0;
}
fun(){
#undef i
#define i 20
}
If I assume like if a function call fun() returned back to main() then the original i value is printed then again I am wrong by looking at the output the below program,
#include <stdio.h>
#define i 10
fun(){
#undef i
#define i 20
}
int main()
{
printf("%d\t",i);
fun();
printf("%d",i);
return 0;
}
Expected output: 10 20 but the output is: 20 20
Can anybody please explain me the behaviour?
The #define is a preprocessor MACRO. The value is substituted at compile time instead of runtime.
So, the processing happens as per the presence (sequence) of the #defines. That means, you cannot expect the #undef and #define to work on runtime.
To elaborate, your case 1 code looks like
#include <stdio.h>
#define i 10
int main()
{
printf("%d\t",10);
fun();
printf("%d",10);
return 0;
}
fun(){
#undef i
#define i 20
}//now i is 20, but no one is using it, at compile time
and, your second code looks like
#include <stdio.h>
#define i 10
fun(){
#undef i
#define i 20 // i get a new definition here
}
int main()
{
printf("%d\t",20);
fun();
printf("%d",20);
return 0;
}
A note: The recommended signature of main() is int main(void).
One of the very first steps when compiling is to replace all the PREPROCESSING TOKENS with their value. So the evaluation is done at compile time, not at run-time.
So what you get for your first example is:
#include <stdio.h>
int main()
{
printf("%d\t",10); // we have only seen define i 10 until now
fun();
printf("%d",10); // we have only seen define i 10 until now
return 0;
}
fun(){
// the two in here would have made any i after this location be replaced with 20
}
And similar for your second case.
Related
#include <stdio.h>
#define ONE 1
#define TWO 2
#define OOPS 42
#define DEF_FOO(x) void foo_##x(void){ printf(#x" %d\n",x);}
DEF_FOO(ONE);
DEF_FOO(TWO);
DEF_FOO(OOPS);
int main()
{
foo_ONE();
foo_TWO();
foo_OOPS();
return 0;
}
gives :
ONE 1
TWO 2
OOPS 42
I would like this:
#include <stdio.h>
#define ONE 1
#define TWO 2
#define OOPS 42
#define DEF_BAR(x) void bar_??x??(void){ printf(#x" %d\n",x);}
DEF_BAR(ONE);
DEF_BAR(TWO);
DEF_BAR(OOPS);
int main()
{
bar_1();
bar_2();
bar_42();
return 0;
}
gives :
ONE 1
TWO 2
OOPS 42
or (its does not matter)
1 1
2 2
42 42
Could I use the value of a token? In theory, preprocessor should know its value, does it?
You'll need two levels of macros:
#define ONE 1
#define TWO 2
#define OOPS 42
#define INNER_DEF_BAR(x) void bar_##x(void){ printf(#x" %d\n",x); }
#define DEF_BAR(x) INNER_DEF_BAR(x)
DEF_BAR(ONE);
DEF_BAR(TWO);
DEF_BAR(OOPS);
int main()
{
bar_1();
bar_2();
bar_42();
return 0;
}
expands to
void bar_1(void){ printf("1"" %d\n",1); };
void bar_2(void){ printf("2"" %d\n",2); };
void bar_42(void){ printf("42"" %d\n",42); };
int main()
{
bar_1();
bar_2();
bar_42();
return 0;
}
And the other way to AKX's solution.
It's the same trick but at a different point to give the output:
ONE 1
TWO 2
OOPS 42
#define ONE 1
#define TWO 2
#define OOPS 42
#define BAR_NAME(x) bar_##x
#define DEF_BAR(x) void BAR_NAME(x)(void){ printf(#x" %d\n",(x)); }
DEF_BAR(ONE);
DEF_BAR(TWO);
DEF_BAR(OOPS);
int main()
{
bar_1();
bar_2();
bar_42();
return 0;
}
The 'trick' here is that undecorated uses of x in DEF_BAR are substituted but the #x isn't. So what is passed the BAR_NAME parametric macro is the substitution value of the parameter (1, 2 or 42 as applicable).
PS: I've put x in brackets because it's good practice. On the one hand I can't think of a case that would work with the token pasting other than a plain identifier.
PPS: Am I the only one who thinks ## is better called the token splicing not token pasting operator as is common.
Take this simple program and run it in Xcode:
#include <stdio.h>
#include "inttypes.h"
struct {
int32_t nwgts;
;
} clbint_;
#define clbint_1 clbint_
int main()
{
printf("clbint_1.nwgts: %d\n",clbint_1.nwgts);
clbint_1.nwgts=1; //Crash here
printf("clbint_1.nwgts: %d\n",clbint_1.nwgts);
return 0;
}
And when you run it you will get a crash on the line indicated that just says Thread 1: EXC_BAD_ACCESS (code=2, address=0x10078e18c). But if you change clbint_ to clbinta_ like this:
#include <stdio.h>
#include "inttypes.h"
struct {
int32_t nwgts;
;
} clbinta_;
#define clbint_1 clbinta_
int main()
{
printf("clbint_1.nwgts: %d\n",clbint_1.nwgts);
clbint_1.nwgts=1;
printf("clbint_1.nwgts: %d\n",clbint_1.nwgts);
return 0;
}
The results print exactly as I expect:
clbint_1.nwgts: 0
clbint_1.nwgts: 1
I am trying to understand why this is happening, clearly it has something to do with the variable name clbint_ but why does this behavior occur?
Hello folks out there,
this is my code:
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlite3.h"
#include "db_typedefs.h"
#include "operations.h"
int main(){
printf("Text\n");
int f = 3;
void add_mini(3);
}
operations.h
#ifndef ADD_OPERATIONS_H_INCLUDED
#define ADD_OPERATIONS_H_INCLUDED
void add_mini(int flag);
#endif // ADD_OPERATIONS_H_INCLUDED
operations.c
void add_mini(int flag)
{
int rc;
rc = flag;
printf("%i\n", rc);
}
Operations.c has also libraries included similar to main.c.
Compiler Error
error: expected declaration specifiers or '...' before numeric constant
regarding to void add_mini(3)
It seems like I'm unable to pass a simple integer value. While debugging it's even skipping the add_mini line.
Do you have any idea what's going on?
The whole code is embedded in a larger query to determine typed orders but this works fine. I just can't pass this simple integer value.
Thanks in advance.
When you use
void add_mini(3);
the compiler thinks it is a function declaration, not a function call. The argument 3 is not valid for a function declaration. Hence, the compiler complains.
Remove the void part to call the function.
int main(){
printf("Text\n");
int f = 3;
add_mini(3);
}
or, since you have initialized f to 3,
int main(){
printf("Text\n");
int f = 3;
add_mini(f);
}
Call the function like so: add_mini(3); rather than void add_mini(3);
Remove the word void for calling add_mini from main.c :
add_mini(3);
Or
(void)add_mini(3);
I'm a littlebit confused about the behaviour of preprocessing in c.
#include <stdio.h>
#define myMacro anotherMacro
#define anotherMacro 6
int main()
{
int dummy = myMacro;
printf("dummy = %d", dummy);
return 0;
}
in the above code snippet, the result will 6. however the the macro expansion in the initial pass will replace "myMacro" by "anotherMacro".
this is means that preprocessor will make a second pass to resolve "anotherMacro" to value 6.
The preprocessor will make a second pass. He works through the source file line per line.
So if he reaches the first define
#define myMacro anotherMacro
he will replace all occurrences of myMacro with the string anotherMacro.
The file will look like this after the line is handled:
#include <stdio.h>
#define anotherMacro 6
int main()
{
int dummy = anotherMacro;
printf("dummy = %d", dummy);
return 0;
}
Now the preprocessor could continue with the next #define
and replace every anotherMacro with the text 6
This question already has answers here:
Scope of macro puzzle
(2 answers)
Closed 5 years ago.
This question was asked in an interview and i completely had no idea about how the output differs. Following is the question
#define a 10
void foo();
int main()
{
printf("%d..",a);
foo();
printf("%d",a);
}
void foo()
{
#undef a
#define a 50
}
this gives me 10..10 as output while.,
#define a 10
void foo()
{
#undef a
#define a 50
}
int main()
{
printf("%d..",a);
foo();
printf("%d",a);
}
this gives me 50..50 as the output.
please explain. thank you
This happens because, preprocessing substitution happens even before the actual compilation, it does not take place in run-time.
The value (or definition) of a #define directive is valid as per the written code syntax, not as per the calling convention.
So, in the first case
#define a 10 // a is defined here
void foo();
int main()
{
printf("%d..",a); // a == 10, substitution happens
foo(); //nothing useful here
printf("%d",a); // a == 10, substitution happens
}
void foo()
{
#undef a // a vanished
#define a 50 // a is redefined as 50
} // a will be substituted to 50 hereon, is used
whereas, in second case
#define a 10 // a is defined here
void foo()
{
#undef a // a vanished
#define a 50 // a is redefined as 50
} // a will be substituted to 50 hereon, is used
int main()
{
printf("%d..",a); // a == 50, substitution happens
foo(); // nothing useful here
printf("%d",a); // a == 50, substitution happens
}
Preprocessor is run before the code is compiled in the order in which preprocessor directives appear in the file and not in the order of functions defined in the file.