尝试编译时的预期标识符

尝试在没有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 '('
{
^

可能是什么?

评论
ut_et
ut_et

First things first, you have extra semi-colons: in front of main and PrintName function. Remove them.

Secondly, you created char array (aka string) and you scanf-ed it wrong. If you want to get name as string (not as a char, as you did), you have to do it like this:

char fio[10];

printf("Hello, ");

scanf("%9s", fio);

Note that, as I am reading string, my format is %s (note 9 in there to read up to 9 characters, because you have array of 10). Moreover, I pass the address of my char array (which is already an address). That's how you read a string. And printing it in function will be:

void PrintName(char name[])
{
    printf("Your name is %s\n", name);
}

where we pass char[] to function and again print with %s format

点赞
评论