C 語言中的標頭檔案“stdio.h”和“stdlib.h”
stdio.h
標頭檔案 stdio.h 代表標準輸入輸出。它包含與輸入/輸出函式相關的資訊。
下表顯示了 C 語言中 stdio.h 中的一些函式:
| 序號 | 函式及描述 |
|---|---|
| 1 | printf() 用於在輸出螢幕上列印字串、整數、字元等。 |
| 2 | scanf() 從鍵盤讀取字元、字串、整數等。 |
| 3 | getc() 從檔案讀取字元。 |
| 4 | putc() 將字元寫入檔案。 |
| 5 | fopen() 開啟檔案,所有檔案處理函式都在 stdio.h 標頭檔案中定義。 |
| 6 | fclose() 關閉已開啟的檔案。 |
| 7 | remove() 刪除檔案。 |
| 8 | fflush() 重新整理檔案。 |
以下是一個 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 中的一些函式:
| 序號 | 函式及描述 |
|---|---|
| 1 | malloc() 在程式執行期間分配記憶體。 |
| 2 | free() 釋放已分配的記憶體。 |
| 3 | abort() 終止 C 程式。 |
| 4 | exit() 終止程式,不返回值。 |
| 5 | atol() 將字串轉換為長整型。 |
| 6 | atoll() 將字串轉換為長長整型。 |
| 7 | atof() 將字串轉換為浮點數。 |
| 8 | rand() 返回一個隨機整數值 |
以下是一個 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
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP