1 / 22

ปฎิบัติการที่ ห้า

ปฎิบัติการที่ ห้า. โครงสร้างโปรแกรมภาษาซี. /* Program by 07520001 */ #include <stdio.h> void main() { Statement ; ... getch(); }. Comment. Preprocessor directive. Main function. การกำหนดค่าคงที่. รูปแบบ. รูปแบบ #define ชื่อ ค่าคงที่ ตัวอย่าง

Download Presentation

ปฎิบัติการที่ ห้า

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ปฎิบัติการที่ ห้า

  2. โครงสร้างโปรแกรมภาษาซีโครงสร้างโปรแกรมภาษาซี /* Program by 07520001 */ #include <stdio.h> void main() { Statement ; ... getch(); } Comment Preprocessor directive Main function

  3. การกำหนดค่าคงที่

  4. รูปแบบ รูปแบบ #define ชื่อ ค่าคงที่ ตัวอย่าง #define PI 3.14159 #define START 10 #define SU “Silpakorn U” #define CH ‘C’

  5. ตำแหน่ง #include <stdio.h> #define PI 3.14159 void main() { Statement ; ... }

  6. หลักการตั้งชื่อ • ต้องขึ้นต้นด้วยตัวอักษรภาษาอังกฤษ (ตัวใหญ่/เล็ก) หรือขีดล่าง ‘_’ • ตามด้วยตัวอักษรภาษาอังกฤษ ตัวเลข หรือขีดล่าง (Underscore)‘_’ • ไม่มีช่องว่างหรือตัวอักษรพิเศษอื่นๆ เช่น ‘!’, ‘@’, ‘#’, ‘$’, ‘%’, ‘^’ • ตัวพิมพ์ใหญ่และเล็กจะเป็นคนละตัวกันเช่น NAME, name, Name, NamE • ห้ามซ้ำกับคำสงวน (Reserve Words) ของภาษา C • ห้ามตั้งชื่อซ้ำกับคำสั่ง (Function ที่อยู่ใน Library) ของภาษา C

  7. คำสงวน

  8. $age number-person long Person 3com X name age number_person Long Name_of_student A1 total score Name ตัวอย่างชื่อ ✔ ✔ ✔ ✖ ✖ ✔ ✔ ✔ ✔ ✖ ✔ ✖ ✔ ✔

  9. ตัวอย่าง #include <stdio.h> #define PI 3.14159 #define START 10 #define SU “Silpakorn U” #define CH ‘C’ void main() { Statement; ... }

  10. การแสดงผล

  11. รูปแบบหนึ่ง รูปแบบ printf(“ข้อความที่จะแสดงผล”); ตัวอย่าง printf(“Hello World !”); printf(“good bye.”); Hello World ! good bye.

  12. รูปแบบสอง รูปแบบ printf(“ข้อความ และ/หรือ รูปแบบแสดงผล”); ตัวอย่าง printf(“Hello World !\n”); printf(“good\tbye.”); printf(“\n”); printf(“\n\’hello\’”); Hello World ! good bye. ‘hello’

  13. รูปแบบสาม รูปแบบ printf(“รหัสควบคุม”, ค่าที่จะแสดง); ตัวอย่าง printf(“%d\n”, 25); printf(“%f\n”, 9.999”); printf(“X = %d”, 10); 25 9.999 X = 10

  14. รูปแบบแสดงผล \n ขึ้นบรรทัดใหม่ \t แท็บ \’ แสดงตัวอักษร ’ \” แสดงตัวอักษร ” \\ แสดงตัวอักษร \ รหัสควบคุม %d แสดงเลขจำนวนเต็ม %f แสดงเลขจำนวนจริง %c แสดงตัวอักษร 1 ตัว %s แสดงตัวอักษรหลายตัว รูปแบบและรหัสควบคุม

  15. Welcome. Hello How are you? Welcome. Hello How are you? Welcome. Hello How are you? #include <stdio.h> void main() { printf(“Welcome. ”); printf(“Hello How are you?”); printf(“Welcome.\n”); printf(“Hello\tHow are you?\n”); printf(“\n”); printf(“Welcome.\nHello\tHow are you?\n”); } #include <stdio.h> void main() { printf(“Welcome. ”); printf(“Hello How are you?”); printf(“Welcome.\n”); printf(“Hello\tHow are you?\n”); printf(“\n”); printf(“Welcome.\nHello\tHow are you?\n”); }

  16. 25 9.999 X = 10 SU – Silpakorn U. #include <stdio.h> #define X 10 void main() { printf(“%d\n”, 25); printf(“%f\n”, 9.999”); printf(“X = %d\n”, X); printf(“%c%c–%s”, ‘S’, ‘U’, “Silpakorn U.”); } #include <stdio.h> #define X 10 void main() { printf(“%d\n”, 25); printf(“%f \n”, 9.999”); printf(“X = %d\n”, X); printf(“%c%c – %s”, ‘S’, ‘U’, “Silpakorn U.”); }

  17. Welcome to my program. PI = 3.14159 Start = 10 I study at Silpakorn U Grade A is equal to 4.0 #include <stdio.h> #define PI 3.14159 #define START 10 #define SU “Silpakorn U” #define CH ‘A’ void main() { printf(“Welcome to my program.\n”); printf(“PI = %.2f \n”, PI); printf(“Start = %d\n”, START); printf(“I study at %s\n”, SU); printf(“Grade %c is equal to 4.0”, CH); } #include <stdio.h> #define PI 3.14159 #define START 10 #define SU “Silpakorn U” #define CH ‘A’ void main() { printf(“Welcome to my program.\n”); printf(“PI = %f\n”, PI); printf(“Start = %d\n”, START); printf(“I study at %s\n”, SU); printf(“Grade %c is equal to 4.0”, CH); }

  18. การกำหนดค่าคงที่ #define การแสดงผล printf สรุป

  19. โจทย์ เขียนโปรแกรม printf.c โดยกำหนดค่าคงที่ดังนี้ • total มีค่า 100 • A มีค่า 4.00 • code มีค่า ‘G’ • subject มีค่า “517 101” แล้วแสดงค่าคงที่ทั้งหมดดังรูป Constant values Total = 100 A = 4.000000 Code = G Class 517 101 is over!

  20. โจทย์ เขียนโปรแกรม printf.c โดยกำหนดค่าคงที่ดังนี้ • score มีค่า 10 • X มีค่า 9.87 • grade มีค่า ‘A’ • course มีค่า “Intro2Com” แล้วแสดงค่าคงที่ทั้งหมดดังรูป Constant values Score = 10 X = 9.870000 Grade = A Course Intro2Com is over!

  21. โจทย์ เขียนโปรแกรม printf.c โดยกำหนดค่าคงที่ดังนี้ • score มีค่า 10 • X มีค่า 9.87 • grade มีค่า ‘A’ • course มีค่า “Intro2Com” แล้วแสดงค่าคงที่ทั้งหมดดังรูป Constant values Score = 10 X = 9.870000 Grade = A Course Intro2Com is over!

  22. ตัวอย่าง #include <stdio.h> #define PI 3.14159 #define START 10 #define SU “Silpakorn U” #define CH ‘A’ void main() { printf(“Welcome to my program.\n”); printf(“PI = %f\n”, PI); printf(“Start = %d\n”, START); printf(“I study at %s\n”, SU); printf(“Grade %c is equal to 4.0”, CH); } #include <stdio.h> #define PI 3.14159 #define START 10 #define SU “Silpakorn U” #define CH ‘A’ void main() { printf(“Welcome to my program.\n”); printf(“PI = %f\n”, PI); printf(“Start = %d\n”, START); printf(“I study at %s\n”, SU); printf(“Grade %c is equal to 4.0”, CH); } Welcome to my program. PI = 3.14159 Start = 10 I study at Silpakorn U Grade A is equal to 4.0

More Related