I am learning the C programming language. I am doing homework with C programming language. But I am getting a problem. The result is not printing. I am using format specifier %s. Here is my code below. Please have a look.
#include <stdio.h>
int gradeCalculation(int marks) {
switch (marks) {
case 0 ... 49:
return 'F';
break;
case 50 ... 59:
return 'D';
break;
case 60 ... 69:
return 'C';
break;
case 70 ... 79:
return 'B';
break;
case 80 ... 89:
return 'A';
break;
case 90 ... 100:
return 'A ';
break;
default:
return 'Marks is not valid!';
}
}
int main() {
int marks;
printf("Marks: ");
scanf("%d", &marks);
printf("Grade: %s\n", gradeCalculation(marks));
return 0;
}
How to solve my problem? What specifier should I use to get the expected result?
CodePudding user response:
Apostrophes denote character constants; F
is an integer that is the code for the character “F”.
For various legacy/history reasons, you can have multi-character constants such as A
, which has a value that may combine the values of the two characters “A” and “ ”. However, the result is still an int
; it is not generally treated as a string of two characters. Multi-character constants are used for special purposes and are implementation-dependent. You should avoid them outside of special circumstances.
In gradeCalculation
, you want to return a string of characters. To refer to a string, we generally use a pointer to its first element. So make the return type of gradeCalculation
const char *
. Then change all the character constants to string literals, such as "F"
, "A "
, and "Marks is not valid"
. Note the use of quotation marks rather than apostrophes. A string literal denotes an array of characters including a null character to mark the end.
CodePudding user response:
For starters case labels like this
case 90 ... 100:
is not a standard C feature. It is better to use if-else statements. Also the function parameter should have unsigned integer type for example unsigned int
.
Your function returns an object of the type int
int gradeCalculation(int marks) {
So using the conversion specifier %s
with an object of the type int
results in undefined behavior.
The function return type should be const char *
const char * gradeCalculation( unsigned int marks ) {
and instead of integer character literals you need to return string literals as for example
case 80 ... 89:
return "A";
break;
case 90 ... 100:
return "A ";
break;
default:
return "Marks is not valid!";
Your program can look the following way
#include <stdio.h>
const char * gradeCalculation( unsigned int marks )
{
const char *grade = "Marks is not valid!";
if ( marks < 50 )
{
grade = "F";
}
else if ( marks < 60 )
{
grade = "D";
}
else if ( marks < 70 )
{
grade = "C";
}
else if ( marks < 80 )
{
grade = "B";
}
else if ( marks < 90 )
{
grade = "A";
}
else if ( marks <= 100 )
{
grade = "A ";
}
return grade;
}
int main( void )
{
unsigned int marks = 0;
printf("Marks: ");
scanf( "%u", &marks );
printf( "Grade: %s\n", gradeCalculation( marks ) );
return 0;
}
.
CodePudding user response:
There are few points to be noted in your given code:
In the line
printf("Grade: %s\n", gradeCalculation(marks));
, the functiongradeCalculation()
returns anint
whereas the format specifier is%s
which is for a string(char *).In the
int gradeCalculation(int marks)
, all the cases are returning a character, except forcase 90 ... 100:
return 'A ';
break;
default:
return 'Marks is not valid!';
which are returning string.
Therefore, it will be good to convert all to string and change the return type to char *
, i.e., string.
Below is the solution :-
#include <stdio.h>
char * gradeCalculation(int marks) {
switch (marks) {
case 0 ... 49:
return "F";
break;
case 50 ... 59:
return "D";
break;
case 60 ... 69:
return "C";
break;
case 70 ... 79:
return "B";
break;
case 80 ... 89:
return "A";
break;
case 90 ... 100:
return "A ";
break;
default:
return "Marks is not valid!";
}
}
int main() {
int marks;
printf("Marks: ");
scanf("%d", &marks);
printf("Grade: %s\n", gradeCalculation(marks));
return 0;
}