Thread: C initialization

  1. #1
    Registered User
    Join Date
    Nov 2009
    Location
    Mysore, Karnataka, India
    Posts
    12

    C initialization

    Code:
    main(){
    	int a=0010;
    	printf("a: %d\n", a);
    	return 0;
    }
    I got the output as "a: 8", can any one answer why is it so??

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Because 0010 (or more simply: 010) specifies 8 in octal.
    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

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    A leading zero indicates to the compiler that the number is an octal number. You then print it's decimal equivalent. octal 010 -> decimal 8.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Nov 2009
    Location
    Mysore, Karnataka, India
    Posts
    12
    Code:
    int main(){
        int a=1;
        switch(a){
            int b=20;
            case 1: printf("b is %d\n",b);
                       break;
            default: printf("default b is:\t%d", b);
                        break;
        }
        return 0;
    }
    I got output as b is <some garbage value>, why is it so?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by vpshastry
    I got output as b is <some garbage value>, why is it so?
    What are you trying to do?
    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    $ gcc -Wall bar.c
    bar.c: In function ‘main’:
    bar.c:6: warning: ‘b’ may be used uninitialized in this function
    Compile it with some warnings.

    case labels are thinly disguised goto labels. As such, a 'goto' from the switch to case 1 will MISS the initialisation of b.
    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
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    because the declaration of b is skipped by your case statement. You need to declare b before your switch statement and then you can assign specific values inside your case statements
    EDIT:
    Code:
    #include <stdio.h>
    
    int main(void){
    
    	int a=1,b;
    	switch(a){
    		case 1:
    			b=20;
    			printf("b is %d",b);
    			break;
    		default:
    			b = 10;
    			printf("default is %d",b);
    			break;
    	}
    
    	getchar();
    	return(0);
    }
    Last edited by AndrewHunter; 09-16-2011 at 12:40 PM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Salem
    case labels are thinly disguised goto labels. As such, a 'goto' from the switch to case 1 will MISS the initialisation of b.
    Why is it considered an error when compiled with g++ instead of gcc ? (without -Werror)

    Does missing the initialization also mean missing the declaration in C++ and not C?

  9. #9
    Registered User
    Join Date
    Nov 2009
    Location
    Mysore, Karnataka, India
    Posts
    12
    In switch statement control should move directly to case statement skipping any other statements. Here it should skip int b=20; and show an error as b undeclared. Why is it not so?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by vpshastry
    In switch statement control should move directly to case statement skipping any other statements. Here it should skip int b=20; and show an error as b undeclared. Why is it not so?
    Because what is skipped is the initialisation of b. b is still declared to be in scope at the point of use. I would not be too concerned about the exact behaviour specified by the standard (though in this case I referred to the text of C99) as I would identify such code as containing a potential problem and fix it.
    Last edited by laserlight; 09-16-2011 at 01:00 PM.
    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

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by vpshastry View Post
    In switch statement control should move directly to case statement skipping any other statements. Here it should skip int b=20; and show an error as b undeclared. Why is it not so?
    As Laser pointed out this is not the case (pun intended). In fact this behavior is clearly defined in the C99 standard as an example:
    Quote Originally Posted by C99 6.8.4.2 example
    Code:
    switch (expr)
    {
         int i = 4;
         f(i);
    case 0:
         i = 17;
         /* falls through into default code */
    default:
         printf("%d\n", i);
    }
    the object whose identifier is i exists with automatic storage duration (within the block) but is never
    initialized, and thus if the controlling expression has a nonzero value, the call to the printf function will
    access an indeterminate value. Similarly, the call to the function f cannot be reached.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Direct X initialization
    By Know_Your_Role in forum C++ Programming
    Replies: 1
    Last Post: 07-08-2009, 04:05 PM
  2. Array Initialization
    By niteshsood in forum C Programming
    Replies: 2
    Last Post: 04-13-2009, 06:22 AM
  3. Constructor Initialization
    By Kane in forum C++ Programming
    Replies: 5
    Last Post: 04-05-2007, 07:12 AM
  4. Video initialization
    By Lionmane in forum C Programming
    Replies: 2
    Last Post: 05-25-2005, 12:49 PM
  5. Initialization. Are they the same?
    By lockpatrick in forum C++ Programming
    Replies: 3
    Last Post: 08-21-2002, 11:46 PM

Tags for this Thread