38
Chapter 3 C FUNDAMENTALS Electrical Engineering Department

Chapter3 c Fundamentals

Embed Size (px)

DESCRIPTION

s

Citation preview

Page 1: Chapter3 c Fundamentals

Chapter 3C FUNDAMENTALS

Electrical Engineering Department

Page 2: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

Learning Objectives:

◊ Understand and implement the basic structure of computer programming.

EC201 Fundamental Programming

Page 3: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

Involves translating high-level language (programminglanguage such as C,C++, Java, PHP,Visual Basic, C#,etc.)

WHY? Because computers do NOT understand high level language!

Translated to

000111010101010111011111 EC201 Fundamental Programming

Program Development Environment

Page 4: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

Typical program development environment consist of six phases to be executed.

Edit

Preprocess

Compile

Link

Load

Execute EC201 Fundamental Programming

Program Development Environment

Page 5: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

Creating a ProgramPhase 1: Programmer types or creates program in an editor. Makes corrections if necessary. Saves or stores program on disk such as C:\ or A:\ etc. Editor

?Editor or text editor is a type of program used for editing plain text files.

EC201 Fundamental Programming

Program Development Environment

Page 6: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

Turbo C

editor(free)

EC201 Fundamental Programming

Program Development Environment

Page 7: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

Miracle C

editor(free)

EC201 Fundamental Programming

Program Development Environment

Page 8: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

PreprocessingPhase 2:

Programmer gives command to compile the program. Preprocessor program executes automatically and process

the program code. The prepocessor obeys commands from preprocessor

directives. Preprocessing occurs before a program is compiled.

EC201 Fundamental Programming

Program Development Environment

Page 9: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

Compiling a ProgramPhase 3: When compiled, compiler translates program into machine language code and creates object code. The object code will be stored in disk.

Dialog box in Turbo C

editor shows

compiling process.

Programmer click

Compile

EC201 Fundamental Programming

Program Development Environment

Page 10: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

Compiling a ProgramPhase 3:

The object code will be only created if the translation process into machine code is successful.

Otherwise, if unsuccessful, error messages will be displayedin the compiling dialogue box.

Programmer must fix all the errors before proceed to the next phase.

The process of correcting errors is called debugging.

EC201 Fundamental Programming

Program Development Environment

Page 11: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

LinkingPhase 4:

A 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 languageused.

EC201 Fundamental Programming

Program Development Environment

Page 12: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

LoadingPhase 5:

Before a program can be executed, the program must firstbe 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.

EC201 Fundamental Programming

Program Development Environment

Page 13: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

ExecutingPhase 6:

CPU takes each instructions and executes it. Results or output will be displayed.

EC201 Fundamental Programming

Program Development Environment

Page 14: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

Terms DescriptionMachine language Binary number codes understood by a

specific CPU.High-level language

Machine-independent programming language that combines algebraic expressions and English symbols.

Source file File containing a program written in a high-level language; the input for compiler

Compiler Software that translates a high-level language program into machine language.

Linker Software that combines object files and create an executable machine language program. EC201 Fundamental Programming

Program Development Environment

Page 15: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

Computer Program

HOW WHAT

UNDERSTAND THE SYNTAX AND

FORMAT STRUCTURE OF PROGRAMMING

EC201 Fundamental Programming

Program Development Environment

Page 16: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

C Basic Structure

C preprocessor directive

main function{ //Identifiers/Variables //C statements}

Program block components:

1. Preprocessor directive

2. Program body3. Main function4. Identifiers/

Variable5. C statements6. Comment

EC201 Fundamental Programming

Basic Syntax of Programming

Page 17: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

PROGRAMBLOCK

PREPROCESSOR

DIRECTIVEMAIN

FUNCTIONIDENTIFIERS/

VARIABLE

C STATEMENT

COMMENT

PROGRAMBODY

PICK AND MATCH

EC201 Fundamental Programming

Page 18: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

Preprocessor Directive

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.

EC201 Fundamental Programming

Basic Syntax of Programming

Page 19: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

Preprocessor Directive

#include <header file> or #include “user defined files”

Format:

Example#include <stdio.h>#include <conio.h>#include “jam.h”

EC201 Fundamental Programming

Basic Syntax of Programming

Page 20: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

Preprocessor DirectiveExample:

#include <stdio.h>

A directive to the C preprocessor Lines beginning with # are processed by the

preprocessorbefore the program is compiled.

The above code line tells the preprocessor to include thecontents of stdio.h ( standard input/output header)

Called from standard library

EC201 Fundamental Programming

Basic Syntax of Programming

Page 21: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

Standard Library

Consists of built-in functions

Functions contains standard instructions

Function will be called and linked to program via header file

List of header file and its functionFunction Header FileStandard input/output functions

<stdio.h>

Mathematical functions <math.h><stdlib.h><time.h>

String functions <string.h> <ctype.h>

EC201 Fundamental Programming

Basic Syntax of Programming

Page 22: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

Standard Library

List of header file and its functionHeader file List of functions

stdio.h printf(), scanf(),fflush(), dll

conio.h clrscr(),putch().getc().dll

math.h sqrt(),pow(), log(),dll

EC201 Fundamental Programming

Basic Syntax of Programming

Page 23: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

STANDARD INPUT/OUTPUT FUNCTIONS <stdio.h>

stdio.h

getchar()

gets()

scanf()

putchar()

puts()

printf()

INPUT

OUTPUT

EC201 Fundamental Programming

Page 24: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

STANDARD INPUT/OUTPUT FUNCTIONS (cont.)

Function Descriptionprintf() Prints all types of data to the screenputs() Prints string to the screen including newline

(‘\n’)int putchar(int c) Prints a character to the screenscanf() Inputs all type of data from user (up to a

spacing)gets() Inputs string from user (up to enter key). int getchar(void) Inputs a character from userfflush(stdin) During getting inputs from user, there might

be an extra character (such as ‘\n’) still exists in buffer from the previous input. If

the buffer is not empty, the next input reads the extra character and not the input from

user. Write fflush(stdin) to empty the input buffer before the next character input.

OU

TPU

TIN

TPU

T

EC201 Fundamental Programming

STANDARD INPUT/OUTPUT FUNCTIONS <stdio.h>

Page 25: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

MATHEMATICAL FUNCTIONS

math.h

stdlib.h

Mathematical

Functions

Mathematical

Functions

Mathematical

Functions

math.h

Mathematical

Functionsstdlib.h

math.h

Mathematical

Functions

EC201 Fundamental Programming

Page 26: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

MATHEMATICAL FUNCTIONS <math.h>

Function Descriptiontan (x) Tangent of x

(x in radians)sin (x) Sine of x

(x in radians)cos (x) Cosine of x

(x in radians)sqrt (x) Square root of x pow (x, y) x raised to power of y

(xy)abs (a) Absolute value of a

(for integer type only)fabs (x) Absolute value of x

(for floating type only)log (x) Natural algorithm of x (base e).

Returns ln x. (error occurs if x ≤ 0)

log10 (x) Logarithm of x (base 10) (error occurs if x ≤ 0)

MATHEMATICAL FUNCTIONS

EC201 Fundamental Programming

Page 27: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

STRING FUNCTIONS

string.h

ctype.h

StringFunctions

EC201 Fundamental Programming

Page 28: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

STRING FUNCTIONS <string.h>

Function Descriptionstrcat(s1,s2) Combines/concatenates string s2 to string s1.

strcpy(s1,s2) Copies string s2 into string s1.

strcmp(s1,s2) Compares the string s1 with the string s2.

If s1 is equal to s2, returns 0.If s1 is greater than s2, returns positive value.If s1 is less than s2, returns negative value.

strlen(str) Returns the length/size of str.

STRING FUNCTIONS <string.h>

EC201 Fundamental Programming

Page 29: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

STRING FUNCTIONS <ctype.h>

Function Descriptionisdigit(c) Returns a nonzero (true) if c is a digit (0-9), otherwise zero (false).

isalpha(c) Returns a nonzero (true) if c is a letter (A-Z, a-z), otherwise zero (false).

isupper(c) Returns a nonzero (true) if c is an uppercase letter (A-Z), otherwise zero (false).

islower(c) Returns a nonzero (true) if c is a lowercase letter (a-z), otherwise zero (false).

isspace(c) Returns a nonzero (true) if c is a whitespace character such as newline (‘\n’), space (‘ ’), or horizontal tab (‘\t’), otherwise zero (false).

toupper(c) If c is a lowercase letter, toupper() returns c as an uppercase letter. Otherwise, toupper() returns the same value unchanged.

tolower(c) If c is an uppercase letter, tolower() returns c as a lowercase letter. Otherwise, tolower() returns the same value unchanged.

EC201 Fundamental Programming

Page 30: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

User-definedLibrary

List of header file and its function

Contain functions defined by programmer.

Developed by expert programmers.

Header file List of user-defined functions

utama.h cetak(),baca(),papar(),dllkira.h plus(),minus(), divide(),dll

EC201 Fundamental Programming

Basic Syntax of Programming

Page 31: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

Preprocessor Directive

#define “file name” or #define constant_name constant_value

Format:

Example#define MAX 100#define “jam.h”

EC201 Fundamental Programming

Basic Syntax of Programming

Page 32: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

Program Body 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 }

Format:

EC201 Fundamental Programming

Basic Syntax of Programming

Page 33: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

Main function

int main( ){ return 0;} Main function

void main( ){ …………..}

main( ){ return 0;}

EC201 Fundamental Programming

Basic Syntax of Programming

Page 34: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009Principles of Programming - NI 2005

34

Function main• Identify the start of the program• Every C program has a main ( )• 'main' is a C keyword. We must not use it

for any other variable.• 4 common ways of main declaration

int main(void)

{

return 0;}

void main(void)

{

}

main(void)

{

}

main( )

{

}

Page 35: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

The curly braces { }

EC201 Fundamental Programming

•Identify a segment / body of a programThe start and end of a functionThe start and end of the selection or repetition block.

•Since the opening brace indicates the start of a segment with the closing brace indicating the end of a segment, there must be just as many opening braces as closing braces (this is a common mistake of beginners)

Page 36: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

Write the most basic structures of C programming.

#include <stdio.h>void main(){}

EC201 Fundamental Programming

Basic Syntax of Programming

Page 37: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

C StatementInstructions to be executed by computersEvery statements must be ended with semicolon

Types

Declarationstatement

Input/Output statement

Control statement

Functionstatement

EC201 Fundamental Programming

Basic Syntax of Programming

Page 38: Chapter3 c Fundamentals

Faculty of Information Technology and Multimedia, 2008/2009

CommentStatement in program code that will be ignored by compilerDiffers in terms of colour : grey or greenFormat use ; /*……………………….*/

//……………………………

Function

To documenta program

As a future

references

To provide additional

information

To increaseprogram

readability

EC201 Fundamental Programming

Basic Syntax of Programming