The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Funny results with printf() vs putchar(). !!
Discuss Funny results with printf() vs putchar(). !! in the C Programming forum on Dev Shed. Funny results with printf() vs putchar(). !! C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 11th, 2013, 01:24 AM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 7
Time spent in forums: 1 h 9 m 59 sec
Reputation Power: 0
|
|
|
Funny results with printf() vs putchar(). !!
Dear experts,
I am working on a beginner level program and got stuck:
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
char a;
int i;
for (i = 0; i <3; i++)
{
a = getch();
// any two out the four below in ANY ORDER
//printf("%c",a);
printf("%c",a);
putchar(a);
//putchar(a);
}
getch();
}
I see that no matter what combination of printf() and putchar() (total calls 2) I give, I only get the character printed ONCE (instead of 2 as I expected). Giving only one of them does not print the character at all.. Strange. Please throw some light on it.
sample input/output:
aabbcc
Thanks,
Saurabh.
|

March 11th, 2013, 02:42 AM
|
 |
Contributed User
|
|
|
|
|
> #include<conio.h>
This header file is obsolete, stop using it
> void main()
The standard requires that main returns an int.
My guess is you're using TurboC, which basically became obsolete around 1990.
Yes I'm well aware that many colleges have institutionalised obsolescence, but students need to know they're being ripped off all the same. It might not be costing you financially, but you are being screwed by being fed ancient crap.
> a = getch();
Use the portable function.
a = getchar();
After your printf and/or putchar, follow with
fflush(stdout);
|

March 11th, 2013, 10:41 AM
|
 |
Contributing User
|
|
|
|
|
Follow Salem's advice. gcc is a modern compiler alternative.
Based on what you do have:
"getch function prompts the user to press a character and that character is not printed on screen"
printf and putchar are showing the character. It is getch that you're misunderstanding.
__________________
[code] Code tags[/code] are essential for python code!
|

March 12th, 2013, 12:43 AM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 7
Time spent in forums: 1 h 9 m 59 sec
Reputation Power: 0
|
|
|
Thanks
Thanks to the people who replied. I want to know this:
1) Can I get gcc or any other modern compiler for windows (I am not sure how to initiate debugging in Visual studio)
2) what is the substitute of conio.h if it is obsolete?
3) What is meant by portable function (getchar vs getch)?
thanks,
Saurabh.
|

March 12th, 2013, 01:13 AM
|
 |
Contributed User
|
|
|
|
1) Can I get gcc or any other modern compiler for windows (I am not sure how to initiate debugging in Visual studio)
Modern compilers to try.
http://www.microsoft.com/express/Downloads/
http://www.smorgasbordet.com/pellesc/
http://www.codeblocks.org/
http://sourceforge.net/projects/orwelldevcpp/
The last two are IDEs which come pre-packaged with the MinGW port of the GCC compiler.
2) what is the substitute of conio.h if it is obsolete?
Well it depends which OS/Compiler you're using.
A reasonably portable library is pdcurses, (click) (click) which will allow you to do more things on a console than conio.h ever could, and allow you to run the same code in different environments if you chose to later on.
3) What is meant by portable function (getchar vs getch)?
It means EVERY compiler provides getchar() - it's a standard function.
Even if two systems have getch(), there is no guarantee that they do the same thing.
|

March 12th, 2013, 08:43 AM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 7
Time spent in forums: 1 h 9 m 59 sec
Reputation Power: 0
|
|
|
Thanks
Thanks for the answers. How can I close this thread now?
saurabh
|

March 12th, 2013, 09:33 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
Quote: | Originally Posted by saurabhmangal Thanks for the answers. How can I close this thread now?
saurabh |
Don't! That would be extremely rude behavior on your part.
|

March 12th, 2013, 12:28 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 7
Time spent in forums: 1 h 9 m 59 sec
Reputation Power: 0
|
|
|
Done
I meant that, do I have to do anything now that I have the answer(s) I needed?
thanks
Saurabh.
|

March 12th, 2013, 01:03 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
Outside of acknowledging and thanking, no.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|