7
votes

Ok, I'm a noob with C, but I think the code is basic and straightforward. This program is for a college assignment, and is supposed 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 function ‘main’:
week3work1.c:14:2: warning: format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]

What on earth is going on? I am desperate for help. Any help at all. I am seriously about to just give up on this program. Why is it saying it expects argument of 'char *' when my textbook shows that "%c" is for regular ole 'char'? I am using nano, gcc, and Ubuntu if that makes any difference.

3
gcc produces multiple warnings for your program. You should fix all of them. For example, you're missing #include <stdlib.h> (required for srand() and rand()) and #include <time.h> (required for clock()).Keith Thompson
@KeithThompson When compiling, I no longer have errors, and the program now works as I wanted it to. So, I guess I am asking if it will continue working? Because in my textbook, the only preprocessor statement needed is '#include <stdio.h>' and the '#include <ctype.h>' is for the isdigit() function.Nyxm
You need the headers I described above if you're going to call those functions. There are circumstances in which you can get away without using them, but I won't go into the details; just add the #include directives. gcc will warn about the function calls if you use -std=c99 -pedantic. What textbook are you using? If it shows an example that calls rand() without #include <stdlib.h>, or that calls clock() without #include <time.h>, then your textbook is wrong.Keith Thompson
@KeithThompson The book that I was given is "C Programming for the absolute beginner 2nd Edition" by Michael A. Vine. I have noticed that some of the exercises at the end of each chapter have to do with advanced topics that haven't even come close to be discussed. For example, at the end of the 2nd chapter (on primary data types) one excercise was to create a program that uses scanf() to allow a user to enter their name. Which would be a string, and the chapter had barely covered characters, let alone strings.Nyxm
Something I should have mentioned before: printf and scanf formats are not the same. For printf, %c requires an argument of type int, which should be a character value. For scanf, %c requires a pointer to char. Be sure you're reading the documentation for the function you're using.Keith Thompson

3 Answers

16
votes

For scanf(), you need to pass a pointer to a char, otherwise it doesn't have any way to store the character, since said char would be passed in by value. So you need &userChar instead.

Let's say userChar is 0 before the call. With your current code, you're basically doing this (as far as utility goes):

scanf("%c", 0);

What you want is this:

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

Which is &userChar.

The man page for scanf mentions this:

   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.
1
votes

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

0
votes

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

scanf("%c",&userChar);