Thread: How value substitute

  1. #1
    Registered User
    Join Date
    Feb 2022
    Posts
    73

    How value substitute

    I have defined preprocessor directive value in code.

    Code:
     #include<stdio.h>
    
    #define value 1
    
    
    int main ()
    { 
        int x;
        x = value; // 1 substitute for the value  
        printf(" x = %d", x);
        
        return 0;
    }
    I don't understand when 1 will subsitute for value. Does it substitute after code compile or it substitute before code compile ?

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It happens before.
    Code:
    $ gcc -E foo.c | tail
    
    # 6 "foo.c"
    int main ()
    {
        int x;
        x = 1;
        printf(" x = %d", x);
    
        return 0;
    }
    The -E option allows you to see what your code looks like after it's been through the pre-processor.
    This is what the compiler proper will compile.

    Note that all your #include files are expanded at this time, so the output of -E gets large pretty quickly.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Feb 2022
    Posts
    73
    i have read but my misunderstanding are not getting cleared

    Quote Originally Posted by Salem View Post
    It happens before.
    [code]
    $ gcc -E foo.c | tail
    The -E option allows you to see what your code looks like after it's been through the pre-processor.
    This is what the compiler proper will compile.

    Note that all your #include files are expanded at this time, so the output of -E gets large pretty quickly.
    I tried command gcc -E hello.c | tail

    I get following message

    'tail' is not recognized as an internal or external command,
    operable program or batch file.

    I am using windows 10 operating system

  5. #5
    Registered User
    Join Date
    Oct 2019
    Posts
    82
    Quote Originally Posted by Dadu@ View Post

    I tried command gcc -E hello.c | tail

    I get following message

    'tail' is not recognized as an internal or external command,
    operable program or batch file.

    I am using windows 10 operating system
    'tail' is a unix command. I am not sure there is a a windows equivalent but if you can probably get it via cygwin. Maybe via some other ways too?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Poor windows users can probably get by with
    gcc -E foo.c > foo.txt
    Then load the resulting text file in an editor.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Feb 2022
    Posts
    73
    Quote Originally Posted by Salem View Post
    Poor windows users can probably get by with
    gcc -E foo.c > foo.txt
    Then load the resulting text file in an editor.
    gcc -E hello.c > hello.txt

    one text file generates after running command. I don't know next what I need to do? How it's releated to my original question

    Code:
     # 1 "hello.c"
    # 1 "<built-in>"
    # 1 "<command-line>"
    # 1 "hello.c"
    
    
    # 2 "hello.c" 2
    
    
    
    
    
    
    
    
    
    
    # 6 "hello.c"
    int main ()
    {
        int x;
        x = 1;
        printf(" x = %d", x);
    
    
        return 0;
    }
    Last edited by Salem; 03-04-2022 at 10:50 AM. Reason: No need to post the processed stdio.h

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You wanted to see what the pre-processor did.

    And this shows you.

    Before
    How value substitute-screenshot-2022-03-04-14-18-58-png

    After
    How value substitute-screenshot-2022-03-04-14-19-26-png
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Feb 2022
    Posts
    73
    Quote Originally Posted by Salem View Post
    You wanted to see what the pre-processor did.
    I tried different code
    hello.c
    Code:
     #include<stdio.h>#define add(y) ((y) + 1)
    
    
    int main ()
    {
    	int y = 5;
    
    
    	printf("y = %d ", add(y));
    	
    	return 0;
    }
    what should be observed in this text file

    hello.text

    Code:
     # 1 "hello.c"# 1 "<built-in>"
    # 1 "<command-line>"
    # 1 "hello.c"
    
    
    
    
    
    # 2 "hello.c" 2
    
    
    
    
    
    
    # 4 "hello.c"
    int main ()
    {
     int y = 5;
    
    
     printf("y = %d ", ((y) + 1));
    
    
     return 0;
    }
    Last edited by Salem; 03-04-2022 at 10:49 AM. Reason: No need to post the pre-processed stdio.h

  10. #10
    Registered User
    Join Date
    Feb 2022
    Posts
    45
    This may help understand some of the broader ideas: Why function implementations shouldn't be put in header files.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concepts substitute/emulation for C++ (17)?
    By Chris87 in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2017, 02:22 AM
  2. Substitute string with file
    By mmk1234 in forum C Programming
    Replies: 4
    Last Post: 01-20-2015, 06:03 PM
  3. input asteriks to substitute characters
    By eastgod in forum C Programming
    Replies: 2
    Last Post: 12-10-2012, 08:37 AM
  4. any substitute for conio.h(turbo c++) in g++ compiler?
    By aditya1 in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2005, 04:10 PM

Tags for this Thread