简体   繁体   English

在C中的同一行写入2个字符串

[英]Writing 2 strings on the same line in C

So I want to make the hello.c program write both the first name and the last name on one line so in this form but when I run my program in this current form it gives me the error "expected â)â before string constant" I think I have the rest of the code down because I have removed that line and ran it and it works. 所以我想让hello.c程序在一行上写下名字和姓氏,所以在这种形式下,但是当我在这个当前形式中运行我的程序时,它给出了错误“期望â)—字符串常量之前“我想我剩下的代码了,因为我已经删除了该行并运行了它,并且可以正常工作。 So I just want to ask how to get 2 strings that I have already pointed to to go on the same line. 所以我只想问一下如何获得我已经指出要在同一行上的2个字符串。

This is my code 这是我的代码

#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!");
  }

You want 你要

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

instead of 代替

printf("Hello %s\n", firstname "%s", lastname);
#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;
}

I have shown the same using sprintf. 我使用sprintf显示了相同的内容。

1) also in your program you are not returning anything, if dont want to return anything make it as void function. 1)同样在你的程序中你没有返回任何东西,如果不想返回任何东西使它成为void函数。 When you write int function, always make it an habbit to return the integer. 编写int函数时,请务必使其习惯于返回整数。

2) Also when you write the printf function always make it habit to add \\n(new line) so the output looks good 2)当你编写printf函数时,总是习惯添加\\ n(新行),这样输出看起来很好

happy coding. 快乐的编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM