?
Solved

confused with " %c "

Posted on 2003-10-29
3
Medium Priority
?
173 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 300 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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.

807 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