溫馨提示×

c語言如何輸入字符串

小億
83
2023-12-22 05:36:50
欄目: 編程語言

在C語言中,可以使用scanf函數或gets函數來輸入字符串。

  1. 使用scanf函數:

    #include <stdio.h>
    
    int main() {
        char str[100];
        
        printf("請輸入字符串:");
        scanf("%s", str);
        
        printf("您輸入的字符串是:%s\n", str);
        
        return 0;
    }
    
  2. 使用gets函數:

    #include <stdio.h>
    
    int main() {
        char str[100];
        
        printf("請輸入字符串:");
        gets(str);
        
        printf("您輸入的字符串是:%s\n", str);
        
        return 0;
    }
    

注意:使用gets函數時要注意字符串的長度,避免溢出。推薦使用fgets函數來輸入字符串,可以指定字符串的最大長度,避免溢出問題。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女