Thread: scanf return code

  1. #1
    Registered User
    Join Date
    May 2011
    Location
    Alberta, Canada
    Posts
    34

    scanf return code

    Using the latest gcc incarnation, why wouldn't the following work??

    Code:
    (void) scanf("%d", &blah);
    gcc keeps on barfing up the "unused value" warning....

    It'll shut up, if I test the return value etc etc, but for small practice code I kinda liked the idea of casting the return value to void. No biggie!! Just wondering? TIA ...
    --
    duke

  2. #2
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    I don't understand why you'd ever do that...

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Alberta, Canada
    Posts
    34
    For example:

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    	int x;
    	int *ptr;
    	
    	ptr = &x;
    	printf("Gimme a number: ");
    	
    	// always check to make sure that what was
    	// input was what was required
    	if(scanf("%d", ptr) == 1)
    	{
    		printf("You input: %d\n", *ptr);
    	} else
    	{
    		printf("failed to read integer.\n");
    	}
    
    	// if you don't care about the scanf return code
    	// then casting it to void should work
    	// but it doesn't with gcc
    	
    //	(void) scanf("%d", ptr);    // or
    //	scanf("%d", &x);
    	
    	getchar();
    
       return 0;
    }
    The comments explain where I'm at with this.
    --
    duke

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    scanf is just pointing to an address. That's why you don't need * for ptr. There is no return value.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > // (void) scanf("%d", ptr); // or
    > // scanf("%d", &x);
    OK, so where do you USE x after this point in the code?

    It's not the return result of scanf it's complaining about.
    It's the stored value in x which has no purpose in the code.
    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.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by dukester View Post
    Code:
    (void) scanf("%d", &blah);
    If you want to ignore the return value, the normal way is to just ignore it by writing:

    scanf("%d", &blah);

    For example:

    Code:
    #include <stdio.h>
    
    int print_message(char *msg)
    {
        printf("%s\n", msg);
        return 12345;
    }
    
    int main()
    {
        print_message("Hello"); // return value of 12345 ignored
        return 0;
    }
    That is the normal way to do it. Casting to (void) should be legal but unnecessary. Are you trying to make sure your function does not return void? You could also do this:

    Code:
    #include <stdio.h>
    #define NOVOID (void)(void *)
    
    void print_message(char *msg)
    {
        printf("%s\n", msg);
    }
    
    int main()
    {
        NOVOID print_message("Hello"); // compile error: void cannot be cast to anything except (void)
        return 0;
    }
    Now NOVOID before a function call will make the compiler ensure that the function does not return void, but it will ignore the return value.

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Alberta, Canada
    Posts
    34
    I should explain the situation a bit more clearly ....

    My original test code was:

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    	int x;
    	int *ptr;
    	
    	ptr = &x;
    	printf("Gimme a number: ");
    	
    	scanf("%d", ptr);    // or
    //	scanf("%d", &x);
    
    	printf("You input: %d\n", *ptr);
    	getchar();
    
       return 0;
    }
    for which I get this warning:

    warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]

    I Googled looking for a solution, and stumbled on using:

    Code:
    (void)scanf("%d", ptr);
    to nuke the warning. No joy that way!!

    So I simply checked the input, and solved the issue that way.

    I was just wondering why some guru on the 'net would offer casting scanf to void if that solution is bogus to begin with?
    --
    duke

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    With GNU C, you can declare an attribute in a function declaration to force the user to check the return value. This is not standard but it could be useful if you want to force people to check for errors. Here is an example:

    Code:
    int print_message(char *s) __attribute__((warn_unused_result));
    
    int main()
    {
        print_message("Hello");
        return 0;
    }
    
    int print_message(char *s)
    {
        printf("%s\n", s);
        return 12345;
    }
    This will cause gcc to choke because I am not checking the return value of print_message. If your implementation of scanf has declared that attribute, then under the rules of gcc you must check the return value. Casting to (void) does not help.

    Code:
    int main()
    {
        int x = print_message("Hello");
        printf("Result is %d\n", x);
        return 0;
    }

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Alberta, Canada
    Posts
    34
    The following should help me and others:

    C Printf and Scanf Reference
    --
    duke

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf return values
    By pirog in forum C Programming
    Replies: 15
    Last Post: 09-13-2009, 03:58 AM
  2. scanf to return after user presses enter?
    By sp2 in forum C Programming
    Replies: 9
    Last Post: 04-23-2008, 05:33 PM
  3. Replies: 12
    Last Post: 04-07-2007, 11:11 AM
  4. Check the return status of scanf
    By Taka in forum C Programming
    Replies: 10
    Last Post: 11-01-2006, 06:27 AM
  5. scanf() value return
    By Spectrum48k in forum C Programming
    Replies: 2
    Last Post: 06-06-2002, 11:49 PM