Thread: sending constants as parameters

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    228

    sending constants as parameters

    What's the problem with the following:


    Code:
    #define K 3;
    
    int max(int a, int b) {
        return a>b? a : b;
    }
    
    int main() {
    
        cout<<max(K, K+3);
        return 0;
    }
    Why is it not allowed, and how is it different from:

    Code:
    int max(int a, int b) {
        return a>b? a : b;
    }
    
    int main() {
    
        cout<<max(3, 3+3);
        return 0;
    }
    Thanks in advanced.

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Well the problem is your #define is wrong

    Code:
    #define K 3;
    should be

    Code:
    #define K 3
    Edit: what compiler are you using? If you use gcc then you can look at the code that the "expanded" #defines produce by using gcc -E srcfname.c. You'll then be able to see the problem immediately.
    Last edited by Hodor; 11-21-2013 at 06:17 AM.

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    228
    Quote Originally Posted by Hodor View Post
    Well the problem is your #define is wrong

    Wonderful, thank you!

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You shouldn't really use #define anyway. Use

    constexpr my_type my_var = some_value;

    If your compiler doesn't support C++11, then use const instead of constexpr.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sending fewer parameters then arguments
    By gar35 in forum C++ Programming
    Replies: 1
    Last Post: 05-01-2010, 09:20 PM
  2. COnstants
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 09-01-2007, 04:59 AM
  3. Replies: 1
    Last Post: 02-06-2003, 03:33 PM
  4. Help with Constants and enum constants. Newbie learning C++.
    By UnregJDiPerla in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2003, 08:29 PM
  5. XOR-on constants
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 02-17-2002, 07:20 AM