By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,203 Members | 1,392 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,203 IT Pros & Developers. It's quick & easy.

why is the below code not printing the ASCII value of the inputed character ?

P: 56
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int main()
  3. {
  4. int a;
  5. int b=scanf("%d",&a);
  6. printf("%d %c ",a,a);
  7. return 0;
  8.  
  9. }
In this code if I am giving input as any character , it gives me output as character using %c format specifier but does not give me the ASCII code of that character using %d ,what is the issue here ,please guide me .
Jun 10 '16 #1

✓ answered by weaknessforcats

The return value of scanf is meaningless if an error occurred.

If A is an int and scanf uses %c and I enter 65 the return is 1 and a is -858993610.

You have to check for any errors before you use that scanf return using feof and/or ferror.

Try not to get mired in this. Validating input is a lot of code. I tell students that when they are learning, do not try to validate input. If the program needs specific data then enter only valid values.

Share this Question
Share on Google+
6 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
What is the value in the variable a?

Only values between 33 and 126 will print a symbol. See the ASCII table.
Jun 10 '16 #2

P: 56
Say I input character A , so now using %c , although it displays character A but using %d , it doesnt display the ascii code of A which is 65 .
Jun 10 '16 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
You can't scanf an A into an int. A is not an integer value. Most likely the A you see is the one you entered. The variable a now has an indeterminate value.

You need to scanf into a char to get the A accepted and then you display the char as %d and you should see the 65.
Jun 10 '16 #4

P: 56
If that was the case that scanf did not read the character successfully then why is the value of b printed as 1 ?Since scaanf returns the number of inputs read successfully so if character A cannot get inside an int variable then why is the value of b =1 ?
Jun 11 '16 #5

weaknessforcats
Expert Mod 5K+
P: 9,197
The return value of scanf is meaningless if an error occurred.

If A is an int and scanf uses %c and I enter 65 the return is 1 and a is -858993610.

You have to check for any errors before you use that scanf return using feof and/or ferror.

Try not to get mired in this. Validating input is a lot of code. I tell students that when they are learning, do not try to validate input. If the program needs specific data then enter only valid values.
Jun 11 '16 #6

Expert 100+
P: 2,400
A couple of issues with what @akbarkhan said:
  1. Not all compilers use ASCII encoding for chars. This may not matter for small demonstration programs but production software you get paid for needs to be more robust. If your program must return an ASCII code then IMHO you should write explicit conversion code rather than relying on the intrinsic conversion when you cast char to int.
  2. Each compiler implementation is free to decide for itself if char is signed or unsigned. A compiler that doesn't conform to your expectations may cause your expression to compute the wrong value.
Apr 17 '17 #7

Post your reply

Sign in to post your reply or Sign up for a free account.