Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

c - As string is stored as array of characters then why in this question the string is stored without initializing the array of character?

using g++ compiler and code is compiling and running successfully. as I know that C doesn't have separate data type for string and stores the string as an array of character I don't know why the heck this is compiling without initializing the char as array.

#include <stdio.h>
int main(){

int total_ch=0;
int total_courses=2;
float sum=0.0;
float gpa=0.0;


for(int i=0;i < total_courses; i++){
    printf("%d 
",total_courses);
char course;
int marks=0;
int ch=0;
float cgpa=0.0;
printf("Enter the course name: 
");
scanf(" %s",course);
printf("Enter the credit hours of the course
");
scanf(" %d",&ch);
printf("Enter the marks of the course
");
scanf(" %d",&marks);
total_ch += ch;
if (marks > 85){
 printf("Your Cgpa of %s is 4.00 and your grade is A
",&course);
 sum+=(ch*4.00);
}else if (marks > 79 && marks < 86 ){
 printf("Your Cgpa of %s is 3.67 and your grade is A-
",&course);
 sum+=(ch*3.67);
}else if (marks > 75 && marks < 80){
 printf("Your Cgpa of %s is 3.33 and your grade is B+
",&course);
 sum+=(ch*3.33);
}else if (marks > 71 && marks < 76){
 printf("Your Cgpa of %s is 3.00 and your grade is B
",&course);
 sum+=(ch*3.00);
}else if (marks > 67 && marks < 72){
 printf("Your Cgpa of %s is 2.67 and your grade is B-
",&course);
 sum+=(ch*2.67);
}else if (marks > 63 && marks < 68){
 printf("Your Cgpa of %s is 2.50 and your grade is C+
",&course);
 sum+=(ch*2.50);
}else if (marks > 59 && marks < 64){
 printf("Your Cgpa of %s is 2.00 and your grade is C
",&course);
 sum+=(ch*2.00);
}else if (marks > 56 && marks < 60){
 printf("Your Cgpa of %s is 1.67 and your grade is C-
",&course);
 sum+=(ch*1.67);
}else if (marks > 53 && marks < 57){
 printf("Your Cgpa of %s is 1.33 and your grade is D+
",&course);
 sum+=(ch*1.33);
}else if (marks > 49 && marks < 54){
 printf("Your Cgpa of %s is 1.00 and your grade is D
",&course);
 sum+=(ch*1.00);
}else if (marks < 50){
 printf("Your Cgpa of %s is 0.00 and your grade is F
",&course);
 sum+=(ch*0.0);
}


}
gpa=sum/total_ch;
printf("Your total GPA of this semester is %f",gpa);


}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

So why does the compiler allow this at all?

C is a very old language, by today's standards. Undefined Behavior is an artifact of the days when you could write a program that conformed to the quirks of a specific machine, without having to worry about the compiler complaining.

In the early versions of C, undefined behavior's primary advantage was the production of performant compilers for a wide variety of machines: a specific construct could be mapped to a machine-specific feature, and the compiler did not have to generate additional code for the runtime to adapt the side effects to match semantics imposed by the language. The program source code was written with prior knowledge of the specific compiler and of the platforms that it would support.

https://en.wikipedia.org/wiki/Undefined_behavior

The article goes on to say that

For C and C++, the compiler is allowed to give a compile-time diagnostic in these cases, but is not required to: the implementation will be considered correct whatever it does in such cases, analogous to don't-care terms in digital logic. It is the responsibility of the programmer to write code that never invokes undefined behavior, although compiler implementations are allowed to issue diagnostics when this happens.

Which is why you should always compile with warnings on.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...