Your SlideShare is downloading. ×
0
Faculty of Mechanical and Manufacturing <br />Chapter 2<br />Basic Structure of Programming<br />Prepared by  Alish Ahmad ...
Learning Objectives:<br /><ul><li>Understand and implement the basic structure of computer programming.
Write a computer program using C programming language.
Convert algorithm into computer program.</li></li></ul><li>Program Development Environment<br />Basic Syntax of Programmin...
Involves translating high-level language (programming<br />language such as C,C++, Visual Basic, C#)<br />Because computer...
Program Development Environment<br />Typical program development environment consist of<br /> six phases to be executed.<b...
Program Development Environment<br />1. Creating a Program<br /><ul><li>Types or creates program in an editor
Makes corrections if necessary
Saves or stores program on disk </li></ul>	such as C: <br />      or A: etc.<br />Editor or text editor is a type of progr...
Program Development Environment<br />Example of text editor<br />Turbo C editor<br />(free)<br />
Program Development Environment<br />Example of text editor<br />DEV C++<br />(free)<br />
Program Development Environment<br />2. Preprocessing<br /><ul><li>Write a command to compile the program.
Preprocessor program executes automatically</li></ul>	and process the program code.<br /><ul><li>The preprocessor obeys co...
Program Development Environment<br />3.Compiling a Program<br /><ul><li>The object code will be only created if the transl...
The process of correcting errors is called debugging.</li></li></ul><li>Program Development Environment<br />4.Linking<br ...
A linker will creates an executable file and stores it on disk</li></ul>	if the program compiles and links correctly.<br /...
Program Development Environment<br />5. Loading<br /><ul><li>Before a program can be executed, the program must first</li>...
Program Development Environment<br />6. Executing<br /><ul><li>CPU takes each instructions and executes it.
Results or output will be displayed.</li></li></ul><li>Program Development Environment<br />
Program Development Environment<br />Common Programming Errors<br />Error<br />(bugs)<br />Run-time Errors<br />Syntax Err...
Program Development Environment<br />Common Programming Errors<br />Syntax Error<br />1<br />Error occurred during compila...
 Declaration syntax error
 Undefined symbol ‘_main’ in module.
 Statement missing in function main()</li></li></ul><li>Program Development Environment<br />Common Programming Errors<br ...
Not detected during compilation.</li></li></ul><li>Program Development Environment<br />Common Programming Errors<br />Whi...
Program Development Environment<br />Common Programming Errors<br />Run-time Error<br />3<br /><ul><li>Error occurred due ...
User’s mistake.
System would either display alert message   or hang.</li></li></ul><li>Program Development Environment<br />C Basic Struct...
Program Development Environment<br />Preprocessor Directive<br /><ul><li>Utility program which link files from compiler li...
Must be started with the symbol #, otherwise syntax errors</li></ul>	will be occurred.<br /><ul><li>Two types of common pr...
Program Development Environment<br />Preprocessor Directive<br />Format:<br />   #include  <header file> or #include “user...
Program Development Environment<br />Preprocessor Directive<br />Example:<br />Called from standard library<br />#include ...
Lines beginning with # are processed by the preprocessor</li></ul>	before the program is compiled.<br /><ul><li>The above ...
Program Development Environment<br /><ul><li>Consists of  built-in functions</li></ul>Standard <br />Library<br /><ul><li>...
Function will be called  and linked to program</li></ul>   via header file<br />
Program Development Environment<br /><ul><li> Contain functions defined by programmer.</li></ul>User-defined<br />Library<...
Program Development Environment<br />Preprocessor Directive<br />Format:<br /> #define “file name” or #define constant_nam...
Program Development Environment<br />Program body<br /><ul><li>The part in which the program code will be started to</li><...
Use { to start the program code and } to end the </li></ul>	program code.<br />Format :<br />   main function<br />   {  /...
Basic Syntax of Programming<br />Main Function<br />void main( )<br />{ …………..}<br />Main function<br />int main( )<br />{...
Program Development Environment<br />Write the most basic structures of <br />C programming.<br />#include <stdio.h><br />...
Program Development Environment<br />C Statement<br /><ul><li>Instructions to be executed by computers
Every statements must be ended with semicolon</li></ul>Declaration<br />statement<br />Function<br />statement<br />Types<...
Program Development Environment<br />Comment<br /><ul><li>Statement in program code that will be ignored by compiler
Differs in terms of colour : grey</li></ul>To increase<br />program readability<br />To document<br />a program<br />Funct...
Program Development Environment<br />Using references from any C book, find and study<br />the following concepts (definit...
Program Development Environment<br />Reserved Word<br /><ul><li>Standard/special word in standard library
Contain special meaning understood by compiler</li></ul>Rules<br />Case –sensitive<br />Must be written in<br />small case...
Program Development Environment<br />Reserved Word<br />Example:<br />int<br />The acronym for integer<br />void<br />Refe...
Program Development Environment<br />Identifier<br /><ul><li>Representing particular name in programming
Store values to be used in programming
Refers to the storage in computer</li></ul>Type<br />Standard <br />identifier<br />User-defined<br />identifier<br />
Program Development Environment<br />Identifier<br /><ul><li>Special built-in words
Referred asfunction name</li></ul>  which will called from C library<br />Standard<br />identifier<br />printf()   scanf()...
Program Development Environment<br />Identifier<br /><ul><li>Name given to the declaration of</li></ul>  data to be used i...
Store data values/result/output</li></ul>User-defined<br />identifier<br />Type<br />Constant<br />Variable<br />
Program Development Environment<br />Identifier<br />User-defined identifier<br />RULES<br /><ul><li>Identifiers name can ...
Identifiers name cannot be started with numbers
Symbol cannot be used in identifier name
Cannot contains spaces between two identifiers name
Upcoming SlideShare
Loading in...5
×

Chap 2 structure of c programming dti2143

1,978

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,978
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
129
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Chap 2 structure of c programming dti2143"

  1. 1. Faculty of Mechanical and Manufacturing <br />Chapter 2<br />Basic Structure of Programming<br />Prepared by Alish Ahmad Al-shahab<br />alishahmad.alshahab@gmail.com<br />
  2. 2. Learning Objectives:<br /><ul><li>Understand and implement the basic structure of computer programming.
  3. 3. Write a computer program using C programming language.
  4. 4. Convert algorithm into computer program.</li></li></ul><li>Program Development Environment<br />Basic Syntax of Programming<br />CHAPTER 2<br />Data Types<br />Variable Declarations<br />
  5. 5. Involves translating high-level language (programming<br />language such as C,C++, Visual Basic, C#)<br />Because computers do NOT <br />understand high level language!<br />Translated to<br />Computer's machine language<br />00011101<br />01010101<br />11011111<br />
  6. 6. Program Development Environment<br />Typical program development environment consist of<br /> six phases to be executed.<br />Edit/Write<br />Preprocess<br />Compile<br />Link<br />Load<br />Execute<br />
  7. 7. Program Development Environment<br />1. Creating a Program<br /><ul><li>Types or creates program in an editor
  8. 8. Makes corrections if necessary
  9. 9. Saves or stores program on disk </li></ul> such as C: <br /> or A: etc.<br />Editor or text editor is a type of program used for editing plain text files. E.gdev/turbo<br />
  10. 10. Program Development Environment<br />Example of text editor<br />Turbo C editor<br />(free)<br />
  11. 11. Program Development Environment<br />Example of text editor<br />DEV C++<br />(free)<br />
  12. 12. Program Development Environment<br />2. Preprocessing<br /><ul><li>Write a command to compile the program.
  13. 13. Preprocessor program executes automatically</li></ul> and process the program code.<br /><ul><li>The preprocessor obeys commands from </li></ul> preprocessor directives.<br /><ul><li>Preprocessing occurs before a program is compiled.</li></li></ul><li>Program Development Environment<br />3.Compiling a Program<br /><ul><li>When compiled, compiler translates program into machine</li></ul> language code and creates object code.<br /><ul><li>The object code will be stored in disk.</li></ul>Select <br />Compile<br />Dialog box in Turbo C editor shows compiling proccess.<br />
  14. 14. Program Development Environment<br />3.Compiling a Program<br /><ul><li>The object code will be only created if the translation </li></ul> process into machine code is successful.<br /><ul><li>If unsuccessful, error messages will be displayed</li></ul> in the compiling dialogue box.<br /><ul><li>fix all the errors before proceed to the next phase.
  15. 15. The process of correcting errors is called debugging.</li></li></ul><li>Program Development Environment<br />4.Linking<br /><ul><li>A linker links the object code with the libraries.
  16. 16. A linker will creates an executable file and stores it on disk</li></ul> if the program compiles and links correctly.<br /><ul><li>A linker might name the executable file with .exe file </li></ul> extension depending on type of programming language<br /> used.<br />
  17. 17. Program Development Environment<br />5. Loading<br /><ul><li>Before a program can be executed, the program must first</li></ul> be placed in memory.<br /><ul><li>Loader takes the stored program from disk and puts in </li></ul> memory.<br /><ul><li>Additional components from shared libraries that support </li></ul> the program are also loaded.<br />
  18. 18. Program Development Environment<br />6. Executing<br /><ul><li>CPU takes each instructions and executes it.
  19. 19. Results or output will be displayed.</li></li></ul><li>Program Development Environment<br />
  20. 20. Program Development Environment<br />Common Programming Errors<br />Error<br />(bugs)<br />Run-time Errors<br />Syntax Errors<br />Logic Errors<br />
  21. 21. Program Development Environment<br />Common Programming Errors<br />Syntax Error<br />1<br />Error occurred during compilation<br />normally due to syntax problem<br />Example:<br /><ul><li> Misplaced else.
  22. 22. Declaration syntax error
  23. 23. Undefined symbol ‘_main’ in module.
  24. 24. Statement missing in function main()</li></li></ul><li>Program Development Environment<br />Common Programming Errors<br />Logic Error<br />2<br /><ul><li>Error occurred due to inappropriate</li></ul> output.<br /><ul><li>Programming mistake.
  25. 25. Not detected during compilation.</li></li></ul><li>Program Development Environment<br />Common Programming Errors<br />Which ONE?<br />number pin or pin number?<br />
  26. 26. Program Development Environment<br />Common Programming Errors<br />Run-time Error<br />3<br /><ul><li>Error occurred due to wrong user input.
  27. 27. User’s mistake.
  28. 28. System would either display alert message or hang.</li></li></ul><li>Program Development Environment<br />C Basic Structure<br />Program block components:<br />Preprocessor directive<br />Program body<br />Main function<br />Identifiers/Variable<br />C statements<br />Comment<br />C preprocessor directive<br />main function<br />{<br /> //Identifiers/Variables<br /> //C statements<br />}<br />
  29. 29. Program Development Environment<br />Preprocessor Directive<br /><ul><li>Utility program which link files from compiler library to the</li></ul> program code.<br /><ul><li>Must be included in the first line of a computer program.
  30. 30. Must be started with the symbol #, otherwise syntax errors</li></ul> will be occurred.<br /><ul><li>Two types of common preprocessor directive:#include and</li></ul>#define.<br />
  31. 31. Program Development Environment<br />Preprocessor Directive<br />Format:<br /> #include <header file> or #include “user defined files”<br />Example<br />#include <stdio.h><br />#include <conio.h><br />#include “jam.h”<br />
  32. 32. Program Development Environment<br />Preprocessor Directive<br />Example:<br />Called from standard library<br />#include <stdio.h><br /><ul><li>A directive to the C preprocessor
  33. 33. Lines beginning with # are processed by the preprocessor</li></ul> before the program is compiled.<br /><ul><li>The above code line tells the preprocessor to include the</li></ul> contents of stdio.h ( standard input/output header)<br />
  34. 34. Program Development Environment<br /><ul><li>Consists of built-in functions</li></ul>Standard <br />Library<br /><ul><li>Functions contains standard instructions
  35. 35. Function will be called and linked to program</li></ul> via header file<br />
  36. 36. Program Development Environment<br /><ul><li> Contain functions defined by programmer.</li></ul>User-defined<br />Library<br /><ul><li> Developed by expert programmers.</li></ul>List of header file and its function<br />
  37. 37. Program Development Environment<br />Preprocessor Directive<br />Format:<br /> #define “file name” or #define constant_nameconstant_value<br />Example<br />#define MAX 100<br />
  38. 38. Program Development Environment<br />Program body<br /><ul><li>The part in which the program code will be started to</li></ul> execute.<br /><ul><li>Consists of main function, C statements and identifiers.
  39. 39. Use { to start the program code and } to end the </li></ul> program code.<br />Format :<br /> main function<br /> { //identifiers<br /> //C statements }<br />
  40. 40. Basic Syntax of Programming<br />Main Function<br />void main( )<br />{ …………..}<br />Main function<br />int main( )<br />{ <br /> return 0;<br />}<br />main( )<br />{ <br /> return 0;<br />}<br />
  41. 41. Program Development Environment<br />Write the most basic structures of <br />C programming.<br />#include <stdio.h><br />void main()<br />{<br />}<br />
  42. 42. Program Development Environment<br />C Statement<br /><ul><li>Instructions to be executed by computers
  43. 43. Every statements must be ended with semicolon</li></ul>Declaration<br />statement<br />Function<br />statement<br />Types<br />Control statement<br />Input/Output statement<br />Compound statement<br />
  44. 44. Program Development Environment<br />Comment<br /><ul><li>Statement in program code that will be ignored by compiler
  45. 45. Differs in terms of colour : grey</li></ul>To increase<br />program readability<br />To document<br />a program<br />Function<br />As a future<br />references<br />To provide additional<br />information<br />
  46. 46. Program Development Environment<br />Using references from any C book, find and study<br />the following concepts (definition and examples): <br /> Reserved Word<br /> Variable<br /> Constant<br />
  47. 47. Program Development Environment<br />Reserved Word<br /><ul><li>Standard/special word in standard library
  48. 48. Contain special meaning understood by compiler</li></ul>Rules<br />Case –sensitive<br />Must be written in<br />small case<br />Cannot be used as identifier<br />or variables<br />
  49. 49. Program Development Environment<br />Reserved Word<br />Example:<br />int<br />The acronym for integer<br />void<br />Refer to the function that will not return any value<br />case default switch break<br />for continue float double<br />return while if do int <br />
  50. 50. Program Development Environment<br />Identifier<br /><ul><li>Representing particular name in programming
  51. 51. Store values to be used in programming
  52. 52. Refers to the storage in computer</li></ul>Type<br />Standard <br />identifier<br />User-defined<br />identifier<br />
  53. 53. Program Development Environment<br />Identifier<br /><ul><li>Special built-in words
  54. 54. Referred asfunction name</li></ul> which will called from C library<br />Standard<br />identifier<br />printf() scanf()<br />puts() gets()<br />
  55. 55. Program Development Environment<br />Identifier<br /><ul><li>Name given to the declaration of</li></ul> data to be used in program <br /><ul><li>Refer to the storage name
  56. 56. Store data values/result/output</li></ul>User-defined<br />identifier<br />Type<br />Constant<br />Variable<br />
  57. 57. Program Development Environment<br />Identifier<br />User-defined identifier<br />RULES<br /><ul><li>Identifiers name can only consists of name, number and underscore
  58. 58. Identifiers name cannot be started with numbers
  59. 59. Symbol cannot be used in identifier name
  60. 60. Cannot contains spaces between two identifiers name
  61. 61. Identifiers name should be unique
  62. 62. Identifiers is not case sensitive</li></li></ul><li>Basic Syntax of Programming<br />Example:<br />Identifier<br />Valid identifiers<br />UThM DIT1064 Seven_eleven integer<br />WHY?<br />WHY?<br />Invalid identifiers<br />8Century BIT 1033 Two*four <br /> ‘Sixsense’ void<br />WHY?<br />WHY?<br />WHY?<br />
  63. 63. Program Development Environment<br />Identifier<br /><ul><li>Name which used to store data value
  64. 64. Refer to name of one cell in computer </li></ul> storage<br /><ul><li>Contants value is fixed</li></ul>Constant<br />How to give name to a constant value?<br />Follow identifiers rules<br />
  65. 65. Program Development Environment<br />1<br />Declaration format:<br />const data_type const_name = const_value;<br />Data type<br />const float pi = 3.142;<br />Reserved word<br />Constant <br />Value<br />Constant name<br />
  66. 66. Program Development Environment<br />2<br />Declaration format:<br />#define const_name const_value;<br />Constant value<br />#define pi 3.142;<br />Reserved word<br />Constant name<br />
  67. 67. Program Development Environment<br />Example of constant:<br />#define minimum 0;<br />#define MAX 100;<br />const int counter = 100;<br />const char alphabet = ‘J’;<br />const float value = 4.5;<br />
  68. 68. Program Development Environment<br />Identifier<br /><ul><li>Name which used to store data/input </li></ul> value<br /><ul><li>Refer to the name of one cell in computer</li></ul> storage<br /><ul><li>Variable’s value can be modified/changed</li></ul> during execution<br />Variable<br />Declaration Format:<br />data_type variable_name;<br />
  69. 69. Program Development Environment<br />Declaration Example<br />Declaration of a variable number of integer data type.<br />int number;<br />float weight;<br />char alphabet;<br />Declaration of a variable weight of<br />floating point data type.<br />Declaration of a variable alphabet <br />of character data type.<br />
  70. 70. Program Development Environment<br />Variable/constant declaration example<br />//Variable and constant declration<br />#include <stdio.h><br />int number;<br />float weight;<br />void main()<br />{ <br /> const float pi =3.142;<br /> int bilangan;<br /> float berat;<br /> char abjad;<br /> }<br />Variable declaration<br />Constant declaration<br />Variable declaration<br />
  71. 71. Program Development Environment<br />Variable and constant declaration example:<br />//Variable and constant declaration<br />#include <stdio.h><br />const float pi=3.142;<br />void main()<br />{ <br /> int bilangan, bil, bilang;<br /> float berat, kg;<br /> char A;<br /> }<br />
  72. 72. Program Development Environment<br />Method to give/assign value to variable<br />Initialization<br />Interactive<br /><ul><li>Input/enter data </li></ul> through input devices<br /><ul><li> Use input statement </li></ul> (scanf/gets)<br /><ul><li>Assign value to variable </li></ul> during declaration.<br /><ul><li>Assign value to variable.
  73. 73. Use assign symbol, “=“</li></li></ul><li>Program Development Environment<br />Example:<br />Assigning value to variables<br />#include <stdio.h><br />void main()<br />{ <br /> int number = 10;<br /> float weight;<br /> weight = 60.00;<br /> printf(“Enter the value of number :”);<br /> scanf(“%d”,&number);<br /> number = 50.00;<br />}<br />Initialize a variable<br />Interactive<br />Initialize a variable<br />
  74. 74. Program Development Environment<br />Data types<br /><ul><li>Represents types of data can be stored in computer
  75. 75. Types of data to be stored and used in </li></ul> programming should be<br /> informed to the compiler/system <br />Types<br />Integer<br />Character<br />Floating<br />point<br />
  76. 76. Program Development Environment<br />Data types<br /><ul><li>Represents any round number with +/- </li></ul> values.<br /><ul><li>Divided into short and long integer.
  77. 77. Reserved word for integer – int
  78. 78. Valid until 5 places of integer number.</li></ul>Integer<br />Example:<br />age is used to represent the age of students between 18 and 25 years old. The declaration for the<br />variable is as follow:int age;<br />
  79. 79. Program Development Environment<br />Data types<br />Floating <br />number<br /><ul><li>Represents any floating point numbers +/-
  80. 80. Reserved word– double /float</li></ul>Example:<br />height is used to represent the student’s height between 150 cm and 180 cm. The declaration for the<br />variable is as follow:<br />float height;<br />
  81. 81. Program Development Environment<br />Data types<br /><ul><li>Represents character data.
  82. 82. Reserved word – char</li></ul>Character<br />Example:<br />gender is used to represent the gender of a student. The declaration for the variable is as follow:<br />char gender;<br />
  83. 83. Program Development Environment<br />Exercise:<br />Determine whether the following identifiers is valid or invalid. Give reason for invalid cases.<br />1)Parit Raja <br /> 2) 20thCentury <br /> 3) int<br /> 4) INTEGER<br />5) _BMW2003<br /> 6) Reservedword<br /> 7) BIT1033<br /> 8) markah_pelajar<br /> 9) jam*kredit<br /> 10) printf<br />
  84. 84. Program Development Environment<br />Exercise:<br />Write a suitable variable declaration for each of the following statement:<br /> i. Salary of an employee<br /> ii. Student’s mark for programming subject<br /> iii. ATM pin number<br /> iv. Phone number<br /> v. Price of one item<br /> vi. Bus seat number<br /> vii. Student name<br />
  85. 85. Program Development Environment<br />Exercise:<br />Based on the following problem, determine the appropriate<br /> variables can be declared:<br />Given the value of x is 10 and a is 12, <br />find the result of the following equation:<br />y = 2x + a - 6<br />
  86. 86. Program Development Environment<br />Exercise:<br /> Based on the following problem, determine the appropriate<br /> variables can be declared:<br />MrsLeeya needs to determine her students grade for <br />programming subject based on the mark scored during<br />final examination. The ‘A’ grade will be given if the <br />Mark scored is between 85 to 100. If a student has scored<br />90 marks, what is the grade should MrsLeeya<br />give to the student?<br />
  87. 87. Program Development Environment<br />Exercise:<br />Based on the following problem, determine the appropriate<br /> variables can be declared:<br />A box has height, width and length. <br />Calculate the volume of a box.<br />
  88. 88. Program Development Environment<br />Exercise:<br />Based on the following problem, determine the appropriate<br /> variables can be declared:<br />Uncle Degawan wants to buy 5 tins of paint from <br />Cinda’s shop. The price of each tin of the paint is <br />RM 15.60. Calculate the price which Uncle Degawan<br />have<br />to pay for all the tin of paints he bought.<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×