2. โครงสร้างภาษาซี
TRANSCRIPT
![Page 1: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/1.jpg)
โครงสร างภาษาซเบ องต น
ใชส ำาหร บ Turbo C++ Version 3.0
![Page 2: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/2.jpg)
โปรแกรมภาษาโปรแกรมภาษา ในการเขยนโปรแกรมคอมพวเตอร ดวย
ภาษาระดบตำาหรอระดบสง จะตองเปลยน ภาษานนใหเปนภาษาเครอง เพอใหเครอง
คอมพวเตอรทำางานได โปรแกรมตนฉบบ (Source Program)
โปรแกรมทเครองทำางานได (Executable Program)
การเขยนโปรแกรมดวยแอสเซมบล (ภาษาระดบตำา) เปนภาษาเครอง
ขนตอนการแปลงภาษาแอสเซมบลเป นภาษาเคร อง
![Page 3: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/3.jpg)
โปรแกรมภาษาโปรแกรมภาษา การเขยนโปรแกรมดวยภาษาระดบสงเปน
ภาษาเครอง อนเทอรพรเตอร (Interpreter)
คอมไพเลอร (Compiler)
ขนตอนการแปลภาษาโปรแกรม
![Page 4: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/4.jpg)
การเขยนโปรแกรมคอมพวเตอรใหทำางานไดตามเรา ตองการ ผเขยนโปรแกรมจะตองรวาจะใหโปรแกรม ทำาอะไร มขอมลอะไร และตองการอะไรจาก
โปรแกรม รวมทงรปแบบการแสดงผลดวย โดยทวไป จะมขนตอนการพฒนาโปรแกรม ดงน
การกำาหนดและวเคราะหปญหา การเขยนผงงานและซโดโคด การเขยนโปรแกรม การทดสอบและแกไขโปรแกรม การทำาเอกสารและบำารงรกษาโปรแกรม
ขนตอนพฒนาโปรแกรมขนตอนพฒนาโปรแกรม
![Page 5: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/5.jpg)
ภาษาทเปนโครงสราง คำาสงประกอบดวยพจน (term) ซงจะมลกษณะเหมอน
กบนพจนทางพชคณต มสวนขยายเปนคำาหลก (keyword) ในภาษาองกฤษ
เชน if, else, for, do และ while สามารถใชงานในระดบตำา (low-level) ได
สามารถใชกบงานดานโปรแกรมระบบ (system programming) เชน เขยนโปรแกรมระบบปฏบตการ
(operating system) หรอใชกบงานทว ๆ ไป สามารถยายไปทำางานในเครองอนได
แนะนำาภาษาซแนะนำาภาษาซ
![Page 6: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/6.jpg)
โครงสร างโปรแกรมภาษาโครงสร างโปรแกรมภาษาซซ
void main(void)
{
}
Statements ;
Local Declarations
Preprocessor directive
Global Declarations
main function
User define functions
User define functions
int function ()
{
}
Statements ;
Local Declarations
โครงสร างภาษาซประกอบ ดวยหลายสวน แตในการ
เข ยนไมจ ำาเป นจะต องเข ยนทกส วน
![Page 7: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/7.jpg)
#include#include #define#define #undef #if
#ifdef #ifndef #else #elif
#endif #line #error #pragma
ทกโปรแกรมตองม ใชเรยกไฟลทโปรแกรมใชในการทำางานรวมกน ใชกำาหนดคาคงทใหกบโปรแกรม เรมตนดวยเครองหมาย ## ทเราจะใชกนม 2 directives คอ
#include ใชสำาหรบเรยกไฟลทโปรแกรมใชในการทำางาน
#define ใชสำาหรบกำาหนดมาโครทใหกบโปรแกรม
การใช การใช Preprocessor Preprocessor DirectiveDirective
![Page 8: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/8.jpg)
การใช การใช #include#include
วธการใชงาน
ตวอยาง#include <stdio.h> ( เปนการเรยกใชไฟลstdio.h เขามาในโปรแกรม)#include <mypro.h> ( เปนการเรยกใชไฟลmypro.h เขามาในโปรแกรม)
#include <<ชอไฟล>> หรอ#include ““ชอไฟล””
< >< > จะเรยกไฟลใน directory ทกำาหนดโดยตวคอมไพลเลอร“ ”“ ” จะเรยกไฟลใน directory ททำางานอยในปจจบน
![Page 9: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/9.jpg)
การใช การใช #define#define
#define ชอ คาทตองการวธการใชงาน
ตวอยาง#define START 10 ( กำาหนดคา START = 10)
#define A 3*5/4 ( กำาหนดคาA=3*5/4)
#define pi 3.14159 ( กำาหนดคา pi = 3.14159)
#define sum(a,b) a+b ( กำาหนดคา sum(ตวแปรท1, ตวแปรท2) = ตวแปรท1+ตวแปรท2
![Page 10: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/10.jpg)
สวนประกาศ สวนประกาศ ((Global Global Declarations)Declarations)
เปนการประกาศตวแปรเพอใชงานในโปรแกรมโดยตวแปรนนสามารถใชไดในทกทในโปรแกรม
เปนสวนทใชในการประกาศ Function Prototype ของโปรแกรม
สวนนในบางโปรแกรมอาจจะไมมกไดตวอยาง
int summation(float x, float y) ; ( ประกาศ function summation)
int x,y ; ( กำาหนดตวแปรx,y เปนจำานวนเตม)
float z=3; ( กำาหนดตวแปรz เปนจำานวนจรง)
![Page 11: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/11.jpg)
สวนประกาศ สวนประกาศ ((Global Global Declarations)Declarations)
ตวอยาง#include <stdio.h>
int feet,inches;
void main()
{
feet = 6;
inches = feet * 12;
printf("Height in inches is %d",inches);
}
Height in inches is 72Height in inches is 72
ผลการทำางาน
![Page 12: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/12.jpg)
ฟงกช นหลกของโปรแกรม ฟงกช นหลกของโปรแกรม((Main Function)Main Function)
สวนนทกโปรแกรมจะตองม โดยโปรแกรมหลกจะเรมตนดวยmain() และตามดวยเครองหมายปกกาเปด ‘{’ และปกกาปด ‘}’
ระหวางปกกาจะประกอบไปดวยคำาสง(Statement) ตางๆ ทจะใหโปรแกรมทำางาน
แตละคำาสงจะตองจบดวยเซมโคลอน ‘;’ (Semicolon)
#include <stdio.h>void main(void){
...Statement ;
}
![Page 13: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/13.jpg)
ฟงกช นหลกของโปรแกรม ฟงกช นหลกของโปรแกรม((Main Function)Main Function)
ตวอยาง#include <stdio.h>
int feet,inches;
void main()
{
feet = 6;
inches = feet * 12;
printf("Height in inches is %d",inches);
}
Height in inches is 72Height in inches is 72
ผลการทำางาน
![Page 14: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/14.jpg)
การสร างฟงกช นใชงานเอง การสร างฟงกช นใชงานเอง((User Define Function)User Define Function)
#include <stdio.h>int function()void main(void){
...Statement ;
}int function(){
Statement ;...return (int value);
}
สรางฟงกชนหรอคำาใหม ขนมาใชงานตามทเราตองการ ระหวางปกกาจะประกอบดวยคำาสง(Statement) ตางๆ ทจะใหฟงกชนทำางาน
สามารถเรยกใชภายในโปรแกรมไดทกท
![Page 15: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/15.jpg)
การสร างฟงกช นใชงานเอง การสร างฟงกช นใชงานเอง((User Define Function)User Define Function)
ตวอยาง#include <stdio.h>int Feet2Inch(int);int feet,inches;void main(){ feet = 6; inches = Feet2Inch(feet); printf("Height in inches is %d",inches);}int Feet2Inch(int f){ return f*12;} Height in inches is 72Height in inches is 72
ผลการทำางาน
![Page 16: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/16.jpg)
การใชค ำาอธบาย การใชค ำาอธบาย((Program Comments)Program Comments)
การเขยนสวนอธบายโปรแกรม (comments) ทำาได 2 วธคอ
//// สำาหรบคำาอธบายไปจนถงทายบรรทด และ
/*/* คำาอธบาย */*/ ลกษณะการใชเหมอนวงเลบนนเอง
ใชเขยนสวนอธบายโปรแกรม (คอมเมนต) ชวยใหผศกษาโปรแกรมภายหลงเขาใจการทำางานของโปรแกรม
สวนของคำาอธบายจะถกขามเมอคอมไพลโปรแกรม
![Page 17: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/17.jpg)
การใชค ำาอธบาย การใชค ำาอธบาย((Program Comments)Program Comments)
ตวอยาง#include <stdio.h> //// Change Feet to Inches
void main() //// main function
{ //// Start
int feet,inches;
feet = 6; //// feet 6
inches = feet * 12; //// inches feet * 12
printf("Height in inches is %d", inches);
//// write inches
} //// Stop
Height in inches is 72Height in inches is 72
ผลการทำางาน
![Page 18: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/18.jpg)
การใช การใช printf()printf()
เปนคำาสงทใชในการแสดงผลออกทางจอภาพ โดยมรปแบบการใชงานดงน
printf(““controlcontrol หรอ format stringformat string””, variable listvariable list …);
control หรอ format string เป นส วนทใสข อความทจะแสดงผล และ
สวนควบคมล กษณะการแสดงผล รวมทงบอกตำาแหนงท ต วแปรจะแสดงผล
variable list เป นต วแปรทตองการจะแสดงผล ในกรณ
ทต องการแสดงขอความ ไมจ ำาเป นต องมสวนน
![Page 19: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/19.jpg)
โปรแกรมท 1 สราง folder ชอ 517111/รหสนกศกษา สรางไฟล hello.c โดยใหพมพคำาวา hello world การใชงาน turbo c
พมพชอตวเองเพมอกหนงบรรทด
F2 SaveAlt+F9 CompileCtrl+F9 Compile & RunAlt+F5 Output
![Page 20: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/20.jpg)
ตวอย างโปรแกรม
#include <stdio.h>
void main() { printf(“Hello world\n\n"); printf(“Welcome to Computer Programming 1"); return ;}
Hello world Welcome to Computer Programming 1
ผลการทำางาน
โปรแกรมBackslash n n
ขนบรรทดใหม
![Page 21: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/21.jpg)
คำาแนะนำา
#include <stdio.h>
main ()
{ clrscr();
….
getch();}
เคลยรหนาจอ
โปรแกรม
รอรบคาจากคยบอรด
![Page 22: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/22.jpg)
การใช การใช ControlControl ดวยดวย BackslashBackslash
จากตวอย างท 3 จะเหนไดวาหากตองการใหแสดง ผลขามบรรทดจะตองเพม \n ลงไป เรยกวา
backslash นอกจากนยงมตวอนๆ เชน\n\n ขนบรรทดใหม\t\t เวนระยะ 1 tab
\xhh\xhh ใสตวอกษร hh เมอ hh
เปนเลขฐานสบหก เชน 41 = 'A', 42 = 'B'
\a\a สงเสยงปบ\\\\ แสดง \\\"" แสดง "
![Page 23: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/23.jpg)
ตวอย างโปรแกรม
#include <stdio.h>
main() { printf("%%dd %%5.2f5.2f %%ss", 12, 20.3, "Example");}
12 20.30 Example
โปรแกรม
ผลการทำางาน
%%d %%5.2f %%s คอ รหสควบคม
![Page 24: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/24.jpg)
รหสควบคมล กษณะรหสควบคมล กษณะ (Format String)(Format String)
%d%d พมพจำานวนเตมฐานสบ%u%u พมพเลขไมมเครองหมาย%f%f พมพเลขทศนยม%e%e พมพในรปจำานวนจรงยกกำาลง%c%c พมพตวอกษรตวเดยว%s%s พมพชดตวอกษร (String)
%%%% พมพเครองหมาย %
%o%o พมพเลขฐานแปด%x%x พมพเลขฐานสบหก
![Page 25: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/25.jpg)
ตวอย างโปรแกรม
#include <stdio.h>#define x 65main() { printf("%%d %%c %%o %%x\n", x, x, x, x); printf(“x = %d”, x);}
65 A 101 41X = 65
โปรแกรม
ผลการทำางาน
![Page 26: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/26.jpg)
การจ ดการหนาจอดวยรหสการจ ดการหนาจอดวยรหสควบคมล กษณะควบคมล กษณะในกรณทตองการจดการหนาจอแสดงผลสามารถใช
ตวเลขรวมกนกบรหสควบคมได เชน%5d หมายถง แสดงตวเลขจำานวนเตม 5 หลก
อยางตำา%5.2f หมายถง แสดงตวเลขจำานวน จำานวน 5 หลกอยางตำา และ
ทศนยม 2 ตำาแหนงคา %d %5d
12 12 ___12
123 123 __123
1234 1234 _1234
12345 12345 12345
คา %f %5.2f
1.2 1.200000 _1.20
1.234 1.234000 _1.23
12.345 12.345000 12.35
123.456 123.456000 123.46
![Page 27: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/27.jpg)
โปรแกรมท 2
สรางไฟล print.c โดย กำาหนด #define ดงตอไปน
จำานวนเตม X มคา 65 จำานวนจรง Y มคา 1.23456 ตวอกษร CH มคา ‘C’ ชดตวอกษร SU “มคา Silpakorn
university” พมพคาตางๆ ทกำาหนด ใหแสดงผลดงรป
X in decimal = 65 X in octadecimal = 101X in Hexadecimal = 41Y = 1.234Y = 1.23e+00
CH = %CSU = “Sipakorn
university”
![Page 28: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/28.jpg)
การเกบคาในภาษา C
ทำาได 2 ลกษณะ คอ แบบคาคงท (Constant) แบบตวแปร (Variable)
การสรางตวแปร ตองรวาจะใชตวแปรเกบคาอะไร ประกาศตวแปรใหเหมาะสมกบคาทจะเกบ
ชนดของตวแปรหลกในภาษา C ตวแปรทใชเกบอกขระ (Character variable) ตวแปรทใชเกบเลขจำานวนเตม (Integer variable) ตวแปรทใชเกบเลขจำานวนจรง (Float variable)
![Page 29: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/29.jpg)
การประกาศตวแปร
รปแบบของการประกาศตวแปร
int i; ประกาศ i ใหชนดเปนinteger
float realnum; ประกาศ realnum ให มชนดเปน float
char ch; ประกาศ ch ใหชนด เปน character
ชนดตวแปร ชอตวแปร ;
![Page 30: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/30.jpg)
ชนดของตวแปร
ประเภทขอมล
คำาอธ บาย คาท เก บได ขนาด
(ไบต)
char ตวอ กษร 1 ตว -128 ถง 127 1
short ตวเลขจำานวนเตม -128 ถง 127 1
int ตวเลข
จำานวนเต ม
-32768 ถง32767 2
long ตวเลขจำานวนเตม -232 ถง 232-1 4
float ตวเลขทศนยม
3.4E+/-38 (7 ตำาแหนง) 4
double ตวเลขทศนยม
1.7E+/-308 (15 ตำาแหนง) 8
![Page 31: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/31.jpg)
การประกาศตวแปรชนดเด ยวกน
เราสามารถ ประกาศตวแปรหลายๆต ว ท ม ชนดเด ยวก น โดยใชเพยงประโยค(statement) เดยวได โดยใชรป
แบบ 1. การประกาศทละต ว เชน
int i;
int j;
int k;
2. การประกาศพรอมกนหลายตว เชนint i, j, k;
![Page 32: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/32.jpg)
การประกาศตวแปรพรอมใหค าเร มต น ในภาษา C ประโยค (statement) ของการ
ประกาศตวแปร สามารถกำาหนดคาเรมตนใหกบ ตวแปรไดทนท
โดยใชรปแบบ
เชน int i = 5;
ชนดต วแปร ชอ ตวแปร = คาเร มต น ;
นอกจากนยงสามารถประกาศ หลายๆ ตวแปรในบรรทดเดยว
กนไดอกเชน int i = 5, k = 3, y;
![Page 33: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/33.jpg)
หลกการตงช อ หลกการตงช อ ((IdentifierIdentifier))
ชอ (Identifier)
ไอเดนตฟายเออร เปนชอทผใชกำาหนดขนใน โปรแกรม เชน ชอคาคงท ชอตวแปร ชอ
ฟงกชน เปนตน ตองขนตนดวยตวอกษรภาษาองกฤษ (ตวใหญหรอ
เลกกได) หรอขดลาง ‘_’ ตามดวยตวอกษรภาษาองกฤษ ตวเลข หรอขดลาง
(Underscore) ‘_’ ไมมชองวางหรอตวอกษรพเศษอนๆ เชน ‘!’, ‘@’,
‘#’, ‘$’, ‘%’, ‘^’ เปนตน ตวพมพใหญและเลกจะเปนคนละตวกนเชน NAME,
name, Name, NamE หามซำากบคำาสงวน Reserve Words ของภาษา C หามตงชอซำากบ Function ทอยใน Library ของ
ภาษา C
![Page 34: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/34.jpg)
คำาสงวน คำาสงวน Reserve Words Reserve Words ของภาษา ของภาษา CC
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static While
asm _cs _ds _es
_ss cdecl far huge
interrupt near pascal _export
![Page 35: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/35.jpg)
วธการสรางตวแปรและกำาหนดคา#include <stdio.h>void main (){
int age; char sex; f loat grade;
age = 20; sex = ‘ f ’ ; grade = 3.14;}
#include <stdio.h>void main (){
int age = 20; char sex = ‘ f ’ ; f loat grade = 3.14; char name[10] = “malee” printf(“you are
%s\n”,name); . ..}
![Page 36: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/36.jpg)
a + b x = y c = a + b x == y ++i
นพจน
นพจนอาจประกอบดวย ตวแปร คาคงท การเร ยกใชฟ งก ช น หรอม ต วด ำาเน นการร วมอยก ได
![Page 37: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/37.jpg)
ตวด ำาเนนการ
ลำาดบความสำาคญนอย
ลำาดบความสำาคญมาก�»n¤�°��ª�εÁ���µ¦· �ª�εÁ���µ¦·
�ª�εÁ���µ¦¥¼�µ¦¸· �¼�®µ¦Â³®µÁ«¬Á® º° �ª�³ � �ª�εÁ���µ¦Á�¦¥�Á�¥�· �ª�εÁ���µ¦Á�¥�Á�nµ· AND OR �ª�εÁ���µ¦Á�ºÉ°�Å�· �ª�εÁ���µ¦�ε®���nµ·
- ++ -- ! sizeof (type) * / %
+ - < <= > >=
== != && || ? :
= += -= *= /= %=
![Page 38: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/38.jpg)
โปรแกรมท 3
สรางไฟล triangle.c โดยให รบคาฐานเปนเลขจำานวนจรง รบคาความสงเปนเลขจำานวนจรง คำานวนหาคาพนทของสามเหลยม
Area = ½ * ฐาน * สง
![Page 39: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/39.jpg)
การใช การใช scanf()scanf()
เปนคำาสงทใชในการรบคา โดยมรปแบบการใชงานดงน
scanf(“format string”, address list …);
format string เป นสวนทใช ในการใสร ปแบบของการร บ
ข อมลaddress list
เป นตำาแหนงต ำาแหนงของตวแปรทต องการจะเกบข อมล
![Page 40: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/40.jpg)
ตวอย างโปรแกรม
#include <stdio.h>
void main() { int x ;
scanf("%%d",&&x); printf("%%d %%c", x, x); return ;}
6666 B
6565 A
โปรแกรม
ผลการทำางาน
![Page 41: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/41.jpg)
ตวอย างโปรแกรม
#include <stdio.h>
void main() { char s1[80], s2[80] ;
scanf("%%[0-9]%%[a-zA-Z]", s1, s2); printf("%%s %%s", s1, s2); return ;}
1234test1234 test
test1234 test
โปรแกรม
ผลการทำางาน
![Page 42: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/42.jpg)
ตวอย างโปรแกรม
#include <stdio.h>
void main() { float b,h,area ; printf("Input Base = "); scanf("%%f",&&b); printf("Input Height = "); scanf("%%f",&&h); area = 0.5*b*h ; printf("Area of triangle is %%5.2f",area); return ;}
Input Base = 12.0Input Height = 6.0Area of triangle is 36.00
Input Base = 3.2Input Height = 1.2Area of triangle is 1.92
![Page 43: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/43.jpg)
โปรแกรมท 4
สรางไฟล circle.c โดยให รบคารศมเปนเลขจำานวนจรง กำาหนดคาคงท PI มคา 3.14159 คำานวนหาคาพนทของวงกลม
Area = PI* (รศม)2
![Page 44: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/44.jpg)
ตวอย างโปรแกรม
/* program to calculate area of a circle */#include <stdio.h>#define PI 3.14159main(){
float radius, area;printf(“Input Radius = ?");scanf("%f", &radius);area = PI * radius * radius;printf("Area of circle is %7.2f ", area);
}
Input Radias = 12.0Area of circle is 452.39
![Page 45: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/45.jpg)
โปรแกรมท 5
สรางไฟล donut.c โดยให รบคารศมของวงกลม 2 วง กำาหนดคาคงท PI มคา 3.14159 คำานวนหาคาพนทของวงกลมสวนสเทา
![Page 46: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/46.jpg)
ตวอย างโปรแกรม#include <stdio.h>#define PI 3.14159main() { float radius1,radius2, area1, area2; printf("Input outer radius ="); scanf(%f, &radius1); printf("Input inner radius ="); scanf(%f, &radius2); if (radius2 < radius1) { area1 = PI * radius1 * radius1; area2 = PI * radius2 * radius2; printf("Area of donut is %5.2f", area1-area2); }}
![Page 47: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/47.jpg)
จบโครงสรางภาษาซเบองตน
Question ?Question ?
![Page 48: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/48.jpg)
คำาถามเก ยวก บ คำาถามเก ยวก บ printf()printf()
yards = 8;feet = yards * 3;printf(“%d yards is \n”, yards);Printf(“%d feet”, feet);
จากสวนของโปรแกรม
8 yards is24 feet
ผลการทำางาน คอ ?
![Page 49: 2. โครงสร้างภาษาซี](https://reader033.vdocuments.site/reader033/viewer/2022060206/55a212111a28aba72d8b4775/html5/thumbnails/49.jpg)
ตวอย างโปรแกรม
test1234test 1234
1234test 1234
ผลการทำางาน
#include <stdio.h>
void main() { char s1[80], s2[80] ;
scanf("%%[^0-9]%%[^a-zA-Z\n]", s1, s2); printf("%%s %%s", s1, s2); return ;}
โปรแกรม