ݺߣ

ݺߣShare a Scribd company logo
ฟังก์ชันในภาษา
ซี
ใช้สำาหรับ Turbo C++
Version 3.0
เนื้อหาเนื้อหา
 ฟังก์ชันคืออะไร
 โครงสร้างของฟังก์ชัน
 ฟังก์ชันชนิดต่าง
 การเขียน prototype ของฟังก์ชัน
 Library Function
ฟังก์ชันคืออะไรฟังก์ชันคืออะไร
ในการเขียนโปรแกรมคอมพิวเตอร์บางครั้ง
จะต้องมีชุดคำาสั่งบางชุดที่จะต้องทำาบ่อยๆ ถ้า
หากจะต้องเขียนชุดคำาสั่งนั้นบ่อยครั้ง ก็จะ
ทำาให้โปรแกรมมีขนาดใหญ่ เราจึงสามารถ
รวมเอาชุดคำาสั่งบ่อยๆ นั้นมารวมเป็นฟังก์ชัน
 ฟังก์ชัน คือ กลุ่มของคำาสั่งที่สามารถใช้เพื่อ
ทำางานเฉพาะอย่าง
 ประโยชน์ที่สำาคัญของฟังก์ชันคือช่วยลด
ความซำ้าซ้อนของโปรแกรม นอกจากนี้ยัง
ฟังก์ชันในภาษาซีฟังก์ชันในภาษาซี
ฟังก์ชันในภาษาซี แบ่งออกได้เป็น 2 ประเภท
คือ
 ฟังก์ชันมาตรฐาน (Standard Function)
เป็นฟังก์ชันที่ตัว Compiler มีมาให้อยู่แล้ว
เช่นฟังก์ชันการคำานวณทางคณิตศาสตร์
ฟังก์ชันการรับและแสดงผล เหล่านี้เรียกว่า
Library Function ในการใช้งานจะต้อง
เรียกใช้ Include Directives จึงสามารถใช้
งานฟังก์ชันนั้นๆ ได้
 ฟังก์ชันที่ผู้เขียนโปรแกรมสร้างขึ้น
ฟังก์ชันในภาษาซีฟังก์ชันในภาษาซี
ในการเขียนโปรแกรมด้วยภาษาซีจะต้องมี
ฟังก์ชัน main() เสมอ และถ้าต้องการให้
โปรแกรมทำางานใดๆ ฟังก์ชัน main() จะไป
เรียกใช้ฟังก์ชันอื่นๆ ให้ทำางาน และในฟังก์ชัน
ที่สร้างขึ้นก็ยังสามารถเรียกใช้ฟังก์ชันอื่นๆ ได้
อีกด้วย
โครงสร้างของฟังก์ชันโครงสร้างของฟังก์ชัน
ฟังก์ชันมาตรฐานและฟังก์ชันที่ผู้ใช้สร้างขึ้น มี
โครงสร้าง ดังนี้
 type เป็นชนิดของผลลัพธ์ที่ได้จากการทำางานของ
ฟังก์ชัน หากไม่กำาหนด จะถูกกำาหนดเป็น int ใน
กรณีที่ไม่ต้องการส่งค่ากลับให้ใส่ void
 function-name คือ ชื่อของฟังก์ชันที่สร้างชึ้น
type function-name(type para-1, type para-2, ... , type para-n)
{
local variable-declarations ;
statement-1 ;
statement-2 ;
...
statement-n ;
return(value) ;
}
type function-name(type para-1, type para-2, ... , type para-n)
{
local variable-declarations ;
statement-1 ;
statement-2 ;
...
statement-n ;
return(value) ;
}
type function-name(type para-1, type para-2, ... , type para-n)
{
local variable-declarations ;
statement-1 ;
statement-2 ;
...
statement-n ;
return(value) ;
}
โครงสร้างของฟังก์ชันโครงสร้างของฟังก์ชัน
ฟังก์ชันมาตรฐานและฟังก์ชันที่ผู้ใช้สร้างขึ้น มี
โครงสร้าง ดังนี้
 type para-1, type para-2, ... , type para-n
หรือ parameter เป็นตัวแปรที่ต้องส่งเข้ามาเพื่อ
นำามาประมวลผลเมื่อมีการเรียกใช้ฟังก์ชันพร้อมทั้ง
กำาหนดชนิดของตัวแปรด้วย type หากมีตัวแปร
type function-name(type para-1, type para-2, ... , type para-n)
{
local variable-declarations ;
statement-1 ;
statement-2 ;
...
statement-n ;
return(value) ;
}
type function-name(type para-1, type para-2, ... , type para-n)
{
local variable-declarations ;
statement-1 ;
statement-2 ;
...
statement-n ;
return(value) ;
}
โครงสร้างของฟังก์ชันโครงสร้างของฟังก์ชัน
ฟังก์ชันมาตรฐานและฟังก์ชันที่ผู้ใช้สร้างขึ้น มี
โครงสร้าง ดังนี้
 ชุดคำาสั่งทั้งหมด จะต้องรวมอยู่ในวงเล็บปีกกา { }
 local variable-declarations เป็นการสร้าง
ตัวแปรเพื่อใช้งานภายในฟังก์ชัน

type function-name(type para-1, type para-2, ... , type para-n)
{
local variable-declarations ;
statement-1 ;
statement-2 ;
...
statement-n ;
return(value) ;
}
type function-name(type para-1, type para-2, ... , type para-n)
{
local variable-declarations ;
statement-1 ;
statement-2 ;
...
statement-n ;
return(value) ;
}
type function-name(type para-1, type para-2, ... , type para-n)
{
local variable-declarations ;
statement-1 ;
statement-2 ;
...
statement-n ;
return(value) ;
}
type function-name(type para-1, type para-2, ... , type para-n)
{
local variable-declarations ;
statement-1 ;
statement-2 ;
...
statement-n ;
return(value) ;
}
โครงสร้างของฟังก์ชันโครงสร้างของฟังก์ชัน
ฟังก์ชันมาตรฐานและฟังก์ชันที่ผู้ใช้สร้างขึ้น มี
โครงสร้าง ดังนี้
 return(value) ใช้คืนค่าออกจากฟังก์ชัน โดย
ค่าที่คืนออกไปจะเป็นชนิดเดียวกันกับที่ประกาศไว้
ก่อน หน้าชื่อฟังก์ชัน หากไม่มีการคืนค่าก็จะไม่
ต้องใส่ในส่วนนี้
type function-name(type para-1, type para-2, ... , type para-n)
{
local variable-declarations ;
statement-1 ;
statement-2 ;
...
statement-n ;
return(value) ;
}
type function-name(type para-1, type para-2, ... , type para-n)
{
local variable-declarations ;
statement-1 ;
statement-2 ;
...
statement-n ;
return(value) ;
}
ฟังก์ชันประเภทต่างๆฟังก์ชันประเภทต่างๆ
ฟังก์ชันสามารถแบ่งออกเป็น 3 ประเภท ตาม
การรับ-ส่งค่าข้อมูล
 ฟังก์ชันประเภทไม่มีการรับส่งค่าใดๆ เป็นแบบ
ที่ง่ายที่สุด คือไม่ต้องส่งพารามิเตอร์ให้กับฟังก์ชัน
และฟังก์ชันไม่ส่งค่าอะไรคืนออกมา โดยปกติใช้
แสดงข้อความที่มีการใช้งานบ่อยๆ
 ฟังก์ชันแบบส่งค่าผ่านทางเดียว สามารถเป็น
ได้ 2 แบบคือ ฟังก์ชันที่มีการรับค่าเข้ามาทำางานใน
ฟังก์ชันอย่างเดียว หรือมีการคืนค่าออกมาอย่าง
เดียว
 ฟังก์ชันแบบส่งค่าสองทาง ฟังก์ชันที่มีการรับค่า
ตัวอย่าง 1 โปรแกรมที่ยังไม่ได้
สร้างฟังก์ชัน
#include <stdio.h>
#include <conio.h>
void main()
{
printf("n");
printf("StarLine Printingn");
printf("n");
getch();
}
โปรแกรม
ผลการ
ทำางาน ?
StarLine Printing
ตัวอย่าง 1 โปรแกรมที่ยังไม่ได้
สร้างฟังก์ชัน
#include <stdio.h>
#include <conio.h>
void main()
{
printf("n");
printf("StarLine Printingn");
printf("n");
getch();
}
โปรแกรม
ผลการ
ทำางาน ?
StarLine Printing

แทรกช่อง
ว่าง
ตัวอย่าง 1 โปรแกรมที่ยังไม่ได้
สร้างฟังก์ชัน
#include <stdio.h>
#include <conio.h>
StarLine( )
printf("n");
void main()
{
StarLine();
printf("StarLine Printingn");
StarLine();
getch();
}
โปรแกรม
ผลการ
ทำางาน ?
StarLine Printing

ชื่อที่ต้องการ
เรียกใช้งาน
คำาสั่งเดิม
ที่ใช้
ชื่อของฟังก์ชัน
ตัวอย่าง 1 ฟังก์ชันที่ไม่มีการ
รับส่งค่า
#include <stdio.h>
#include <conio.h>
void StarLine(void)
{
printf("n");
}
void main()
{
StarLine();
printf("StarLine Printingn");
StarLine();
getch();
}
โปรแกรม
ผลการ
ทำางาน ?
StarLine Printing

ไม่มีการรับ
ส่งค่า
การเรียก
ใช้งาน
ตัวอย่าง 2 ฟังก์ชันที่มีการรับ
ค่า
#include <stdio.h>
#include <conio.h>
void StarLine(int n)
{
int i;
for(i=1; i<=n; i++)
printf("*");
printf("n");
}
void main()
{
StarLine(17);
printf("StarLine Printingn");
StarLine(17);
getch();
}
โปรแกรม
ผลการ
ทำางาน ?**
StarLine Printing
**
มีการรับค่า
n
การเรียก
ใช้
ฟังก์ชันที่มีการรับค่าฟังก์ชันที่มีการรับค่า
ฟังก์ชันที่มีการรับค่าสามารถแบ่งย่อยได้
เป็นประเภทต่างๆ ตามลักษณะของพารามิเตอร์
ที่ส่งให้ฟังก์ชัน ดังนี้
 Pass by Value ฟังก์ชันจะรับค่าตัวแปร ค่าคงที่
หรือนิพจน์ โดยการเปลี่ยนแปลงของตัวแปรภายใน
จะไม่มีผลกับตัวแปรด้านนอก ถึงแม้ตัวแปรจะมีชื่อ
เดียวกัน
 Pass by Reference ฟังก์ชันจะรับตำาแหน่งใน
หน่วยความจำาของตัวแปร หากมีการเปลี่ยนแปลง
ค่าภายในฟังก์ชัน ตัวแปรที่อยู่นอกฟังก์ชันจะ
เปลี่ยนแปลงด้วย เนื่องจากเป็นตัวแปรที่ใช้หน่วย
ตัวอย่าง 3 ฟังก์ชัน Pass by
Value
#include <stdio.h>
#include <conio.h>
void NumChange(int number)
{
printf("Before change : %dn",number);
number += 5;
printf("After change : %dn",number);
}
void main()
{
int number = 15;
NumChange(number);
printf("After entering the function : %dn",number);
getch();
}
โปรแกรม
ผลการ
Before change : 15
After change : 20
After entering the function : 15
มีการรับค่า
number
Pass by Value
ตัวอย่าง 4 ฟังก์ชัน Pass by
Reference
#include <stdio.h>
#include <conio.h>
void NumChange(int &number2)
{
printf("Before change : %dn",number2);
number2 += 5;
printf("After change : %dn",number2);
}
void main()
{
int number = 15;
NumChange(number);
printf("After entering the function : %dn",number);
getch();
}
โปรแกรม
ผลการ
Before change : 15
After change : 20
After entering the function : 20
มีการรับค่า
number2
Pass by Reference
ตัวอย่าง 5 ฟังก์ชัน Pass by
Reference
#include <stdio.h>
#include <conio.h>
void NumChange(int *number2)
{
printf("Before change : %dn",*number2);
*number2 += 5;
printf("After change : %dn",*number2);
}
void main()
{
int number = 15;
NumChange(&number);
printf("After entering the function : %dn",number);
getch();
}
โปรแกรม
ผลการ
Before change : 15
After change : 20
After entering the function : 20
มีการรับค่า
number2
Pass by Reference
ตัวอย่าง 6 ฟังก์ชันที่รับค่าเป็น
อาร์เรย์
#include <stdio.h>
#include <conio.h>
void Dollar(float baht[], int num)
{
int i;
printf("Before changing : %.02fn", baht[0]);
for(i=0; i<num; i++)
baht[i] *= 40.0;
printf("After changing : %.02fn", baht[0]);
}
void main()
{
float money[3] = {200.0, 400.0, 600.0};
Dollar(money, 3);
printf("After entering function: %.02fn", money[0]);
getch();
}
โปรแกรม
ผลการ
ทำางาน ?
Before changing : 200.00
After changing : 8000.00
After entering function: 8000.00
ตัวอย่าง 7 ฟังก์ชันที่มีการส่งค่า
กลับ
#include <stdio.h>
#include <conio.h>
float FindArea(float radius)
{
return(3.14*radius*radius);
}
void main()
{
float radius, area;
printf("Enter Radius: ");
scanf("%f", &radius);
area = FindArea(radius);
printf("Area is %0.2fn", area);
getch();
}
โปรแกรม
ผลการ
Enter Radius: 5Enter Radius: 5
Area is 78.50
มีการรับส่ง
ค่า
ส่งค่ากลับ
เรียกใช้
ฟังก์ชัน
และมี
ตัวแปรรับ
ค่า
การเรียกใช้ฟังก์ชันที่มีการการเรียกใช้ฟังก์ชันที่มีการ
ส่งค่ากลับส่งค่ากลับ
การเรียกใช้ฟังก์ชันจะเรียกใช้ในส่วนคำา
สั่งของฟังก์ชันหลัก (main) ลักษณะการเรียก
ใช้จะเหมือนกับฟังก์ชันมาตรฐานที่มีอยู่แล้ว
ดังนี้
area = FindArea(radius);
หรือ
printf("%f",FindArea(radius));
จะเห็นได้ว่าการเรียกฟังก์ชันในฟังก์ชัน
หลักจะต้องมีตัวแปร มารองรับผลลัพธ์ที่ได้จาก
ฟังก์ชันเสมอ เนื่องจากฟังก์ชันจะต้องมีการส่ง
การเขียนการเขียน PrototypePrototype ของของ
ฟังก์ชันฟังก์ชัน
หากต้องการเขียน main() เป็นฟังก์ชันแรก
ของโปรแกรมแล้ว จะใช้ prototype เพื่อ
ประกาศฟังก์ชันอื่นไว้ก่อน โดยประกาศไว้หลัง
Preprocessor Directive หรือไว้ใน main()
เลยก็ได้
#include <stdio.h>
#include <conio.h>
void StarLine(void);
void NumChange(int number);
float FindArea(float radius);
void main()
{...}
void StarLine(void)
{...}
void NumChange(int number)
{...}
float FindArea(float radius)
{...}
Prototype
Function
คือ หัวของ
ฟังก์ชันที่สร้างไว้
การเรียกใช้ฟังก์ชันการเรียกใช้ฟังก์ชัน
การเรียกใช้ฟังก์ชัน ในกรณีที่มีการสร้าง
ฟังก์ชันไว้หลายฟังก์ชัน จะสามารถเรียกใช้
ฟังก์ชันที่อยู่ก่อนหน้าเท่านั้น
ถ้าต้องการที่จะเรียกใช้ฟังก์ชันที่อยู่ด้าน
ล่าง จะต้องมีการกำาหนด Prototype ของ
ฟังก์ชันนั่น ไว้ก่อนฟังก์ชันที่จะเรียกใช้
#include <stdio.h>
#include <conio.h>
void StarLine(void)
{...}
void NumChange(int number)
{...}
float FindArea(float radius)
{...}
void main()
{...}
เรียกใช้ StarLine และ
NumChange ได้
เรียกใช้ StarLine ได้
ไม่สามารถเรียกฟังก์ชัน
ใดได้
เรียกใช้ได้ทุกฟังก์ชัน
แทรก Prototype จะทำาให้เรียก
ใช้ได้ทุกฟังก์ชัน
void NumChange(int number);
float FindArea(float radius);
#include <stdio.h>
#include <conio.h>
void StarLine(void);
void main()
{
StarLine();
printf("StarLine Printingn");
StarLine();
getch();
}
void StarLine(void)
{
printf("n");
}
โปรแกรม
ผลการ
ทำางาน ?
StarLine Printing

Prototype
Function
ตัวอย่าง 8 การเขียน
Prototype ของฟังก์ชัน
ตัวอย่าง 9 การเขียน
Prototype ของฟังก์ชัน
#include <stdio.h>
int ADD_AB(int A, int B);
void main()
{
int A = 2, B=7, C;
C = ADD_AB(A,B);
printf("Answer is %d",C);
}
int ADD_AB(int A, int B)
{
int x;
x = A+B;
return x;
}
โปรแกรม
ผลการ
ทำางาน ?
Answer is 9
Prototype
Function
ชนิดของตัวแปรในฟังก์ชันชนิดของตัวแปรในฟังก์ชัน
ชนิดของตัวแปรแยกตามขอบเขตการทำางาน
 ตัวแปร Gobal เป็นตัวแปรที่เรียกใช้จากที่
ใดก็ได้ในโปรแกรมโดยจะถูกประกาศไว้หลัง
Preprocessor Directive
 ตัวแปร Local ถูกสร้างขึ้นภายในฟังก์ชัน
การเปลี่ยนแปลงจะมีผลภายในฟังก์ชัน
เท่านั้น หากมีชื่อซำ้ากันจะถือว่าเป็นคนละ
ตัวแปรกัน ตัวแปรประเภทนี้ เมื่อโปรแกรม
ออกจากฟังก์ชันจะถูกทำาลาย
ตัวอย่าง 10 ตัวแปร Gobal
#include <stdio.h>
#include <conio.h>
int num = 19;
void increase()
{
num = num + 5;
}
void main()
{
printf("num = %dn", num);
increase();
printf("num = %dn", num);
getch();
}
โปรแกรม
ผลการ
num = 19num = 19
num = 24
Gobal variable
ตัวอย่าง 11 ตัวแปร Local
#include <stdio.h>
#include <conio.h>
void new_value()
{
int x;
x = 100;
}
void main()
{
int x = 59;
printf("x = %dn", x);
new_value();
printf("x = %dn", x);
getch();
}
โปรแกรม
ผลการ
num = 59num = 59
num = 59
Local variable
Local variable
ฟังก์ชันมาตรฐานฟังก์ชันมาตรฐาน ((LibraryLibrary
FunctionFunction))
Library Function เป็นฟังก์ชันสำาเร็จรูปที่
มีมาให้พร้อมกับ Compiler โดยเก็บอยู่ใน
ไฟล์ Header หรือ *.h การใช้งานจะต้อง
#include <file.h> ไว้ก่อน
Library Function ที่สำาคัญ
 math.h ฟังก์ชันการคำานวณทาง
คณิตศาสตร์
 string.h ฟังก์ชันสำาหรับอักขระและ
ข้อความ
ฟังก์ชันการคำานวณทางฟังก์ชันการคำานวณทาง
คณิตศาสตร์คณิตศาสตร์ –– math.hmath.h
FunctionFunction DescriptionDescription
sinsin((xx)) sine ของ x, x มีหน่วยเป็นเรเดียน
coscos((xx)) cosine ของ x, x มีหน่วยเป็นเรเดียน
tantan((xx)) tangent ของ x, x มีหน่วยเป็นเรเดียน
sqrtsqrt((xx)) รากที่สองของ x, x>=0
powpow((x,yx,y)) x ยกกำาลัง y
loglog((xx)) logarithm ฐาน e ของ x, x>=0
log10log10((xx)) logarithm ฐาน 10 ของ x, x>=0
expexp((xx)) e ยกกำาลัง x
absabs((xx)) ค่าสัมบูรณ์ของ x, x เป็นเลขจำานวนเต็ม
fabsfabs((xx)) ค่าสัมบูรณ์ของ x, x เป็นเลขจำานวนจริง
ตัวอย่าง 12 การใช้ math.h
#include <stdio.h>
#include <conio.h>
#include <math.h>
double x1 = M_PI, x2 = M_PI_4, y;
void main()
{
y = sin(x1);
printf("sin(pi) = %7.4fn", y);
printf("cos(pi/4) = %7.4fn", cos(x2));
printf("tan(pi/6) = %7.4fn", tan(M_PI/6));
printf("sqrt(2) = %7.4fn", sqrt(2));
printf("2.5^3 = %7.4fn", pow(2.5,3));
printf("loge(e) = %7.4fn", log(M_E));
printf("log(100) = %7.4fn", log10(100));
printf("e^3 = %7.4fn", exp(3));
printf("abs(-7) = %7.4fn", fabs(-7));
getch();
}
โปรแกรม
ผลการ
sin(pi) = 0.0000
cos(pi/4) = 0.7071
tan(pi/6) = 0.5774
sqrt(2) = 1.4142
2.5^3 = 15.6250
loge(e) = 1.0000
log(100) = 2.0000
e^3 = 20.0855
abs(-7) = 7.0000
ฟังก์ชันสำาหรับข้อความฟังก์ชันสำาหรับข้อความ ––
string.hstring.h
FunctionFunction DescriptionDescription
strcpystrcpy((str1, str2str1, str2)) คัดลอกข้อความจาก str2 ไปเก็บที่
str1
strcatstrcat((str1, str2str1, str2)) ต่อข้อความใน str1 ด้วย str2
strcmpstrcmp((str1, str2str1, str2))
เปรียบเทียบตัวอักษรในข้อความ (case
sensitive) ถ้า str1 =
str2 จะได้ 0
strlenstrlen((strstr)) หาความยาวข้อความ
ฟังก์ชันสำาหรับอักขระฟังก์ชันสำาหรับอักขระ ––
ctype.hctype.hFunctionFunction DescriptionDescription
tolowertolower((chch)) เปลี่ยนอักขระเป็นตัวพิมพ์เล็ก
touppertoupper((chch)) เปลี่ยนอักขระเป็นตัวพิมพ์ใหญ่
ตัวอย่าง 13 การใช้ strcpy
และ strcat
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char str1[30] = "Current Date ";
char str2[30] = "18 August 2006";
char str3[30];
strcpy(str3, str1);
strcat(str1, str2);
printf("str1 = %sn", str1);
printf("str3 = %sn", str3);
getch();
}
โปรแกรม
ผลการ
str1 = Current Date 18 August 2006
str3 = Current Date
ตัวอย่าง 14 การใช้ strcmp
และ strlen
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char buf1[] = "Nanometer", buf2[] = "Nanometer";
int ptr;
printf("buffer 1 : "%s"n",buf1);
printf("buffer 2 : "%s"n",buf2);
ptr = strcmp(buf2, buf1);
if(ptr == 0)
printf("buffer 2 is equal to buffer 1n");
printf("Length of buffer 1 : %dn", strlen(buf1));
getch();
}
โปรแกรม
ผลการ
buffer 1 : "Nanometer"
buffer 2 : "Nanometer"
buffer 2 is equal to buffer 1
Length of buffer 1 : 9
ตัวอย่าง 15 การใช้ tolower
และ toupper
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
void main()
{
int length, i;
char *string = "This Is a String";
length = strlen(string);
for(i=0; i<length; i++)
string[i] = toupper(string[i]);
printf("%sn",string);
for(i=0; i<length; i++)
string[i] = tolower(string[i]);
printf("%sn",string);
getch();
}
โปรแกรม
ผลการ
ทำางาน ?THIS IS A STRING
this is a string
ฟังก์ชันจัดการหน้าจอฟังก์ชันจัดการหน้าจอ ––
conio.hconio.h
FunctionFunction DescriptionDescription
clrscrclrscr()() ลบหน้าจอ
clreolclreol()()
ลบข้อความตั้งแต่ตำาแน่ง cursor จนจบ
บรรทัด
gotoxygotoxy((x,yx,y))
ย้าย cursor ไปตำาแหน่งที่ต้องการ
x : column 1-80 | y : row 1-25
dellinedelline()()
ลบบรรทัดที่ cursor อยู่แล้วเลื่อน
บรรทัดล่างขึ้น
inslineinsline()()
แทรกบรรทัดว่างเข้ามาก่อนบรรทัดที่
cursor อยู่
getchgetch()() หยุดคอยให้กดคีย์ใดก็ได้ 1 ครั้ง
ตัวอย่าง 16 การใช้ฟังก์ชัน
จัดการหน้าจอ
#include <stdio.h>
#include <conio.h>
void main()
{
char line1[] = "Ferguson wants Rooney for life";
char line2[] = "Henry pleading with Cole to stay";
char line3[] = "Mourinho not giving up on Gallas";
clrscr();
printf("%sn", line1);
printf("%sn", line2);
printf("%sn", line3);
gotoxy(14,1);
clreol();
gotoxy(1,2);
insline();
getch();
}
โปรแกรม
ผลการ
ทำางาน ?Ferguson want
Henry pleading with Cole to stay
Mourinho not giving up on Gallas
ฟังก์ชันฟังก์ชัน sizeof(x)sizeof(x)
sizeof(x) หาขนาดพื้นที่ในหน่วยความจำา
ที่เก็บตัวแปร มีหน่วยเป็น byte, x คือตัวแปรที่
ต้องการหาขนาด ไม่ต้องมี include ใดๆ เพิ่ม#include <stdio.h>
#include <conio.h>
void main()
{
int age = 14;
float grade = 4.00;
char song[] = "More than word";
printf("size of age = %d bytesn", sizeof(age));
printf("size of grade = %d bytesn", sizeof(grade));
printf("size of song = %d bytesn", sizeof(song));
printf("size of double = %d bytesn", sizeof(double));
getch();
}
ตัวอย่าง 17 โปรแกรมใช้
งานฟังก์ชัน sizeof(x)
ผลการ
size of age = 2 bytes
size of grade = 4 bytes
size of song = 15 bytes
size of double = 8 bytes
จบฟังก์ชันในภาษา
ซี
Question ?Question ?

More Related Content

4.ฟังก์ชันในภาษาซี