Thread: constant integeral expression?

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    230

    constant integeral expression?

    i'v been reading the tutorial about the switch statement on this website(http://www.cprogramming.com/tutorial/c/lesson5.html) and it mentions that the switch statement can't be used with "constant integeral expressions". exactly what is a "constant integeral expressions"?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It says exactly the opposite and gives an example of non-working code. For cases you need literals: case 10: or case 'A': etc

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    sorry, typing error. so what are literals?

  4. #4
    Jaguar
    Join Date
    Sep 2006
    Posts
    12
    They are compile time constants. You can't switch constants defined in your program.
    #define etc.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    They are compile time constants. You can't switch constants defined in your program.
    #define etc.
    Yes, you can, if the #defines are literals themselves. The compiler never sees the pre-preprocessor code so it doesn't care.
    Code:
    #define YES 0
    #define NO 1
    
    /* ... */
    
    switch(answer) {
    case YES: break;
    case NO: break;
    }
    You just can't use an expression that must be evaluated at runtime, eg
    Code:
    int x;
    switch(y) {
    case x:
    }
    You can use a constant expression, as long as that expression can be evaluated by the compiler (I think, not sure on this one):
    Code:
    int array[BUFSIZ];
    switch(size) {
    case sizeof(array)/sizeof(*array):
    }
    It's like initializing a global/static variable.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > exactly what is a "constant integeral expressions"?
    const - as in not a variable
    So you can't have case myVariable:

    integral - as in being an integer, and not a pointer or a float
    So you can't have case "hello": or case PI:

    expression - as in using simple arithmetic operators.
    Eg
    case 10:
    case '\n':
    case 5+5:
    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.

  7. #7
    Jaguar
    Join Date
    Sep 2006
    Posts
    12
    Quote Originally Posted by dwks
    Code:
    #define YES 0
    #define NO 1
    
    /* ... */
    
    switch(answer) {
    case YES: break;
    case NO: break;
    }
    but you can't have the defines in switch expression.
    Code:
    switch(YES){
    }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > but you can't have the defines in switch expression.
    Of course you can.
    It's dumb, not illegal - that's all.
    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.

  9. #9
    Jaguar
    Join Date
    Sep 2006
    Posts
    12
    Yep, that's right.
    it's useless.

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Unless you're trying to implement an 11-bit ALU in the preprocessor or something similiarly insane...
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM