C 語言中的標頭檔案“stdio.h”和“stdlib.h”


stdio.h

標頭檔案 stdio.h 代表標準輸入輸出。它包含與輸入/輸出函式相關的資訊。

下表顯示了 C 語言中 stdio.h 中的一些函式:

序號函式及描述
1printf()
用於在輸出螢幕上列印字串、整數、字元等。
2scanf()
從鍵盤讀取字元、字串、整數等。
3getc()
從檔案讀取字元。
4putc()
將字元寫入檔案。
5fopen()
開啟檔案,所有檔案處理函式都在 stdio.h 標頭檔案中定義。
6fclose()
關閉已開啟的檔案。
7remove()
刪除檔案。
8fflush()
重新整理檔案。

以下是一個 C 語言中 stdio.h 的示例:

示例

 線上演示

#include<stdio.h>

int main () {
   char val;

   printf("Enter the character: \n");
   val = getc(stdin);
   printf("Character entered: ");
   putc(val, stdout);

   return(0);
}

輸出

以下是輸出結果

Enter the character: s
Character entered: s

stdlib.h

標頭檔案 stdlib.h 代表標準庫。它包含記憶體分配/釋放函式的資訊。

下表顯示了 C 語言中 stdlib.h 中的一些函式:

序號函式及描述
1malloc()
在程式執行期間分配記憶體。
2free()
釋放已分配的記憶體。
3abort()
終止 C 程式。
4exit()
終止程式,不返回值。
5atol()
將字串轉換為長整型。
6atoll()
將字串轉換為長長整型。
7atof()
將字串轉換為浮點數。
8rand()
返回一個隨機整數值

以下是一個 C 語言中 stdlib.h 的示例:

示例

 線上演示

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

int main() {
   char str1[20] = "53875";
   char str2[20] = "367587938";
   char str3[20] = "53875.8843";

   long int a = atol(str1);
   printf("String to long int : %d\n", a);

   long long int b = atoll(str2);
   printf("String to long long int : %d\n", b);

   double c = atof(str3);
   printf("String to long int : %f\n", c);
   printf("The first random value : %d\n", rand());
   printf("The second random value : %d", rand());

   return 0;
}

輸出

以下是輸出結果

String to long int : 53875
String to long long int : 367587938
String to long int : 53875.884300
The first random value : 1804289383
The second random value : 846930886

更新於: 2020年6月25日

15K+ 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.