|
-
September 28th, 2005, 06:04 AM
#1
how does it Works ?
Hi All,
Please see the code below.
This is the problem.
Code:
#include<stdio.h>
void change()
{
/*Write something in this function so that the
output of printf in main function should give 5 .
Do not change the main function */
}
int main()
{
int i=5;
change();
i=10;
printf("%d",i);
getch();
return 0;
}
The solution of the problem is given below.
Code:
#include<stdio.h>
void change()
{
/*Write something in this function so that the
output of printf in main function should give 5 .
Do not change the main function */
#define printf(fmt,x) printf ( "%d\n", 5 )
}
int main()
{
int i=5;
change();
i=10;
printf("%d",i);
getch();
return 0;
}
Now my Question is how does it Work ? since macro is define in the function i.e. it becomes local to function then how the out put of printf has changed?
Regards,
x8086
-
September 28th, 2005, 06:14 AM
#2
Re: how does it Works ?
Macros are evaluated by the preprocessor, and are not limited to any scope.
- petter
-
September 28th, 2005, 06:17 AM
#3
Re: how does it Works ?
Hello,
Preprocessor directives are taken care of before compiling, as and when they are encountered in the source file when parsing, whether they appear inside a function or not. You can control their inclusion only by other preprocessor directives like #if, #ifdef, #ifndef and #else or their logical combinations.
Regards.
Pravin.
28-09-2005.
-
September 28th, 2005, 06:25 AM
#4
Re: how does it Works ?
You do not want to change main function at all, then this is absolutely impossible 'printf' in main to print 5.
It seems that change function should return something, may take some pointer as an argument or you you should have 'i' as global variable. Either way this question is asked by STUDENT!
-
September 28th, 2005, 06:27 AM
#5
Re: how does it Works ?
One solution 
Code:
#define printf(fmt,x) putch('5')
-
September 28th, 2005, 06:47 AM
#6
Re: how does it Works ?
Hello Ajay Vijay,
You do not want to change main function at all, then this is absolutely impossible 'printf' in main to print 5.
I also thought so. But it does!
It is a surprise that why the printf defined in the macro does not call the macro.
Regards.
Pravin.
28-09-2005.
-
September 28th, 2005, 07:05 AM
#7
Re: how does it Works ?
There are three techniques used to define constants in C++. The first technique comes from the C programming language. Constants may be defined using the preprocessor directive, #define. The preprocessor is a program that modifies your source file prior to compilation. Common preprocessor directives are #include, which is used to include additional code into your source file, #define, which is used to define a constant and #if/#endif, which can be used to conditionally determine which parts of your code will be compiled. The #define directive is used as follows.
#define pi 3.1415
#define id_no 12345
Wherever the constant appears in your source file, the preprocessor replaces it by its value. So, for instance, every "pi" in your source code will be replace by 3.1415. The compiler will only see the value 3.1415 in your code, not "pi". The problem with this technique is that the replacement is done lexically, without any type checking, without any bound checking and without any scope checking. Every "pi" is just replaced by its value. The technique is outdated, exists to support legacy code and should be avoided.
So if u use ur #define after main then it does not impact like above and it prints 10 not 5 because #define appear in file after main function.
Code:
#include <iostream.h>
#include <conio.h>
#include<stdio.h>
void change();
int main()
{
int i=5;
change();
i=10;
printf("%d",i);
getch();
return 0;
}
void change()
{
/*Write something in this function so that the
output of printf in main function should give 5 .
Do not change the main function */
#define printf(fmt,x) printf ( "%d\n", 5 )
}
A Person who is polite is given goodness and a person who is away from Politeness is away from Goodness.
NAUMAAN
-
September 28th, 2005, 07:09 AM
#8
Re: how does it Works ?
It is a surprise that why the printf defined in the macro does not call the macro.
Well, the answer lies in the standard:
16.3.4 - Rescanning and further replacement [cpp.rescan]
-1- After all parameters in the replacement list have been substituted, the resulting preprocessing token sequence is rescanned with all subsequent preprocessing tokens of the source file for more macro names to replace.
-2- If the name of the macro being replaced is found during this scan of the replacement list (not including the rest of the source file's preprocessing tokens), it is not replaced. Further, if any nested replacements encounter the name of the macro being replaced, it is not replaced. These nonreplaced macro name preprocessing tokens are no longer available for further replacement even if they are later (re)examined in contexts in which that macro name preprocessing token would otherwise have been replaced.
-3- The resulting completely macro-replaced preprocessing token sequence is not processed as a preprocessing directive even if it resembles one.
-
September 28th, 2005, 07:13 AM
#9
-
September 28th, 2005, 08:11 AM
#10
Re: how does it Works ?
Thanls cilu for the clarification.
Have a nice day.
Regards.
Pravin.
28-09-2005.
-
September 28th, 2005, 08:57 AM
#11
Re: how does it Works ?
Just to be clear, this is a toy example that seems as if it was encountered as part of a class. Never do something like this. This sort of technique would be a code-maintenance nightmare.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|