#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep 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.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,393
    Rep Power
    1871
    > #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);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,902
    Rep Power
    481
    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 and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep 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.
  8. #5
  9. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,393
    Rep Power
    1871
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0

    Thanks


    Thanks for the answers. How can I close this thread now?

    saurabh
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,179
    Rep Power
    2222
    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.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0

    Done


    I meant that, do I have to do anything now that I have the answer(s) I needed?

    thanks
    Saurabh.
  16. #9
  17. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,179
    Rep Power
    2222
    Outside of acknowledging and thanking, no.

IMN logo majestic logo threadwatch logo seochat tools logo