Thread: need help with scanf("%*c") stuff.

  1. #1
    Registered User
    Join Date
    Apr 2002

    need help with scanf("%*c") stuff.

    #include <stdio.h>
    int main()
    {	struct student {
    			int snum;
    			char name[30];
    			int age;
    			char sex;
    			float score;
    	struct student info;
    	printf( "Input student number:" );
    	scanf( "%d", &info.snum );
            printf( "Input student name:" );
    	scanf( "%*c" );
            gets( );
            printf("Name: %s",;
    	return 0;
    can anyone tell me what's the purpose of scanf( "%*c" );
    because if i exclude that statement, the program compiles but does not work as i expected.

    By the way, those are not my codes, my teacher gave it to me.

    Thanks in advance.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002

    Re: need help with scanf("%*c") stuff.

    >my teacher gave it to me.
    Then your teacher gave you crud, I'm afraid.

    The use of gets() is now outdated, you should really be using fgets() or similar. And to be strict, the main should be defined like this:
    >int main(void)

    You should always check the return code from scanf() to ensure that it read in what you asked it to, particularly when reading numbers.

    When inputing string with scanf(), it will delimit based on white space, so going by your code, if you enter the name of "Fred Bloggs", your code will only pickup Fred. The Bloggs will be left in the input buffer.

    Anyway, enough ranting... in answer to your question, this:
    >scanf( "%*c" );
    scans for one character and doesn't assign it to a variable. This has the affect of eating one byte from the input stream, and is most likely intended to remove the newline character as was left by the first call to scanf().
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Apr 2002
    >scanf( "%*c" );
    >scans for one character and doesn't assign it to a variable.

    if that the case, what's the difference between putting

    scanf( "%*c" ); and scanf( "%c" );

    i've tried both, and both work, is there anything special with the * sign?

  4. #4
    Registered User
    Join Date
    Sep 2001
    scanf("%*c"); // reads the character in then disregards it.

    scanf("%c"); //shouldn't work, you haven't specified a variable
    scanf("%c", &x); // will work correctly

    BTW you shouldn't define your struct inside the main function.
    Last edited by kwigibo; 07-23-2002 at 10:20 PM.

  5. #5
    Registered User
    Join Date
    Apr 2002
    i see, thanks for the replies.

  6. #6
    Registered User
    Join Date
    Jul 2002

    * in %*d

    '*' in '%*d' is called the suppression character.

    it means that a character , integer or whatever datatype it is , is read from the stdin,but is not alloted to a variable..
    u will understand better with the following code.

    int main()
    int a=0,b=1;
    scanf("%*d %d",&a,&b);
    printf("\n%d %d",a,b);
    return 0;

    here, though two numbers are read from the stdin, only one is allocated memory... so the value entered first is ignored and the value entered second is set to the first variable in this case 'a'.
    so the value of a changes and takes the new value while the value of b is the older one only..

    hope this is clear.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tab key stuff. C+WinAPI is killing me. Please help.
    By Templario in forum Windows Programming
    Replies: 5
    Last Post: 11-21-2002, 02:35 PM
  2. arguments, directories and stuff...
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 06-26-2002, 05:46 PM
  3. Your stuff
    By smog890 in forum C Programming
    Replies: 6
    Last Post: 06-13-2002, 11:50 PM
  4. Linked lists and file i/o, and some other stuff
    By ninja in forum C++ Programming
    Replies: 9
    Last Post: 05-19-2002, 07:15 PM
  5. Stocks 'n' stuff...
    By Cheeze-It in forum A Brief History of
    Replies: 2
    Last Post: 09-20-2001, 05:36 PM
Website Security Test