Thread: What is the difference between #define n 10 and const int n=10???

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    82

    What is the difference between #define n 10 and const int n=10???

    can any one explain me plz that What is the difference between #define n 10 and const int n=10???

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    const int n = 10; is a numerical constant stored as an integer value.

    Code:
    const int n = 10;
    
    // the compiler sees this exactly as you wrote it
    if (n == 10)
     puts("n = 10");


    #define n 10 works by text substitution... that is where it finds n it replaces it with 10...

    Code:
    #define n 10
    
    // how you wrote it
    if (n == 10)
      puts("n = 10");
    
    
    // now the compiler sees it
    if (10 == 10)
      puts("n = 10");

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    82
    thanks alot......

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. const and #define
    By mohnish_khiani in forum C Programming
    Replies: 14
    Last Post: 02-27-2011, 02:29 AM
  2. const vs define
    By kotoko in forum C Programming
    Replies: 5
    Last Post: 11-14-2009, 11:02 AM
  3. difference between #define and #define ()
    By bored_guy in forum C Programming
    Replies: 8
    Last Post: 07-20-2009, 06:58 PM
  4. const vs #define
    By boss_nova in forum C Programming
    Replies: 2
    Last Post: 07-02-2008, 01:57 PM