I have a doudbt about following.

Discussion in 'C Programming' started by GayatriS, Oct 4, 2018.

  1. GayatriS

    GayatriS

    Joined:
    Oct 4, 2018
    Messages:
    1
    Likes Received:
    0
    what will be the value of variable c ?

    main()
    {
    int a=10,c;
    c=printf("%d",a);
    printf("%d",c);
    }
     
    GayatriS, Oct 4, 2018
    #1
    1. Advertisements

  2. GayatriS

    John Joe

    Joined:
    Apr 25, 2017
    Messages:
    94
    Likes Received:
    14
    Location:
    Malaysia
    Why not use a compiler to compile ?
     
    John Joe, Oct 5, 2018
    #2
    1. Advertisements

  3. GayatriS

    JasKinasis

    Joined:
    Jun 27, 2018
    Messages:
    10
    Likes Received:
    4
    Location:
    /dev/null
    Initially the value of c could be anything, because it has not been initialised.

    And if you take a look at the function signature of printf, you will see that it returns an int, which represents the number of characters that printf wrote to the screen.
    So in this line:
    Code (Text):

    c=printf("%d",a);
     
    printf is called, passing the value of a (which is 10) - causing the string "10" to be printed to the screen. The string "10" contains two characters, so the printf function will return the value 2, which will be assigned to the variable c.
    So the final value of c will be 2.
    And because there are no newline characters or spaces in the output in any of the printf statements, your program will output:
    102

    Where "10" is the value of a and "2" is the value of c.

    Does that make sense?
     
    JasKinasis, Oct 8, 2018
    #3
    Ian likes this.
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.