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

#define reg

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. Void fun();
  4. Main()
  5. {
  6. Int a=20;
  7. #define a 10
  8. Clrscr();
  9. Printf(“%d”,a);
  10. Fun();
  11. Printf(“\n%d”,a);
  12. Getch();
  13. }
  14. Void fun()
  15. {
  16. Printf(“\n%d”,a);
  17. #undef a
  18. }
Output:
10
10
10
Doubt:
a)my first doubt iws whenever we print the value of a in the main function it prints 10(it does not print the value of the variable a=20).how to use the value of the variable a in the main function?
b)my second doubt is that as the a is un defined in the function fun(),the printf statement executed after the fun(bolded in the above program) should have printed 20 or it should have shown an error.instead it prints 10.then what is the use of undef statement?
May 31 '12 #1
5 2253
hai,
i have a doubt.please look into the snippet.my question is based on the snippet.
Snippet1:
#include<stdio.h>
#include<conio.h>
Void fun();
Main()
{
Int a=20;
#define a 10

Clrscr();
Printf(“%d”,a);
Getch();
}
Snippet2:
#include<stdio.h>
#include<conio.h>
Void fun();
Main()
{
#define a 10
Int a=20;

Clrscr();
Printf(“%d”,a);
Getch();
}
snippet 1 does not show an error whereeas snippet 2 show an error why.please explain ehy this happens.thanks for your reply in advance.
May 31 '12 #2
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. Void fun();
  4. Main()
  5. {
  6. Int a=20;
  7. #define a 10
  8. Clrscr();
  9. Printf(“%d”,a);
  10. Fun();
  11. Printf(“\n%d”,a);
  12. Getch();
  13. }
  14. Void fun()
  15. {
  16. Printf(“\n%d”,a);
  17. #undef a
  18. }
  19.  
Output:
10
10
10
Doubt:
a)my first doubt iws whenever we print the value of a in the main function it prints 10(it does not print the value of the variable a=20).how to use the value of the variable a in the main function?
b)my second doubt is that as the a is un defined in the function fun(),the printf statement executed after the fun(blue font in the above program) should have printed 20 or it should have shown an error.instead it prints 10.then what is the use of undef statement?
May 31 '12 #3
hai,
i have a doubt.please look into the codes and then into my queries.
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. Void fun();
  4. Main()
  5. {
  6. Int a=20;
  7. #define a 10
  8. Clrscr();
  9. Printf(“%d”,a);
  10. Getch();
  11. }
  12.  
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. Void fun();
  4. Main()
  5. {
  6. #define a 10
  7. Int a=20;
  8. Clrscr();
  9. Printf(“%d”,a);
  10. Getch();
  11. }
  12.  
snippet 1 does nor show an error whereas snippet 2 shows an error.why is it so?in which way they are different?
thanks for your reply in advance
May 31 '12 #4
weaknessforcats
9,208 Expert Mod 8TB
You are confused on the order of events. First, the preprocessor goes through your code and does the #define. Then, the code is compiled.

So:

Expand|Select|Wrap|Line Numbers
  1. int a = 20;
  2. #define a 10
  3.  
  4. printf("%d",a);
produces this code for the compiler:

Expand|Select|Wrap|Line Numbers
  1. int a = 20;
  2. printf("%d",10);
so every time you run the program you print 10.

But do it this way:
Expand|Select|Wrap|Line Numbers
  1. #define a 10
  2.  
  3. int a = 20;
  4.  
  5. printf("%d",a);
and you get this code to compile:
Expand|Select|Wrap|Line Numbers
  1.  
  2. int 10 = 20;
  3.  
  4. printf("%d",10);
which gives you an error.
May 31 '12 #5
Hi Jeyshree,

First thing you need to know is when we compile the code, all the preprocessor statements(which starts with #) will be modified and the modified code will taken for compilation.

let us see the below code snippet
Expand|Select|Wrap|Line Numbers
  1. void main()
  2. {
  3.  #define ONE 1
  4.  #define TWO 2
  5.  
  6.  int val1,val2;
  7.  
  8.  val1 = ONE;
  9.  val2 = TWO;  
  10.  
  11.  printf("value1 is %d & value2 is %d",val1,val2);
  12.  getch();
  13. }
  14.  
All preprocessor statements will be modified and the below code will be considered for compilation
Expand|Select|Wrap|Line Numbers
  1. void main()
  2. {
  3.  int val1,val2;
  4.  
  5.  val1 = 1;
  6.  val2 = 2;  
  7.  
  8.  printf("value1 is %d & value2 is %d",val1,val2);
  9.  getch();
  10. }
  11.  
When you compile your code, after prepocessor statements modification your code will be
Expand|Select|Wrap|Line Numbers
  1. Main()
  2. {
  3. Int a=20;
  4. Clrscr();
  5. Printf(“%d”,10);
  6. Fun();
  7. Printf(“\n%d”,10);
  8. Getch();
  9. }
  10. Void fun()
  11. {
  12. Printf(“\n%d”,10);
  13. // after this if you use 'a' that won't be replaced, as you 
  14. // undefined a here  
  15. }
  16.  
Expand|Select|Wrap|Line Numbers
  1. void main()
  2. {
  3.  int a = 20;
  4. #define a 10
  5.  printf("%d",a);
  6. #undef a
  7.  printf("%d",a);   
  8. }
  9.  
will be converted as
Expand|Select|Wrap|Line Numbers
  1. void main()
  2. {
  3.  int a = 20;
  4.  printf("%d",10);
  5.  printf("%d",a); //as a is undefined hereafter   
  6. }
  7.  
Jun 1 '12 #6

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

Similar topics

18
by: Bryan Parkoff | last post by:
"#define" can only be inside the global scope before main() function. "#if" can be tested after "#define" is executed. The problem is that "#define" can't be inside main() function. I do not wish...
2
by: Andreas | last post by:
Hi! I'm using an IplImage library from camellia.sourceforge.net, and the testbench calls a file only containing code like the one below. In cam_morphomaths_code.c the real computation is made,...
3
by: theotyflos | last post by:
Hi all, I have the following: /*--- SNIP ---*/ typedef struct Argument_s { char *address; int type;
9
by: pozz | last post by:
Hi all, I have the below #defines #define NUMBER1 30 #define NUMBER2 50 #define SUM (NUMBER1+NUMBER2) #define STRING1 "Byte: \x30" #define STRING2 "Byte: \x50"...
34
by: BQ | last post by:
Hello Is there a way to declare 'FUNCT' via a define so that if its parameter x, a constant, is greater than 35, it returns 56, if not, 20. I would like that at compile time, not at run time. ...
17
by: niraj.tiwari | last post by:
What is meaning of the following define:- #define x(argl...) x1(##argl)
71
by: David T. Ashley | last post by:
Where is the best place to define TRUE and FALSE? Are they in any of the standard include files, ever? Do any standards apply? What I've traditionally done is something like: #ifndef...
6
by: anirbid.banerjee | last post by:
Hi, I need to write a macro which would have a lot many conditional #ifdef ... #endif blocks in it. #define _xx_macro (x) { ... \ ... \ /* some code (); */ #ifdef _SOME_STMT \ ... \ ... \
23
by: anon.asdf | last post by:
Hello! In the following code-snippet, is it possible to initialize each element of arr, with STRUCT_INIT? struct mystruct { int a; char b; };
2
by: badc0de | last post by:
Hello.. a header file of one of my project has macro definitions like this (for example) #define PART_A_SORT_1_LABEL_STRING1 100 #define PART_A_SORT_1_LABEL_STRING2 200 #define...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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.