Version 1.0.0 �������ก��ก���� 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
�� �������������� ���. http://student.sut.ac.th/comclub
��������� ��ก������������������������������ก��ก��� !! �� Computer Programming #�����$�! �������$�����%�����ก�����&�'��(ก�) �����������*��)��*��ก�+�ก�������ก�����,���� ���� �� ��ก�����������,)��*� �������$����ก�� (+�!��.���������)�ก�����(��ก���)/0���12*���&��,�) << ��!�&�� ������ !�)ก����+�ก������ (,)�2*���!&,�*�������??) ������� �����$��*��ก��*�4��)� �� ก��/����(�����&5 (��*� “���&55”) ��*�+�*�!��&�� �����4*����(�����6� &��*�) �� ���,),�*��,� ��)�... (&��,)�)�) ก��/����(�*!&��!���) (����(�*!&��!���) �����)��&�� ��4*!� ��&�����(*!ก�� ก�������ก��������� �!�ก�������� &�&�������(*!ก�� 4*�,)����*!&��!��� &�),)��.�����!�� (&�),)���ก) �8�555 ... ������ก+�!���ก�����,�����& �& ����)�1 ��������... )����&���,�(������*!& �8�55 ������ก�&���� ��� �#�!� �&� ��&�(*!ก���� ���
����*�&����ก� �����(*!ก���� ��� ��ก����*�1 ����+�55 ����,)���+��!�,�� ก.,�(...��*& ,������) ,�� !�) � !�)��.�ก�� ����,� �&ก��+��!.�����)�) �)� !����0 )��.�� ���
����� !��"�# ��ก����$� �� (� !�)�)�&��&55�& !��������,��*���� ��� ... ����&�� ��� )���!�*������� ��� " %�
1. ! 9�������'��&0�:;�� (ก���!�ก��(ก*�:;��) 2
2. �����ก ��)������ C (Introduction to C) (�)�&4����<��� ,)+��ก�� ... �������)���'=</�+�����) 7
3. ��!(�� (���!����� �ก�� (... ���,)��!�*�������ก�����) 12
4. ��!���� �+� (�ก��!�2� (... ���ก����ก�) 21
5. >:�ก0��� (,)�*���ก+� ��� �� �)����ก�&���������) 30
6. )?����!(�� (�8�55 +�*��*�ก���4��ก��/0�*���)���) (Array) 34
7. �!�� �@<+�,� 30 38
��������������ก+��*�� �8�55 -_-!!
�$��%�����" %��ก
�)&���ก����*����!���$���*��ก������&��*���� 4*����,�4�&��ก��� ก��/�4�&��*����,��*!&�� ��� ��A! ��������4�&����,)��*�+�ก��
- 2 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
"���%���& 1
! 9�������'��&0�:;��
(ก���!�ก��(ก*�:;��)
�ก��& ��
��!�*������!��*!&ก�� �ก���!�ก��(ก*,��:;����$���ก ��&��,),�*�2�4��'��(ก�)+���!�*���� (���)��*��(��*�&)�ก) ����ก4��! 9����ก��)���:;�� (�(ก*,��:;����$���ก
- 3 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
��'�ก������ ��()"� (ก��� ก���ก%�()"�)
ก��(ก*�:;����$� !�)��)��4��������)��<&0 (�� �55��) ��)��)��<&0��� ���)�ก��(ก*,��:;���&2��ก!�� ����(�����&�������� (+�B:��*!& ...����)�������!���!) (�ก��(ก*,��:;�������ก�*���*����� ��$�ก��(ก*,��:;��(����� ������&ก�&�����!���$�(�����'�)�� (�,)��$������&� ��� �������,�������� (��� ������,)��) ���ก.���*����! 9�+�ก�����+�*��!����ก��ก��ก���&�,���������*!&! 9����5 �������$� ��������4����) (*!�ก�&����.! ��,����)�/��� ����ก. ��ก��(ก*�:;�� ก��ก ��*�! 4*�4�)!� ����ก ��*�!&��,� ก.����� �����!� “ก.&��+���ก � ��&!5555 ก�� ��” �8�55
(�ก��(ก*�:;����������@�ก<���������&�+�+� �)� !����0 1)� &,�*& ���ก�����&0����������ก!� +�*)��!� �)� !����0����& )��'�)�ก55 �� )��,)��)��4 ����,�* �*�� �&�C�� ������+�*)����� �����������*��)� !�)����������������+�* �'�������,�* (� �������*��)�ก����������$�ก���!�ก����������� &ก��!�&�����
��"���'�ก��ก� �%������ $,�� (������&ก��!�&���6&55�� ,)�*��#����&�) 1. ����*��
2. ��ก�*�!�*!&�*��������
3. &ก�*������
4. 4*�&��,)���ก����ก+�* ก��,�����*� 3 ��ก (��� ���ก!�4*��&�� ... �8�55��!�&���6&55) 5. ����*�!+��*��+�+���ก
6. � ��&!�*�!
7. 4*��*�!&��,)����&�+�*ก��,����+��*� 6 ��ก ����
8. ก���*�!
9. 4*��*�!&��,)�)�+�*ก��,��� �)����*� 1 ��ก ����
����.�,�*!������� ��! 9�ก��ก ��*�!(����$�ก���!�ก�� ���)� ������������(�)�ก���!�ก����������� ก�����&��*���*�(��������&ก!� ก�����&� Algorithm ��ก�����&���$�ก���!�ก�����)�������������� (�(���������)� ����������������*!&
Algorithm �*���*���� �����&��,)����ก �� ������ก � Bug �ก �����,�* ���&5�& ����*� 5 �����)� �(&*�!� ������*�!+���ก,�*,� ����5���&��,),�*�*���ก (�8�55 ��� �) ��� ��������ก!� 4*�ก ����ก�&������&��,� (�������� �)ก!��!�&!��!��� ����ก!�����)�,�) 4*�� � ������&��,��� (&��5...&��,)��) Algorithm ���*��ก������ ��������&ก!� ������ ���� ก�/����5 �������ก��/0+�* �� ��������ก��(ก*,��:;����,�*)� !�)������ (���&��ก�����&� Algorithm (�)����2*��ก��+�*(�� ��!� ก���!�ก��(ก*,��:;���&(*!ก�� ... ,),�*(�?���) ก�����&� Algorithm ��,)�� �:���&(�ก �� ��)��4(ก*,��:;��+������� ����ก�/����5 ,�*��)�ก�*�&( ,�� ��+�������������)�&4�� !�)��.!+�ก����������� Algorithm �����������$� !�)ก!*����������� ����ก�/����5
��ก�����ก��/0���� �� �)�*� !� � Algorithm (�� ��ก+�;,��.ก �8�55 ���# �)�&4�� ก��)���:;��(������!),������(����&����&�.... ��)��2ก��
- 4 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
��� Algorithm � ��ก#")-.��/0ก
&ก��!�&����� �����&� Algorithm ���ก���� �)�ก�������������!���������*�)� 10 ����!� �� �)�*� !�)�����ก!*��5��� Algorithm ก��!���)��������ก!*��5&��,��*��
Algorithm ก�%��1 1. ��� ��������!����� 10 ����!�
2. ����&����&�����!����� 10 ������� �)�ก������
3. (��� ����)�ก����������
Algorithm �/������ 1. ��� ��������!� �*���� 1 ��� Algorithm (��ก!*��5
2. 4*����&��,) �� 10 ��!+�*ก��,�����*� 1 �*���� 1 ��� Algorithm (��ก!*��5 3. +�* �(�ก��$� ����)�ก������ �*���� 2 ��� Algorithm (��ก!*��5
4. ����&����&� �4��,�ก�� ����)�ก�������:������ 4*� �����)� �)�กก!�+�* �������$� ����)�ก������
�*���� 2 ��� Algorithm (��ก!*��5
5. 4*�&������&����&�,) �� 10 ��!+�*ก��,�����*� 4 �*���� 2 ��� Algorithm (��ก!*��5
6. (��� ����)�ก������ �*���� 3 ��� Algorithm (��ก!*��5
����.�!� Algorithm ����)�ก��)�����(��ก!*��5 ���(��ก!*��5 ���� 4*�+�* ����ก.�����,�*'�&���'�)�� #������� �� !�)��)��4� �@<���)��<&0���ก�!,!*�*���*� �����)���2!���$�ก�����&�(����&5 (�(����$�����5���)�ก���!�ก�������� (� ����������&��,)������ �������*��)��������������������&�+�*������& ������ �����.�,�*��ก Algorithm (������&�
Algorithm �*���*� �����)��ก��(ก*�:;����ก�2�(�������,�*!�
Algorithm ก�%��1 1. ��� ��������!�(������!� (�����&����&� +�* ������ 10 ����!�
2. (��� ����)�ก������
Algorithm �/������
1. ��� � (�+�* �������$� ����)�ก������
2. ��� �4��,�
3. ����&����&� �4��,�ก�� ����)�ก�������:������ 4*� �����)� �)�กก!�+�* �������$� ����)�ก������
4. 4*�&������&����&�,) �� 10 ��!+�*ก��,�����*� 2-3 +�) 5. (��� ����)�ก������
- 5 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
*������ก�!� Algorithm (����� 2 ��� *�&ก��(��(�ก (�4*�)��+�(��ก�����&�'��(ก�)��������&�+���!�*�4��5,����� ����!�)��*����*����&���ก�� (�,�*���1��90 (Output) ��)���ก�� (��(�ก��+�*��!(��+�ก���ก.�)�กก!�(���)��4�����!(������)�+�*,�*+���&��� (����������+�*��!(���*�&ก!� (�,)��)��4�����!(����!������ก��ก �)�ก���)�+�*,�*�������,),�*42ก�ก.���ก,)+�* ����)�ก������ ... ��4�)... (*!���,����ก!�ก�� ... ��������&����� ���!� +�������'��(ก�)+�*���6��� ����)�ก������ ��������&5!� Algorithm 2 ��ก!� ��������'��&0,�* (�+�*��!(���*�&ก!� (� Algorithm 1 ,)+�!���,)��ก!� �����4*�����'��&0�� �)�� ) Algorithm 1 ����)��4�����!(������5,�+�*��,�*�����+��/���� Algorithm 2 �*������&� Algorithm (*!�����)��4���,�+�*,�*
�23ก"��
1. �����&�ก���!�ก��ก���*))�)�
2. �����&�ก���!�ก����1�!)�� ����(� 1 4�� N �)��� N ��$�����!���������*�)�
3. ����*� !�)��,���� (*!���&���$� Algorithm
"��ก���&����4)@�ก<� �)��� �2������! ���� ���*�)�+��*�� ��ก���&���ก ��ก���� ��ก���&���ก ��ก������� !�)� ���
�2��ก��ก���&������ �����ก���� �2�2�!� ��ก���&���ก ����&�,�*(*! ��������&�! ����<�,�&ก�� �2����!�'�&4�)
��ก���&�!���ก���&�)����������<�,�&����,) 4*�+ �,))�+�*&*�&,�����ก�����������)�������� �)�����ก ����&��*�&(*! +�*
��ก���&���D����������*� 35 (*!�����*� 35-36 ��ก���&���*�)ก�� �����ก����+�*���(��BEก������ 5 4*�+ ����(��BEก���,),�* +�*ก��,�������������*� 35 - 36 +�) 4*����,�*���(��BEก����������.� (*!���(��BEก�������)��� �2 " 4. �����&�ก���!�ก�����ก��(&ก(&���!�� '�&ก�������!�� N )���ก12*+�* 4*� N ��)��4(���,�*+���ก���&!
+�*(��� ��!� One-Digit ���������&!ก�� 4*������ก (���)��ก+�*(�����$� Two-Digit ,Three-Digit
) 6�����(*!+�*(���!� Over-Three-Digit
5. �����&�ก���!�ก�����ก��(&ก(&���!�� '�&ก�������!�� N )���ก12*+�* 4*� N ��$��� 2 +�*(��� ��!� “It is
an even number” 4*���$��� ��+�*� )�0!� “It is an odd number”
�ก��� ((4))
1. )���� 9 ��� ��)���ก�����+�*+��2� ���ก��*���� 4 ��*� +�*1��������� 9 ������ '�&,)&ก��กก� (� ���..ก�� ���!�)��� � � )
- 6 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
2. ������!�������)���)(�� �+� ABCDE �����+�*,�*�)ก������2�
3. �:;�������) )�'��&0�&2!�)�����&;����&2 8 ����&; ��$�����&;��) 1 ����&;#���)��������ก���ก!�����&;
�� � ����! 9�+�ก���� ����&;��)'�&ก�������*!&������ 2 (�����&� 2 ����
�!��!� ������ก��,�*�� ��� 4*�&��,)��� ,)(�����+�*���+��!����ก���� ��� (�4*����(*!ก. �����.�!� ����(��BEก���(�'��&0 ��$�ก�����+�*���)� !�) ���*����� 0+�ก��(ก*�:;�� ���กก��� !�) ��� )���)� (�)�ก��4�&�����ก)��&����$������&�
��$�
��!�*�������+�*��.�4��ก�����! 9�ก��(ก*�:;�����'��&0������*!&ก�����&���ก)��&����$������&� )�������������� (�(���������)� ���������������
�����������&��,)��.����)�ก��ก �����*�+�)�ก����+���!�*���5,��� ��� 4*�(��BEก���ก���ก)�0&��,)��+��� ��� ���� �)�� )����4�)�� �)�� ),�*+��!.�,#�0����)�)�� ���
- 7 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
"���%���& 2
�����ก ��)������ C
(Introduction)
�ก��& ��
��!�*���� ���ก��&!ก����<� C ������ก������@�ก<� ���(��!��������*� ����� ��!�*���� �ก� ��( ���(*!ก�� ��������&��
- 8 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
�"$�ก����!&���� C
��<�#� ��$���<� �)� !����0��<������+��������<� �)� !����0��&�����<� (�&�����) ��<�#�)�ก��42ก+�*��$���<��� �)�*��������)��+�) �������$���<����)� ���������� ก������� �)��4���2� ������,�*��.! )�>:�ก0��������.��2� (���)��4� ก(��+�ก�����&�,�*��&(�� (!�,����� (�ก.�� �) ��<�#�4��!���$���<�+������ก�� #����!) �/�ก</���������<�+�������2�ก�������������*��*!&ก�� ��<�#���$���<�' ����*�� (Structured Language) ���� ��12*���&���)��4(��(&ก'��(ก�)��$��!�5 ,�* '�&(���!���$�� �����ก�� (����ก��) �:������ )���<������G������ก#�)�ก)�& (�������5 (�&��+�*���� C �&2 ก.)� C++, C# (��ก,!*�1���+���� � 4*�&���&�ก������&2��) ...������ �� �!���� “ !�” ���2* (��) ก�����&���<� �)� !����0���������$��*����@�&�:���&�&2 2 �&�� �&��(�ก���� �� Syntax ��5����)�&4�� ! 9�ก�����&�'��(ก�) กHก� ก�)��&�����5 ��ก�!� �� Algorithm (Logic) ���� �� ก���!�ก�� � ����ก���!�ก�����������'��(ก�) ��������!�������*��)��&2+���!�!���������&�'��(ก�)��,�#�ก�&������� 4*�,))�(*! ... &�),)��)��4���&�'��(ก�),�* 4*�����&� �����)���ก��������� (������ก����� )�0 ก�����2��,��*!&) Syntax ��)���ก� ก�ก������� ��ก!������,�,�* �����,�,),�* ��ก����*�����ก��ก� ก� �!� Algorithm ก.����)���ก���� !������ก)�0�&��,�+�*��� !���!��(1�&��,� ��,����)�/��� (��� �5(*!&��)���ก�!��� ���� �� Trick �����������&2ก��!�)�)�ก�*�&���&�+� 4*�& ��)�)�ก ก.��)��4(ก*�:;��'�&ก�����)�����+�*ก��'��&0������,�*)�ก (��$�9��)��) (��&#�&�! &��,))���������& �8�55)
�-� ���ก����4�,� (�&����&)
#include <stdio.h>
�!����)!1ก�� ��������)�,!*���ก�@��,����5��5
��� ก��'�� ��������������.��2� (,)�*�����&���� #include) ก��� &�) �����&�� (#define) ����ก�...�����*��*!& #
�!������.�!��5 ���!�����&2��*� main �!������,!*�������ก�����ก�@ �����*��ก��+�* main �2*��ก �����&2ก��!������&���&����&��& ���� �����,����&�,!*��� main (���)�/!�&��,�ก.�*�����ก�@
int main(){
//This is one line comment
/*This is multi-line comment
Na-Ja */
printf(“Hello World\n”);
return 0;
}
�!���� �� main ��$��!�������� �)�*����'��(ก�)�!���� Run
'��(ก�) ����ก� !���)� Comment ��$��!����,!*��������������'�&����� C ��,) �!���$� ������
**�-� �����&��$ก�� �� ��%���� ; �5��%�������&��$ก����� ������)�ก��1 �ก��������
�!������,!*�������ก�������&����&� �������� ��������ก�@,!*��*� main
- 9 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
(����&)��&!����,)�*���� �)�*���� main ... �)�� �8�55 ... ������4*����,),�* ��,�*�1��& �8�55 )�ก��) '��(ก�)�*���*����&��,)��*�+�ก.,)��$�,� ��� +��&.�5
���6�����!���% (6����*��5 ... �4��������&���<����กI<) 1. Source Code �)�&4�� �!����������&���*�,����(�� �8�55 ' *��� 2. Compile �)�&4�� ก����!���� Source Code (�(����$�,>0�����)��4 Run ,�* 3. Compiler �)�&4�� ��!���+�* Compile (ก!� (�ก.�� �) ��� Turbo C, Anjuta
4. Error �)�&4�� �*�1 �������'��(ก�)������+�*,)��)��4���'��(ก�),�*�&��42ก�*��
a. Syntax Error �� ก�����&� Syntax 1 � ��� �)� )�0 ; ��� ������ ��������,)��)��4���'��(ก�),�*�&4*�,)(ก*+�*42ก�*�� Compiler ����$���!��ก!�1 ����,����� (�ก��J���)
b. Logic Error �� ก�����&�'��(ก�)���1 �������ก���!�ก�������� �����������'��(ก�),�* (�������ก �����ก��/0,) ��B:����� (����2*�)���'��(ก�)��*� �8�55
c. User Error �)�&4�� ��!'��(ก�)�� ���42ก� (��!����ก��+��*�)21 � �8�55 �����ก+�*+���!�� ก��,�+���!��ก<� ��,����)�/��� #������������*�����&�ก���*!&
5. Warning �)�&4�� �*�1 ������� “�����” �ก �������ก���'��(ก�)��� (���&5!� Compiler ����&!���������+�*�ก � Error �)������,�*
���( ���ก���ก�� ���K&! ���ก�� �)(*!����ก (�8�5 +�*��+�( ���ก�� ��A!�&��(*!�����)�������)
���ก����5�7�� ('��(ก�)(�ก) �� �)��)�����!�*��(*!ก���� ��� ���� �� Hello World!!! (�!������!'ก) ��ก������������&�)���ก�� �!��ก ��*�&� 80 �����*��*!&'��(ก�)��� (��ก�����ก��/0��)
#include <stdio.h>
// '�� ����������%����ก,>0 stdio.h (standard input output header file) int main(){ // �� �)�*� main
printf(“Hello World!!!”); // (���1��ก����� ��!� Hello world
return 0; // �� �ก������ (��'��(ก�)) ���������K&! �&!�ก��+���!�*���� } // �� main
1ก����� Hello World!!!
��$�,��*�� ��� �������'��(ก�)(�ก ��������ก�� ��� �������������+�ก�����&�'��(ก�)��+�*&�� Pattern ���,�ก���� ��� ��$��2�(���� �)�*��&
- 10 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
#include <stdio.h>
int main(){
// ���������5���'��(ก�) return 0; }
�����&���ก (printf)
printf ��$� ���������+�*+�ก��(���1��ก�����*��� '�&)��%��������$������+�*1��90��ก)���)����������+�,!* (���� ����������,),�*����+�ก.��$�,�*) )��2�2�(��ก����ก!�
printf(“�*� !�) !� �)”[,��!(������ ����5]);
[,��!(������ ����5] �)�&4����+�����,)+�ก.,�* ������+�ก�� ����ก.,�* ((�����)���ก�&��) ����-��
printf(“Testing text”); ��ก�������� : Testing text
printf(“word1\nword2”); ��ก�������� : word1
word2
+�ก�� !� �)ก��(���1������������ )���ก���� �@<���+�* !� �)�&2�*!&��� ( \n �����.��)�&4�� ... ) ����&��ก!�)��� �)ก���&�� ( ����*�����)�L& ,)�2*)
��ก������6, ����"��� ��ก������6, ����"���
\n ����������+�) \t Tab (�!*�!�� 8 ����) \r ก��,�&�����(���(�ก
���������
\a ������&� Beep (���2)
\\ \ \” “ %s (������ � %d (�������!���.) %c (�����!��ก<� %f (����@� &)
&��)���ก�&���� ��� (����( ���ก��(*!ก�� �&��$� �&,���ก!� (!�����) % �����*� ����$���ก����������������� ���!(������ �������+�����*� !�) !� �))�+� )��2��!�&��ก����ก!�
����-��
Code ��4�� printf(“My Name is %s\n”,“Pong”); My Name is Pong printf(“Age : %d\t GPAX : %f\n”,20,3.94); Age : 20 GPAX : 3.94
printf(“\”%c\””,’A’); “A”
�*�����ก� 4*����+�* %s ����ก�!������+�* “ �!� %c �����+�* ‘ (���K&!����ก��&����&�+���!�*�4��,�(*!ก��) (��������2*( ���ก�� �,)���&��&
- 11 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
�23ก"��ก/��"���%� (�&� ��&!�������!�*����( ����� ... �2*�����) 1. �����&�'��(ก�)(�����&����&������!��� *�&5ก�����*����
printf (�� � ����) &��,)#�+�ก��+��J� �����)��4 !� �)ก��(���1��� printf �*!& ���������� ����������ก� ����& ��
%d ��)��4+�+��2�(�� %[-][0]d [-] �)�&4�� ��+� - ����,)ก.,�* %s ��)��4+�+��2�(�� %[-][0]s [0] �)�&4����+���!����,�����,)ก.,�* %f ��)��4+�+��2�(�� %[-][0][.0]f [.0] �)�&4����+��@� &)����,)ก.,�* ������)���������ก�!�)������ก�� ���*������� *�&ก�� ��)��2 !�)�)�&�&(*!ก���� ���
%-5d �)�&4�� +�*��!��������)�+�� �#*�& (-) (��������&���*�&�����+���!�� 5 ��� (5) %15s �)�&4�� +�*+���� ����+�*)�� ��!� (,))� -) (��������&���*�&�����+���� � 15 ��� (15) %4.2f �)�&4�� �������&���*�& 4 ��� (�(����@� &) 2 ���(��� (.2)
Code ��4�� printf(“My Name is %15s\n”,“Pong”); My Name is Pong printf(“Age : %5d|\t GPAX : %4.2f\n”,20,3.94); Age : 20 | GPAX : 3.94
��ก��!�&�����+�* �������.�����*���� ��� �������� ) !�)��*�+� ���'��&0�ก��
�23ก"��
1. �����&�'��(ก�)�����(���1��90��$�
12345678901234567890123456789012345
NAME LAST NAME AGE SEX
Somchai Jaidee 20 M
Pong Pean 21 M
Pattara Jintana 22 F
2. ����!�'��(ก�)���(���1�&��,� (�&����ก������� ... � &���)�ก�� )����,),�*��,�) #include<stdio.h>
int main(){
printf(“%15s\t”,”Test Line 1”);
printf(“%-15s\n” ,”Test Line 2”);
printf(“%4s” ,”Test Line 3”);
printf(“\n%-4s\n\n” ,”Test Line 4”);
printf(“%|%20.4|s” ,2.5);
}
- 12 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
"���%���& 3
��!(�� (���!����� �ก�� (Variable and Operator)
�ก��& ��
��!�*�������� �))�ก�����&�'��(ก�))�ก& ������ �*����ก�&������&(*!ก��!� �*�� Focus �&2�����!(�� (� ��!����� �ก��+�*)�ก �����4*�%��+����������,)�� �����+�*�����,)��*�+�+���!�*�4��,�,�*
- 13 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
����� (Variable) ��!(�� ����,� (���� �� ��4�)&��8 ��&) ��!(�� �� � �������*������ ����������+�*�ก.��*�)2 �����)��*�)2��&�2�(�� ��� ��!��ก<� �*� !�) ����!���.) ����!��@� &) (� ���ก� (�+���<�#����� �*�)2��42ก�ก.�+��2������!��������� '�&)�ก��(����$� 2 �ก</� �� (��)�� ������)�& (Signed) (� (��,))�� ������)�& (Unsigned) ��ก�ก</������ �� (����$�����!���.) ก�� �@� &) (������(���� �5���5 �&��) (�+������� �)�*��&�ก+�*)��(�����)�กก!� ��!(�� (����ก��$� 3
������ �� 1. ����!���.) ,�*(ก int (� long (���&���ก�.ก ,�+�;) 2. �@� &) ,�*(ก float (�double
3. ��!��ก<� ,�*(ก char (� String (char[]) ก��������9 ��&�����������!(��(���� � ���*��)��2! 9����ก�@��!(�� (�ก������������!(��ก��ก��
ก�����ก�6�����
���������+�*+�ก�����ก�@��!(�� �� �� ������!(�� ������!(�� (= ��� �)�*�) [,������!(�� (= ��� �)�*�)];
�� ������!(��)��&2�*����(*! �!� ��� �)�*���+�ก.,�*,)+�ก.,�* ��� ��ก������+��&)��2������!(��ก��ก����ก!� ก������������!(��������+��*����)� ���*���������$������������+�*+�ก����ก����& ���������!(�� ���*��)�กHก� ก�)��&�� (�&�����) +�ก�����������*!& ������
1. !���������+�*���� !�)�)�& (������(������&��(���&) 2. ��ก<������*� �*����$� a 4�� z ���� A 4�� Z ����� ������)�&�����*�+�* ( _ ) ( <<< ��*� ) 3. ��ก<���,� ����$���!��ก<� ������!�� ����� ������)�&�����*�+�* ก.,�* (�"%��)����!����&+�����
4. ������)���&��� �;���&� 32 ��!(�ก������� ( ��&5 �� ,) !��ก � 31 ��!�� ... + ���,�����4�� ) 5. ��!��ก<����+�*��$� Case-sensitive �� ��! a �����ก�� A
6. *** !���ก���&������+�* _ �������!(�������������!������+�* _ ������ก ���ก����ก��,�*'�&,)�2*��!
7. "%�������������ก�� ����!� (Reserved Keywords) #���)������,���� ����!�+���<�#�
asm double int static _cs _BP
auto else interrupt struct _ds _CH
break enum long switch _es _CL
case extern near typedef _ss _CX
cdecl far pascal union _AH _DH
char float register unsigned _AL _DL
const for return volatile _AX _DX
continue goto short void _BH _DI
default huge signed while _BL _SI
do if sizeof _BX _SP
- 14 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
����-��ก����%������� �/�ก������!&������
89ก%�� :��
int a,b,sum=0;
float gpa;
char grade=’A’;
char name[50];
int sizeof; // ����!� float 2g; //�����*��*!&��!�� char this grade=’A’; //)��!*�!��
� ������%��9/�!� ;� (��� !��2*��ก+�������)
1. int �� �� ��*�)2���+�*�ก.�����!���.) +�*��!& !�)���+�ก������ก.� 2 ,��01 ��)��4�ก.�����!���.),�*
����(� -32,768 4�� 32,768 ������� 2 �����)�� unsigned int �� *�&ก�� (��ก.�����!���.)����(� 0 4�� 65,535 (4*��� ) signed ���ก.�����!���.)�!ก�� �)������$� 2 ��� (����ก.�����!��,),�*)
2. char �� �� ��*�)2���+�*�ก.���!��ก<� 1 ��! ��� ‘A’,’c’,’5’ (����ก�!���+�* ‘ ��$���!��ก!���$� char) (�+�������
Compiler ��,))����$���!��ก<� (����ก.���$���!��(�� '�&+�*)���%���������!� ASCII2 �����!��ก<����5
+�*��������+�ก���ก.� 1 ,��0 ( -127 4�� 127) 3. char[] (String)������.�-�����9-# ������!� ;� ���4�,�7� (������$��*����$��� ��*�)2����%����� !����2* +�*
�ก.��*� !�) ��� “Pong” , “Hello World” ��,����)�/��� ����ก�!�+�* “ ��$���!��ก!���$� String ���K&! �&�2* !�)�)�&�� �5+���!�*���� 6 (*!ก���� ���
4. float �� �� ��*�)2����������ก.���!���@� &) +�*��!& !�)��� 4 ,��0 ���ก.� �,�*����(� 3.4E-38 4�� 3.4E+38 (�ก.��@� &)42ก�*�����)�/ 8 ��ก)
5. double �� �� ��*�)2��� *�& float (�+�*����������$� 2 ��� ��� float �ก.��*�)2,�*����(� 1.7E-308 4�� 1.7E+308 �ก.��@� &)42ก�*�����)�/ 15 ��ก
6. long int �� int ���&�!���� (ก!���) +�*���������ก.��*�)2)�กก!� int 2 ��� ���� �� 4 ,��0 �ก.��*�)2,�*����(� -2,014,153,648 4�� 2,014,153,648 (�!��0)��&��) (*!���2��!��� 2ก��)�� �� unsigned long int ���ก.�,�*����(� 0 4�� 4,294,486,648 ���)�/����&
7. long double �� double ���&�!���� +�*�������� 10 ,��0 �ก.��*�)2,�*����(� 3.4E-4032 4�� 1.1E+4032 �ก.��@� &),�*42ก�*�����)�/ 18 ��ก (������������,)�����$��� �5 (�����!��&�,�(��)���&)
ก��ก��" ��-�#"%ก������
������!(�� = �����*��ก��ก�����;
��� x=5; �)�& !�)!� ��� � 5 ,��ก.�+���!(�� x �����*��5�&����ก!�+�* x )� ���$� 5
1
��������ก�� Compiler ����ก������������������������� �������������������� Turbo C
2 American Standard for Information Interchange
- 15 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
����-��
#include <stdio.h> int main(){
char name[20]= “Pong”;
int age=20;
float gpa;
gpa = 3.94;
printf(“My Name is %15s\n”,name);
printf(“Age : %5d|\t GPAX : %4.2f\n”,age,gpa);
return 0; }
1ก����� My Name is Pong
Age : 20 | GPAX : 3.94
�-�����& (Constant Variable) ��ก����*��ก����*�� � ����+�5 �����,))�ก������&� !�����*�� � ������� ! 9�ก����*�� � �������&2+��!���� �!����)!1ก�� (�!����&!ก�� #include) +���!�*�(�ก5�� ก����*�� � �����*��+�* ������ #define )��2�(�� ������ ��
#define ������!(�� � ���� � ��������*��ก���ก.� ����ก�� �������,))� ; ��
#include <stdio.h>
#define PI 3.1415926
int main(){
printf(“PI is %f\n”,PI);
return 0; }
1ก����� PI is 3.141593
�����&����%��9/ (scanf , gets) scanf �� ���������+�*+�ก������*�)2��ก �&0���0� '�& ���������*�)��������&2ก���*� !�) !� �)
gets �� ���������+�*����*� !�)��ก �&0���0� (�*� !�)�&�����&!) '�&���ก.�+���!(���������,!* scanf(“�*� !�) !� �)”[,��!(����������]);
gets(��!(�� char[] ��������);
�*� !�) !� �) ��+�* %s,%f,%d ��$���ก (�����������$�)�ก ������� ���*��,)��ก����&) ��!(�������)���� 4*���$����������&!ก�� char[] .�-%���� & �%��" %� 8%�.�-#�-�/%�#"%#�- & ��" %��%�� (����&�����,�ก��)
- 16 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
����-����- #include <stdio.h> int main(){
char name[20],school[30];
int age;
printf(“Please Enter Your Name : ”);
scanf(“%s”,name);
printf(“Please Enter Your School : ”);
gets(school);
printf(“Please Enter Your Age : ”);
scanf(“%d”,&age);
printf(“Name : %s\n”,name);
printf(“School : %s\n”,school);
printf(“Age : %d”,age);
return 0; }
1ก����� Please Enter Your Name : Pong
Please Enter Your School : SUT University
Please Enter Your Age : 20
Name : Pong
School : SUT University
Age : 20
��+����� '�&+�*)�ก���!*�!�� �*!&+������2 ��� (*!����ก�1��90�� ���!��ก ���,����� ������+�*������� ��� ,)��ก1�ก�� (�)�! 9�(ก* ��� 4*��*��ก��+�* scanf ��������)���ก�� gets #"%#�%�%��������$���< “%[^\n]” �� “%s”
��ก�&��� �@<+�+� ������� scanf �� ��� �� 4*��*��ก��+�*����6�����!��ก<�+� +�*+��,�+� [ ] ��� 4*��*��ก������6�����ก<� B,0,1,2,3,4,5,6,7,8,9 ก.��+�*�*� !�) !� �)!� “%[B0123456789]” +��ก</�����*�)4*�,)�*��ก��+�*+�* ^ �����*� ��� 4*�,)�*��ก��+�*)���! a +��*� !�) ���*��+�*�*� !�) !� �)��$� “%[^a]” ���)�/��� ��� (*!4*� scanf
�����ก<���� .�-%��ก�� )�������ก���&���������� ��������2�������� ��� �)��ก,��� ���!� ��!���*�����ก�����ก�@��� name[20] �)�&4���������)��4�ก.��*� !�),�* 19 ��!��ก<��� ��� 4*��*��ก��+�*�ก.�,�* 20 ��! ก.���*��+���$� 21 (���� ��� (** �����ก...�& ...,),�*��ก��,���)
�23ก"��
1. �����&�'��(ก�)��� ���ก12*+�* '�&��������$�����!���.) ����!��@� &) (���� � �&��� 1 ��! (*!���ก��(���1��90��ก�����*��� ��*�)������ก!���$���!(���� ���,�
2. �����&�'��(ก�)��� � ��!(������!���.) 2 ����!� (*!(���1 ��ก������� ����!����!(��(*!(���1��ก ���� (�&����'ก���)
- 17 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
������ � ก��
�)������,�*��!(��)�(*! ��������)�(*!(���1�& )��ก.��ก�,��&2�� )�� ����*��)���!����� �ก��ก���*�� '�&��!����� �ก�� 1)�&�ก��(����$� 5 (�� �� ��!����� �ก���ก��&!ก��ก��ก����� �(��� �) � ��!����� �ก����� / �@����0 ��!����� �ก��������ก� ��!����� �ก���� �� � ��!����� �ก��ก����� �(��)�������,�
������ � ก������=�6���� ( �!���&���������&) ��!����� �ก����� / �@����0 )�������
Operator ก��+�*��� ��!�&��
+ ���ก���!ก a=a+b; a= +3; (+3 ����,�*ก. �� 3)
c=3+5; b=a+5;
- ���ก��� a=a-b; a= -3;
c=3-5; b=a-5;
* ���ก�� 2/ a=2*3; c=a*b;
/ ���ก����� a=a/2; b=5/3; (1��90����$� 1)
% ���ก���������@< a=3%2; b=a%4;
��������.ก�*�&�������� ��� �����9 ��&�&������&!�+���<�#����� int ���ก�� int ��,�* int ������� 5 ��$� int
,3 ก.��$� int ก�����ก�����ก�� 1��90��) !�)�� � �� 1.6666… (� Compiler ���*������&�+�*��$� int �������$�,���)กH ������� Compiler ���,)��+��@<�@� &)����ก ��������+�*,�* ���$� 1 (�� (��������*� �������.�1 �������)
����������)��4��� ��+�*���ก������&��� ������!(��,�* ���&ก!� Type cast ���,�*'�&ก���� �) (�� ��������*��ก������&���$�) ��*���!(���������*��ก�� ��� printf(“%.2f”,4/(float)5); 1��90����ก)���$� 0.80 �����������ก������&�+�* 5 ��$��� � float ก����������ก�����
������ � ก��# ก��ก��" ��-��/����&��-�
Operator ��!�&�� !�)�)�&
= n=8; ��� � 8 ,�+�+���!(�� n
+= n+=8 n = n+8;
-= n-=8 n = n-8;
*= n*=8 n = n*8;
/= n/=8 n = n/8;
%= n%=8 n = n%8;
++ ++n
n++
n=n+1;
-- --n
n--
n=n-1;
- 18 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
���� �)�� )���� ������)�& ++ ก�� -- �� ������ ���*���������� �;�&�����&! �� ���2 ���������(����� (*!�������*�+�)�ก& ������
������ num1=8;
num2=++num1 -3;
num1=8;
num2=num1++ - 3;
����,�* num1=9 , num2=6 num1=9,num2=5;
!�)�)�&��� ++ , -- 4*������+�*��& �)�&4��!� 4*� ++num �)�&4�� �� �) � num ������ก 1 (*! �&��� � num )�+�* (�4*� num++ �)�&4�� ��� � num )�+�*ก��(*! �&�� �) � num ������ก����� ��������2+�' *��*������ ��� (-- ��)���ก���� ,)�&�ก���&�#���) �����&5 ++ ��ก-� ���&��-�ก-� ++ ��"/�� ���&��-�"/�� (�������ก!��� ))
������ � ก��������ก�
����ก.��ก!� ���ก� ������� ��*��+�* ���ก@����0 +�ก�� ���!/(�55 ก�������*����ก�&�����!� +���<�#� ��)��!� 0 �� �-���0� (� �+�5ก.��)��� .�-#�- 0 �� �-����� ((�+� !�)� &)��+�* 1 (���� � (�1���,�*��ก� ������)�&���ก.��,�* 1 �)����� ����ก��) '�� )��2ก��� ������)�&ก���&
Operator ��!�&�� !�)�)�&
== a==b ���ก��
> a>b )�กก!�
< a<b �*�&ก!�
>= a>=b )�กก!��������ก��
<= a<=b �*�&ก!��������ก��
!= a!=b ,)���ก��
&& (a>b)&&(a<c) (�
|| (a>b)||(a<c) ����
! !(a<b) � ��9 (not)
����-����- 4*�ก�����+�* a=5,b=7,c=10
������ ����,�* a>b 0 a<(c-b) 0
(a+b)>c 1
(a*+7)%2==c%2 1
c!=4 1
(a>b)&&(c<b) 0
����.�!� ����,�*��ก� ������)�&�����,�* ���ก)���$� 0 ���� 1 �������
- 19 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
�����ก������%����!����� �ก���� �� � ก����!����� �ก��(��������,�,�ก�� �������������+�*��ก��,�* )��2������������ �;�������� ������)�&�&��ก!� ���� �� /�������������)������!&��"��� �������*�����+�*�� (#����&���)
� ������)�& !�)��� �; �����ก
( ) �2���� #*�&,��!� ! ++ -- (type cast) | ���.�7%��
* / % | #*�&,��!�
+ - | #*�&,��!�
< <= > >= | #*�&,��!�
== !== | #*�&,��!�
&& | #*�&,��!�
|| V #*�&,��!�
= += -= *= /= %= ������ ���.�7%��
�����������&��,)��)��4�9 ��&ก������������ ������)�&,�*�����)� ���2!� ���,���� (����!(���)���1�� ��������,������)� ���$����,���*�� ������ก�(�����1 �����)�ก������ ��*���������. !�����������,!* 42ก�*��������� (...���+�*���(����������4*��9 ��&����& ��ก!��� ) (*!��� ��&� �����(������2��ก!� (*!������$���<������!�������& ��ก!���) ���$��-� x=3,y=4,z=7,a=1,b=2
'��&0 � '��&0 �
x=++y -3%2 x= y= x=y++ - 3%2 x= y= a+=b%2-5*3 a+=b%(2-5)*3
x+= 3%2 + y++ x+= 3%2 + ++y
x= y++ + ++z x= ++y + z++
x=x++ x=++x
x=(a=b++)+2*b x=(a=++b)+2*b
1)!������� �������.�������1),)��)��4�9 ��&,�*�� ����� �!�������!*�+�* +�*����� �����,�*�ก���� (�!&ก�����)���ก �) ��$��-� ��&.�%
- 20 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
�23ก"��
1. �����&�'��(ก�)��� �����!���.) 3 ����!���ก12*+�* (*!���ก���� ��6��&
2. �����&�'��(ก�)(�� ���ก��!&�#�� �)�� ��$� >��(�� �! ก�����+�* (2.54 cm = 1” ,12” = 1 >��) 3. �����&�'��(ก�)(�� ���ก��@�>����,8�0 ��$� ��@��#�#�&�
4. �����&�'��(ก�)��� ��!�)���ก12*+�* 2 �!� ( time1,time2 ) '�&�ก.�+�����!���.) '�& time1 ���!� ��� 1330 �)�&4�� 13:30 �!� time2 ������!����!')� ก������ ��� 340 �)�&4�� 3 ���!')� ก�� 40 ���� �����ก�������ก���!ก � time2 ��*�ก�� time1 (*!(���1��90 ��!�&��1ก�����'��(ก�)
Please enter time1 : 1330
Please enter time2 : 340
Time 13.30 + 3.40 = 17.10
5. �����&�'��(ก�)��� �����!�! ������ก12*+�* (*!���ก��(��+�*��$����!')� ���� (�! ���� ��)�����
��� 7450 ! ���� ก.��$� 2 ���!')� 4 ���� ก����ก 10 ! ���� 6. �� ���!/������������!�ก)���+�;������+��������&)1��1*� �)������ ��*��ก!*�� (��*��&�!)���ก12*+�* 7. �� ���!/�������������*�&�������)������ก�����!�ก)��ก��ก�������&)1��1*� �)������ ��*��ก!*��(��*��&�!��ก12*+�* 8. ���������������������&)����������+�;���������������&2+� ������� ����!�ก)(1/4 ���!�ก)(����ก+�*��) 9. ���������������������&)1��1*����+�;���������������&2+� ����!�ก)
10. ����� ���*�)� 10 �(*!���ก��������!������ 2 (�����!������ �� 11. ���������!� num )���ก12*+�* (��������� num ����(� 0 4�� 9999) (*!���ก���� ����1�!ก��!��(��
��ก��� num ��� 2345 ก.����$� 2+3+4+5 = 14 ��$��*�
12. ���������!� num )���ก12*+�* (��������� num ����(� 0 4�� 9999) (*!���ก���� ����1�!ก��!����ก��� num �6����� 2 ��� 2345 ก.����$� 2+4 = 6 ��$��*�
��$�����"���%� (+�*������ ... ���K&!��,)42ก+� -_-!!! )
- 21 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
"���%���& 4
��!���� �+� (�ก��!�2�
�ก��& ��
��!�*�������� �))�ก�����&�'��(ก�))�ก& ��������ก(��ก�) (��*�� focus ���กก!��� )(����) �������!�*����4��!���$���!�*������$���!+����ก�����&�'��(ก�)�&�����&! (�!�������)������,� ���,),�*��)���ก��) �-� ������ Syntax �-� �%��
%�� �-��.�� % " �ก��!&�� Algorithm ��กก�-����� ,)���&�!��� �)ก���&��ก!�
- 22 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
If…else…
If…else… ��$� ���������+�*+�ก������ �+� �)����*������ �+� (�8�55) &ก��!�&����� 8%�(if) ��)�� ! (������,�) �*��ก ��*�!)�ก��( �������)����� �) "�ก.�-(else) ก.ก ���)�ก� ( �������)�����.�) ((��(#!� �����) ����.�,�*!� �)���)��*� !�)5 �����#��� )�'�ก����$� �� � ������$���.� ,�*�)������'��(ก�) (������,�) (*!)������ก+�*���ก)�กก!� 1 ��� ��)�����!�&�� �����+�* if else
)����ก�� �2�(�� ������ )��*!&ก�� 3 (�� ��
if (������,�){
������s;
}
if (������,�)
������;
else
������;
if (������,�) {
������;
…
} else {
������;
…
}
if (������,�) {
������;
…
} else if (������,� 2) {
}
… ������,�4��,� 4*�)�
} else {
������;
… }
(����� 1 ()�)��#*�&) �� )�������ก 2 ��� ��4*��� �ก.��� ������ (�4*���.�ก.,)�����,�
(����� 2 ()�)��#*�&) �� )�������ก 2 ��� ��4*��� ������� �������� 4*���.���� ��������
(����� 3 ()�)���!�) �� 4*�)�)�กก!� 2 �������,� ก. else if ,������&5 �� �� �!� else ����*�& ��,)��*�ก�/�+�5 �&
��!&� .�+������� �!�+�;����$� � ���0 ������ก��,��*!&� ������)�&(�����ก� (*!+�*1��90 ��$� �� � ������.��������
����ก ��ก��$� ���������&! ��,)�����$��*��+� { } (&ก+�*��.�+� (��)�)���!�) (�4*�)� ������)�กก!� 2 ���������*��+� { } ���
�%�� � �� ��ก���&�'��(ก�) !�)�ก���!*���*�,�+�*��ก!�+ ��&2+�!�,�� ����ก���ก��!�&��!� ��������)�ก���!*���*�,��*��+���ก
- 23 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
���9����-��ก� ���ก��ก� ���
1ก����� Please enter even number : 20
It’s even number
#include <stdio.h> int main(){
int x;
printf(“Please enter even number :”);
scanf(“%d”,&x);
if (x%2 == 0)
printf(“It\’s even number\n”);
else {
printf(“%d is ”,x);
printf(“Odd Number\n”);
}
}
1ก����� Please enter even number : 11
11 is Odd Number
�23ก"�������� if-else
1. �����&�'��(ก�)��� �����!���.) n ��ก12*+�* ��ก12*+�*ก��ก��!�����)�กก!� 30 (���$��� 2 +�*� )�0!� “You’re right” ��ก,)+�+�*� )�0 ��!� “You’re wrong”
2. �����&�'��(ก�)��� �����!���.) score ��ก12*+�* (*!���ก�����ก�� '�&)��ก/M0������ 80 ����,� A
70-79 B
60-69 C
50-59 D
��ก���� F
3. �����&�'��(ก�)4�)(�� ���@���ก>����,8�0��$� �#�#�&� ������ก �#�#�&���$�>����,8�0 '�&ก�������ก<� 1 ��!)���ก12*+�* ��ก��$� C +�*��� ������@��#�#�&� (����$� >����,8�0 ��ก��$� F +�*��� ������@�>����,8�0 (����$� �#�#�&� ��ก,)+�* +�*� )�0 ��!� “You’re wrong”
4. �����&�'��(ก�)�������!���.))� 10 ����!� (*!���ก���� ��2���� ��*�)���� ��6��&
��$���&���&.�%��ก If-else
- 24 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
for loop
��!� loop �� ��)�ก��!�#��� ������� for loop �����$�ก��!�#���(�� for ก��!�#���(������!�+�;+�*ก��ก��!�#������)�ก��!�������(���� (���,�*) (�ก��!�#���(�����)�ก��)���! index ���+�*+�ก����ก!�,�4�����������+�(*! index ����&2+� !�)� &))�ก��+�* i , j , k , l (������ �� !�)� &)) ��)��2�2�(����� for loop ก���&��ก!�
for ( �������� �)�*�;������,�; �����������)
������;
for ( �������� �)�*�;������,�; �����������){
������s;
}
�&�ก����ก!� { } +�*��กก�����&!ก�� if-else �� 4*�)� ��������&+�* for ���&� ���������&! ,)�����$�������*��+� { } (�4*�)�)�กก!� 1 ������ ���*��+��*!& ��กก����������� for �)������&���$� Algorithm ก.,�*(�����
1. �� �)��� �������� �)�*�
2. ��!����������,� 4*���!���$���.�ก.��ก��ก for ��ก��$��� �+�*����� �ก����
3. ��� ��������&+� for
4. ��� �����������
5. ก��,����+��*� 2
���9����-��ก� �/���ก�-�
#include <stdio.h> int main(){
int num,i;
printf(“Please enter a number :”);
scanf(“%d”,&num);
for (i=0;i<num;i++)
printf(“%d ”,i);
}
1ก�����
Please enter a number : 10 0 1 2 3 4 5 6 7 8 9
#include <stdio.h> int main(){
int num,i;
printf(“Please enter a number :”);
scanf(“%d”,&num);
for (i=1;i<=num;i++)
printf(“%d ”,i);
}
1ก�����
Please enter a number : 10 1 2 3 4 5 6 7 8 9 10
- 25 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
�23ก"��
1. �����&�'��(ก�)��� ���!������!���.)�!ก��ก12*+�* (*!���ก��� )�0 ���ก��!������ ��4�� 1 (����) 2. �����&�'��(ก�)��� ���!������!���.)�!ก��ก12*+�* ��ก12*+�* +� ��� �� +�*� )�0����(� 1 4�� ��!������'�&� )�0
�6����� �� ��ก����� 2 +�*� )�0����(� 0 4����!������ '�&� )�0�6����� 2 3. �����&�'��(ก�) *�&ก���*���� 2 (�+�*����(��
4. �����&�'��(ก�)��� ���!������!���.)�!ก (*!���ก��� )�0()�2�� 2/���������!�����
5. �����&�'��(ก�)��1�!)�� ����(� 1 4�� N �)��� N ��$�����!���������*�)�
6. �����&�'��(ก�)�������!���.))� 10 ����!� (*!���ก���� ��2���� ��*�)���� ��6��&
7. �����&�'��(ก�)� )�0 ���ก A 4�� Z
8. �����&�'��(ก�)� )�0 ���ก Z 4�� A
9. �����&�'��(ก�)��� ���!��ก<� 1 ��!��ก12*+�*(*!���ก��� )�0��ก A 4����!��ก<����� (*!� )�0ก��,�&�� A ��ก ����
Please enter a character : L
A B C D E F G H I J K L K J I H G F E D C B A
10. �����&�'��(ก�)��� ������!������!���.) (*!���ก��� )�0�������&)���)� !�)ก!*�����ก������!���.)����
Please enter a number : 4
* * * *
* * * *
* * * *
* * * *
11. �����&�'��(ก�) *�&5ก�� �*� 10 (���$��2�����&)ก!�
Please enter a number : 4
* * * *
* *
* *
* * * *
12. �����&�'��(ก�) *�&ก���*� 11 (���$��������&)ก!������!��ก<�
Please enter a number : 4
A B C D
E F
G H
I J K L
- 26 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
13. ��+�* !�)�2*���,�*��ก�*� 10,11,12 (*!����*���2� Character Diamon
Please enter a number : 4
A
B C
D E
F G
H I
J K
L
14. ��+�* !�)�2*��ก�*���� 13 (*!��*���2���)����&)ก!�
Please enter a number : 4
A
B C
D E
F G H I
15. �����&�'��(ก�)��*����)����&)ก����!
��$���&���&.�%��ก for loop
- 27 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
while loop & do-while loop
while loop ��� &)+�*ก��)�กก�� loop ���'��(ก�)�)��0 ( �������&�) �!�2��������)�����!����,)(���� ( �������&5 ���)�/����) ��!�2�(����)��ก</������&ก!� for loop �.ก�*�& ( �!�����) while loop )� 2�2�&2��!����� �� do-
while loop #���)��ก</������)���ก�� while loop (����ก������ ����&! ���K&!���2 �2�(��(�ก��������ก��
while (������,�){
������s;
}
do{
������s;
}while (������,�);
ก����������� while
1. ��!����������,�!���$��� �����,) ��ก��!���.� ก.�� loop while ��ก�� �+�*����� �ก����,�
2. ��� ��������&+� while loop
3. ก��,�����*� 1
ก����������� do-while
1. ��� ��������&+� do-while loop
2. ��!����������,�!���$��� �����,) ��ก��!���.� ก.�� loop ��ก�� �+�*ก��,�����*� 1
�*�����ก� while ก�� do-while �����ก��������!� ��ก������,���$���.�����(�(�ก while ��,)��� ��������&+�2��& (� do-while ��,�*���ก����� ������ 1 ���� ���(�����ก�� ��!� do-while ��,�*���ก����� ��������&+� loop �&���*�& 1
������)� (� while ����� ������ก.���)��� ������,�&�� ���$��� ��&2
���9����-��ก� �/�
#include <stdio.h> int main(){
int num,i;
printf(“Please enter a number :”);
scanf(“%d”,&num);
while(num>0){
printf(“%d ”,num);
num--;
}
}
1ก�����
Please enter a number : 10 10 9 8 7 6 5 4 3 2 1
Please enter a number : 0
#include <stdio.h> int main(){
int num,i;
printf(“Please enter a number :”);
scanf(“%d”,&num);
do {
printf(“%d ”,num);
num--;
} while(num>0);
}
1ก����� Please enter a number : 10 10 9 8 7 6 5 4 3 2 1
Please enter a number : 0 0
- 28 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
�23ก"��
1. �����ก�����'��(ก�)��ก(��BEก��� for loop (�+�*����&�,�+�* while loop (��
2. �����&�'��(ก�)��� �����(� 5 – 20 ��ก12*+�* ��ก12*+�*ก��ก1 �+�*���� ��!� “Wrong number” (*!���ก����� �+�)��ก!�12*+�*����� �42ก�*��
3. �����&�'��(ก�)��� � 10 ���ก12*+�* '�& �����ก.������*���&2���!�� 0 – 10 (*!���ก���� ��2����
4. ���&�'��(ก�)���������*�)2��� �5����� ���ก���*!& ��@ (‘F’,’M’) ��&� (int 20-50) (��������ก (weight) 4*���$�12*��&��&� 30-50 �������ก 50 – 60 ����12*�; ���&� 20-30 �������ก 45 – 55 (*!� )�0 ��!� “GREAT” 4*�,)+�*� )�0 ��!� “HOHOHO”
5. ��� ���!�� 2 ����!���ก12*+�* a ,b '�&��� 0<a<10 (� 20<b<40 4*�,)�&2+��!����ก�!+�*� )�0!� “ERROR”
(�!����+�) �)���,�* a,b (*!+�*���ก��� )�0 ��� 2����(� a 4�� b
�8�...Break, Continue
������ break; (� ������ continue; ��$� ���������+�*+�ก�� !� �) loop '�&��� break; ��,!*�������ก����ก��ก2� ���� switch (...ก���������&�) '�&�������ก+������ (� continue; ����$� �����������+�*,�*�6���2� �)������ ���������+�2� '��(ก�)��,)��� ������+�5�� (���ก��,���!����������,�+�) (���&5!� *�&ก�� break; (���,��� �)+�) (� break;
����ก,��& (�)�&���� 4*���$� for loop ��ก��,���� ����������� ก��(*!���,���!����������,�)
��$��-� ��� while, do-while, break �/�continue;
- 29 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
Switch…case
switch…case ��$� ���������)��ก</� *�&ก�� if…else ��&������,� ��)��ก��ก����� if…else ���)�ก������&�������,��6��� �������� ��������.ก5 ��)��2�2�(��
switch(��!(�������$�������,�){
case ���� 1 : ������s;
break;
case ����2 : ������s;
break;
….
case ���� n : ������s;
break;
default : ������s;
}
��!�&����� switch(x){
case 1 : printf(“X is One”);
break;
case 2 : printf(“X is Two”);
break;
case 3 : printf(“X is Three”);
break;
default : printf(“X is other”);
}
ก�������������!�&�����+�*ก.���!� 4*� x =1 ก.��� )�0 ��!� “X is One” 4*� x=2 ก.� )�0 ��!� “X is Two” 4*� x=3
ก.� )�0 ��!� “X is Three” (�4*�,)+�*ก.� )�0 ��!� “X is other”
default �������ก�/����,)+� ��������,!*+� case ก����*���� (*!����� ��������&+� default ��)���ก�� if…else
��&5������ก�� default ก.����)���ก�� else ��!���4*� ��4*�,)��*�ก�/�,���&ก.��� ��������� ��,����)�/��� �$���&��� % ��$�� �@< (,),�*��ก��,���) ����ก. �� ����ก�!���)�ก��+� break; ��ก5 case ���� )�0�& +�*�����
�2ก��4*����,)+� break; ���ก ���,�����.... 1��90 �� ���ก(�ก,� ��)����4,))� ���ก(�� )��ก.���&��,),�* ������ก.��)���ก�� ���
��!�&����� switch(x){
case 1 : printf(“X is One”);
case 2 : printf(“X is Two”);
case 3 : printf(“X is Three”);
default : printf(“X is other”);
}
4*� x=1 ก.��� )�0 “X is OneX is TwoX is ThreeX is other” 4*� x=2 ก.��� )�0 “X is TwoX is ThreeX is
other” �8�55 ��.�4��ก�� break (�ก����&�� ��)�����������)������&5���� switch �& ������� �$ก case ��%���� break; ��< �����&�"/���$��$ก�����
- 30 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
"���%���& 5
>:�ก0��� Function
�ก��& ��
��!�*������ก��*�+������+�*��)��4���&�'��(ก�),�*������� ��!�*������& ���ก!�&�ก (�1)ก��)��!�)����& '�&����*������+���� ก���!�ก����&+� ,parameter (� return value ���>:�ก0��� ( ���ก.�����,�*(*!
- 31 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
>(�ก���
��4�)����*����� >:�ก0��� ����,� ... )�� ��ก�����ก���!�ก������!�)����&�(&ก��ก��ก'��(ก�)������� ��$�ก��ก�����ก���!�ก��&�& (Module) ... (&�����&2+��J�) '�&ก���!�ก��&�&��������)��ก��,�4����!�*�(�ก������ ��� ก���*))�)� ก���!�ก������&2��� main ก.����+�( !� �*)����+�*����� ,�����*���)*� ,������ก (���ก+�4*!& (� function ��)��4��!� ก���!�ก���*)����+�*��������&��,� ( ���� (*! �&��� function ���,�* )�+�* main +�* ก.����$� �������*)������&+� main main ��$�>:�ก0��������+� ��<�#� #�����4��!���$�>:�ก0���(�ก�����! Compiler ���������*!&�& (*!(�!� main ��,����&ก>:�ก0���,��)���������ก.���(*!(� (�������*���2*��) ����� ���!�)�,ก���) !�(*!)��2! 9�ก��)��>:�ก0���ก���&��ก!�
>:�ก0����������+���! Parameter �������*�,�+�*ก�� Function ��������&� ก���!�ก����&+� Function (� ������ก��)�+�*ก�� �������&ก)�� (Return Value) ��������)��2�2�(��ก�����ก�@(�ก�����&ก+�*ก��ก����ก!�
ก�����ก�@
�� ���� ��������ก�� ����>:�ก0���(��!(���������+�*>:�ก0���){
������s;
return ������ก��; //+�ก�/����)�ก���� �ก�� }
ก�����ก�@>:�ก0����*�(�� (Prototype)
�� ���� ��������ก�� ����>:�ก0���(��!(���������+�*>:�ก0���);
ก�����&ก+�*ก�/�)�ก���� �ก��
��!(�����+�*��� = ����>:�ก0���( ������+�*>:�ก0�����)���,�*���ก�@);
ก�����&ก+�*ก�/����,))�ก���� �ก��
����>:�ก0���( ������+�*>:�ก0�����)���,�*���ก�@);
� ������-���&���-�ก/� ����)���ก���� ������!(���& (��� �))�������� ����� �� void ���� ��ก����ก!���,)�� �ก�� (4*���$� void ,)�*��)� ������������ return) �������&���-�#"% �����ก�@��)���ก�����ก����!(���& (�!�(����!(���������*!&� ������)�& , (comma) '�&�)������ก�@���.���,))� ; �D��*�& �!&�>(�ก��� +�*��กก�����&!ก��������!(���& (���!��+�ก��+�*����>:�ก0������#���ก��>:�ก0������)��&2(*! ��������&+� ���)�������&2+� main
����.�!�)�ก�����ก�@>:�ก0����*�(�� ������+�*+�ก�/�����&�ก��������&����&����>:�ก0���,!*��� main (���ก����&������ main ��,)�2*��ก(*!�����+�* error ,�* ����������*��)�ก�����ก�@>:�ก0����*�(��,!*�!���*� main ���� ,!*��&+� main ก.,�* (+�ก�/����,!*��&+� main, main ��������������)��4+�*>:�ก0������,�*) ก�����ก�@�*�(��������&ก!� Prototype #�������)���ก��������(�ก���ก�����ก�@>:�ก0���(�����&���ก { ��$� ; �D��*�&�&
Function Parameter
��!(�������+�*>:�ก0���
Return Value
������ก��)�&�� �������&ก>:�ก0���
- 32 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
>:�ก0������)�ก�����&ก+�*>:�ก0������� ���*�����&�,!*���>:�ก0��������!������&ก+�*��)� ,)���� error (�2ก�/� main ��$���!�&��) ����� )��2��!�&��ก�����&�'��(ก�)ก���&��ก!�
,)+�*>:�ก0��� (Before)
#include<stdio.h>
int main(){
int a,b;
do{
printf(“Please enter a :”);
scanf(“%d”,&a);
if(a>=0&&a<=50)
break;
else
printf(“Error Na”);
}while(1);
do{
printf(“Please enter b :”);
scanf(“%d”,&b);
if(b>=0&&b<=50)
break;
else
printf(“Error Naja”);
}while(1);
printf(“%d+%d=%d”,a,b,a+b);
}
+�*>:�ก0��� (After)
#include<stdio.h>
int getnumber(char strwel[],char strerr[]){
int a;
do{
printf(strwel);
scanf(“%d”,&a);
if(a>=0&&a<=50)
break;
else
printf(strerr);
}while(1);
return a;
}
int main(){
int a,b;
a=getnumber(“Please enter a:”, “Error Na”);
b=getnumber(“Please enter b:”, “Error Na Ja”);
printf(“%d+%d=%d”,a,b,a+b);
}
��ก��!�&���*���*������.�!������กก��+�*>:�ก0��� �����+�* main ������&��)�ก (����,)�*�����&� do { … }
while ��&5����*!& ���&����&�������&!ก.�)�2�/0,�* ��ก�&�� �����+�* main �����&�����*!&!���$�'��(ก�)�����,�
>(�ก��� �����0��9�
strlen() �� !�)&�! String
strcat() ก����� String �����)���ก����ก String �����
strcmp() ก������&����&� String 2 ��!
strcpy() ก�� ���� String
���������2!�)�� ����,� (��)��4��,�*+�������� “C Programming for Beginner”) isalpha() toupper()
islower() ceil()
isupper() floor()
isdigit() pow()
isspace() sqrt()
tolower()
- 33 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
�23ก"��
1. �����&�'��(ก�)����*�)2+�*ก�� a,b,c #�����$� !�)&�!�*��������)�����)����&)(*!��+�*ก��>:�ก0��� area()
#�����$�>:�ก0�����������ก�� ���!/ ����������)����&) (*!�� ��ก��+�* main (��� ������������ (+�ก�/��������*�)2�� a,b,c ��$��+�*!���� �+�))
2. ���ก�����&�'��(ก�)����*�)2(����� ����� name ��,�&��>:�ก0��� backward() >:�ก0�����������ก��ก�� String (*!�� � ��+�*ก�� main �����,�� )�0 �
3. �����&�'��(ก�)����*�)2��!������!���.) 3 ��*!&>:�ก0��� findMax() (*!�� �)�ก������ก��)�+�* main
(���1
4. �����&�>:�ก0��� ���!/�� � factorial
5. �����&�>:�ก0��� isTriangle() '�&����*�)2��!�� 3 ����!� (*!�� � ก��)���$� 0 ���� 1 ��ก!���������)��)��4���)���*�� 3 ����&),�*+�*�� 1 ก��)� (�4*�,)+�*�� 0 ก��)�
��$���&���&.�%���&������ก function
- 34 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
"���%���& 6
)?����!(�� (Array)
�ก��& ��
��!�*�����������������,�*��*�ก���4��ก��/0�*���)��� (�8�55) (�����ก.�������$�������ก</���� Array +���!�*�����������)��ก�� �����ก.,�* �����)����)���$��$�
- 35 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
Array
������ก Keyword �����������&0#�ก� �ก���&�� ��� “Array �� ����*�)2�� ����&!ก�� ������)������ก���������(�(�ก (���!(��(����!)� index (��)��������) �6���(����!” ... �)�����ก(��������*����ก+���&����&�ก��#�ก� � �$��%��9/� �������ก� �� ��!(�����,),�*)����&���!���&!(�)�����&5��! ��ก��!)�������)���ก�� (�)��� ����&!ก�� (������ก���������� ���� Index ���� Subscript ������ก��������� ��*� Index ���ก. ���)����)�&���&�)�9&) )��ก.��)�����������(�� ��*��(�ก���ก�� (���ก �ก.+�*�����*�����&!ก�� ���������*�� �.9/20 ����� 2 ��,����)�/��� �������&ก�*����� (����*�)2���) ก.���*�����&ก!� �.9/20 (�4*������&ก��� �ก.���*�����&ก!� �*�� �.9/20 ����� 2
���� �������*�+�ก�� Concept ก�����) !�(*! ��,� ����*��)��2!� Array �����*�����$��&��,� ...(�����ก!�ก!���)�4����!�*���� ��ก � �,�*+�* Array ,�(*!�&��,)�2*��! (�������) ���# ��� char[] ,�*��������������������&ก!� String ������� String 4��!���$� Array of Char ���� ����&0��� char ������� (*!��!,���������ก!���$�����&0 ก. [] ,�4*���.� [] ����*����ก+�* �4�� Array ,!*ก�� )��2! 9���*������&0ก����ก!�
�� ���!(�� ������!(��[��������&0] (={ ��������&0}); (={ ��������&0}) �� ��� �)�*����ก�����+�*ก�� ����&0����(���(�ก�& ��)��4ก��������ก�������������&0 4*�,)��������+�*�ก � error ! 9�(ก*)� 2 ��� �� ���! ������������&0 ����,)ก.,)�*��ก����� �+�* ก��ก����� �����(�� ��*�� ����*!&� ������)�& , (comma)
����ก�!���)��ก</� *�&ก��ก�����ก�@��!(��9��)�� ( )� [] �� �)����)�������� )��2��!�&��ก�����ก�@��!(������&0 ���(������42ก(�1 �ก���&��ก!�
��'���&89ก%�� ��'���&:��
int x[10]; int x[]; //,)+�����
int x[5]={1,2,3,4,5}; int x[5]={1,2,3,4,5,6}; //ก����� �,)���ก������ int x[] = {1,2,3,4,5};
char name[] = {‘H’,‘e’,‘l’,‘l’,‘o’};
char name[]= “Hello”; //� �@<������� char[] �������
������� name[] +��������+���!�&����)�����,)���ก�� ���2+� sizeof �����!�ก�����ก�@�&�������,�*������ �)������ก 1 ��ก����!���!��ก<� �������)�ก����*�� ‘\0’ ������ก��!������������$�ก����ก!���$����� �������� String ��!���� (�ก�����ก�@(��(�ก��,)��*�� ‘\0’ ���� ก�����ก�6���ก��.�-�����8 printf �%�� %s .�%
sizeof ��$� ������+�ก�������������!(�� ���
)�4��! 9����&ก����&0(����!ก���*�� ... ��������&5 ��� ! 9� ��
������!(��[index]
+�*)��!���$���)�����!(����!�����,��& ��� ��)��2��!�&��ก����
- 36 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
����-�� #include<stdio.h>
#include<conio.h>
int main(){
int x[3];
x[0]=4; x[1]=2; x[2]=1;
printf(“%d+%d+%d=%d”,x[0],x[1],x[2],x[0]+x[1]+x[2]);
getch();
return 0;
}
1ก�����
4+2+1=7
�*�����ก�����!� ����&0���� �) index ��� 0 ��,�4�� ��������&0 -1 ��ก���&ก�ก ����ก ���,�����??? ����� ����(��� ���,�ก.,)�2*(����� �8�55 ,) error �� ��*���! index �����)��4+�*��!(���ก.� �(*!���&ก Array '�&+�*��!(��(�� index ,�* (�!�+�;� &)+�*��!(�� i,j,k) )��2��!�&��ก��
#include<stdio.h>
#include<conio.h>
int main(){
int x[7],i,sum=0;
for(i=0;i<7;i++)
x[i]=i+1;
for(i=0;i<7;i++)
printf(“%d ”,x[i]);
getch();
return 0;
}
1ก�����
1 2 3 4 5 6 7
��กก���������&0&��)�����ก� � ก�/������$���&) � ... �����)����$� Matrix ก.,�* ����)��(����$������ ����&��,�ก.,�*������ �,)!�ก�� ... (��&�ก+�*)��(����� ��)��!���$� Array ��� Array �2�*�� ��� int x[2][3] ก. �� )�����&0 (�� 3 ��!�&2 2 ��! (�������ก!��� )) �� x[0] ��$�����&0���� 3 ��! x[1] ��$�����&0���� 3 ��!���ก�� ����$�����&0���#*���&2+�����&0������� ���2��!�&�� (*!������&����&�ก����!�&��ก����*� (*!����*�+������ก
- 37 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
����-��
#include<stdio.h>
#include<conio.h>
int main(){
int x[2][3],i,sum=0;
for(i=0;i<3;i++)
x[0][i]=i+1;
for(i=0;i<3;i++)
x[1][i]=2*i+1;
for(i=0;i<3;i++)
printf(“%d ”,x[0][i]);
printf(“\n”);
for(i=0;i<3;i++)
printf(“%d ”,x[1][i]);
getch();
return 0;
}
1ก�����
1 2 3
1 3 5
�23ก"��
1. �����&�'��(ก�)�����!������!� 15 ��!��ก12*+�* (*!���ก����(��� ������)�������)� (*!�� ��2���� �������� (�1�!)����������)�����
2. �����&�'��(ก�)��� � n ��ก12*+�* '�&��� 5<n<15 (*!���ก�������!������!� n ��!��ก12*+�* (*!+�*(���1��90(���*���� 1
3. ���'��(ก�) *�&ก���*� 2 (�!�+�*�����!��,�*�6�����!�����,)�ก � 20 (*!���ก��(���1
4. �����&�'��(ก�)��� � String 1 ��!��ก12*+�*(*!���ก�����������!���!� )�0+�; ��!� )�0�.ก (����!�� (*!� )�0��ก�����*���
5. �����&�'��(ก�)��� � String ��ก12*+�*����!� 5 ��!(*!���ก����!���!,��)� !�)&�!)�ก������
6. �����&�'��(ก�)��� ���!������!���.)��ก12*+�* (*!���ก��� )�0 �� 2 (��� �� '�&ก��� )�0����� �
7. ���ก�����&�'��(ก�) *�����ก<���&+� String '�&ก����� String (���!��ก<�����*��ก�� *�����ก12*+�* (*!���ก��� )�0�*� !�)��ก)�!���&+� String )���!��ก<�����ก����! (����(���+��*��
String : The river is no return
Character : r
Found character r = 4
in the position : 5 9 17 21
- 38 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
�-� ���6,#�-.�- 30
��$�ก���6&�*����ก���� ���� &��ก�*����)� �*���� ����(*! �&�2�6&�� ��� ����*���$� �����(�4*����,�ก.��,),�*��,��*�������+���!�2ก�� (*!�2ก���6&!����ก��)��& ��ก,)���ก. �&���)�4�)������0��)�)(*!ก���� ��� )��� �)ก���&��ก!� ���
*** ก������5�*����กก��!�ก������*�����*���2�*������5�� �����ก�2,)�������&����,�* ��� ��!�&����� +�' *�'��&0 )� ; �&2(*!����,) ��ก)��&2(*!ก.,)�*��+���*�,��� �)�� ��� ��ก,))�ก.�*��+���*�,��� �)�*!& ��ก�&�� ���������1��90�*���2!�)��!*�!�� ������� (���������ก���&��)
�%���ก/��4�� 1/2550
�%���& 1 (�%���ก/��4�� 1/2550) 1. #include<stdio.h>
2. #include<string.h>
3. ------------- ���&� ����� --------------;
4. void main(){
5. int i,n[10],x;
6. ------------- ���&� ����� --------------;
7. for(i=0;i<x;i++){
8. n[i] = 'A'+i;
9. if(n[i]%2==0)
10. printf("n[%d] = %c\n",i,n[i]);
11. else
12. printf("n[%d] = %d\n",i,n[i]);
13. }
14. }
15. int test(){
16. int k;
17. char str[11];
18. do{
19. printf("Enter a string (5-10 characters) : ");
20. gets(str);
21. k=strlen(str);
22. }while(------------- ���&� ����� --------------);
23. return k;
24. }
�������& 3 >:�ก0��� prototype ���>:�ก0��� test() �� int test()
�������& 6 ���������+�*+�ก�����&ก>:�ก0��� test()
�� x=test()
�������& 22 ���������+�*+�ก������&����&�'�&+�* || ��$���!�����) ��
k<5 || k>10
�������& 22 ���������+�*+�ก������&����&�'�&+�* && ��$���!�����) ��
k>=5 && k<=10
4*�12*+�*�C����� � “AAAAAA” ��*�)� � �����(�����ก�������� ��
n[0] = 65
n[1] = B
n[2] = 67
n[3] = D
n[4] = 69
n[5] = F
- 39 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
�%���& 2 (ก/��4�� 1/2550) 1. #include<stdio.h>
2. #include<string.h>
3. void main(){
4. char str1[30]="Trimester 1/2550"
,str2[30]="Com Pro";
5. int n1,n2,n3;
6. n1=sizeof(str1);
7. n2=strlen(str1);
8. strcpy(str2,"SUT");
9. strcat(str1,str2);
10. n3=strlen(str2);
11. printf("n1=%d\n",n1);
12. printf("n2=%d\n",n2);
13. printf("n3=%d\n",n3);
14. printf("str1=%s\n",str1);
15. printf("str2=%s\n",str2);
16. }
��กก�����'��(ก�) � �����(�����ก�������� ��
n1 = 30
n2 = 16
n3 = 3
str1 = Trimester 1/2550SUT
str2 = SUT
�%���& 3 (1/2550)
1. #include<stdio.h>
2. void main(){
3. int test[3][5]={15,14,13,12
,11,10,9,8,7,6,5,4,3,2,1};
4. int i,j,a=0,b=0,c=0;
5. for(i=0;i<3;i++){
6. b++;
7. for(j=0;j<5;j++){
8. if(j%2==1)
9. continue;
10. c=c+test[i][j];
11. a++;
12. i++;
13. }
14. }
15. printf("i=%d\n",i);
16. printf("j=%d\n",j);
17. printf("a=%d\n",a);
18. printf("b=%d\n",b);
19. printf("c=%d\n",c);
20. }
��กก�����'��(ก�)� �����(���1��ก�����*��� ��
i = 4
j = 5
a = 3
b = 1
c = 24
- 40 of 40 - 10/16/2008 11:01:00 PM
��ก������ก�ก���� (Part I)
���ก����������� Computer Programming
12*���&� KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]
�%���& 4 (1/2550)
1. #include<stdio.h>
2. void main(){
3. char ch='C';
4. int i=1, total=0;
5. printf("Output: ");
6. while(i){
7. if(ch>'E')
8. i=0;
9. else if (ch%2==0)
10. ch=ch+2;
11. else if (ch<'E')
12. ch--;
13. printf("%c ",ch);
14. total++;
15. }
16. printf("\nTotal = %d",total);
17. }
��กก�����'��(ก�) 1��90�������� ��
Output: B D F F
Total = 4
�$��%��
������ก������ก��ก��� !���&������� (� ��*�������)������&) (��,�����&��) ��ก���(*!)��*�� �)+�5 �!)ก��(��� !�) ���.�,�*����!.��)�)�� ��� http://student.sut.ac.th/comclub ��ก)� ��4�)���,)��*�+�+��!�,��ก.,�'��4�),�*�� ��� ����� ���'�?# ก�����/�ก��!&�.����ก��ก���#�1 ก-� .�%��� $)���-����< /��/�ก,=���ก,���ก����ก-� ����
����� �/ ��ก5 ���� “��” �������
����� �/ ������5 ���5 �*��5 �)�) �)� !����0���+�*ก����������
��&���/� ��ก�ก����0
���'� �������������� ���. KAPONG CPE#11
:9%��:���� ���ก�� :9%����
Recommended