在 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 ( ) 也接受空格作為字元。
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP