โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter...

32
L/O/G/O โครงสร้างภาษาซีเบื ้องต ้น

Transcript of โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter...

Page 1: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

L/O/G/O

โครงสรางภาษาซเบองตน

Page 2: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

www.themegallery.com

วตถประสงคการเรยนร

• อธบายโครงสรางภาษาซได

• เขยนโปรแกรมภาษาซเบองตนได

• ใชฟงกชนมาตรฐานในการรบและแสดงขอมลเบองตนได

• สามารถประกาศตวแปรและใชงานตวแปรได

Page 3: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

www.themegallery.com

โครงสรางโปรแกรม

• Preprocessor directives

• Global declarations

• The main() function

• Uses-defined function

• Program comments

Page 4: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

www.themegallery.com

Preprocessor directives

• ใชส าหรบเรยกไฟลทโปรแกรมตองการใชในการท างาน

• คอมไพเลอรจะกระท าตามค าสงกอนทจะคอมไพลโปรแกรม

• # คอเครองหมาย directives

#include<stdio.h>

#include<conio.h>

รแบบ #define name value

ตวอยาง #define a 100;

Page 5: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

www.themegallery.com

Global declarations

• สวนทใชประกาศตวแปรทตองใชในโปรแกรม

• ทกสวนของโปรแกรมสามารถเรยกใชงานตวแปรนได

• สวนนอาจ ม หรอ ไมมกได

Page 6: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

www.themegallery.com

Main () function

• สวนนทกโปรแกรมจะตองม

• แตละประโยคค าสงจะจบดวยเครองหมาย (; เซมโคลอน)

• เรมตนดวย main()

• ตามดวยเครองหมาย ({ } ปกกาเปดและปกกาปด)

Page 7: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

www.themegallery.com

Uses-defined functions

• การก าหนดฟงกชนขนมาใชงานเอง

• ตองอยในเครองหมายปกกา

• สามารถใหโปรแกรมหลกเรยกใชงานได

#include<stdio.h>

main()

{

function();

}

function()

{

return

}

Page 8: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

www.themegallery.com

Program comments

• เปนสวนอธบายโปรแกรม

• ท าใหอานโปรแกรมไดงายยงขน

• จะไมถกคอมไพเลอรน าไปประมวลผล

Page 9: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

www.themegallery.com

โปรแกรมภาษาซเบองตน

• สวนหวคอสวนเรยกโมดลอน โดยโมดลเหลานจะเกบค าสงพนฐาน

• สวนฟงกชนหลก คอ เปนสวนของค าสง

#include<stdio.h>

main()

{

…….

}

#include<stdio.h>

main()

{

printf(“COMPUTER”);

}

Page 10: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

www.themegallery.com

โปรแกรมภาษาซเบองตน

• ค าอธบายโปรแกรม

/* ...........................*/

/* program by Komvech dangprapai

#include<stdio.h>

main()

{

printf(“COMPUTER”);

printf(“KMITL”);

}

Page 11: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

www.themegallery.com

โปรแกรมภาษาซเบองตน

• การขนบรรทดใหม

/* program by Komvech dangprapai

#include<stdio.h>

main()

{

printf(“COMPUTER\n”);

printf(“KMITL\n”);

}

Page 12: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

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

Page 13: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

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

Page 14: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

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

Page 15: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

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

Page 16: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

www.themegallery.com

การเกบขอมลของตวแปร

#include<stdio.h>

int x;

main()

{

x = 6;

x = 8;

printf(“X is %d”,x);

}

6

8

Page 17: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

www.themegallery.com

การเกบขอมลของตวแปร

num1 = 5;

num2 = 14;

num1 = num2;

printf(“%d”,num1);

printf(“%d”,num2);

14

14

Page 18: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

www.themegallery.com

การเกบขอมลของตวแปร

count count + 1 {เพมคาในตวแปร count ขนหนง}

sum = sum + x {น าคาใน sum บวกกบ x โดยคาใน x จะไมเปลยน}

num = 3 * num {น าคาใน num คณกบ 3 และเกบไวทเดม}

Page 19: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

www.themegallery.com

การรบและพมพขอมลเบองตน (printf)

printf(“ขอความ”); หรอprintf(“รหสควบคมรปแบบ”, ตวแปร) หรอprintf(“Control string”, variable list…);

Page 20: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

www.themegallery.com

การรบและพมพขอมลเบองตน (printf)

#include<stdio.h>

main()

{

printf(“COMPUTER\n”);

printf(“\n\nCOMPUTER”);

printf(“%d\n”,20);

}

Page 21: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

www.themegallery.com

รหสแบคสแลซ

\n ใหขนบรรทดใหม\t ใหเวนแทบ เปนระยะ 8 ชวง\xhh แสดงตวอกษรเมอ hh เปนเลขฐาน 16

\a สงเสยงบป\\ เครองหมาย \\

Page 22: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

www.themegallery.com

Format code

%d ใหพมพเลขจ านวนเตมฐานสบ%u ใหพมพเลขจ านวนเตมไมมเครองหมาย%f ใหพมพเลขทศนยม%e ใหพมพเลขจ านวนจรงแบบยกก าลง%c ใหพมพอกษรตวเดยว%s ใหพมพชดตวอกษร (ขอความ)%% ใหพมพเครองหมาย %%o ใหพมพเลขฐานแปด%x ใหพมพเลขฐานสบหก

Page 23: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

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

Page 24: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

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);

}

Page 25: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

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);

Page 26: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

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);

}

Page 27: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

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);

}

Page 28: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

www.themegallery.com

ค าสงพนฐานการรบและแสดงผลขอมล

putchar() ใชส าหรบแสดงตวอกขระตวเดยวออกทางหนาจอ

putchar(ch);

char m=‘T’;

putchar(m);

putchar(‘z’);

Page 29: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

www.themegallery.com

ค าสงพนฐานการรบและแสดงผลขอมล

puts() ใชแสดงขอความออกทางจอภาพ

put(str);

char name[10] = “Computer”;

puts(name);

puts(“THAI”);

Page 30: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

www.themegallery.com

ค าสงพนฐานการรบและแสดงผลขอมล

getchar() รบขอมลตวอกขระหนงตวเมอปอนขอมลแลวตองกด Enter

ch = getchar();

getch(); รบขอมลตวอกขระหนงตว แตไมตองกดคย Enter

ch = getch();

Page 31: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

www.themegallery.com

ค าสงพนฐานการรบและแสดงผลขอมล

get() ใชรบขอความสตรง

gets(str);

Page 32: โครงสร้างภาษาซีเบื้องต้นe-learning.wpm.ac.th/M3-Programming/File/ppt/chapter 2 C Structure.pdf · วัตถุประสงค์การเรียนรู้

L/O/G/O

www.themegallery.com

Thank You!