%d=%C, how it works?

Discussion in 'Programmer's Corner' started by engrrehmat, Jan 30, 2013.

  1. engrrehmat

    Thread Starter New Member

    Dec 17, 2010
    26
    0
    int main()
    {

    int i;

    for(i=1; i<256; i++)

    printf("%d=%c",i,i);


    }

    how this program works, i mean how character(%c) works, i am confused, any one kindly explain.
     
  2. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    printf format commands

    That link explains it better than I can. printf is a big library for displaying data in many different ways.

    A quick rundown: The %c means "Display as a character", while %d means "display as a decimal number". Around those formatting characters are the rest of the string you want to display. Then, after the closing quotes, you enter the variables that "fill in" each % format directive.
     
  3. engrrehmat

    Thread Starter New Member

    Dec 17, 2010
    26
    0
    while(getch() !='\r')

    how it works here, i mean the \r??

    the program is this

    int count=0;

    printf("type in a phrase:");

    while(getch() !='\r')

    count++;

    printf("character count is:%d",count);
     
  4. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    '\r' is a carriage return...
    ...take a look here
     
  5. WBahn

    Moderator

    Mar 31, 2012
    17,786
    4,806
    Just like the % character in a format string tells the printf() function to use the following characters in a special way as opposed to just printing them directly, the \ character does the same thing. Basically (and somewhat over simplistically) is says to use the next character to print something that I can't include directly in the format string. So \r tells it to print a carriage return (return the curser to the front of the present line) while \n tells it to print a newline (move the curser to the front of the next line). What if you want to print a double quote mark? You can't just put it in the string because that will be seen as the end of the string, so you use \". If you want to print a backslash, the same thing creeps up because if you just use it the function will think you are telling it to do something special, so you use \\.

    You need to read a good reference on the printf() function. There are tons of them online.
     
  6. jaygatsby

    New Member

    Nov 23, 2011
    185
    23
    printf pulls format string data off of the stack... %c tells printf to pull a character, %d an integer, and "\r" is a representation for carriage return. On Unix you do this with "\n"... read 'man 3 printf'
     
  7. jaygatsby

    New Member

    Nov 23, 2011
    185
    23
    Since nobody's been posting, I'll do some examples:

    print("%s\n", string);
    printf("%s", "Lawel this forum is awesome!");
    printf("%i", float_far); <- prints decimal point stuff
    printf("%f", float_far); <- prints only integer part

    When you said %d... most people do that with %i
     
  8. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,017
    Nobody is posting??? I see at least 3 other than you. How many answers are needed? The one the posted the best answer was thatoneguy. Something the OP could have easily looked up for him/herself.
     
  9. WBahn

    Moderator

    Mar 31, 2012
    17,786
    4,806
    Huh??? The OP posted his query nearly two weeks ago and received three responses within five hours. Since the OP has not followed up with any requests for further information, it is reasonable to assume that they are satisfied with the responses they got.
     
  10. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,017
    Me thinks a shameless attempt to bump up jaygatsby's post count. There were more answers by forum members than questions from the OP, And again all at the good graces of forum member's when the answer could have been obtained with 5 minutes of searching.
     
    Last edited: Feb 10, 2013
  11. WBahn

    Moderator

    Mar 31, 2012
    17,786
    4,806
    I've noticed a real schitzophrenic (sp?) tendency in "today's generation", and I know that it is somewhat a matter of lumping the behavior of two subsets of a group together and ascribing them to the whole group, but on the one hand they are so in tune with being connected all the time that they will search for the most trivial things at the drop of a hat, yet they seem to be ever less willing to spend time searching for "important" things.

    For instance, I had to go through a day of orientation and part of it was this exercise on "active learning". The problem we were given was to estimate how many ping pong balls it would take to fill the auditorium we were in to the ceiling. Since the point wasn't to get a defensible answer, but just to work in a group through a process to get an estimate, there was no need for any accuracy in any of the numbers used, especially given that it was an odd shaped room with a slanted ceiling at least thirty feet high. Yet a lot of the younger folks jumped on their computers and smart phones in order to get the diameter of a pinf pong ball. While several people in my group (all a bit older) had computers in front of us, none one even thought of doing that. We just agreed that an estimate of about 1.5" was probably pretty close and that using either 1" or 2" to make the calculations easier was almost certainly within the accuracy of out estimates of room dimensions. So we used face-packed 1" balls as an estimate of close-packed 1.5" balls.

    On the other hand, when I was a student and we didn't have the internet or e-mail, if we had a question we looked in out text books and our notes first, then maybe consulted a classmate, then hit the library, then went and saw the professor. Now, they shoot the instructor an e-mail asking for an answer to the simplest questions that are in bold print in the text book and when copying and pasting the exact question that was asked into Google yields the answer within the handful of words displayed in the top result.

    I'm sure that the very ease of asking the instructor or members of a forum accounts for a lot of it, but I think there really is something in which searching for mindless trivia is natural but thinking about a question of substance hard enough to do the same thing is asking too much for a growing fraction of the population (which is not to say that there aren't a lot of really sharp "kids" out there that do a lot of research to answer their own questions -- or to be able to come up to me and prove that something I said was wrong or incomplete, which I love and have even given extra credit for).
     
Loading...