Thread: help with a printf statement

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    32

    Exclamation help with a printf statement

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    void main()
    {
         char var;
       
         scanf("%c",&var);
         printf("%c\n%d\n%d ",var-'0',(int)var);
         getch();
       
    }
    what does var-'0' means . Please help .

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Assuming that var is in the range ['0', '9'] (i.e., digit characters), it gives you the corresponding value in the range [0, 9].
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    The format string expects three parameters.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    29
    First, you have one too many arguments in the quotes. Remove the last %d.

    Now. If you compile this:

    Code:
    	char var = 0;
    	// scanf("%c",&var);
    	printf("%c\n%d\n%d", (var-'0') ,(int)var, '0');
    Output:
    \320
    0
    48

    You'll understand that '0' integer representation is 48.
    So, I set var to 0 for simplicity. It would be the character representation of 0 - 48. Which gives the character /320 (on this Mac).

    Also, I do not believe you needed to type cast var. But it's good practice I suppose.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Dr.Xperience View Post
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    void main()
    {
         char var;
       
         scanf("%c",&var);
         printf("%c\n%d\n%d ",var-'0',(int)var);
         getch();
       
    }
    what does var-'0' means . Please help .
    You are calling the printf function incorrectly... It wants 3 parameters not 2...

    Try this...
    ]
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    void main()
    {
         char var;
       
         scanf("%c",&var);
         printf("%c\n%d\n%d ",var,var-'0',(int)var);
         getch();
       
    }
    Maybe it will make more sense to you.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    32
    Thanks for reply and sorry for extra %d in printf statement.

    But tilll the extend I have got the idea here '0' represent zero and we are reducing var ie. character variable var with '0'. So the ASCII code for 0 is 48 and if i give input for var as q.
    So, ASCII code of q i.e.113 this means 113-48=65and we get ans as A which have ASCII code 65.
    So, indeed this just an arithmetic expression in which ASCII code of character stored in var is reduced by ASCII code of 0 which is 48.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 01-29-2011, 04:54 AM
  2. Replacing switch statement with a nested if/else
    By dev123 in forum C Programming
    Replies: 27
    Last Post: 09-06-2010, 12:30 AM
  3. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  4. please help me im dieing
    By phatomphreak in forum C Programming
    Replies: 9
    Last Post: 03-05-2010, 05:11 PM
  5. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 06:54 AM