Solved

confused with " %c "

Posted on 2003-10-29
3
166 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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 and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

839 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