473,387 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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.

6 1728
weaknessforcats
9,208 Expert Mod 8TB
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
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
9,208 Expert Mod 8TB
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
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
9,208 Expert Mod 8TB
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
donbock
2,426 Expert 2GB
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

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

Similar topics

4
by: Steve Horsley | last post by:
How can I get the ASCII value of a character, e.g. I have: str = "A B" for index in range(0, len(str)): value = WHAT_GOES_HERE?(str) print value and I hope to get: 65
6
by: Haas | last post by:
Hello folks, I just started a C++ course. An exercise is to give the ASCII value of a character read from keyboard. I place the input from the keyboard in a char-variable. How can i get the...
2
by: questions | last post by:
How do I convert a number to its ascii value in c#? What is the equivalent of VB's chr() function in c#? thanks in advance
15
by: Craig Wagner | last post by:
I have a situation where I have a series of character codes stored in the database. In some cases they are the ASCII value of the character, in other cases they are the > 127 character code value...
7
by: datttanand | last post by:
can anybody send me the code snippet to find the ascii value of character?
1
by: sajit | last post by:
hi folks, I am developing a module in asp.net with c# as code behind. The module is used to encrypte the password. The problem is how to find the ascii value of particular character. Any help...
1
by: ssetz | last post by:
Hello, For work, I need to write a password filter. The problem is that my C+ + experience is only some practice in school, 10 years ago. I now develop in C# which is completely different to me....
3
by: krishnaneeraja | last post by:
Hi, im developing one web application using asp.net,c#.net.In that i want to find ASCII value of first character in a string.plz help me. thanks.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.