Thread: evaluation of const variables

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    29

    evaluation of const variables

    In this piece of code the switch statement is evaluated only one time or not?

    Code:
     
     const int n=1;
      
     while(1)
     {
      switch( n )
      { 
       case 1:
       {
       printf("1\n");
       break;
       } 
       case 2:
       {
       printf("2\n");
       break;
       }
      }
     }
    Last edited by frs; 09-05-2010 at 12:24 PM.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    a break in a switch only breaks from that switch-case.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The code does not make sense. Why do you declare opt as const but never initialise it, and then go on to use it?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    29
    Quote Originally Posted by laserlight View Post
    The code does not make sense. Why do you declare opt as const but never initialise it, and then go on to use it?
    Edit. Sorry my bad.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Right. I suspect that rags_to_riches has guessed the real question that you are asking. If not, I can tell you that the code either will not compile, or we cannot say, since it depends on the input and what exactly is the "do something" and "do something else".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    29
    Edit Again.

    Maybe I haven't expressed myself well.

    Since n is a const variable, the compiler replaces the 'while' content into printf("1\n") only?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by frs
    Since n is a const variable, the compiler replaces the 'while' content into printf("1\n") only?
    What do you mean? Ignore compiler optimisations: do you understand what your code snippet does? Tell us.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Are you saying that you believe the compiler optimizes away the rest of the switch case because the use of a constant guarantees that only one case will be entered?

    EDIT: Beaten by laserlight. This seems like another straight-up exam/quiz/homework question.

  9. #9
    Registered User
    Join Date
    Jun 2010
    Posts
    29
    Quote Originally Posted by laserlight View Post
    What do you mean? Ignore compiler optimisations: do you understand what your code snippet does? Tell us.
    print "1\n" in an infinite loop..

  10. #10
    Registered User
    Join Date
    Jun 2010
    Posts
    29
    Quote Originally Posted by rags_to_riches View Post
    Are you saying that you believe the compiler optimizes away the rest of the switch case because the use of a constant guarantees that only one case will be entered?
    I'm not saying I believe, I'm asking.

    EDIT: Beaten by laserlight. This seems like another straight-up exam/quiz/homework question.
    What this has to do with the case?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by frs
    print "1\n" in an infinite loop..
    Excellent. Now, observe that the switch's conditional expression is evaluated once on every iteration. Since we are dealing with an infinite loop, the answer to your question must be a no. That was easy

    If you want to bring in compiler optimisations, then it seems to me that the best that can be achieved in optimisation is to write it akin to:
    Code:
    while (1)
    {
        printf("1\n");
    }
    Look ma, no switch! But will this happen? It depends on the compiler and the options used. (Oh, and the answer to your question will still be a no, since the switch's conditional expression is then evaluated zero times.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Quote Originally Posted by frs View Post
    @rags_to_riches: This seems like another straight-up exam/quiz/homework question.
    What this has to do with the case?
    I'm against just giving people the answers to these kinds of questions, that's what. I'm funny and old fashioned that way.

    In my mind you were given the question by your teacher because you were expected to be able to answer it on your own, either by applying the knowledge received through your class or your textbook or by otherwise researching it. In the latter case, I'm pretty sure research is not equivalent to running to a forum and get someone to hand you the answer; it's tantamount to asking the person sitting next to you in the exam, or copying another person's homework.
    Last edited by rags_to_riches; 09-05-2010 at 12:58 PM. Reason: Fixed quote

  13. #13
    Registered User
    Join Date
    Jun 2010
    Posts
    29
    Look ma, no switch! But will this happen? It depends on the compiler and the options used. (Oh, and the answer to your question will still be a no, since the switch's conditional expression is then evaluated zero times.)
    The idea was to assign an input parameter to the 'const char' , so it would have to be evaluated at least one time.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by frs
    The idea was to assign an input parameter to the 'const char' , so it would have to be evaluated at least one time.
    And the point of all this is to...?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    The compiler will see two things
    1) n is const. It can safely replace n with 1, rather read from a variable read from n. As lasersight said it depends on compiler/options. But you can imagine that "it is a good idea to do so"
    2) You check a switch statement with a constant. I would think that some compilers will give a warning about this. Like "why do you switch with something constant?". Again, depends but it seems a "good idea" to warn. Well, if it went to that point, it probably will optimize the code if optimization are enabled.

    Now how will it optimize? It might do
    Code:
    {
       printf("1\n");
       goto ATRIP;
       printf("2\n");
       goto ATRIP:
    }
    ATRIP:
    ...
    Then it will have to think that some code is never going to be executed and not have it (note that break is not mandatory to exist in a case). So finally have what lasersight posted.

    Again, it might give you a warning also about this like "Unreachable code". So two warnings. One because you check a constant, two because some code is unreachable.

    The thing is that Compilers are NOT standardize. They can do whatever they want as long as the follow the standards of C. Or at least the standard.

    So my final answer: it depends of the compiler. Meaning you should NOT assume anything. Once you have found the compiler you would have to specify the options you use (or just say default). Then you can get a specific answer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why can't my perceptron learn correctly?
    By yann in forum C Programming
    Replies: 25
    Last Post: 10-15-2010, 12:26 AM
  2. stdio.h?
    By kiros88 in forum C Programming
    Replies: 5
    Last Post: 05-21-2010, 07:09 PM
  3. Another problem with templates
    By robatino in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2006, 04:32 PM
  4. "error: incomplete type is not allowed"
    By Fahrenheit in forum C++ Programming
    Replies: 9
    Last Post: 05-10-2005, 09:52 PM
  5. Color Variety
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 10-23-2002, 09:17 AM