Thread: using const

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    380

    using const

    Why won't this usage of const work? If I compile it as c++ is works. I'm using Borland C++ 4.5.
    Code:
    #include <stdio.h>
    #include <conio.h>
    const char K_ESC = 0x1B;
    
    int main()
    {
    
     char c;
     c = getch();
     switch(c)
     { case K_ESC : printf("You have escaped!");
    	          break;
     }
    getch();
    }

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Code:
    const char K_ESC = '0x1B'; /*note quotes*/
    not sure of 0x1B for escape key try '\x1b'
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Code:
    #define K_ESC '\1B'
    By the way. Even a const char is a variable, although it may not be altered. The cases in the switch structure do really need real constants.
    Last edited by Shiro; 01-12-2002 at 09:49 AM.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    have modified your program slightly

    Code:
    #include <stdio.h>
    #include <conio.h>
    #define K_ESC  '\x1B'
    
    int main()
    {
    
     char c;
     c = getch();
     if(c == K_ESC)
           printf("You have escaped!");
    
     else 
           printf("You didnot hit Esc");
    
    getch();
    return 0;
    }
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    const has different meanings in C and C++

    In C
    &nbsp; const char K_ESC = 0x1B;
    All this means is that K_ESC should not be changed, and that the compiler should warn you if you try and do something like K_ESC++;

    The case statement requires
    &nbsp; case constant-expression (eg. 2 + 3 - 4 )
    but in your context, you have
    &nbsp; case identifier
    and the compiler can't look inside that identifier at compile time to get the const value.

    In C++
    &nbsp; const char K_ESC = 0x1B;
    This really is a constant, and is somewhat akin to
    &nbsp; #define K_ESC 0x1B
    but with all the advantages of type checking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM