Basic C programming

Discussion in 'C' started by vivekgupta, Jun 11, 2012.

  1. vivekgupta

    vivekgupta New Member

    Joined:
    Jun 10, 2012
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    #include<stdio.h>
    void main()
    {
    printf("%d",printf("Computer"));
    }



    has a output = 8

    Why ? please explain someone.
    :confused:
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Can you explain what is %d used for and that would give you your answer.
     
  3. pein87

    pein87 Active Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    28
    Occupation:
    Web Dev
    Location:
    Limbo
    why are you making two calls to printf for? also you formating is returning the length of the text computer. Computer is 8 characters long. when using printf you need to use a format that goes with your data type. Your using decimal %d instead of string %s and making an additional call to printf as well. Try this

    Code:
    #include<stdio.h>
    void main()
    { 
    printf("%s","Computer");
    }
    additionally you need to use some sort of debugger or make a scanf reference to see the output.

    Code:
    #include<stdio.h>
    void main()
    { 
    printf("%s","Computer");
    char i = scanf("i");
    }
    in practice you don't call scanf like that(without formatting) but it will exit the program one you press enter allowing you to check the output. I'd say use system("PAUSE"); but that is windows only.
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    The code is valid, the output should actually be "Computer8". 8 is returned by printf, which returns the number of characters it prints.
     
  5. vivekgupta

    vivekgupta New Member

    Joined:
    Jun 10, 2012
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Why dont we use '&' in the case of strings in function scanf ??

    e.g. :-


    scanf("%s",date);

    here date is a character array means string.
    There is no & before date :(
    This the part of a code.


    Plz help
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Because date is already a pointer.
     
  7. annahussy

    annahussy Banned

    Joined:
    Jun 20, 2012
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Even I had the same question what vivek is asking to you? But, Now I got it that date is already a pointer then it should be use scanf("%s",date) like that. I am learning the C language, So this information will really be useful for me.
     
  8. jraco11

    jraco11 Banned

    Joined:
    Jun 25, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    The ampersand symbol '&' otherwise known as the address operator takes in the address of a certain variable. the scanf function requires addresses so therefore the '&' is necessary for data types such as int, short, long, double, float, etc....the fact the date is already a pointer (variables that hold ONLY addresses), there is no need to use the '&' to retrieve the address since this is what they are already holding.
     

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