Thread: getchar and scanf

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    8

    getchar and scanf

    First i want to know whats the difference between scanf(); and getchar();

    second can anyone please write 2 examples, 1 with getchar/putchar and one with scanf which would take a name and print it all. i was able to do it with getchar/putchar but it printed only the first letter of the name.

    Thanks!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Read the FAQ, or your C book. Or look at the man page, or look at a search engine. getchar reads one character. scanf reads specifically formatted input.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    8
    So i cant use getchar to get all the input and print it ?? and in scanf how can i make it scan the input and print it out ?? can you write an example ?
    Thanks

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    45
    you can use getchar to get all of your input, you will just get the input one character at a time.

    your question about scanf should be covered in any basic scanf tutorial

  5. #5
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    getchar works better for strings and characters. scanf works better with integers. Here's some examples.
    Code:
    #include <stdio.h>
    
    main()
    {
    	char x;
    	x = getchar();
    	putchar(x);
    	putchar(0x0a);
    }
    Code:
    #include <stdio.h>
    
    main()
    {
    	char x;
    	scanf("%c", &x);
    	printf("%c", x);
    	printf("\n");
    }
    0x0a is the newline character, if you didn't know that.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    #include <stdio.h>
    
    int main()
    {
    	char x;
    	x = getchar();
    	putchar(x);
    	putchar('\n');
    }
    Code:
    #include <stdio.h>
    
    int main()
    {
    	char x;
    	scanf("%c", &x);
    	printf("%c", x);
    	printf("\n");
    }
    Much better. Implicit main is deprecated. Magic numbers are evil.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by Elysia View Post

    Much better. Implicit main is deprecated. Magic numbers are evil.
    I didn't know you could do that with putchar

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Now we shouldn't you be able to? '\n' is a char, just like x that you also pass in.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by Elysia View Post
    Now we shouldn't you be able to? '\n' is a char, just like x that you also pass in.
    I thought you could only do a '\n' with printf.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can use a char's value anywhere you use an integer for the most part. You can put that value into any function expecting a value where that might fall into valid context. Let's say that '\n' has the decimal value of 10 (because it does .) If you write a function that expects you to pass it 1 through 10. You could pass it '\n' instead. It's just a value. Use it wherever you want.
    Code:
    putchar( '\n' );
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple XOR Program
    By dolfaniss in forum C Programming
    Replies: 8
    Last Post: 05-24-2010, 01:27 PM
  2. getchar won't take any input after scanf
    By pshirishreddy in forum C Programming
    Replies: 2
    Last Post: 08-02-2009, 11:46 AM
  3. scanf, getchar, gets, etc. are ignored.
    By daltore in forum C Programming
    Replies: 19
    Last Post: 12-27-2007, 10:47 PM
  4. scanf and getchar
    By axe in forum C Programming
    Replies: 7
    Last Post: 01-11-2006, 04:45 AM
  5. Replies: 2
    Last Post: 11-10-2003, 09:12 PM