簡體   English   中英

在C中的同一行寫入2個字符串

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

所以我想讓hello.c程序在一行上寫下名字和姓氏,所以在這種形式下,但是當我在這個當前形式中運行我的程序時,它給出了錯誤“期望â)—字符串常量之前“我想我剩下的代碼了,因為我已經刪除了該行並運行了它,並且可以正常工作。 所以我只想問一下如何獲得我已經指出要在同一行上的2個字符串。

這是我的代碼

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

你要

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

代替

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;
}

我使用sprintf顯示了相同的內容。

1)同樣在你的程序中你沒有返回任何東西,如果不想返回任何東西使它成為void函數。 編寫int函數時,請務必使其習慣於返回整數。

2)當你編寫printf函數時,總是習慣添加\\ n(新行),這樣輸出看起來很好

快樂的編碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM