โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter...
Transcript of โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter...
L/O/G/O
โครงสรางภาษาซเบองตน
www.themegallery.com
วตถประสงคการเรยนร
• อธบายโครงสรางภาษาซได
• เขยนโปรแกรมภาษาซเบองตนได
• ใชฟงกชนมาตรฐานในการรบและแสดงขอมลเบองตนได
• สามารถประกาศตวแปรและใชงานตวแปรได
www.themegallery.com
โครงสรางโปรแกรม
• Preprocessor directives
• Global declarations
• The main() function
• Uses-defined function
• Program comments
www.themegallery.com
Preprocessor directives
• ใชส าหรบเรยกไฟลทโปรแกรมตองการใชในการท างาน
• คอมไพเลอรจะกระท าตามค าสงกอนทจะคอมไพลโปรแกรม
• # คอเครองหมาย directives
#include<stdio.h>
#include<conio.h>
รแบบ #define name value
ตวอยาง #define a 100;
www.themegallery.com
Global declarations
• สวนทใชประกาศตวแปรทตองใชในโปรแกรม
• ทกสวนของโปรแกรมสามารถเรยกใชงานตวแปรนได
• สวนนอาจ ม หรอ ไมมกได
www.themegallery.com
Main () function
• สวนนทกโปรแกรมจะตองม
• แตละประโยคค าสงจะจบดวยเครองหมาย (; เซมโคลอน)
• เรมตนดวย main()
• ตามดวยเครองหมาย ({ } ปกกาเปดและปกกาปด)
www.themegallery.com
Uses-defined functions
• การก าหนดฟงกชนขนมาใชงานเอง
• ตองอยในเครองหมายปกกา
• สามารถใหโปรแกรมหลกเรยกใชงานได
#include<stdio.h>
main()
{
function();
}
function()
{
return
}
www.themegallery.com
Program comments
• เปนสวนอธบายโปรแกรม
• ท าใหอานโปรแกรมไดงายยงขน
• จะไมถกคอมไพเลอรน าไปประมวลผล
www.themegallery.com
โปรแกรมภาษาซเบองตน
• สวนหวคอสวนเรยกโมดลอน โดยโมดลเหลานจะเกบค าสงพนฐาน
• สวนฟงกชนหลก คอ เปนสวนของค าสง
#include<stdio.h>
main()
{
…….
}
#include<stdio.h>
main()
{
printf(“COMPUTER”);
}
www.themegallery.com
โปรแกรมภาษาซเบองตน
• ค าอธบายโปรแกรม
/* ...........................*/
/* program by Komvech dangprapai
#include<stdio.h>
main()
{
printf(“COMPUTER”);
printf(“KMITL”);
}
www.themegallery.com
โปรแกรมภาษาซเบองตน
• การขนบรรทดใหม
/* program by Komvech dangprapai
#include<stdio.h>
main()
{
printf(“COMPUTER\n”);
printf(“KMITL\n”);
}
www.themegallery.com
ตวแปร
#include<stdio.h>
int feet, inches;
main()
{
feet = 6;
inches = feet * 12;
printf(“Height in inches is %d”,inches)
}
ผลลพธ Height in inches is 72
www.themegallery.com
ค าสง printf
printf(“Hello”);
X=7;
printf(“%d\n”,x);
printf(“%d\n”,x+9);
Hello
7
16
Yard =8;
Feet = yards * 3;
printf(“%d yards is”,yard);
8 yard is 24 feet
www.themegallery.com
ค าสง printf
printf(“Yes \n”);
printf(“No \n”);
Yes
No
printf(“Yes \n”);
printf(“\n”);
printf(“No \n”);
Yes
No
printf(“Yes”);
printf(“No”);YesNo
www.themegallery.com
ค าสง printf
#include<stdio.h>
int nickels, dimes, TotCenter;
main()
{
nickels = 3;
dimes = 7;
TotCenter = (nickels * 5) + (dimes*10);
printf(“%d nickels and %d dimes \n”,nickels,dimes)
printf(“= %d cents \n”,TotCenter);
}
3 nickels and 7 dimes
= 85 cents
www.themegallery.com
การเกบขอมลของตวแปร
#include<stdio.h>
int x;
main()
{
x = 6;
x = 8;
printf(“X is %d”,x);
}
6
8
www.themegallery.com
การเกบขอมลของตวแปร
num1 = 5;
num2 = 14;
num1 = num2;
printf(“%d”,num1);
printf(“%d”,num2);
14
14
www.themegallery.com
การเกบขอมลของตวแปร
count count + 1 {เพมคาในตวแปร count ขนหนง}
sum = sum + x {น าคาใน sum บวกกบ x โดยคาใน x จะไมเปลยน}
num = 3 * num {น าคาใน num คณกบ 3 และเกบไวทเดม}
www.themegallery.com
การรบและพมพขอมลเบองตน (printf)
printf(“ขอความ”); หรอprintf(“รหสควบคมรปแบบ”, ตวแปร) หรอprintf(“Control string”, variable list…);
www.themegallery.com
การรบและพมพขอมลเบองตน (printf)
#include<stdio.h>
main()
{
printf(“COMPUTER\n”);
printf(“\n\nCOMPUTER”);
printf(“%d\n”,20);
}
www.themegallery.com
รหสแบคสแลซ
\n ใหขนบรรทดใหม\t ใหเวนแทบ เปนระยะ 8 ชวง\xhh แสดงตวอกษรเมอ hh เปนเลขฐาน 16
\a สงเสยงบป\\ เครองหมาย \\
www.themegallery.com
Format code
%d ใหพมพเลขจ านวนเตมฐานสบ%u ใหพมพเลขจ านวนเตมไมมเครองหมาย%f ใหพมพเลขทศนยม%e ใหพมพเลขจ านวนจรงแบบยกก าลง%c ใหพมพอกษรตวเดยว%s ใหพมพชดตวอกษร (ขอความ)%% ใหพมพเครองหมาย %%o ใหพมพเลขฐานแปด%x ใหพมพเลขฐานสบหก
www.themegallery.com
ฟงกชน printf
printf(“%s %d %f %c \n”,”Sam”,14,-8.76,’x’);
Sam 14 -8.760000 X
printf(“%f %.3f %.2f
%.1f”,4.5678,4.5678,4.5678,4.5678
4.567800 4.568 4.57 4.6
www.themegallery.com
printf
#include<stdio.h>
main()
{
int x,y;
x =5;
y =6;
printf(“%d\n”,x);
printf(“%c\n”,x);
printf(“%d %d\n”,x,y);
printf(“%d\n”,125);
printf(“%c\n”,125);
printf(“The total is $ %6.2f\n”,12.5);
printf(“The total is $ %6.3f\n”,12.5);
}
www.themegallery.com
scanf
รปแบบscanf(“Control string”, &variable list,…..); หรอscanf(“รหสรบขอมล”, &ตวแปรเกบขอมล);
ตวอยางint num;
scanf(“%d”,&num);
ตวอยางint i, j;
scanf(“%o %x”, &i, &j);
printf(“%o %x”, i, j);
www.themegallery.com
Interactive Program
โปรแกรมแปลงหนวยฟตเปนหนวยนว#include<stdio.h>
main()
{
int feet, inches;
printf(“Enter number of feet”);
scanf(“%d”, &feet);
inches = feet * 12;
printf(“%d inches”,inches);
}
www.themegallery.com
Interactive Program
โปรแกรมค านวณหาเสนรอบวงmain()
#define TWOPI (3.1415926 * 2.0)
{
double radius, circumf;
printf(“Enter radius:”);
scanf(“%f”,&radius);
circumf = TWOPI * radius;
printf(“Circumference: %1.2f\n”, circumf);
}
www.themegallery.com
ค าสงพนฐานการรบและแสดงผลขอมล
putchar() ใชส าหรบแสดงตวอกขระตวเดยวออกทางหนาจอ
putchar(ch);
char m=‘T’;
putchar(m);
putchar(‘z’);
www.themegallery.com
ค าสงพนฐานการรบและแสดงผลขอมล
puts() ใชแสดงขอความออกทางจอภาพ
put(str);
char name[10] = “Computer”;
puts(name);
puts(“THAI”);
www.themegallery.com
ค าสงพนฐานการรบและแสดงผลขอมล
getchar() รบขอมลตวอกขระหนงตวเมอปอนขอมลแลวตองกด Enter
ch = getchar();
getch(); รบขอมลตวอกขระหนงตว แตไมตองกดคย Enter
ch = getch();
www.themegallery.com
ค าสงพนฐานการรบและแสดงผลขอมล
get() ใชรบขอความสตรง
gets(str);
L/O/G/O
www.themegallery.com
Thank You!