You are on page 1of 38

Chapter 3

C FUNDAMENTALS
Electrical Engineering Department

Faculty of Information Technology and Multimedia, 2008/2009
Learning Objectives:
Understand and implement the basic
structure of computer programming.


EC201 Fundamental Programming
Faculty of Information Technology and Multimedia, 2008/2009
Involves translating high-level language (programming
language such as C,C++, Java, PHP,Visual Basic, C#,etc.)
WHY? Because computers do NOT
understand high level language!
Translated to
00011101 01010101 11011111
EC201 Fundamental Programming
Program Development Environment
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
Faculty of Information Technology and Multimedia, 2008/2009
Creating a Program
Phase 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
Faculty of Information Technology and Multimedia, 2008/2009
Turbo C
editor
(free)
EC201 Fundamental Programming
Program Development Environment
Faculty of Information Technology and Multimedia, 2008/2009
Miracle
C editor
(free)
EC201 Fundamental Programming
Program Development Environment
Faculty of Information Technology and Multimedia, 2008/2009
Preprocessing
Phase 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
Faculty of Information Technology and Multimedia, 2008/2009
Compiling a Program
Phase 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
Faculty of Information Technology and Multimedia, 2008/2009
Compiling a Program
Phase 3:

The object code will be only created if the translation
process into machine code is successful.
Otherwise, if unsuccessful, error messages will be displayed
in 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
Faculty of Information Technology and Multimedia, 2008/2009
Linking
Phase 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 language
used.
EC201 Fundamental Programming
Program Development Environment
Faculty of Information Technology and Multimedia, 2008/2009
Loading
Phase 5:

Before 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.
EC201 Fundamental Programming
Program Development Environment
Faculty of Information Technology and Multimedia, 2008/2009
Executing
Phase 6:

CPU takes each instructions and executes it.
Results or output will be displayed.
EC201 Fundamental Programming
Program Development Environment
Faculty of Information Technology and Multimedia, 2008/2009
Terms Description
Machine 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
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
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 body
3. Main function
4. Identifiers/Variable
5. C statements
6. Comment
EC201 Fundamental Programming
Basic Syntax of Programming
Faculty of Information Technology and Multimedia, 2008/2009
PROGRAM
BLOCK
PREPROCESSOR
DIRECTIVE
MAIN FUNCTION
IDENTIFIERS/
VARIABLE
C
STATEMENT
COMMENT
PROGRAM
BODY
PICK AND MATCH
EC201 Fundamental Programming
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
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
Faculty of Information Technology and Multimedia, 2008/2009
Preprocessor Directive
Example:
#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)
Called from
standard library
EC201 Fundamental Programming
Basic Syntax of Programming
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 function
Function Header File
Standard 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
Faculty of Information Technology and Multimedia, 2008/2009
Standard
Library
List of header file and its function
Header 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
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
Faculty of Information Technology and Multimedia, 2008/2009
STANDARD INPUT/OUTPUT
FUNCTIONS (cont.)
Function Description
printf()
Prints all types of data to the screen
puts()
Prints string to the screen including newline (\n)
int putchar(int c)
Prints a character to the screen
scanf()
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 user
fflush(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.
O
U
T
P
U
T

I
N
T
P
U
T

EC201 Fundamental Programming
STANDARD INPUT/OUTPUT FUNCTIONS
<stdio.h>
Faculty of Information Technology and Multimedia, 2008/2009
MATHEMATICAL FUNCTIONS
math.h
stdlib.h
Mathematical
Functions
Mathematical
Functions
Mathematical
Functions
math.h
Mathematical
Functions
stdlib.h
math.h
Mathematical
Functions
EC201 Fundamental Programming
Faculty of Information Technology and Multimedia, 2008/2009
MATHEMATICAL
FUNCTIONS <math.h>
Function Description
tan (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 (x
y
)
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
Faculty of Information Technology and Multimedia, 2008/2009
STRING FUNCTIONS
string.h
ctype.h
String
Functions
EC201 Fundamental Programming
Faculty of Information Technology and Multimedia, 2008/2009
STRING FUNCTIONS
<string.h>
Function Description
strcat(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
Faculty of Information Technology and Multimedia, 2008/2009
STRING FUNCTIONS <ctype.h>
Function Description
isdigit(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
Faculty of Information Technology and Multimedia, 2008/2009
User-defined
Library
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(),dll
kira.h plus(),minus(), divide(),dll
EC201 Fundamental Programming
Basic Syntax of Programming
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
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
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
Faculty of Information Technology and Multimedia, 2008/2009
Principles 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( )
{

}
Faculty of Information Technology and Multimedia, 2008/2009
The curly braces { }
EC201 Fundamental Programming
Identify a segment / body of a program
The start and end of a function
The 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)
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
Faculty of Information Technology and Multimedia, 2008/2009
C Statement
Instructions to be executed by computers
Every statements must be ended with semicolon
Types
Declaration
statement
Input/Output
statement
Control
statement
Function
statement
EC201 Fundamental Programming
Basic Syntax of Programming
Faculty of Information Technology and Multimedia, 2008/2009
Comment
Statement in program code that will be ignored by compiler
Differs in terms of colour : grey or green
Format use ; /*.*/
//

Function
To document
a program
As a future
references
To provide additional
information
To increase
program readability
EC201 Fundamental Programming
Basic Syntax of Programming

You might also like