คำสั่ง การประมวลผลก่อน

คำสั่ง การประมวลผลก่อน

 

           ��������ҵ�ҧ� �����ٻẺ�����ç���ҧ੾�з��ᵡ��ҧ�ѹ� ����Ѻ��������ҫ� ���ç���ҧ����ӴѺ�����¹�ѧ���

  1. ��ǹ�����żš�͹ (Preprocessor statement)
  2. ��ǹ��С�ȵ������ǹ��ҧ (Global declarations statement)
  3. ��ǹ�ѧ��ѹ��ѡ (Function main)
  4. ��ǹ�ѧ��ѹ����˹��ͧ (User-defined function)
  5. ��ǹ͸Ժ������� (Program Comment)

    �ٻẺ

1 //StructureOfC.c //��ǹ͸Ժ�������
2 /*Program by Jaturapad Pratoom*/ //��ǹ͸Ժ�������
3 #include <stdio.h> //��ǹ�����żš�͹
4    
5 int a,b; //��ǹ��С�ȵ������ǹ��ҧ
6 int sum(int x, int y); //��ǹ��С�ȵ������ǹ��ҧ
7    
8 int main() //��ǹ�ѧ��ѹ��ѡ main()
9 { // ������鹿ѧ��ѹ��ѡ main()
10   int c; //��ǹ��С�ȵ���÷�ͧ���
11   printf("Input number 1: "); //�����㹿ѧ��ѹ��ѡ main()
12   scanf("%d", &a); //�����㹿ѧ��ѹ��ѡ main()
13   printf("Input number 2: "); //�����㹿ѧ��ѹ��ѡ main()
14   scanf("%d", &b); //�����㹿ѧ��ѹ��ѡ main()
15   c = sum( a, b); //�����㹿ѧ��ѹ��ѡ main()
16   printf("\n%d + %d = %d", a, b,c); //�����㹿ѧ��ѹ��ѡ main()
17   getch(); //�����㹿ѧ��ѹ��ѡ main()
18 } // ���ѧ��ѹ��ѡ main()
19    
20 int sum(int x, int y) //�ѧ��ѹ sum()
21 { // ������鹿ѧ��ѹ sum()
22   return(x + y); //�����㹿ѧ��ѹ sum()
23 } // ���ѧ��ѹ sum()

��ǹ�����żš�͹ (Preprocessor statement)

          ��ǹ���е�ͧ�շء����� �Ҩ���¡�����ǹ��Ǣͧ����� ���繵�ͧ��˹����㹵͹����������� �¨Т�鹵鹴�������ͧ���� # ������¤�������礷տ (Directive) ����ͧ��á�˹���� ��

          #include <stdio.h> ���� #include "stdio.h"

          ���¶֧��ü�ǡ�δ���������� stdio.h �������ҹ ��駹���δ������� stdio.h ��鹨�����쪹Դ��ͤ��� (Text file) ���������������ա�û�С�Ȥ�ҵ���� ��Ф�Ҥ�����ҧ� ��èؿѧ��ѹ�ҵðҹ��ҧ� �����Ҵ��¡ѹ�����Ǵ�������ǡѹ��� ��ШѴ��ŧ��ź���� �¨ж١���������ҹ����ѹ�Ѻ�ش�������������з������������

��ǹ��С�ȵ������ǹ��ҧ (Global declarations statement)

          ��ǹ������㹡�û�С�ȵ�������Ϳѧ��ѹ����ͧ�������� �·ء� ��ǹ�ͧ���������ö���¡������ŷ���С��������ǹ�����

��ǹ�ѧ��ѹ��ѡ (Function main)

          ��ǹ���ء������е�ͧ�� ������㹿ѧ��ѹ��ѡ�л�Сͺ���¤���觵�ҧ� �������������ӧҹ �ѧ��ѹ���������鹡�÷ӧҹ���¿ѧ��ѹ main() ��ǹ����觵�ҧ� �е�ͧ��¹���������ҧ����ͧ���� { ��� } ���� ��Ф���觷ء���������ҫըе�ͧ�Դ���´�������ͧ���� ; (semicolon) ����

��ǹ�ѧ��ѹ����˹��ͧ (User-defined function)

          �繡����¹�������пѧ��ѹ��ҧ� ����������� �µ�ͧ���������ͧ���� { } ��е�ͧ���ҧ�ѧ��ѹ���ͤ����������ӧҹ�����ҵ�ͧ������Ѻ������������ö���¡���� ���������

��ǹ͸Ժ������� (Program Comment)

          �������� (comment) �����ǹ����������˵آͧ����� ����������������¹���������ͤ���͸Ժ�¡ӡѺŧ�� source code ��觤������������ӡ���ż����ǹ����繤���������

          ������������ҫ��� 2 Ẻ���

  1. ��������Ẻ��÷Ѵ���� ������ͧ���� //
  2. ��������Ẻ���º�÷Ѵ ������ͧ���� /* ��� */

    ������ҧ��ä�����������ҫ�

1 //Comment only one line
2  
3 #include <stdio.h>
4  
5 int main()
6 {
7    /*Comment
8       many
9          line*/
10 }

������á�ͧ�ѹ

1 //MyFirstProgram.c
2  
3 #include <stdio.h>
4  
5 int main()
6 {
7   clrscr();
8   printf("Hello World !\n");
9   getch();
10 }

     ���Ѿ����

Hello World ! 

คำสั่ง การประมวลผลก่อน

    �����˵�
  • คำสั่ง การประมวลผลก่อน
      ���¶֧ ��顴���� Enter ���鹾����

     ͸Ժ�������

��÷Ѵ��� 1: ����ǹ����������˵�Ẻ��÷Ѵ���Ǣͧ����� ��������Һ������������ MyFirstProgram.c
��÷Ѵ��� 3: �繡�ú͡�������������δ���������� stdio.h ���������㹡���żŴ��� �� stdio ����Ҩҡ standard input/output ��� .h ��� ���ʡ�Ţͧ�δ�����������ҫ� (h ����Ҩҡ header) ��� stdio.h ��� �δ��������������ҡ�û�С�� (declaration) �ͧ�ѧ��ѹ�ҵðҹ�ͧ���ҫշ������ǡѺ��èѴ��ô�ҹ�Թ�ص�����ҵ�ص����������¡ѹ���������ա�����¡��ҹ�ѧ��ѹ printf() �����ʴ��������͡�ҧ���Ҿ ������ͧ�ҡ��ǹ�ͧ��û�С�ȿѧ��ѹ printf() �١��è�������δ������� stdio.h �ѧ��鹨֧���繵�ͧ���δ������� stdio.h �������㹡���żŴ���
��÷Ѵ��� 5: ��Ϳѧ��ѹ main() ����繿ѧ��ѹ��ѡ�ͧ����� ��÷ӧҹ�ͧ��������ҫը�������鹷��ѧ��ѹ���
��÷Ѵ��� 6: ����ͧ���� { �кبش������鹢ͧ�ѧ��ѹ main()
��÷Ѵ��� 7: �繤�������������˹�Ҩ������ʴ����Ѿ��
��÷Ѵ��� 8: �繡�����¡��ѧ��ѹ printf() ����繿ѧ��ѹ�ҵðҹ�ͧ���ҫշ�˹�ҷ���ʴ��Ţ������͡�ҧ���Ҿ 㹷������ʴ���ͤ��� Hello World ! �͡�ҧ���Ҿ
��÷Ѵ��� 9: �繡�����¡��ѧ��ѹ getch() ����繿ѧ��ѹ�ҵðҹ�ͧ���ҫշ�˹�ҷ���Ѻ����ѡ��� �����͡�ҡ˹�ҨͻѨ�غѹ
��÷Ѵ��� 10: ����ͧ���� } �кبش����ش�ͧ�ѧ��ѹ main()