Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

Header Files stdio.h และ stdlib.h ใน C

stdio.h (Standard Input Output)

ฟังก์ชัน I/O หลัก

Functionคำอธิบาย
printf()พิมพ์ข้อมูลจอภาพ
scanf()อ่านข้อมูลจากคีย์บอร์ด
getc()อ่านตัวอักษรจากไฟล์
putc()เขียนตัวอักษรลงไฟล์
fopen()เปิดไฟล์
fclose()ปิดไฟล์
remove()ลบไฟล์
fflush()ล้าง Buffer

ตัวอย่าง

#include <stdio.h>
int main() {
    char val;
    printf("Enter the character: \n");
    val = getc(stdin);
    printf("Character entered: ");
    putc(val, stdout);
    return 0;
}

stdlib.h (Standard Library)

Memory Allocation, Conversion, Process Control, Random

Functionคำอธิบาย
malloc()จองหน่วยความจำ
free()ปล่อยหน่วยความจำ
abort()ยกเลิกโปรแกรมทันที
exit()จบโปรแกรม
atol()String → Long Int
atoll()String → Long Long Int
atof()String → Double
rand()สุ่มเลข

ตัวอย่าง

#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 : %ld\n", a);
    
    long long int b = atoll(str2);
    printf("String to long long int : %lld\n", b);
    
    double c = atof(str3);
    printf("String to double : %f\n", c);
    
    printf("Random 1: %d\n", rand());
    printf("Random 2: %d\n", rand());
    return 0;
}

ผลลัพธ์

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