Thread: # operator in C

  1. #1
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95

    # operator in C

    I am reading about the preprocessor features and functions. The author states "If you place a # in front of a parameter in a macro definition, the preprocessor creates a constant string out of the macro argument when the macro is invoked." And one of the examples they give is:

    Code:
    #include <stdio.h>
    #define printint(var) printf( # var " = %i\n", var)
    int main(void)
    {
    int count = 2;
    printint (count);
    }
    Now they just say that the #define statement would cause printint to change to printf with the added content. But the above code does not print anything. Do I need anything else I am missing?
    Last edited by zolfaghar; 06-13-2016 at 11:53 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    No, there is nothing missing. In my own tests, it produces the expected outcome. Maybe your window is closing before you can see the output?

  3. #3
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95
    Quote Originally Posted by whiteflags View Post
    No, there is nothing missing. In my own tests, it produces the expected outcome. Maybe your window is closing before you can see the output?
    yes; it runs. I am not sure why it did not earlier. I added some more stuff, and now I get error messages. Here is what I have:
    Code:
    #include <stdio.h>
    int count, x;
    #define printx(n) printint (x ## n)
    #define printint(var) printf( # var " = %i\n", var)
    int main(void)
    {
    count = 2;
    x = 3;
    printx (10);
    printint (count);
    }
    Code:
    $ gcc test.c -o test
    test.c: In function 'main':
    test.c:3:29: error: 'x10' undeclared (first use in this function)
     #define printx(n) printint (x ## n)
                                 ^
    test.c:4:48: note: in definition of macro 'printint'
     #define printint(var) printf( # var " = %i\n", var)
                                                    ^
    test.c:9:1: note: in expansion of macro 'printx'
     printx (10);
     ^
    test.c:3:29: note: each undeclared identifier is reported only once for each function it appears in
     #define printx(n) printint (x ## n)
                                 ^
    test.c:4:48: note: in definition of macro 'printint'
     #define printint(var) printf( # var " = %i\n", var)
                                                    ^
    test.c:9:1: note: in expansion of macro 'printx'
     printx (10);
     ^
    I tried declaring the variables globally. I am not sure what else to do.
    Last edited by zolfaghar; 06-14-2016 at 12:38 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Did you compile it?

    Calling the program 'test' is a bad idea in Unix/Linux, and other environments which use a unix-like shell (such as mingw).
    test is a built-in command of many shells, so just typing 'test' invokes that built-in.

    You might get further if you type in ./test.exe
    assuming that the program is called test.exe
    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.

  5. #5
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95
    Quote Originally Posted by Salem View Post
    Did you compile it?

    Calling the program 'test' is a bad idea in Unix/Linux, and other environments which use a unix-like shell (such as mingw).
    test is a built-in command of many shells, so just typing 'test' invokes that built-in.

    You might get further if you type in ./test.exe
    assuming that the program is called test.exe
    Thanks. It worked. I moved the content to another file. Here is the corrected file:
    Code:
    #include <stdio.h>
    int count, x10=1;
    #define printx(n) printint (x ## n)
    #define printint(var) printf( # var " = %i\n", var)
    int main(void)
    {
    count = 2;
    printx (10);
    printint (count);
    }
    Last edited by zolfaghar; 06-14-2016 at 01:28 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ternary Operator to choose which operator to use
    By cncool in forum C Programming
    Replies: 7
    Last Post: 06-27-2011, 01:35 PM
  2. [ ] operator requires + operator?
    By m37h0d in forum C++ Programming
    Replies: 10
    Last Post: 02-04-2009, 10:21 AM
  3. Replies: 3
    Last Post: 12-09-2008, 11:19 AM
  4. Replies: 2
    Last Post: 07-07-2008, 03:46 AM
  5. Replies: 1
    Last Post: 07-07-2008, 03:38 AM

Tags for this Thread