#define reg

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeyshree
    New Member
    • Jul 2010
    • 75

    #define reg

    Code:
    #include<stdio.h>
    #include<conio.h>
    Void fun();
    Main()
    {
    [B]Int a=20;
    #define a 10[/B]
    Clrscr();
    Printf(“%d”,a);
    Fun();
    [B]Printf(“\n%d”,a);[/B]
    Getch();
    }
    Void fun()
    {
    Printf(“\n%d”,a);
    #undef a
    }
    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?
    Last edited by Meetee; May 31 '12, 06:57 AM. Reason: code tags added
  • jeyshree
    New Member
    • Jul 2010
    • 75

    #2
    #define reg

    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.

    Comment

    • jeyshree
      New Member
      • Jul 2010
      • 75

      #3
      #define reg

      Code:
      #include<stdio.h>
      #include<conio.h>
      Void fun();
      Main()
      {
      Int a=20;
      #define a 10
      Clrscr();
      Printf(“%d”,a);
      Fun();
      Printf(“\n%d”,a);
      Getch();
      }
      Void fun()
      {
      Printf(“\n%d”,a);
      #undef a
      }
      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?

      Comment

      • jeyshree
        New Member
        • Jul 2010
        • 75

        #4
        preprocessor directive reg

        hai,
        i have a doubt.please look into the codes and then into my queries.
        Code:
        #include<stdio.h>
        #include<conio.h>
        Void fun();
        Main()
        {
        [B]Int a=20;
        #define a 10[/B]
        Clrscr();
        Printf(“%d”,a);
        Getch();
        }
        Code:
        #include<stdio.h>
        #include<conio.h>
        Void fun();
        Main()
        {
        [B]#define a 10
        Int a=20;[/B]
        Clrscr();
        Printf(“%d”,a);
        Getch();
        }
        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

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          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:

          Code:
          int a = 20;
          #define a 10
          
          printf("%d",a);
          produces this code for the compiler:

          Code:
          int a = 20;
          printf("%d",10);
          so every time you run the program you print 10.

          But do it this way:
          Code:
          #define a 10
          
          int a = 20;
          
          printf("%d",a);
          and you get this code to compile:
          Code:
          int 10 = 20;
          
          printf("%d",10);
          which gives you an error.
          Last edited by weaknessforcats; May 31 '12, 03:56 PM. Reason: typo

          Comment

          • Venki315
            New Member
            • Jun 2012
            • 1

            #6
            Hi Jeyshree,

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

            let us see the below code snippet
            Code:
            void main()
            {
             #define ONE 1
             #define TWO 2
             
             int val1,val2;
            
             val1 = ONE;
             val2 = TWO;  
             
             printf("value1 is %d & value2 is %d",val1,val2);
             getch();
            }
            All preprocessor statements will be modified and the below code will be considered for compilation
            Code:
            void main()
            {
             int val1,val2;
            
             val1 = 1;
             val2 = 2;  
            
             printf("value1 is %d & value2 is %d",val1,val2);
             getch();
            }
            When you compile your code, after prepocessor statements modification your code will be
            Code:
            Main()
            {
            Int a=20;
            Clrscr();
            Printf(“%d”,10);
            Fun();
            Printf(“\n%d”,10);
            Getch();
            }
            Void fun()
            {
            Printf(“\n%d”,10);
            // after this if you use 'a' that won't be replaced, as you 
            // undefined a here  
            }
            Code:
            void main()
            {
             int a = 20;
            #define a 10
             printf("%d",a);
            #undef a
             printf("%d",a);   
            }
            will be converted as
            Code:
            void main()
            {
             int a = 20;
             printf("%d",10);
             printf("%d",a); //as a is undefined hereafter   
            }

            Comment

            Working...