Problem to understand the O/p of the CODE

Discussion in 'C' started by Mohit_Malhotra, Nov 20, 2010.

  1. Mohit_Malhotra

    Mohit_Malhotra New Member

    Joined:
    Nov 20, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hello i have created a simple program but not finding the solution to understand the Output of the code.


    Please any one Solve this code and output:----- Help!!!!!!
    {
    int a,b,c,d;
    printf("%d%d%d%d",3,3+2,c,a+b*c-d);
    getch();
    }
     
  2. ihatec

    ihatec New Member

    Joined:
    Sep 1, 2010
    Messages:
    20
    Likes Received:
    3
    Trophy Points:
    0
    I am not surprised, you don't understand your output. To get proper output first you should initialize your variables with some values.
     
  3. animadhu

    animadhu New Member

    Joined:
    Nov 21, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    when u dont initialize ur variables how can u expect the appropriate output?
    if u want u initialize den try the below statements before ur printf statement:
    printf("enter the valuse of a,b,c,d);
    scanf("%d,%d,%d,%d",&a,&b,&c.&d);
    now u cn get ur desired ans :happy:
     
  4. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    let me change your code a little bit in order to explain better

    Code:
    #include <stdio.h>
    
    int main(){
    [COLOR=Red]int a=1,b=2,c=3,d=4;[/COLOR]
    printf("%d%d%d%d",3,3+2,c,a+b*c-d);
    getchar();
    return 0;
    }
    
    in your printf you have a string inside the quotes "%d%d%d%d "
    and a series of numbers and variables separated by comma (,)

    when printf finds inside the string this--->%d it means replace it with an integer value
    the first %d is replaced by the first integer after the string
    so we have
    %d-->3
    %d-->3+2 --->5
    %d-->c ---->in my code is 3
    %d-->a+b*c-d --> in my code is 1+2*3-4=3

    so what it prints now?
    3533
     
    shabbir likes this.

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