Thread: What is the effect of the simple code?

  1. #1
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    What is the effect of the simple code?

    Code:
    int n;
    n=7;
    printf("%c\n", n);
    i said it will print out the character '7' but i was wrong i guess.
    The correct answer is it will print out the character whose internal code is 7. Now when i see this is it telling me it printed out the ASCII code for 7 which is 7. Not alot of sense is being made here?
    can anyone please explain briefly why the correct answer is correct, please?
    cj
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    It's printing a character whose "internal value" (as you put it) is 7. Look in the ASCII chart to find out more codes.

    If you want a "real" 7, you have to use
    >n = '7';

    [edit]http://www.asciitable.com/
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    printf("%c\n", n);
    Notice how it is treating it as a character with "%c"? If you wanted to print a 7 you would use "%d". See, if you wanted to print the character '7', you would have ot set n = 55 which is its ascii value.

    Check this out, it might help explain it to you.
    http://www.asciitable.com/
    [edit] hehe u beat me to it, Hammer [/edit]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple C code problem
    By neo28 in forum C Programming
    Replies: 24
    Last Post: 05-16-2009, 10:48 AM
  2. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Game Programming
    Replies: 0
    Last Post: 10-14-2002, 01:27 PM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum C# Programming
    Replies: 0
    Last Post: 10-14-2002, 01:26 PM
  4. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM
  5. Simple Code, looking for input.
    By Alien_Freak in forum C Programming
    Replies: 3
    Last Post: 03-03-2002, 11:34 AM