"static char" vs. "char" in C

4k Views Asked by At

To practice variable declaration, placeholders and I/O calls, I did a sample assignment in the book that I am using to study. However, I keep running into a particular problem, in that when I try to declare more than one character variable for the purpose of input, even if the compiler doesn't catch any syntax error, the program when executing will only return one character variable. This is the code in question:

#include <stdio.h>

int main()

{
double penny=0.01;
double nickel=0.05;
double dime=0.1;
double quarter=0.25;

double value_of_pennies;
double value_of_nickels;
double value_of_dimes;
double value_of_quarters;
double TOTAL;
int P;
int N;
int D;
int Q;
char a,b; 

//used "static char" instead of "char", as only using the "char" type caused a formatting error where only the latter character entered in its input would appear

printf("Please enter initials> \n");
printf("First initial> \n");
scanf("%s", &a);
printf("Second initial> \n");
scanf("%s", &b);

//"%s" was used as the input placeholder for type "char"

printf("%c.%c., please enter the quantities of each type of the following coins.\n", a, b);


printf("Number of quarters> \n");
scanf("%d", &Q);
printf("Number of dimes> \n");
scanf("%d", &D);
printf("Number of nickels> \n");
scanf("%d", &N);
printf("Number of pennies> \n");
scanf("%d", &P);

printf("Okay, so you have: %d quarters, %d dimes, %d nickels, and %d pennies.\n", Q, D, N, P);

value_of_pennies=penny*P;
value_of_nickels=nickel*N;
value_of_dimes=dime*D;
value_of_quarters=quarter*Q;

TOTAL=value_of_quarters+value_of_dimes+value_of_nickels+value_of_pennies;

printf("The total value of the inserted coins is $%.2lf. Thank you.\n", TOTAL);

//total field width omitted as to not print out any leading spaces
return(0);
}

And this is the transcribed output ("a", "e", and the four "1"s are sample arbitrary input values:

Please enter initials>
First initial>
a
Second initial>
e
.e., please enter the quantities of each type of the following coins.
Number of quarters>
1
Number of dimes>
1
Number of nickels>
1
Number of pennies>
1
Okay, so you have: 1 quarters, 1 dimes, 1 nickels, and 1 pennies.
The total value of the inserted coins is $0.41. Thank you.

I entered the characters "a" and "e" as input values for the char variables "a" and "b", but only "e" had shown up. On the other hand, should I have put a "static" in the "char" variable declaration, both inputted char values will be displayed in the relevant print call.

For future reference, I would like to ask about why such a thing would occur the way that it did, and the value of the "static" word in the declaration.

(As an aside, I am aware that I could have simply made the "value_of_(insert coin here)" variables as constant macros.)

2

There are 2 best solutions below

6
On BEST ANSWER

With a definition like

char a,b; 

writing a statement like

scanf("%s", &a);
scanf("%s", &b);

invokes undefined behaviour. %s is not a format specifier for a char. Using wrong format specifier can and will lead to UB. You should use %c to scan a char.

To elaboreate,

  • %s format specifier expects a corresponding argument which is a pointer to char array. It scans multiple characters until it encounters a space (whitespace character, to be pedantic) or newline or EOF.
  • %c format specifier expects a corresponding argument which is a pointer to char variable. It reads only one char from the input buffer.

So, with %s, if you supply the adress of a char variable, it will try to access beyond the allocated memory region for writing the scanned data, which will invoke UB.

You may want to read some more on printf(), scanf() and format specifiers before moving forward.

0
On

You have used the wrong format specifier for char. char uses %c not %s. As far as static goes, I'm a bit confused about your question. You say in a comment that you are using static char, but I do not see any variables declared as static char.