How to print a statement which includes a scanned character and an integer in C?

161 Views Asked by At

This is the code that I am trying to run -

#include <stdio.h>
int main()
{
char a;
int b;
printf("Name of Prisoner\n");
scanf("%c" ,a);

printf("How many years in jail as stated by the court?\n");
scanf("%d", b);

printf("Prisoner "a" goes to jail for "b" years\n"a,b);

return 0;    

}

This is the Error shown -

P.c: In function 'main':
P.c:14:23: error: expected ')' before 'a'
printf("Prisoner "a" goes to jail for "b" years\n"a,b);
                   ^

As you can see I am at a very beginner level at C or any programming language. I want to collect the name of the prisoner and the years of prison sentenced to him and simply print a statement which includes the data collected. But it is not being compiled. The error is given. I am pretty sure it's a very small and simple fix but I am trying to run a little bit ahead of my classes and don't have any personal guide or teacher with me. Can anyone please educate me on this issue?

2

There are 2 best solutions below

2
Mohamed Yasar On BEST ANSWER

Kindly note that the statement printf("Prisoner "a" goes to jail for "b" years\n"a,b); contains an error, indicative of a common mistake made by beginner programmers. I encourage you to thoroughly review the error message provided, aiming to comprehend its nuances and rectify similar errors in the future. Additionally, investing time in studying format specifiers in C will enhance your understanding of proper usage in print and scan statements, contributing to more robust and error-free code.Websites like Format specifiers in C helps you to move on from the beginner level.

#include <stdio.h>
int main()
    {
        char a[10]; //Use character array for declaring sequence of character
        int b;
        printf("Name of Prisoner\n");
        scanf("%s" ,a);

        printf("How many years in jail as stated by the court?\n");
        scanf("%d", &b);

        printf("Prisoner %s goes to jail for %d years\n",a,b);

        return 0;    

     }

output:

Name of Prisoner
Mike
How many years in jail as stated by the court?
4
Prisoner Mike goes to jail for 4 years

And while learning about arrays, Additionally go through the similar of Arrays and pointers C pointers and arrays

0
ad absurdum On

There are several mistakes in the posted code involving the use of the scanf and printf functions.

scanf("%c" ,a);
/* ... */
scanf("%d", b);

scanf needs to store the values it reads someplace, and it needs pointers to storage locations for this purpose. In particular, the %c directive requires and expects a pointer to char argument to provide access to storage for a char value, and the %d directive requires and expects a pointer to int to provide access to storage for an int value.

Given char a; and int b; you can get pointers by using the address operator &:

scanf("%c", &a);
/* ... */
scanf("%d", &b);

A further issue here is that scanf returns a value, and you should always check the values returned by functions that return values. In this case, scanf returns the number of successful matches (or EOF in some circumstances). You should check this value. It should be 1 for both of the examples above. scanf with %c might return EOF in the rare event of an input failure, but with %d could return 0 instead of 1 if the user fails to input a number. If the code then attempts to print the value of b it would have undefined behavior since the value of b is indeterminate. It would not be a bad idea to initialize b to some value that is not expected as input; this avoids potential undefined behavior and provides another path to error-checking. The complete code below shows rudimentary checking of these return values; note that more complete input validation is usually desirable.

printf("Prisoner "a" goes to jail for "b" years\n"a,b);

Here the first problem is a syntax problem. printf uses conversion specifiers in a format string, but the posted code has no conversion specifiers and seems to interpolate the desired identifiers between portions of string to be printed. Further, there is a missing comma between the format string and the next argument. printf is a (variadic) function like any other; it takes a comma-separated sequence of arguments. The first argument is a format string, and the remaining arguments provide values to be printed as specified by the format string. A char can be printed using %c, and an int can be printed using %d:

printf("Prisoner %c goes to jail for %d years\n", a, b);

printf also returns a value (the number of bytes printed), but it is uncommon to check this value unless you need to know this number. If you don't check this one, still check other function return values.

Note that printing a line of text without interpolated values is better done with puts or fputs. puts adds a newline to the end of the text (fputs does not), and both of these functions are more efficient than printf since they don't need to parse a format string. The code below demonstrates using puts.

Finally, main should have either the signature int main(void) or int main(int args, char *argv[]). There are other implementation-defined signatures (MSVC allows int main(), IIRC) but these are not portable, while the two listed first are blessed by the Standard and must be supported by all implementations. Note that with the upcoming C23 int main() will be acceptable (I believe, but there may be room for argument here).

Here is a corrected version of the posted code:

#include <stdio.h>

int main(void) {
    char a;
    int b = -42;  // Initialized to nonsense value to avoid potential UB

    puts("Name of Prisoner");
    if (scanf("%c" ,&a) != 1) {  // Handle error condition
        puts("Error: scanf");
        return 1;                // Just exit with error code 1
    }

    puts("How many years in jail as stated by the court?");
    if (scanf("%d", &b) != 1) {  // Handle error condition
        puts("Error: non-numeric input");
        return 1;                // Just exit with error code 1
    }

    printf("Prisoner %c goes to jail for %d years\n", a, b);

    return 0;    
}