Thread: Printing define variable

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    11

    Printing define variable

    Hello everyone I want to print my define variable in a function. Can anyone explain me how to do this?

    Code:
    #define Page 5
    #define Test 7
    void printDefineVariable() {
    
    
    }
    Thanks for helping !

  2. #2
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    Use printf statement.

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    Quote Originally Posted by Jimmy589 View Post
    Hello everyone I want to print my define variable in a function. Can anyone explain me how to do this?

    Code:
    #define Page 5
    #define Test 7
    void printDefineVariable() {
    
    
    }
    Thanks for helping !
    Code:
    #define Page 5
    #define Test 7
    void printDefineVariable() {
    printf("%i", Page);
    
    }

  4. #4
    Registered User
    Join Date
    Apr 2017
    Posts
    11
    Well I googled this problem and I read something that you cant just simple print it. Looks like you can thats confusing me a bit now. One more thing can I also Print the Page too? So my console looks like this : "Page 5" and not only 5

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    I don't know why you would have that idea but I think it's because you don't know what #define does.

    First of all, it's not a variable. It's simply a temporary name. Before the compiler runs, the preprocessor goes around and replaces the defined names with what they were defined as, doing nothing other than simple substitution.

    In your case, it replaces Page and Test with 5 and 7 respectively. Nothing more complicated than that. If you know how to print integers, you know how to print those defines you have.

    On the other hand, if you want the name of the define, there are some preprocessor tricks you can do, but why bother. Just use the name inside a string.
    Last edited by GReaper; 04-26-2017 at 06:30 AM.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-21-2012, 04:51 PM
  2. How to define a variable for storing string value?
    By adnilsah017 in forum C Programming
    Replies: 2
    Last Post: 06-05-2011, 06:57 PM
  3. using environment variable: gcc -D vs. #define
    By MK27 in forum C Programming
    Replies: 8
    Last Post: 03-13-2011, 09:20 PM
  4. How to define variable of specific size.
    By User Name: in forum C Programming
    Replies: 4
    Last Post: 06-05-2010, 01:33 AM
  5. how to define the variable of a edit box?
    By Jasonymk in forum Windows Programming
    Replies: 2
    Last Post: 02-10-2003, 09:23 PM

Tags for this Thread