GCC compilation error: format '% c expects an argument of type' char *, but argument 2 is of type 'int [-Wformat]

Ok, I'm noob with C, but I think the code is simple and straightforward. This program is intended for college assignment, and is assumed to have the isdigit () function in it. Here is the code

//by Nyxm
#include <stdio.h>
#include <ctype.h>

main()
{
    char userChar;
    int userNum, randNum;
    srand(clock());
    printf("\nThis program will generate a random number between 0 and 9 for a user to guess.\n");
    /*I changed it from '1 to 10' to '0 to 9' to be able to use the isdigit() function which
    will only let me use a 1 digit character for an argument*/
    printf("Please enter a digit from 0 to 9 as your guess: ");
    scanf("%c", userChar);
    if (isdigit(userChar))
    {
            userNum = userChar - '0';
            randNum = (rand() % 10);
            if (userNum == randNum)
            {
                    printf("Good guess! It was the random number.\n");
            }
            else
            {
                    printf("Sorry, the random number was %d.\n", randNum);
            }
    }
    else
    {
            printf("Sorry, you did not enter a digit between 0 and 9. Please try to run the program again.\$
    }
}

When I try to compile, I get the following error

week3work1.c: In functionmain’:
week3work1.c:14:2: warning: format ‘%cexpects argument of typechar *’, but argument 2 has typeint[-Wformat]

What's happening? I desperately need help. Any help whatsoever. I'm seriously going to just abandon this program. Why does he say that he expects the argument 'char *' when my tutorial shows that '% c' is for a regular ole 'char'? I use nano, gcc and Ubuntu if that matters.

+5
source share
3 answers

scanf() char, , char . &userChar.

userChar 0 . ( ):

scanf("%c", 0);

, :

scanf("%c", some-location-to-put-a-char);

&userChar.

man scanf :

   c      Matches  a  sequence  of characters whose length is specified by
          the maximum field width (default 1); the next pointer must be  a
          pointer to char, and there must be enough room for all the char
          acters (no terminating null byte is added).  The usual  skip  of
          leading  white  space is suppressed.  To skip white space first,
          use an explicit space in the format.
+11

scanf("%c", userChar); scanf("%c", &userChar);.

+1

You need to pass a pointer instead of a char value.

scanf("%c",&userChar);
0
source

All Articles