Stupid Question, please help

This is a discussion on Stupid Question, please help within the C Programming forums, part of the General Programming Boards category; OK ok, I know this has been asked before but I did not understand one word of it to tell ...

  1. #1
    CAP
    CAP is offline
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179

    Stupid Question, please help

    OK ok, I know this has been asked before but I did not understand one word of it to tell you the truth.
    All I am trying to understand is something like

    main()
    {
    printf("What is this: %s or %d")
    return 0;
    }

    Now I don't think that is exactly correct but it is just to give you a clue so you can answer my question.
    I know that these % dealies are supposed to be "placeholders"?
    I don't understand this, someone said that if I have %s it will pass the argument onto something and they lost me.
    So I would like a clear answer, what I am trying to understand is if this code makes a line appear and the % things do speacil things like for one program I made from an example in a book to start to learn C. It is talking about memory management and it says to make the program and what it does is displays the amount of bits it takes up and they had something like:

    printf( "\nA char is %d bytes", sizeof( char ));

    how does the program know what %d does and how does it do it I know this question is worded oddly but I don't quite know even how to ask about this so please help.
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Okay, printf has what are called format flags. What they do is replace the flag with a corresponding argument to printf after the first string. The printf function takes a variable number of arguments, but it must have a string to begin with, such as:

    printf ( "This has to be here" );

    That first string is what printf prints to the screen, and if there are any format flags in that string then printf will replace them with further arguments:

    printf ( "This has to be %s", "here" );

    The output is the same, %s is replaced by the second argument "here" when printf is executed. These format flags and arguments go in order, the first flag goes with the first agument, the second flag with the second argument, etc...

    printf ( "%s %s %s %s %s", "This", "has", "to", "be", "here" );

    Once again the same output because each flag is replaced by the corresponding argument in order. Now, printf has several flags, one for each data type that can be passed to it. Such as %d for a decimal value:

    printf ( "%d", 10 );

    This will print the number 10 to the screen. What printf does is change the number into a string and print it, the format flags tell printf how to convert the argument to the correct string. You can guess that mixing up flags and arguments will cause problems:

    printf ( "%s %d", 10, "An int:" ); /* This won't work */

    The previous call to printf will hopefully not work because 10 is not a string and "An int:" is most certainly not a decimal integer. The use of these flags gives you a great deal of control over what printf prints to the screen and how it does so. Hopefully this clears things up at least a little bit.

    -Prelude
    My best code is written with the delete key.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The printf function takes arguments such as:

    int printf(const char *format, ...);

    This basicly means that the first argument to the function is a constant string of characters.

    The additional parameters are of variable length. This means that here may be no other arguments, or there may be 50 (or whatever).

    The printf function parses the first string and reads the "%" sections of the code. It encounters a %, then reads the next character to see what the next argument to the function is supposed to be.

    If it encounters no % symbols, it assumes there are no other arugments to the printf function.

    If it does encounter a %, it pulls the value of the argument and inserts it into the string in place of the %whatever.

    [edit]
    Doh. Beat me to it.
    [/edit]

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    16
    The % is a placeholder for printing numbers (ie - 1, 3, 4, 1882...), characters (ie - f, ' , 1 , E, and so on... Any key you can type ont he screen is a character), and strings (ie- "This is a String"). What you do, is put the % in the printf() statement wherever you want to print a number, character, or string, and after the %, put a d - decimal, c - character, f - floating point number, s - string, and so on(there are many more but i dont' want to explain them all, and for now you probably don't need them).

    here is an example :

    int main()
    {
    printf("I hope %s because this has been discussed here %d
    times last week.", "this makes sense", 5);
    }

    Now this will print out to the screen

    "I hope this makes sense because this has been discussed
    here 5 times last week"

    All you do is place one of the format specifiers (ie - f, d, c, s) after the %. And then outside of your quotes, you put your string, character, decimal in place of the format specifier. The computer doesn't know what to put there, thats why you have to put it there as a programmer. Also, most of the time you will not know what the program you are writing is going to have to print out, thats why you usually put one of your variables in the place of the 5 in the example above. ie :

    int main()
    {
    int big_number = 5;

    printf("I hope %s because this has been discussed here %d
    times last week.", "this makes sense", big_number);
    }


    this still prints out the same message. And for sizeof() function, all that does is tell you the size in bytes of a char in the example above, so that one message will probably print out:

    A char is 1 bytes

    I hope that can clear things up. Now, since i hopefully helped you CAP, you should go over to my site and write me a tutorial about what the format specifiers do. I need more www.snakshak.net Okay, you don't really have to HOPE THIS HELPS
    always learning, always frustrated

    http://www.snakshak.net

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    16
    man, i didn't even read down the page to find out that other wonderful programmers have already answered this, i suddenly feel pretty stupid now.
    always learning, always frustrated

    http://www.snakshak.net

  6. #6
    Unregistered
    Guest
    THANK YOU Prelude(sorry about the bold letters)
    That has to be the most clear explanation that I have ever seen, I actually know what you said and I get it now, thank you again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Question Probably
    By Kyrin in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 12:51 AM
  2. Replies: 7
    Last Post: 11-03-2005, 11:17 PM
  3. Stupid Question
    By digdug4life in forum C++ Programming
    Replies: 22
    Last Post: 05-17-2005, 11:43 AM
  4. stupid, stupid question
    By xelitex in forum C++ Programming
    Replies: 5
    Last Post: 12-22-2004, 07:22 PM
  5. Stupid question: What does Debugger do?
    By napkin111 in forum C++ Programming
    Replies: 6
    Last Post: 05-02-2002, 10:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21