Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Chapter 2


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Chapter 2

  1. 1. Faculty of Mechanical and ManufacturingPrepared by Alish Ahmad
  2. 2. ◊ Understand and implement the basic structure of computer programming.◊ Write a computer program using C programming language.◊ Convert algorithm into computer program.
  3. 3. Program Basic SyntaxDevelopment of ProgrammingEnvironment CHAPTER 2 Data Types VariableDeclarations
  4. 4. Involves translating high-level language (programminglanguage such as C,C++, Visual Basic, C#) Because computers do NOT understand high level language! 00011101 01010101 11011111
  5. 5. Edit/Write Preprocess Compile Link Load Execute
  6. 6. o Types or creates program in an editor Editor or texto Makes corrections if necessary editor is a type ofo Saves or stores program on disk program used such as C: for editing or A: etc. plain text files. E.g dev/turbo
  7. 7. Turbo C editor (free)
  8. 8. DEV C++(free)
  9. 9. SelectCompile
  10. 10. Terms DescriptionMachine language Binary number codes understood by a specific CPU.High-level Machine-independent programminglanguage language that combines algebraic expressions and English symbols.Source file File containing a program written in a high- level language; the input for compilerCompiler Software that translates a high-level language program into machine language.Linker Software that combines object files and create an executable machine language program.
  11. 11. 1 Syntax Error Error occurred during compilation normally due to syntax problem Misplaced else. Declaration syntax error Undefined symbol „_main‟ in module. Statement missing in function main()
  12. 12. 2 Logic Error
  13. 13. 3 Run-time Error
  14. 14. C preprocessor directive Program block components:main function 1. Preprocessor{ directive //Identifiers/Variables 2. Program body //C statements 3. Main function} 4. Identifiers/Variable 5. C statements 6. Comment
  15. 15.  Utility program which link files from compiler library to the program code. Must be included in the first line of a computer program. Must be started with the symbol #, otherwise syntax errors will be occurred. Two types of common preprocessor directive: #include and #define.
  16. 16. #include <header file> or #include “user defined files” Example #include <stdio.h> #include <conio.h> #include “jam.h”
  17. 17. Called from#include <stdio.h> standard library
  18. 18. Consists of built-in functions Functions contains standard instructions Function will be called and linked to program via header file Header file List of functionsstdio.h printf(), scanf(),fflush(), dllconio.h clrscr(),putch().getc().dllmath.h sqrt(),pow(), log(),dll
  19. 19.  Contain functions defined by programmer.  Developed by expert programmers. List of header file and its Header file List of user-defined function functionsutama.h cetak(),baca(),papar(),dllkira.h plus(),minus(), divide(),dll
  20. 20. Format:#define “file name” or #define constant_name constant_value Example #define MAX 100
  21. 21.  The part in which the program code will be started to execute. Consists of main function, C statements and identifiers. Use { to start the program code and } to end the program code. main function { //identifiers //C statements }
  22. 22. void main( ) { …………..}int main( ) main( ){ { return 0; return 0;} }
  23. 23. Write the most basic structures of C programming. #include <stdio.h> void main() { }
  24. 24. Instructions to be executed by computersEvery statements must be ended with semicolon Function Declaration statement statement Control Types Input/Output statement statement Compound statement
  25. 25. Statement in program code that will be ignored by compilerDiffers in terms of colour : grey To increase To program document readability a program Function As a To provide future additional referen information ces
  26. 26. Using references from any C book, finthe following concepts (definition an A) Reserved Word B) Variable C) Constant
  27. 27. Standard/special word in standard libraryContain special meaning understood by compiler Rules Case –sensitive Cannot be used as ide Must be written in small case or variables
  28. 28. Example: void intRefer to the function that will not The acronym for integer return any value case default switch break for continue float double return while if do int
  29. 29. Representing particular name in programmingStore values to be used in programmingRefers to the storage in computer
  30. 30. Standard Special built-in wordsidentifier Referred as function name which will called from C library printf() scanf() puts() gets()
  31. 31. Name given to the declaration ofUser-defined data to be used in program Refer to the storage name identifier Store data values/result/output Constant Type Variable
  32. 32. RULESIdentifiers name can only consists of name, numberand underscoreIdentifiers name cannot be started with numbersSymbol cannot be used in identifier nameCannot contains spaces between two identifiersnameIdentifiers name should be uniqueIdentifiers is not case sensitive
  33. 33. WHY? Invalid identifiers 8Century BIT 1033 Two*four ‘Sixsense’ voidWHY? WHY? WHY?
  34. 34. Name which used to store data value Refer to name of one cell in computer storage Contants value is fixedHow to give name to a Follow identifiers rules constant value?
  35. 35. Declaration format: 1const data_type const_name = const_value; ConstantReserved word const float pi = 3.142; Value
  36. 36. Declaration format: 2#define const_name const_value; #define pi 3.142; Reserved word
  37. 37. Example of constant: #define minimum 0; #define MAX 100; const int counter = 100; const char alphabet = ‘J’; const float value = 4.5;
  38. 38. Name which used to store data/input value Refer to the name of one cell in computer storage Variable’s value can be modified/changed during executionDeclaration Format: data_type variable_name;
  39. 39. Declaration Example Declaration of a variable number of integer data type. Declaration of a variable weight of floating point data type. Declaration of a variable alphabet of character data type.
  40. 40. Variable/constant declaration example //Variable and constant declration #include <stdio.h> int number; float weight; Variable declaration void main() { const float pi =3.142; Constant declaration int bilangan; float berat; Variable declaration char abjad; }
  41. 41. //Variable and constant declaration#include <stdio.h>const float pi=3.142;void main(){ int bilangan, bil, bilang; float berat, kg; char A; }
  42. 42. Method to give/assign value to variable Interactive Initialization  Assign value to variable during declaration. Input/enter data through input devices  Assign value to variable. Use input statement  Use assign symbol, “=“ (scanf/gets)
  43. 43. Assigning value to variables#include <stdio.h>void main(){ Initialize a variable int number = 10; float weight; weight = 60.00; Interactive printf(“Enter the value of number :”); scanf(“%d”,&number); number = 50.00; Initialize a variable }
  44. 44. Represents types of data can be stored in computerTypes of data to be stored and used in programming should be informed to the compiler/system Types Integer Character Floating point
  45. 45. Represents any round number with +/- values.Integer Divided into short and long integer. Reserved word for integer – int Valid until 5 places of integer number. Example:age is used to represent the age of studentsbetween 18 and 25 years old. The declarationfor thevariable is as follow: int age;
  46. 46. Floating Represents any floating point numbers +/- number Reserved word– double /float Example: height is used to represent the student’s height between 150 cm and 180 cm. The declaration for the variable is as follow: float height;
  47. 47. Character Represents character data. Reserved word – char Example: gender is used to represent the gender of a student. The declaration for the variable is as follow: char gender;
  48. 48. Determine whether the following identifiers isvalid or invalid. Give reason for invalid cases.1) Parit Raja2) 20thCentury3) int4) INTEGER5) _BMW20036) Reservedword7) BIT10338) markah_pelajar9) jam*kredit10) printf
  49. 49. Write a suitable variable declaration for eachof the following statement:i. Salary of an employeeii. Student’s mark for programming subjectiii. ATM pin numberiv. Phone number v. Price of one item vi. Bus seat number vii. Student name
  50. 50. Based on the following problem, determine theappropriatevariables can be declared:Given the value of x is 10 and a is 12,find the result of the following equation: y = 2x + a - 6
  51. 51. Based on the following problem, determine the appropriate variables can be declared:Mrs Leeya needs to determine her students grade forprogramming subject based on the mark scored duringfinal examination. The ‘A’ grade will be given if theMark scored is between 85 to 100. If a student has scored90 marks, what is the grade should Mrs Leeyagive to the student?
  52. 52. Based on the following problem, determine the appropriatevariables can be declared:A box has height, width and length.Calculate the volume of a box.
  53. 53. Based on the following problem, determine the appropriate variables can be declared:Uncle Degawan wants to buy 5 tins of paint fromCinda’s shop. The price of each tin of the paint isRM 15.60. Calculate the price which Uncle Degawanhaveto pay for all the tin of paints he bought.