- #include<stdio.h>
-
int main()
-
{
-
int a;
-
int b=scanf("%d",&a);
-
printf("%d %c ",a,a);
-
return 0;
-
-
}
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 .
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
What is the value in the variable a?
Only values between 33 and 126 will print a symbol. See the ASCII table.
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 .
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.
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 ?
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.
A couple of issues with what @akbarkhan said: - 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.
- 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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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
|
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...
|
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
|
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...
|
by: datttanand |
last post by:
can anybody send me the code snippet to find the ascii value of character?
|
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...
|
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....
|
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.
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
| |