C - 使用者輸入



C 語言中使用者輸入的必要性

每個計算機應用程式都接受使用者輸入的某些資料,對這些資料執行預定義的過程以產生輸出。C 語言中沒有可以讀取使用者輸入的關鍵字。

與 C 編譯器捆綁在一起的標準庫包含 stdio.h 標頭檔案,其庫函式 scanf() 最常用於 從標準輸入流接受使用者輸入。此外,stdio.h 庫還提供了其他用於接受輸入的函式。

示例

為了理解使用者輸入的必要性,請考慮以下 C 程式 -

#include <stdio.h>

int main(){
   int price, qty, ttl;

   price = 100;
   qty = 5;
   ttl = price*qty;

   printf("Total: %d", ttl);
   
   return 0;
}

輸出

上述程式透過將客戶購買商品的價格和數量相乘來計算總購買金額。執行程式碼並檢查其輸出 -

Total: 500

對於具有不同價格和數量值的另一筆交易,您需要編輯程式,輸入值,然後再次編譯和執行。每次都這樣做是一項繁瑣的操作。相反,必須在程式執行後為變數賦值的規定。scanf() 函式在執行時讀取使用者輸入,並將值賦給變數。

C 使用者輸入函式:scanf()

C 語言 將標準輸入流識別為 stdin,並由標準輸入裝置(如鍵盤)表示。C 始終以字元的形式從輸入流中讀取資料。

scanf() 函式 使用適當的格式說明符將輸入轉換為所需的 資料型別

scanf() 的語法

以下是如何在 C 語言中使用 scanf() 函式 -

int scanf(const char *format, &var1, &var2, . . .);

scanf() 函式的第一個引數是格式字串。它指示將使用者輸入解析到的變數的資料型別。後面跟著一個或多個指向 變數指標。以 & 為字首的變數名給出變數的地址。

使用者輸入格式說明符

以下格式說明符用於格式字串 -

格式說明符 型別
%c 字元
%d 有符號整數
%f 浮點數
%i 無符號整數
%l 或 %ld 或 %li 長整型
%lf 雙精度浮點數
%Lf 長雙精度浮點數
%lu 無符號整數或無符號長整型
%lli 或 %lld 長長整型
%llu 無符號長長整型

示例:C 語言中的使用者輸入

回到前面的示例,我們將使用 scanf() 函式來接受“price”和“qty”的值,而不是為它們分配任何固定值。

#include <stdio.h>

int main(){

   int price, qty, ttl;

   printf("Enter price and quantity: ");
   scanf("%d %d", &price, &qty);

   ttl = price * qty;

   printf("Total : %d", ttl);
   
   return 0;
}

輸出

當執行上述程式時,C 會等待使用者輸入值 -

Enter price and quantity:

輸入值並按下 Enter 後,程式將繼續執行後續步驟。

Enter price and quantity: 100 5
Total : 500

更重要的是,對於另一組值,您無需再次編輯和編譯。只需執行程式碼,程式將再次等待使用者輸入。這樣,程式可以用不同的輸入執行任意次數。

Enter price and quantity: 150 10
Total : 1500

整數輸入

%d 格式說明符已定義為有符號整數。以下程式讀取使用者輸入並將其儲存在整數變數 num 中。

示例:C 語言中的整數輸入

檢視以下程式程式碼 -

#include <stdio.h>

int main(){
   
   int num;
   
   printf("Enter an integer: ");
   scanf("%d", &num);
   
   printf("You entered an integer: %d", num);
   
   return 0;
}

輸出

執行程式碼並檢查其輸出 -

Enter an integer: 234
You entered an integer: 234

如果輸入非數字值,則輸出將為“0”。

scanf() 函式可以讀取一個或多個變數的值。在提供輸入值時,必須用空格、製表符或 Enter 分隔連續的值。

示例:C 語言中的多個整數輸入

#include <stdio.h>

int main(){

   int num1, num2;

   printf("Enter two integers: ");
   scanf("%d %d", &num1, &num2);

   printf("You entered two integers : %d and %d", num1, num2);

   return 0;
}

輸出

執行程式碼並檢查其輸出 -

Enter two integers: 45 57
You entered two integers : 45 and 57

Enter two integers: 45
57

浮點數輸入

對於浮點數輸入,需要使用 %f 格式說明符。

示例:C 語言中的浮點數輸入

#include <stdio.h>

int main(){

   float num1;
   
   printf("Enter a number: ");
   scanf("%f", &num1);
   
   printf("You entered a floating-point number: %f", num1);
   
   return 0;
}

輸出

執行程式碼並檢查其輸出 -

Enter a number: 34.56
You entered a floating-point number: 34.560001

示例:C 語言中的整數和浮點數輸入

scanf() 函式可以讀取不同型別變數的輸入。在以下程式中,使用者輸入儲存在整數和浮點變數中 -

#include <stdio.h>

int main(){

   int num1;
   float num2;
   
   printf("Enter two numbers: ");
   scanf("%d %f", &num1, &num2);
   
   printf("You entered an integer: %d a floating-point number: %6.2f", num1, num2);
   
   return 0;
}

輸出

執行程式碼並檢查其輸出 -

Enter two numbers: 65 34.5678
You entered an integer: 65 a floating-point number:  34.57

字元輸入

%c 格式說明符從鍵盤讀取單個字元。但是,我們必須在格式字串中的 %c 之前留一個空格。這是因為 %c 轉換說明符不會自動跳過任何前導空格。

如果輸入流中存在雜散換行符(例如,來自之前的條目),則 scanf() 呼叫將立即消耗它。

scanf(" %c", &c);

格式字串中的空格告訴 scanf 跳過前導空格,並且第一個非空格字元將使用 %c 轉換說明符讀取。

示例:C 語言中的字元輸入

檢視以下示例 -

#include <stdio.h>

int main(){

   char ch;
   
   printf("Enter a single character: ");
   scanf(" %c", &ch);
   
   printf("You entered character : %c", ch);
   
   return 0;
}

輸出

執行程式碼並檢查其輸出 -

Enter a single character: x
You entered character : x

示例:C 語言中的多個字元輸入

以下程式將用空格分隔的兩個字元讀取到兩個 char 變數中。

#include <stdio.h>

int main(){

   char ch1, ch2;
   
   printf("Enter two characters: ");
   scanf("%c %c", &ch1, &ch2);
   
   printf("You entered characters: %c and %c", ch1, ch2);
   
   return 0;
}

輸出

執行程式碼並檢查其輸出 -

Enter two characters: x y
You entered characters: x and y

stdio.h 標頭檔案還提供了 getchar() 函式。與 scanf() 不同,getchar() 沒有格式字串。此外,它讀取單個擊鍵,無需 Enter 鍵。

示例:使用 gets() 讀取字元

以下程式將單個鍵讀取到 char 變數中 -

#include <stdio.h>

int main(){

   char ch;
   
   printf("Enter a character: ");
   ch = getchar();
   
   puts("You entered: ");
   putchar(ch);
   
   printf("\nYou entered character: %c", ch);
   
   return 0;
}

輸出

執行程式碼並檢查其輸出 -

Enter a character: W
You entered:
W
You entered character: W

您還可以使用無格式的 putchar() 函式列印單個字元。

示例:讀取字元序列

以下程式顯示瞭如何讀取一系列字元,直到使用者按下 Enter 鍵 -

#include <stdio.h>

int main(){

   char ch;
   char word[10];

   int i = 0;
   printf("Enter characters. End by pressing the Enter key: ");
   
   while(1){
      ch = getchar();
      word[i] = ch;
      if (ch == '\n')
         break;
      i++;
   }
   printf("\nYou entered the word: %s", word);
   
   return 0;
}

輸出

執行程式碼並檢查其輸出 -

Enter characters. End by pressing the Enter key: Hello

You entered the word: Hello

字串輸入

還有一個%s格式說明符,用於將一系列字元讀入字元陣列。

示例:使用scanf()進行字串輸入

以下程式接受來自鍵盤的字串輸入:

#include <stdio.h>

int main(){

   char name[20];
   
   printf("Enter your name: ");
   scanf("%s", name);
   
   printf("You entered the name: %s", name);
   
   return 0;
}

輸出

執行程式碼並檢查其輸出 -

Enter your name: Ravikant
You entered the name: Ravikant

C語言使用空格作為分隔符。因此,如果您嘗試輸入包含空格的字串,則只有空格之前的字元會被儲存為值。

Enter your name: Ravikant Soni
You entered the name: Ravikant

gets()函式克服了此限制。它是一個非格式化字串輸入函式。直到您按下Enter鍵為止的所有字元都將儲存在變數中。

示例:使用gets()進行字串輸入

檢視以下示例 -

#include <stdio.h>
#include <stdlib.h>

int main(){

   char name[20];
   
   printf("Enter your name: ");
   gets(name);
   
   printf("You entered the name: %s", name);
   
   return 0;
}

輸出

執行程式碼並檢查其輸出 -

Enter your name: Ravikant Soni
You entered the name: Ravikant Soni

使用者輸入是C程式設計應用程式的一個重要方面。在本章中,我們透過不同的示例解釋了格式化和非格式化控制檯輸入函式scanf()getchar()gets()的用法。

廣告