Google Answers Logo
View Question
 
Q: Finding the definitive answer for ANSI c questions ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Finding the definitive answer for ANSI c questions
Category: Computers > Programming
Asked by: borkmark_squil-ga
List Price: $15.00
Posted: 25 Feb 2003 11:28 PST
Expires: 27 Mar 2003 11:28 PST
Question ID: 166997
Hi,

I'm interested in finding the source of definitive answers for
advanced ANSI c questions. I program in c, and oftentimes wonder how
the compiler must intepret what I write.

An example question is the following:

switch (var)
{
    case 4:
            /* bla bla bla */
            break;
    default:
           /* bla bla bla */
           break;
    case 8:
          /* bla bla bla */
          break;
}

Question: If the value of 'var' is 8 in the above example, would it
enter the 'default' case or the 'case 8' case (note the order of the
cases)?

Again, I'm looking for the website(s) or book(s) that would contain
the final answer to such a question. Also, if you can answer the above
specific question itself that would be great, but it's not required.

Thanks.
Answer  
Subject: Re: Finding the definitive answer for ANSI c questions
Answered By: hammer-ga on 25 Feb 2003 12:01 PST
Rated:5 out of 5 stars
 
The ANSI standard for the switch statement says the following:
"[#5] The integer promotions are performed on the controlling
       expression.  The constant expression in each case  label  is
       converted   to   the   promoted   type  of  the  controlling
       expression.  If  a  converted  value  matches  that  of  the
       promoted   controlling  expression,  control  jumps  to  the
       statement following the matched case label.   Otherwise,  if
       there  is  a  default  label,  control  jumps to the labeled
       statement.  If no converted case constant expression matches
       and there is no default label, no part of the switch body is
       executed."

According to this, if var = 8, then case 8 will run. I have confirmed
this with a small sample program.

I found this information in the C ANSI standard. A copy can be found
at the DKUUG web site.

DKUUG 
http://std.dkuug.dk/JTC1/SC22/WG14/www/docs/n843.htm

Additional Resources
---------------------

Lysator University ANSI C Resources
http://www.lysator.liu.se/c/

American National Standards Institute
http://www.ansi.org/

Search Strategy
-----------------
Google Search Terms:
ANSI

By the way, for simple questions, writing a small test program is
often the quickest way to get an answer. <grin>

Good luck!

- Hammer
borkmark_squil-ga rated this answer:5 out of 5 stars
The response answered both questions I had, and was very timely
(within a couple of hours). Thanks!

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy