在 C 语言中字符串的输入和输出是什么?


一组字符(或)字符集合称为字符串。

字符串的输入和输出

示例

以下是字符串输入和输出的 C 程序 −

#include<stdio.h>
main ( ){
   char a[30];
   printf("enter your name");
   scanf ( "%s",a);
   printf ("your name is %s",a);
   getch ( );
}

输出

执行上述程序时,将生成以下结果 −

1. Enter your name : Lucky 2. Enter your name : Lucky Lol
Your name is Lucky Your name is Lucky

注意 −

  • 不对字符串使用 ‘&’ 因为字符串的名称本身指定了字符串的基本地址。

  • scanf( ) 不接受空格作为字符。

  • 编译器在最后放置 ‘\0’。

示例

以下是使用 gets ( ) 和 puts ( ) 读写字符串的 C 程序 −

#include<stdio.h>
main ( ){
   char a[30];
   printf ( "enter your name");
   gets (a);
   printf("Your name is");
   puts (a);
}

输出

执行上述程序时,将生成以下结果 −

1. Enter your Name : Lucky 2) Enter your name : Lucky Lol
Your name is Lucky Your name is Lucky Lol

注意 − gets ( ) 也接受空格作为字符。

更新于: 08-Mar-2021

4K+ 浏览量

开启你的 职业生涯

完成课程并获得认证

开始
广告
© . All rights reserved.