โครงสร้างภาษาซี2
DESCRIPTION
TRANSCRIPT
���������� C
Outline
• C programming
• �� ���������� ��������
• ��� � �! �"������
• �#��$�%�����������
• Flowchart ( ��� %��-.����� ��)
Programming language
• ����������� #0� #1�$�2�3�245%6��7!���� ��6��7!�89!6:02�$�2�;.%#��:<=6>����?<"�><>��#1�$�2�
• ����3�2#��:<=6>����5%@� 6��7 =A� ��������� ! (machine language) �82��75A;!�5�6��B�! 2
– 7� >A� ��6��7!, ���#=��.��7, 31�#=��6�%�;@
Programming language
• �������� ����E�"$5� - �������� ��3�2:�G!�6:02�;.%45%; %$����H6��7!���31�#=��6�%�;@IE%�E7�A�7 @� !�9!@8�!1�I�����6�J!����6#�02���� 3�.!82� (4A�! Compiler, Interpreter)
• 6�J!�������� ����E�"$5�3�2�� ��; %��!�:�A.��7
C Programming
• ��$�%������ �������� 6�<2�@� – ; % text editor :<�:� source code ������� ��3�2H5 >%��>��.�� ����
– !1� source file 3�26��7!6$�R@��%=�� compile �E7; % C compiler • H%����%�4<E:��E>%��� %I� source code ��� compile ;.�A@!I�A���%�4<E:��E�89!��
• 4���:S�@�IE%6�J! object file (�.�$6 <�=�>HV���#1�$�2�)
C Programming
– !1� object file �� link �" library ������� (6�J! ��E8�6�� VE#1�$�2�3�26��7!6>��7�I=%$1�.��" ��31���!��� object code) 4���:S�3�2IE%6�J! execute file
– execute file 3�2IE%@�6�J!#1�$�2�����6#�02��3�2$����H!1�I�; %��! (run) IE%3�!3� �E7I�A>%�� compile �91���
C Programming
– ��@@V"�! �� tools 3�2�=�6�� text editor + compiler + linker 6�%�I=%E%=7 �! 6:02� A=76:<2�#=��$�E= ����=E6�R=;! ��:�G!������� (���; %;! ��6��7! ��$�!=< �!�9E%=7) #0� Turbo C compiler
Source file (*.c)Source file (*.c)
Object file (*.obj)Object file (*.obj)
Execute file (*.exe)Execute file (*.exe)
C compiler
C linkerLibrary (header file , *.h)Library (header file , *.h)
Error
Text editor
create
'(��)�* !�����������+,
• Bitwise Control a @�E ���%��5�IE%H8���E�""<> (bit) �82�6�J!.!A=73�26�R 3�2$VE;! ��6 R"�%��5�– 31���!;!��E�"b���E�=��IE%�7A�������$<3S<��:
• Pointer Implementation a ��>�=��� !<E>�= �9 (pointer)– @�E �� �".!A=7#=��@1����#��:<=6>���IE%�E7>��
'(��)�* !�����������+,
• Case Sensitive a �� ��>�=;.-A �"�� ��>�=6�R ��#=��.��7>A�� �! – >�=��� var1 �">�=��� Var1 6�J!#!��>�=��� �!–main() �" Main() 6�J!#!����� � �! �!
• �4567�8���9(�!57:8����� !;��8 ; (semicolon) a ; %6#�02��.��7 ; ;! ���7 #1�$�2��>A��#1�$�2��� @� �!– ��6=%!"��3�E.�0�6��7!#1�$�2�I�A>A�6!02�� �!I�A��4�>A� ��#��I:������ ��
'(��)�* !�����������+,
• Structure language a ���� ���6�J!�#��$�%�� E��!�9!@8�$����H; %#1�$�2�#="#V��#��$�%�� (Control structure) IE%
• Procedural language a $����H6��7!���� ��6�J!�� ��� module (�"A����� ���� 6�J!$A=!7A�7h 3�231���!;!$A=!���>�=��!6��) IE% �82�@�6��7 =A� function
���!9�7�!* !���� C
#include <stdio.h>
int main()
{
/* display message */
printf(“Hello World”);
return 0;
}
Header
Function
Preprocessor
Comment
Statement
���!9�7�!* !���� C
1. 9@:�;(: (Header)
• �75A>�!>%!������� �� �E7�75A!� $A=!��� � �!
• #1�$�2�3�2; % 1�.!E#A�.�0� 1�.!E>�=���������� ��
• �"A��� IE%6�J! 2 $A=!1.1 Preprocessor statement1.2 Declaration statement
���!9�7�!* !���� C
1.1 Preprocessor statement
• #1�$�2�3�2@�IE%��" ��31� A�!3�2@��� ��#��I:����� ��
• #1�$�2�@��89!>%!E%=76#�02��.��7 #
• >�=����=�4� A�!3�2$1�#�-����������"A��� 6�J! 2 ���6�3 #0�
• #define �'� #include
���!9�7�!* !���� C
1.1.1 #include
• 6��7 ; %��!��� � �!��>�B�!3�2IE%@�E6>��7�I=% (Library)
• @�E6 R";! header file (*.h) 6 A! stdio.h, string.h k�k• 6 A! �VA�#1�$�2� (��� � �!) 6 �27= �" �� ��"/�$E� �%��5�
��#1�!=� k�k 6 A!#1�$�2� printf() �82�6�J!#1�$�2�3�2; %�$E��%�#=���� 3���V� ����$E�4� (6 A! @���:)
���!9�7�!* !���� C
1.1.1 #include
• printf() 6�J!#1�$�2� (��� � �!) ��>�B�!3�2H5 @�E�75A;! �VA� standard input output
• E��!�9! @8�>%���� ��6��7 #1�$�2� #include <stdio.h>• E�� FG'HI7 ! 8J@��8KI7����� !;��8 < >
���!9�7�!* !���� C
1.1.2 #define
• ; % 1�.!E#A�#�3�2;.%� A���� �� A�!@�!1�I�#��I:��
#define pi 3.14159- 1�.!E;.% idenfier 02� pi �3!3�2E%=7#A�#�3�2 3.14159
#define pi �3.14159�- �3!3�2E%=7#A�#�3�23�26�J! string q3.14156s I�A; A>�=6��
• ������ !"#$%& preprocessor ()*+,- ). ; !01&,�2������
���!9�7�!* !���� C
1.2 Declaration statement
• $A=!��� ����� �t>�=��� (GLOBAL Variable)• $A=!��� ����� �t����>I3�x
���!9�7�!* !���� C
2. GP!�HE(� (Function)
• #0�$A=!���#1�$�2�3�2"� ;.%#��:<=6>���31���!>A�� h
• ;!��� � �!��� �"E%=7���9(�! (statement) >A�� h
• ��������� �"E%=7��� � �!7A�7.��7 h ��� � �!
• �>AI7 !����� � �!.�� ��� � �!.!82�3�2 02�=A� GP!�HE(� main() �E73�2 ��31���!������� ��@�6�<2�@� ��� � �!!�96$��
���!9�7�!* !���� C
comment
• ;!������ $����H;$A#1��S<"�7���� �� (comment) "�<6=�I.! RIE%
• ��"6�>���#��6�!>�@�6�<2�>�9��>A6#�02��.��7 /*@!H8�6#�02��.��7 */
• �%�#=��;E h 3�2�75A��.=A��6#�02��.��7E�� �A�=@�I�AH5 ����E7#��I:6����
• .�0�H%�>%�� ��#��6�!>�3�9�"��3�E @�; %6#�02��.��7 // =��I=%.!%�"��3�E3�2>%�� ��;.%6�J!#��6�!>�
21
MeaningSymbol
Input/Output Symbol������������� ! ���"#$%&!��� !
Process Symbol�������+,��-.
Terminator Symbol��"#$%0,�"1�%��%34$5�67�0� 34$#68�#4$9:�"���
���KE79(R'(��)H flowchart �6�����*,8��������
Decision Symbol�������5:�<=�5><=�5%?7��@�1�?�0�$#6��3
Keyboard Symbol������������� !3��":�D6�D
Document Symbol��5�?7�0�%����F�4�1"#$%��� !��5+�?7�%D6�D
MeaningSymbol
External Subroutine Symbol��5�<=�I%����0�% K ><7 C Compiler #��%@-"!-
Display /Monitor Symbol��5�?7�0�%����F�4�1"#$%��� !��3�M�D
On-page Connector Symbol�������5�?7��9=%��1��5$<=-���
Off-page Connector Symbol�������5�?7��9=%@:1���?7�
Flowline Symbol��5:�34$5�?7��0��F1-�%#�S!��T.
MeaningSymbol
Comment Symbol��"#$%+-��51U� +,��V6��= 1�?���#�%5�00�% K
���KE79(R'(��)H flowchart �6�����*,8��������
• Examples:Terminator Symbol : KE7�95!I���;�@!* !XY5��Z��I7�
XY59Z[�9Y5�������
START
STOP
���KE79(R'(��)H flowchart �6�����*,8��������
• Examples:
Process Symbol : KE7K��������:)
���KE79(R'(��)H flowchart �6�����*,8��������
• Examples:Input/Output Symbol : KE7K�����(]*7 �J' ����95!^'*7 �J'
���KE79(R'(��)H flowchart �6�����*,8��������
• Examples:Display /Monitor Symbol : KE7���� I7 !�����]YK;7�95!*7 �J']�X ��`
Write sum
���KE79(R'(��)H flowchart �6�����*,8��������
• Examples:
• Document Symbol : KE7���� I7 !�����]YK;7�95!*7 �J']������ !`Z�`H
���KE79(R'(��)H flowchart �6�����*,8��������
• Examples:Decision Symbol : KE7K�������,8]�6,8]�!�� �F*;�� I(59Z�KX
���KE79(R'(��)H flowchart �6�����*,8��������
• Examples:External Subroutine Symbol : KE7��,8�GP!�HE(�I@�! a 6,� C Compiler 9�7�!F:7�'7:
���KE79(R'(��)H flowchart �6�����*,8��������
• Examples:External Subroutine Symbol
���KE79(R'(��)H flowchart �6�����*,8��������
• Examples:Keyboard Symbol : KE7K�����(]*7 �J'X����c�`Z�`HComment Symbol : KE7�95!�:���;d� ��� eZ]�8 ;�� *7 9(!��II@�! a
���KE79(R'(��)H flowchart �6�����*,8��������
• Examples:
Offpage Connector Symbol : KE7K�����E�� ��8!F�;�7� ���
���KE79(R'(��)H flowchart �6�����*,8��������
• Examples:Flowline Symbol : KE7��f�XY5�E�� �I@ ��;:@�!9(R'(��)HOn-page Connector Symbol : KE7K�����E�� ��8!K�;�7��5,8:�(�
No
Yes
���KE79(R'(��)H flowchart �6�����*,8��������
• I(: 8@�!6,� 1 ���KE79(R'(��)H flowchart �6�����*,8��������K�����(]�@� 3 �@�X���,8H] �H5 �'7:`Z�`H^']:�* !�'*6([! 3 �6,�X ��`
���KE79(R'(��)H flowchart �6�����*,8��������
• I(: 8@�!6,� 2 ��������:]�Y�8 5'J�;�,[ +i�!���� 'J��7�I7 !���+�[ 9Z��7�57:88 5�!Z�X���:�;�i�! �������I�:X9 ]8 5;�,[�!�7�! j7��:��(];�,[6,�X��`Z��K;�@�'7:�,�@��7 8�:@�:!�!Z�6,����;�5 (10,000 ]�6) Xi!8 �K;7�`Z��;�,[F57
���KE79(R'(��)H flowchart �6�����*,8��������
�]]mn�;(5 1
• X!�*,8�^(!!��K����9�Y�^'6,�e������;@!;�i�!X� �](I����5ZIK�:!�!Z� 100,000 ]�6 K;7�(]'J��7�* !e����� �I@�,�!�� �F*�� e�����X� �](I����5ZIK;7j7�'J��7�j7�
1. �,6�( 8H9�](IZF�@I����:@� 500,000 ]. ;��
2. �,��8F57F�@I����:@��5� �'� 50,000 ].
• �*,8� Flowchart �`�� ;�^'�:��'��@��p',�8* !*7 �J' �586������(]�@�F576Y��@�F�@:@�X���:�*7 �J'X���f��6@�F� � �X��X�K;7�@� -999 �������Xi!;8Y56��!��
�]]mn�;(5 2
• X!�*,8� flowchart K�����(]I(: (��� 1 I(:X���,8H] �H5 +i�!J7KE7X��c �I(: (��� A, B, C, D, ;�� F I(:K5I(:;�i�!�*7���
�6@��([� �'7: Z� H*7 �:��5(!I@ F��,[ �6,�X ��`
– j7� J7KE7�c � A �*7��� K;7�95! sVery Goodv
– j7� J7KE7�c � B �*7��� K;7�95! sGoodv
– j7� J7KE7�c � C �*7��� K;7�95! sSowSov
– j7� J7KE7�c � D �*7��� K;7�95! sPoorv
– j7� J7KE7�c � F �*7��� K;7�95! sFail !v
– j7� J7KE7�c � (���I(: ����*7��� K;7�95! sInvalid !v
�]]mn�;(5 3
• X!�*,8� flowchart K�����(]�@�X���:��Id� 1 X���:�
X���,8H] �H5 +i�!�,�@�I([!�I@ 1 ji! 10 �6@��([� j7��@�6,�
�(]�*7���F�@ 8J@K�`Z9(8 K;7 Z�`H*7 �:��:@� sInvalid
Number !v �'7:�������X�:�'J]X��:@�X�F57�@�6,�
I7 !��� �58���F���d]F:76,�I(:��� num ;'(!X���([�K;7
`Z�`H�@� num F�X�ji! 10
�]]mn�;(5 4
• @�6��7!4����!;.%45%; %;$A�%��5�$A=!>�= 3 �7A�� #0� 02�6:t ����y3�26 <E @� !�9!���� ��@��$E��%�#=��3� 3�745%; %>��6:t ���"� ��7V���45%; %�� ��
• >�=�7A��Input name : SuchadaInput sex (Male / Female) : FemaleInput year of birth : 2500-----------------------------Hello Miss. Suchadayou are 52 years old