基本的C编程错误

时间:2015-09-14 22:12:08

标签: c gcc

我是编程世界的新手,我开始运行一些非常基本的代码,但遇到了麻烦。我正在尝试使用gcc在Linux上运行它,但它不起作用,是否有人能够告诉我我正在做什么错误?

#include <stdio.h>

int main(){

    char letter;
    int grade1;
    int grade2;
    float grade3;
    float grade4;

    letter=Y
    grade1=98
    grade2=76
    grade3=67.5
    grade4=80.5

    printf("Please enter the first letter of your name:%c\n",letter);
    printf("Please enter the grade for your first test:%d\n",grade1);
    printf("Please enter the grade for your second test:%d\n",grade2);
    printf("Please enter the grade for your final test:%f\n",grade3);
    printf("The average mark:%f",grade4);

    return 0;

}

非常感谢任何帮助!

3 个答案:

答案 0 :(得分:1)

letter=Y
grade1=98
grade2=76
grade3=67.5
grade4=80.5

在每个陈述后你都需要一个分号。此外,Y需要使用单引号,因为它是您要分配给变量的字符,而不是变量本身:letter = 'Y'

printf("Please enter the first letter of your name:%c\n",letter);

printf用于显示内容,scanf是您想要阅读的内容。所以你需要这样的东西:

printf("Please enter the first letter of your name:\n");
scanf("%c", &letter);

我建议从一本书或tutorial开始。

答案 1 :(得分:0)

我建议您使用IDE,因为它会标记并在这种情况下立即给出关于缺失的半冒号的错误。

Visual Studio非常适合Windows,也有免费版本,对于linux,你可以在这里查看:C++ IDE for Linux?

此外,您应该谷歌您不熟悉的每个功能,并检查其示例以确保正确使用。

答案 2 :(得分:-1)

你的错误:

char letter='Y'//if you want to initialize letter variable  

//no need to enter the letter from the user because you initialized above 
// printf("Please enter the first letter of your name:%c\n",letter);

//otherwise you can get the value of letter variable using printf and scanf
char letter;  
printf("Please enter the first letter of your name:");
scanf("%c",&letter);

//same way you initialize the grades 
grade1=98
grade2=76
grade3=67.5
grade4=80.5 
//if you initialize the grades then no need to ask the user to Enter the grade 
//and if you want to ask the user to enter the grade then see this codes
int grade1;
int grade2;
float grade3;
float grade4;

printf("Please enter the grade for your first test:");
scanf("%d",&grade1);
//and same for all the grade note grade3 and grade4 is float so you have you use "%f" instead of "%d" in scanf.