Faculty of Mechanical and Manufacturing Chapter 2Basic Structure of ProgrammingPrepared by Alish Ahmad Al-shahabalishahmad.alshahab@gmail.com
Learning Objectives:Understand and implement the basic structure of computer programming.
Write a computer program using C programming language.
Convert algorithm into computer program.Program Development EnvironmentBasic Syntax of ProgrammingCHAPTER 2Data TypesVariable Declarations
Involves translating high-level language (programminglanguage such as C,C++, Visual Basic, C#)Because computers do NOT understand high level language!Translated toComputer's machine language000111010101010111011111
Program Development EnvironmentTypical program development environment consist of six phases to be executed.Edit/WritePreprocessCompileLinkLoadExecute
Program Development Environment1. Creating a ProgramTypes or creates program in an editor
Makes corrections if necessary
Saves or stores program on disk	such as C:\ or A:\ etc.Editor or text editor is a type of program used for editing plain text files. E.gdev/turbo
Program Development EnvironmentExample of text editorTurbo C editor(free)
Program Development EnvironmentExample of text editorDEV C++(free)
Program Development Environment2. PreprocessingWrite a command to compile the program.
Preprocessor program executes automatically	and process the program code.The preprocessor obeys commands from	preprocessor directives.Preprocessing occurs before a program is compiled.Program Development Environment3.Compiling a ProgramWhen compiled, compiler translates program into machine language code and creates object code.The object code will be stored in disk.Select CompileDialog box in Turbo C editor shows compiling proccess.
Program Development Environment3.Compiling a ProgramThe object code will be only created if the translation	process into machine code is successful.If unsuccessful, error messages will be displayed	in the compiling dialogue box.fix all the errors before proceed to the next phase.
The process of correcting errors is called debugging.Program Development Environment4.LinkingA linker links the object code with the libraries.
A linker will creates an executable file and stores it on disk	if the program compiles and links correctly.A linker might name the executable file with .exe file	extension depending on type of programming language	used.
Program Development Environment5. LoadingBefore a program can be executed, the program must first	be placed in memory.Loader takes the stored program from disk and puts in	memory.Additional components from shared libraries that support	the program are also loaded.
Program Development Environment6. ExecutingCPU takes each instructions and executes it.
Results or output will be displayed.Program Development Environment
Program Development EnvironmentCommon Programming ErrorsError(bugs)Run-time ErrorsSyntax ErrorsLogic Errors
Program Development EnvironmentCommon Programming ErrorsSyntax Error1Error occurred during compilationnormally due to syntax problemExample: Misplaced else.
 Declaration syntax error
 Undefined symbol ‘_main’ in module.
 Statement missing in function main()Program Development EnvironmentCommon Programming ErrorsLogic Error2Error occurred due to inappropriate output.Programming mistake.
Not detected during compilation.Program Development EnvironmentCommon Programming ErrorsWhich ONE?number pin or pin number?
Program Development EnvironmentCommon Programming ErrorsRun-time Error3Error occurred due to wrong user input.
User’s mistake.
System would either display alert message or hang.Program Development EnvironmentC Basic StructureProgram block components:Preprocessor directiveProgram bodyMain functionIdentifiers/VariableC statementsCommentC preprocessor directivemain function{ //Identifiers/Variables //C statements}
Program Development EnvironmentPreprocessor DirectiveUtility 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.
Program Development EnvironmentPreprocessor DirectiveFormat: #include <header file> or #include “user defined files”Example#include <stdio.h>#include <conio.h>#include “jam.h”
Program Development EnvironmentPreprocessor DirectiveExample:Called from standard library#include <stdio.h>A directive to the C preprocessor
Lines beginning with # are processed by the preprocessor	before the program is compiled.The above code line tells the preprocessor to include the	contents of stdio.h ( standard input/output header)
Program Development EnvironmentConsists of built-in functionsStandard LibraryFunctions contains standard instructions
Function will be called and linked to program via header file
Program Development Environment Contain functions defined by programmer.User-definedLibrary Developed by expert programmers.List of header file and its function
Program Development EnvironmentPreprocessor DirectiveFormat: #define “file name” or #define constant_nameconstant_valueExample#define MAX 100
Program Development EnvironmentProgram bodyThe 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.Format : main function { //identifiers //C statements }
Basic Syntax of ProgrammingMain Functionvoid main( ){ …………..}Main functionint main( ){ return 0;}main( ){ return 0;}
Program Development EnvironmentWrite the most basic structures of C programming.#include <stdio.h>void main(){}
Program Development EnvironmentC StatementInstructions to be executed by computers
Every statements must be ended with semicolonDeclarationstatementFunctionstatementTypesControl statementInput/Output statementCompound statement
Program Development EnvironmentCommentStatement in program code that will be ignored by compiler
Differs in terms of colour : greyTo increaseprogram readabilityTo documenta programFunctionAs a futurereferencesTo provide additionalinformation
Program Development EnvironmentUsing references from any C book, find and studythe following concepts (definition and examples): Reserved Word Variable Constant
Program Development EnvironmentReserved WordStandard/special word in standard library
Contain special meaning understood by compilerRulesCase –sensitiveMust be written insmall caseCannot be used as identifieror variables
Program Development EnvironmentReserved WordExample:intThe acronym for integervoidRefer to the function that will not return any valuecase default switch breakfor continue float doublereturn while if do int
Program Development EnvironmentIdentifierRepresenting particular name in programming
Store values to be used in programming
Refers to the storage in computerTypeStandard identifierUser-definedidentifier
Program Development EnvironmentIdentifierSpecial built-in words
Referred asfunction name which will called from C libraryStandardidentifierprintf() scanf()puts() gets()
Program Development EnvironmentIdentifierName given to the declaration of data to be used in program Refer to the storage name
Store data values/result/outputUser-definedidentifierTypeConstantVariable
Program Development EnvironmentIdentifierUser-defined identifierRULESIdentifiers name can only consists of name, number and underscore
Identifiers name cannot be started with numbers
Symbol cannot be used in identifier name
Cannot contains spaces between two identifiers name

Chap 2 structure of c programming dti2143