Transcript
Page 1: โครงสร้างภาษาซี1

�������������� �������

Page 2: โครงสร้างภาษาซี1

����������� ����������� ������������������ �� ������� ������������� �������� ��!� ����������� �� ����� Interactive

2

Page 3: โครงสร้างภาษาซี1

��� ����������

3

Preprocessor

Directives

Global Declarations

void main(void) {

}

Local Declarations

Statements

Page 4: โครงสร้างภาษาซี1

Preprocessor Directives� 12������� ����� �3������4� file 1�7�������3���89�1�7:; :�

��13����� �9������������� (Header File)� � 7�� ���7�����4 Directive A#C

4

Page 5: โครงสร้างภาษาซี1

Preprocessor Directives

5

Directive ��� ��

#include Include text from file

#define Define a macro

#undef Undefine a macro

#if Test if a compile-time condition hold

#ifdef Test if a symbol is defined

#ifndef Test if a symbol is not defined

#else Indicate alternatives if a test fails

#elif Combination of #if and #else

#endif End a preprocessor conditional

#line Give a line number for compiler message

#error Terminate processing early

#pragma Implementation dependent directive

Page 6: โครงสร้างภาษาซี1

#include

6

��E����F �:� Compiler �9��IJ�!��7��� ��� Compile

8 �4

#include Astdio.hC �� �9��IJ�! stdio.h

#include Aconio.hC �� �9��IJ�! conio.h

����� #include FILE NAME

����49��

Page 7: โครงสร้างภาษาซี1

#define

7

��E����3���89�� �F�!:� ���;�7���������

#define PI 3.14 �� �3���89������ PI �19���� 3.14

����� #define NAME VALUE

����49��

#define X 3.14*2 �� �3���89������ X �19���� 3.14*2

Page 8: โครงสร้างภาษาซี1

Global Declarations� ��E��9��1�7:; :�������\��������

J]��!;��1�7� ��:; :������ � �841�712� ^ �9��F�����_��4�:; � ����1�7

����\I� :��9�����I8

8

Page 9: โครงสร้างภาษาซี1

Main () function �9��J]��!;������� 12� ^ �����F�� ����� ������ 7�13����F��3���7���_�8F��

main()� ��9�����4� ��F�8 �4 ;� � 7�� �8 �4 main() ���8 �4 { ���F�8 �4 }

9

Page 10: โครงสร้างภาษาซี1

�9���c ��4����� (Program Comment)

�9��1�7:; ���4��c ��4��13����

10

//�3���� comment 1 �1�8

/*�3���� comment 1 �1�8*/

/*�3���� comment �����9� 1 �1�8fffffffffff.*/

Page 11: โครงสร้างภาษาซี1

�������� ��� ����������������������������� �� ������� ������������� �������� ��!� ����������� �� ����� Interactive

11

Page 12: โครงสร้างภาษาซี1

#include Astdio.hC

void main() {

ffffffffff.;

ffffffffff.;

}

12

Page 13: โครงสร้างภาษาซี1

#include Astdio.hC

void main() {

printf(AHello WorldC);

//� ��!3��9� Hello World �����

}

13

Hello World

Page 14: โครงสร้างภาษาซี1

#include Astdio.hC

void main() {

printf(AHello\nC);

printf(AWorldC);

}

14

Hello

World

Page 15: โครงสร้างภาษาซี1

#include Astdio.hC

void main() {

printf(AHello\nC);

printf(AWorld\nC);

}

15

Hello

World

Page 16: โครงสร้างภาษาซี1

�������� ��� ����������� ������������������ ��������� ������������� �������� ��!� ����������� �� ����� Interactive

16

Page 17: โครงสร้างภาษาซี1

#include Astdio.hC

#define PI 3.14

void main() {

float r = 20.00, circum = 0.0;

circum = 2 * PI * r;

printf(ACircumference is %f Radius is %f\nC, circum,r); }

17

20.00

r

0.0

area

125.6

3.14

PI

Page 18: โครงสร้างภาษาซี1

�������� ��� ����������� ������������������ �� ������� ��������������������� ��!� ����������� �� ����� Interactive

18

Page 19: โครงสร้างภาษาซี1

�����9�� ����

19

keyboard

monitor

scanf(...)

printf(...)

Memory

Page 20: โครงสร้างภาษาซี1

����8�n������9�� ������ �

20

� J]��!;����8�n�� ����� printf()

� putchar()

� puts()

Page 21: โครงสร้างภาษาซี1

����8�n������9�� ������ �

21

� J]��!;����� ������ �� scanf()� getchar()� getch()� getche()� gets()

Page 22: โครงสร้างภาษาซี1

����8�n������9�� ������ �

22

� J]��!;����8�n�� ����� printf()

� putchar()

� puts()

Page 23: โครงสร้างภาษาซี1

J]��!;�� printf� ��E�J]��!;��:���� ��!� �����9�� ^ ���1��

F����� � ��13��� #include Astdio.hC�����

23

printf(“Control String”,arg1,arg2,arg3,..);

printf(“Control String”);

Page 24: โครงสร้างภาษาซี1

24

printf(“%d\n%d”,a,b);

%d\n%d a,b

22 30a b

�22 -> 30�.

Output Stream

2230

Memory

Page 25: โครงสร้างภาษาซี1

J]��!;�� printf : ����49��

25

printf(AComputer Programming 1C);

printf(AComputer\nProgramming 1C);

printf(AResult is %fC,area);

printf(AResult is\n%fC,area);

Page 26: โครงสร้างภาษาซี1

26

F����4���������7���8�n����c!��� 10 + 3 ���� �F�

X = 10 + 3

PRINT X

START

END

J]��!;�� printf : ����49��

�3���8 X ��E������1�7����n������(1. 3���q9� 10 + 3 ����:������ X)1. X = 10 +3(2. ��8�9� X ���1��F����)2. PRINT An���� 10 + 3 �� CPRINT X

Page 27: โครงสร้างภาษาซี1

J]��!;�� printf : ����������;

27

������������ � �\�

������������ � �\�

�����������\a

cursor ��� !�"� 1 $!%!&'� ��(� �$!%!&'�"�)(%�\b

�%(+& �,�)�- "� 1 �+( \f

cursor /�0�1�$(+���2!)\r

�%(+�3�3 6 $!%!&'�\t

670+���2!)8���\n

n�1�7I8 ���

Page 28: โครงสร้างภาษาซี1

J]��!;�� printf : �����2���� ��!

28

8�(������ 6: +,�)%o

8�(������ 6: +����&%x

8�(������������ � %%%

8�(����� address 6� pointer%p

8�(�����$!%!&'�$!%�)��% (char)%c

8�(�����/D)$!%!&'� (string) ���6(�% �%s

8�(������1�,��� 6FG +%+F��� 8+�1�� 6�&&G !�%e

8�(������1�,��� 62H+��� long double%Lf

8�(������1�,��� 62H+��� double%lf

8�(������1�,��� 62H+��� float%f

8�(������1�,��� 6FG +%+�$K�"����������� �%u

8�(������1�,��� 6FG +%+�$K� long int%ld

8�(������1�,��� 6FG +%+�$K�: +���%d

��:; �����7�����4

Page 29: โครงสร้างภาษาซี1

J]��!;�� printf : ����49��

29

int j=45, k = -123;

float x = 12.34

char c = ‘A’;

char *m = “Hello”;

char mm[6] = “Hello” ;

printf(“Hello\n”);

printf(“Tax is 10%%\n”);

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

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

printf(“%f\n”,x);

printf(“%6.2f\n”,x);

printf(“%c\n”,c);

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

Page 30: โครงสร้างภาษาซี1

J]��!;�� printf : ����49��

30

printf(“%d\n”,c+1);

printf(“%c\n”,c+1);

printf(“%s\n”,m);

printf(“%s\n”,mm);

printf(“%e\n”,x);

printf(“%E\n”,x);

int j=45, k = -123;

float x = 12.34

char c = ‘A’;

char *m = “Hello”;

char mm[6] = “Hello” ;

Page 31: โครงสร้างภาษาซี1

J]��!;�� printf : ����49��

31

Page 32: โครงสร้างภาษาซี1

J]��!;�� printf : ����49��

32

12345

1234

123

12

1234512345

12341234

123123

1212%4d%d9�������

Page 33: โครงสร้างภาษาซี1

#include Astdio.hC

void main() {

printf(AYes\nC);

printf(ANo\nC);

}

33

#include Astdio.hC

void main() {

printf(AYes\nC);

printf(A\nC);

printf(ANo\nC);

}

Page 34: โครงสร้างภาษาซี1

#include Astdio.hC

void main() {

printf(AYesC);

printf(ANoC);

}

34

#include Astdio.hC

void main() {

printf(AYes\nNoC);

}

Page 35: โครงสร้างภาษาซี1

#include < stdio.h >void main ( ) {

//����\�������w7����;�7� number ��;� 8� ������E����F3��������int number;// �3���89� 8 :� �������� numbernumber = 8; printf (AThe value of number is %d . C, number);

}

35

The value of number is 8 .

Page 36: โครงสร้างภาษาซี1

#include Astdio.hC

#define PI 3.14

void main() {

float r = 20.00, circum = 0.0;

circum = 2 * PI * r;

printf(ACircumference is %.1f\nC, circum); }

36

Circumference is 125.6

Page 37: โครงสร้างภาษาซี1

#include Astdio.hC#define PI 3.14 void main ( ) {

float r, area; printf (AThis program computes the area of a circle\nC);r = 12.5; /* �3���8:� �\����9���E� 12.5 */area = PI * r * r; /* ����1�7�������� = ¶ r2 */printf (AArea = Pi x radius x radius\nC);printf (A = %.2f x %.2f x %.2f\nC, PI, r, r);printf (A = %f\nC, area);

37

This program computes the area of a circleArea = Pi x radius x radius

= 3.14 x 12.50 x 12.50= 490.873444

Page 38: โครงสร้างภาษาซี1

#include < stdio.h >#define SIXTY 60 /* ��4� SIXTY �9���E�9��1�7��}����q! */void main ( ) {

float hour; //�����;�7� hour _������\:� ��;� 8� ������E����F3����F �int minute, second; //����� 2 ���_������\ �84��;� 8� ������E����F3��������hour = 1.5; //�3���89� 1.5 :� �������� hour//�q9��������� hour 8 �4 60 �� ��3���8:� ��E�9��������� minuteminute = hour * SIXTY; //�q9��������� minute 8 �4 60 �� ��3���8:� ��E�9��������� secondsecond = minute * SIXTY;printf (AIn one period : \n %.2f hours\nC, hour);printf (A%d minutes\n%d secondsC, minute, second);

}

38

In one period :

1.50 hours

90 minutes

5400 seconds

Page 39: โครงสร้างภาษาซี1

����8�n������9�� ������ �

39

� J]��!;����8�n�� ����� printf()

� putchar()

� puts()

Page 40: โครงสร้างภาษาซี1

J]��!;�� putchar� ����J ���;��1�7������8�n�1��F��������� 1

�������� � ��13��� #include Astdio.hC�����

40

putchar(variable);

Page 41: โครงสร้างภาษาซี1

J]��!;�� putchar : ����49��

41

#include “stdio.h”

void main ( ) {

char s;

s = ‘A’;

putchar(s);

}

A

Page 42: โครงสร้างภาษาซี1

����8�n������9�� ������ �

42

� J]��!;����8�n�� ����� printf()

� putchar()

� puts()

Page 43: โครงสร้างภาษาซี1

J]��!;�� puts� ����J ���;��1�7������8�n�1��F���� � :; ���7�� �������8�n����� �����1�7��������������

4������� �� 1 ���� variable ���4_w� � �����1�7� �������8�n� F�� �������

� �����;� 8����������4�������� � 1 ������� �����

43

puts(variable);

Page 44: โครงสร้างภาษาซี1

J]��!;�� puts : ����49��

44

#include “stdio.h”

#include<string.h>

void main ( ) {

char str[30];

str = “C Programming”;

strcpy(str,”C programming”);

puts(str);

printf(“\nOutput = %s\n”,str);

} C ProgrammingOutput = C Programming

Page 45: โครงสร้างภาษาซี1

����8�n������9�� ������ �

45

� J]��!;����� ������ �� scanf()

� getchar()

� getch(), getche()

� gets()

Page 46: โครงสร้างภาษาซี1

J]��!;�� scanf� ��E�J]��!;��:����9��9�F�����8�4!��!8I�

����:������1�7�3���8� ����F���8 Enter � �����F�_������I� �:������

����� ���� �F��w���1�8:��9� � ��13��� include Astdio.hC�����

46

scanf(“Control String”,&arg1,&arg2,&arg3,..);

Page 47: โครงสร้างภาษาซี1

47

scanf(“%d%f”,&a,&b);

%d%f a,b

22 30a b�22 30�.

Input Stream int a;float b;scanf(A%d%fC,&a,&b);

Page 48: โครงสร้างภาษาซี1

J]��!;�� Scanf : ����49��

48

int num;scanf(A%dC,&num);printf(Athe value in num variable is %dC,num);

char str[80];printf(AEnter a string : C);scanf(A%sC,str);printf(AHere �s your string : %sC, str);

Page 49: โครงสร้างภาษาซี1

#include < stdio.h >void main ( ) {

int years; printf (AHow long have you been here? C);scanf (A%dC, &years);printf (AYou�ve been here for %d years.C, years);printf (A\tReally?C);

}

49

How long have you been here? 2020You’ve been here for 20 years. Really?

Page 50: โครงสร้างภาษาซี1

����8�n������9�� ������ �

50

� J]��!;����� ������ �� scanf()

� getchar()

� getch() ,getche()

� gets()

Page 51: โครงสร้างภาษาซี1

J]��!;�� getchar� ��E�J]��!;��:; ��3���������������n ���1���� ��� �� �

�84F���������� 1 ����1 ������ �����8����������F����

� ���7������13����_w�3���7���� F��428���7�:� ������������ 1 ��� ����F���8 Enter �������� ���F�_������I� �:������ �w7��� ��;� 8������� ����� ���� �F��w���1�8:�� �

�����

51

ch = getchar();

Page 52: โครงสร้างภาษาซี1

J]��!;�� getchar : ����49��

52

#include “stdio.h”

void main ( ) {

char ch;

ch = getchar();

printf(“The Character you typed is %c\n”, ch);

}

d

The Character you typed is d

Page 53: โครงสร้างภาษาซี1

����8�n������9�� ������ �

53

� J]��!;����� ������ �� scanf()

� getchar()

� getch(),getche()

� gets()

Page 54: โครงสร้างภาษาซี1

� ����J ���;��:������ ����� 1 �����84F������������:� �����:�������� ����� ���I� �� ����8 Enter

�����

J]��!;�� getch(),getche()� ����J ���;��:������ ����� 1 �����84I� �����

����:� �����:���� ���� ����� ���I� �� ����8 Enter

�����

54

ch = getch();

ch = getche();

�������� #include<conio.h>

Page 55: โครงสร้างภาษาซี1

J]��!;�� getch : ����49��

55

#include “stdio.h”

#include “conio.h”

void main ( ) {

char ch;

ch = getch();

printf(“The Character you typed is %c\n”, ch);

}

The Character you typed is d

Page 56: โครงสร้างภาษาซี1

����8�n������9�� ������ �

56

� J]��!;����� ������ �� scanf()

� getchar()

� getch()

� gets()

Page 57: โครงสร้างภาษาซี1

J]��!;�� gets� ����J ���;��:������ �����;� 8�� ������� �����

�w7�� ���1���� ��� �� �� ���7������13����_w�3���7���� F��428���7�:� �����

� ����� ����F���8 Enter � �����1�����8F�_������I� �:������1�7������ ��4 ��� ��� ����� ���� �F��w���1�8:���

�����

57

gets(variable);

Page 58: โครงสร้างภาษาซี1

J]��!;�� gets : ����49��

58

#include “stdio.h”

void main ( ) {

char str[51];

gets(str);

printf(“The Message you typed is %s\n”, str);

}

test

The Message you typed is test

Page 59: โครงสร้างภาษาซี1

�������� ��� ����������� ������������������ �� ������� ������������� �������� ��!� ����������� ������� Interactive

59

Page 60: โครงสร้างภาษาซี1

����� Interactive� �������� ����!1�7�������������13������4����

1�7n� :; 13�������9�� ��2��� �I�� �;9�

L F����4���������7�3���q�����4�����1�78 �������w7�����7����74�n��n � �841��9�����1�7�������� �� �����8�n�1���� �F�

L ���4���������7�3���q����1�7�������� �84�3���8:� n� :; :�9�\���������� ���3���q���7�������1�7��������

L ���4���������7�3���q����1�7��������:��9��1�7��E����1� �84�3���8:� n� :; :�9�\����������1������ ���3���q���7�������1�7��������:��9��1�7��E����1�

60


Recommended