Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用C语言在同一行上写两个字符串_C_Cstring - Fatal编程技术网

用C语言在同一行上写两个字符串

用C语言在同一行上写两个字符串,c,cstring,C,Cstring,所以我想让hello.c程序在一行中同时写上名字和姓氏,所以在这种形式下,但是当我以这种当前形式运行程序时,它会给我一个错误“expected–before string constant”,我想我已经完成了剩下的代码,因为我已经删除了那一行并运行了它,它可以工作了。所以我只想问,如何让我已经指向的两个字符串在同一行上 这是我的密码 #include <stdio.h> int main() { char firstname[20]; char lastname[20];

所以我想让hello.c程序在一行中同时写上名字和姓氏,所以在这种形式下,但是当我以这种当前形式运行程序时,它会给我一个错误“expected–before string constant”,我想我已经完成了剩下的代码,因为我已经删除了那一行并运行了它,它可以工作了。所以我只想问,如何让我已经指向的两个字符串在同一行上

这是我的密码

#include <stdio.h>
int main()
{
  char firstname[20];
  char lastname[20];
  printf("What is your firstname?");
  scanf("%s", firstname);
  printf("What is your lastname?");
  scanf("%s", lastname);
  printf("Hello %s\n", firstname "%s", lastname);
  printf("Welcome to CMPUT 201!");
  }
#包括
int main()
{
charfirstname[20];
char lastname[20];
printf(“你的名字是什么?”);
scanf(“%s”,名字);
printf(“你姓什么?”);
scanf(“%s”,lastname);
printf(“你好%s\n”,姓“%s”,姓);
printf(“欢迎使用CMPUT 201!”);
}
您想要什么

printf("Hello %s %s\n", firstname, lastname);
而不是

printf("Hello %s\n", firstname "%s", lastname);
#包括
#包括
int main()
{
字符名[20]=“”、姓[20]=“”、全名[40]=“”;
printf(“您的名字是什么?\n”);
scanf(“%s”,名字);
printf(“您姓什么?\n”);
scanf(“%s”,姓氏);
sprintf(全名,“%s%s”、名、姓);
printf(“名称为%s\n”,全名);
返回0;
}
我已经用sprintf展示了同样的情况


1) 同样在你的程序中,你没有返回任何东西,如果你不想返回任何东西,就把它作为void函数。在编写int函数时,始终将返回整数作为一种习惯

2) 此外,在编写printf函数时,始终习惯于添加\n(新行),以便输出效果良好


愉快的编码。

(另外,在读取名称时,您可能需要一些保护以防止缓冲区溢出;如果有人为其中一个输入21个字符,该怎么办?)非常感谢。你太棒了。它解决了这个问题。我使用char(firstname),因为我的教授说我们需要使用小于20的名称,但我会使用fgets或增加为char中的字符串分配的内存量。这行得通吗?是的,我主张像
fgets
这样的东西,如果你想扩展它,它只能读取一个设定的限制。不过,如果你保证每件事都少于20件,你应该没事。非常感谢您的帮助。“如果不想返回任何内容,请将其作为无效函数。”不
void main()
是一个严格的否定@哈尔,你能解释一下原因吗。就我所知,当我不想归还任何东西时,必须使用void。请随时更正。根据C99标准。如果您计划只打印
全名
,请不要先将其写入字符串,然后再打印出来。直接打印出来。@Shahbaz ya写这篇文章是为了展示可能的可能性,
#include<stdio.h>
#include<string.h>

int main()
{
    char first_name[20] = " ", last_name[20] = " ", full_name[40] = " ";
    printf("What is your firstname?\n");
    scanf("%s", first_name);
    printf("What is your lastname?\n");
    scanf("%s", last_name);
    sprintf(full_name,"%s %s",first_name, last_name);
    printf("name is %s\n",full_name);
    return 0;
}