尝试编译时的预期标识符
Expected identifier while trying to compile
尝试在没有 cs50
GetString
的情况下执行 cs50
。
执行以下代码中包含的新功能时遇到困难:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
void PrintName(char name);
{
printf("Your name is %c\n", name);
}
int main();
{
char fio[10];
printf("Hello, ");
scanf("%c", &fio);
PrintName(fio);
return 0;
}
接下来说:
hello-0.c:9:1: error: expected identifier or '('
{
^
hello-0.c:14:1: error: expected identifier or '('
{
^
它可能是什么?
首先,您有额外的分号:在 main
和 PrintName
函数前面。删除它们。
其次,您创建了 char 数组(又名字符串),但您 scanf
-ed 错了。如果你想将名称作为字符串(而不是像你那样作为字符),你必须这样做:
char fio[10];
printf("Hello, ");
scanf("%9s", fio);
请注意,当我读取字符串时,我的格式是 %s
(请注意其中的 9 最多可读取 9 个字符,因为您有 10 个数组)。此外,我传递了我的 char 数组的地址(这已经是一个地址)。这就是你读取字符串的方式。在函数中打印它是:
void PrintName(char name[])
{
printf("Your name is %s\n", name);
}
我们将 char[]
传递给函数并再次以 %s
格式打印
尝试在没有 cs50
GetString
的情况下执行 cs50
。
执行以下代码中包含的新功能时遇到困难:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
void PrintName(char name);
{
printf("Your name is %c\n", name);
}
int main();
{
char fio[10];
printf("Hello, ");
scanf("%c", &fio);
PrintName(fio);
return 0;
}
接下来说:
hello-0.c:9:1: error: expected identifier or '('
{
^
hello-0.c:14:1: error: expected identifier or '('
{
^
它可能是什么?
首先,您有额外的分号:在 main
和 PrintName
函数前面。删除它们。
其次,您创建了 char 数组(又名字符串),但您 scanf
-ed 错了。如果你想将名称作为字符串(而不是像你那样作为字符),你必须这样做:
char fio[10];
printf("Hello, ");
scanf("%9s", fio);
请注意,当我读取字符串时,我的格式是 %s
(请注意其中的 9 最多可读取 9 个字符,因为您有 10 个数组)。此外,我传递了我的 char 数组的地址(这已经是一个地址)。这就是你读取字符串的方式。在函数中打印它是:
void PrintName(char name[])
{
printf("Your name is %s\n", name);
}
我们将 char[]
传递给函数并再次以 %s
格式打印