Thread: Clarification on code output

  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    92

    Clarification on code output

    Hey everyone,


    I've been experimenting with C programming and came across an interesting code snippet. I'm a bit confused about its behavior and was hoping you could help me out.

    Code:
     #include <stdio.h>
    
    
    int main()
    
    
    {
    
    
                int x;
    
    
                x = 10;   
    
    
                if(x > 10)
    
    
                            x -= 10;
    
    
                else if(x >= 0)
    
    
                            x += 00;
    
    
                else if(x)
    
    
                            x += 10;
    
    
                else
    
    
                            x -= 10;
    
    
                printf("%d\n",x);
    
    
                return 0;
    
    
    }
    The output of this code surprised me; it's giving 10 as the output, but I expected it to be 20.In the given code, we have an integer variable x initially set to 10. The program then goes through a series of conditional statements to modify the value of x. Let's break it down step by step:

    if (x > 10) - This condition is not satisfied (10 is not greater than 10), so the program moves to the next condition.


    else if (x >= 0) - This condition is satisfied (10 is greater than or equal to 0). However, the statement x += 0; does not change the value of x, so it remains 10.


    else if (x) - This condition is also satisfied (10 is a non-zero value). The statement x += 10; modifies the value of x to 20.
    else - This block is not executed since the previous conditions were satisfied.


    As a result, the final value of x after these conditions is 20,

    Can someone explain why the final value of x is 10 instead of what I anticipated?


    Thanks in advance for your help!

  2. #2
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    there is a big difference between
    Code:
    if ( x == 1 )
       do something
    
    if ( x == 2 )
       do something
    if .......
    and

    Code:
    if ( some condition )
       do something
    else if (another condition )
        do something else
    else if ........
    
    else
      Default case
    the first one is 3 separate if statements each one will be tested. the second one is one block only one condition will be executed

    in your example the first condition that evaluates to true is as you say x >= 0 there for the next line is executed and zero is added to 10. That is then the end of the if block so the next line executed is your printf hence why the answer is 10 ( 10 + 0 )
    Last edited by cooper1200; 08-10-2023 at 02:12 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Just run the code in a debugger, step through it line by line if you're confused.
    Code:
    $ gcc -g foo.c
    $ gdb -q ./a.out
    Reading symbols from ./a.out...
    (gdb) b main
    Breakpoint 1 at 0x1155: file foo.c, line 13.
    (gdb) run
    Starting program: ./a.out 
    [Thread debugging using libthread_db enabled]
    Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
    
    Breakpoint 1, main () at foo.c:13
    13	            x = 10;   
    (gdb) n
    16	            if(x > 10)
    (gdb) 
    22	            else if(x >= 0)
    (gdb) 
    40	            printf("%d\n",x);
    (gdb) 
    10
    43	            return 0;
    (gdb)
    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.

  4. #4
    Registered User
    Join Date
    Oct 2022
    Posts
    92
    Quote Originally Posted by Salem View Post
    Just run the code in a debugger, step through it line by line if you're confused.
    I don't have gdb but I am using printf statement for debugging
    Code:
     
    
    #include <stdio.h>
    
    
    int main()
    {
                int x;
    
    
                x = 10;   
    
    
                if(x > 10){
                    x -= 10;
    				printf("True condition 1 x : %d\n", x);
    			}
    
    
                else if(x >= 0){
                    x += 00;
    				printf("True condition2 x : %d \n", x);
    			}
                else if(x){ // x = 10 none zero I don't understand why it doesn't execute  
                    x += 10;
    				printf("True condition3 x : %d \n", x);
    			}
    
    
                else{
                    x -= 10;
    				printf("True condition4 x : %d \n", x);
    			}
    
    
                printf("final x : %d\n",x);
    
    
                return 0;
    
    
    }
    True condition2 x : 10
    final x : 10

    my only question

    Code:
     else if(x){ // x = 10 none zero I don't understand why it doesn't execute  
                    x += 10;
    				printf("True condition3 x : %d \n", x);
    			}

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    because the condition above evaluates to true
    so that condition isn't tested

  6. #6
    Registered User
    Join Date
    Oct 2022
    Posts
    92
    Because a true condition was encountered , the subsequent else if and else conditions are skipped entirely

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I would like some clarification regarding this iterator code.
    By howardbc14 in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2015, 11:43 AM
  2. Need clarification of the following code...
    By EAX101010 in forum C Programming
    Replies: 2
    Last Post: 09-22-2014, 06:43 AM
  3. Output of this code
    By shah31 in forum C Programming
    Replies: 3
    Last Post: 10-22-2012, 01:39 AM
  4. what is the output of this code?
    By karthik537 in forum C Programming
    Replies: 15
    Last Post: 09-16-2009, 11:20 AM
  5. What will be the output of the following code?
    By developersubham in forum C++ Programming
    Replies: 13
    Last Post: 01-02-2007, 07:36 AM

Tags for this Thread