So I have this code here to take three input from the user, one integer type and 2 character types:
#include <stdio.h>
int main() {
int carNumber;
char customerName, carCode;
printf("Enter Car Number: ");
scanf("%d", &carNumber);
printf("Enter Customer Name: ");
scanf("%s", &customerName);
printf("Enter Car Code: ");
scanf("%s", &carCode);
if (carCode == 'T'){
printf("Toyota");
}
else if (carCode == 'H'){
printf("Honda");
}
printf("\nCar Number: %d", carNumber);
printf("\nCustomer Name: %s", customerName);
return 0;
}
While most of it outputs correct, the customerName
always prints (null)
.
CodePudding user response:
To print a character, its " %c"
and not "%s"
.
Either use :
char customerName[30];
printf("Customer Name: %s", customerName);
OR
char customerName;
printf("Customer Name: %c", customerName);
Here is your entire program :
#include <stdio.h>
int main()
{
int carNumber;
char carCode;
char customerName[30];
printf("Enter Car Number: ");
scanf("%d", &carNumber);
printf("Enter Customer Name: ");
scanf("%s", customerName);
printf("Enter Car Code: ");
scanf(" %c", &carCode);
if (carCode == 'T'){
printf("Toyota\n");
}
else if (carCode == 'H'){
printf("Honda\n");
}
printf("Car Number: %d\n", carNumber);
printf("Customer Name: %s\n", customerName);
printf("Car Code: %c\n", carCode);
return 0;
}