Solved

confused with " %c "

Posted on 2003-10-29
3
163 Views
Last Modified: 2010-04-15

{
 int ch;

 ch = getchar();
 printf("%d", ch);
}

lets say i enter .. 1
output will be   .. 49   the ASII value of the character 1  


the above i have no problems with,
BUT,,  can someone explain the following..


{
 int ch;

 cscanf("%c", &ch);
 printf("%d", ch);
}

lets say i enter .. 1
output will be   .. 4210737  where does this value come from??

A clear and easy explanation would be greatly appreciated.
thanks..

mike...

using devc++
0
Comment
Question by:mikem_2au
  • 2
3 Comments
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9641822
Initialse the int variable to something [say ch = 0 ] and your problem would be solved

0
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 75 total points
ID: 9641842
Basically when u are using %c, only one byte of the integer variable is getting populated with the data and the other byte contains it's initial junk value

Thus the result u obtained

After initialising it to 0, everything works fine
0
 

Author Comment

by:mikem_2au
ID: 9641965
confusion gone..

thank's for the explanation, and the quick response.

mike..
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now