Thread: Preprocessor basic functionality

  1. #1
    Registered User Ranjeet.k's Avatar
    Join Date
    Feb 2013
    Posts
    4

    Preprocessor basic functionality

    Hello all,
    I just want to see the functioning of preprocessor in simple program but its not working accordingly. Please let me know where I am making mistake

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    
    
    {
        int x ;
        printf("Enter the number either 1 or 2\n"); 
        scanf("%d", &x);
    
    
    #ifdef x 1
    printf("Entered number is 1");
    
    
    #else
    printf("Entered number is 2");
    
    
    #endif
    }
    But each time output is
    Entered number is 2.

    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Because it's the PRE-processor.

    PRE meaning before.
    PRE as in before the compiler even sees the code, never mind before the code gets executed.

    You need to think of the pre-processor as a fairly stupid text editor with some simple instructions
    - copy/paste - #include
    - search/replace - #define
    - choice - #if / #elif / #else
    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.

  3. #3
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    ifdefs don't work like that. Preprocessor is called a "pre"-processor for a reason - it knows nothing about the internals of the C program, variables, scope of anything else. It's purely a text processor that runs before the compiler. So it has no idea what x is. It's just a piece of text to the pre-processor.

    And because the pre-processor runs BEFORE the program is compiled, you can't use the result of something like a scanf and test it in an ifdef - the ifdef is evaluated and removed before the program has even finished compiling.

    ifdefs are for things like replacing a piece of code with a platform-specific equivalent, so you could:

    Code:
    #ifdef WIN32
       printf("Windows!");
    #else
      #ifdef LINUX
        printf("Linux!");
      #else
        printf("I have no idea!");
      #endif
    #endif
    and the preprocessor will replace ALL those lines, before the compiler even sees the code with the appropriate one depending on what platform is "#define"'d by the person / files compiling. If you use gcc -E (I believe), it will run just the preprocessor for you and you will literally see only:

    printf("Windows!")

    in that file if it is compiled on a Windows machine, and the other lines if compiled on other machines.

    Pre-processor process the C file PRIOR to the compile. They know nothing of C syntax (in fact, you can use the preprocessors to do a lot of things that aren't even C - I use my C preprocessor to create platform-dependent files like Windows icon resource files, etc.) and thus you can't use them like you think.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can this WinForm functionality be implemented?
    By y99q in forum C# Programming
    Replies: 7
    Last Post: 02-29-2012, 12:23 AM
  2. Questions about cursor functionality
    By kantze in forum Windows Programming
    Replies: 4
    Last Post: 08-22-2008, 06:42 AM
  3. Replies: 7
    Last Post: 09-24-2006, 10:13 AM
  4. Functionality of Linked Lists
    By MisterWonderful in forum C++ Programming
    Replies: 7
    Last Post: 03-07-2004, 05:48 PM
  5. Fork functionality question
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 06-26-2002, 03:15 PM