Doubt in C

Discussion in 'C' started by priyabc, Aug 11, 2007.

  1. priyabc

    priyabc New Member

    Joined:
    Feb 9, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Code:
    int i=3;
    switch(i)
    {
       default:
          printf("zero");
          break;
       case 1:
          printf("one");
          break;
       case 2:
          printf("two");
          break;
       case 3:
          printf("three");
          break;
    }
    the result obtained for this code is three.. what is the reason..

    i think zero might be the result since the default statement comes before the other case statement.. so a soon as it enters the switch it might execute the statements within default and exit the switch on seeing the break statement ... therefore zero will be the output... but it is not why...?????

    what happens exactly during the execution????
    help me friends!!
     
    Last edited by a moderator: Aug 12, 2007
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Look, the first thing you need to do is read the "Before you make a query" thread. It tells you to use code tags to retain the formatting of your code. Failing to do so is being rude to the people from whom you are seeking free help.

    It does not enter the default case because i matches one of the cases, namely 3. It is working as it should.

    This is three "doubt" posts. You need to read a dam' book, son.
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Get a good book in C.
     
  4. kaustubh

    kaustubh New Member

    Joined:
    Aug 13, 2007
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    0
    Priya,
    you have declared that int i = 3;
    therefore the c compiler will take i =3 ;
    if you say int i ; or int i = 0;
    then it will go to default .
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    It will go for default only when its not initialized and when int i =0; its initialized.
     
  6. kaustubh

    kaustubh New Member

    Joined:
    Aug 13, 2007
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    0
    Your right i forgot.
     
  7. indian.21987

    indian.21987 New Member

    Joined:
    Aug 19, 2007
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    default statement can be placed anywhere in the loop.
    But it will be executed only when all the cases didn't match.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice