Professional Documents
Culture Documents
STRUCTURE OF C
Structure of C 1
OBJECTIVES
In this chapter you will learn:
Structure of C 2
Subtopics
Introduction to C
o Why you learn C
o Steps in developing a C program
o Beginning Concept
o Write My First C Program
C Fundamentals
o Keyword o Data types
o Identifier o Preprocessor Directives
o Variable & Constant o Formatting Input
Structure of C 3
Subtopics
Operator in C
Assignment
Arithmetic
Equality, Relational & Logical
Increment &Decrement
Cast
Structure of C 4
2.1 Introduction
2.1.1 Why C ?
Structure of C 5
Steps in Developing C Program
Editor
Linker
Executable File
Structure of C 6
Steps in Developing C Program
Preprocessor
o A program that is used to modify the source code in memory
according to preprocessor directive (example : #include)
embedded in the source code.
o Strip comments & unnecessary white spaces in the source code.
Compiler
o Converts source ( human-readable) program to
object (machine-readable) program
Linker
o Converts object program to
o executable program
Structure of C 7
Basic in C Language
Structure of C 8
Basic in C Language
preprocessor directives
int main(void)
{
declarations;
statements;
}
Structure of C 10
My First C Program
Preprocessor Program comment
directive /* My first programming in C */
/* main function*/
#include <stdio.h>
int main ( ) {
printf (Welcome to C !\n); Function Body
printf (Have Fun);
Output return 0;
statement } /*end main function */
#include <stdio.h> B
void main ()
{
printf ( Selamat Datang ke UTHM!\n ); C
}
Structure of C 12
2.2 C Fundamentals
Keyword / reserved word
o identify language entities, such as statements,
data types, and language element attributes.
o have special meanings to the compiler.
(have predefined uses)
o must be typed fully in lowercase.
o 32 words defined as keywords in C
o Example : const, double, int, return
Structure of C 13
C Fundamentals
Identifiers
o Standard identifiers
special meaning in C
defined in stdio library
can be redefined
User defined Identifiers
defined by users
Structure of C 14
Rules : User Defined Identifiers
Structure of C 15
C Fundamentals
Variable
o Represent certain value of a certain data type and store
data such as numbers and letters.
o The number or letter or other data item in a variables is
called its value.
o The general syntax for declaring variables is :
datatype variableName;
e.g :
char category;
int num;
double weight;
float price;
Structure of C 16
Variable
o If variables are of the same type, they can be declared together, as follows :
o You cannot declare the same variable more than once; therefore, the second
declaration below is invalid because y is declared twice :
e.g : int a, b, c;
int b;
o Initializing variable
After declaring a variable it should be initialized with a suitable value
Structure of C 17
C Fundamentals
Constant
Constants are entities that appear in the program code as fixed values.
o Types of constants:
o Integer (-15, 0, +250, 7550)
o Floating point
(20.35 = 0.2035 x 102 = 0.2035E+2 = 0.2035e+2)
o Character (1, n, A)
o String Constant C Programming is easy
o Named Constant
o Declared constant (in a function body)
Example : const float PI = 3.141;
Structure of C 18
C Fundamentals
Data types
o A set of data with values having predefined characteristics
Structure of C 19
C Fundamentals
Preprocessor Directives
o commands that give instructions to the C preprocessor,
o #include directive
Structure of C 20
C Fundamentals
Formatted Output & Input
o Formatted Output
use printf Function
prints information to the screen
Required argument
control string
specific conversion specifier
Example :
Structure of C 21
C Fundamentals
o Formatted Input
Use scanf Function
required arguments
control string
memory locations that correspond to the specifiers in the
control string
Example:
double distance;
char unit_length;
scanf("%1f %c", &distance, &unit_length);
Structure of C 22
2.3 Operator in C
Assignment Operator
o Use simple equal (=) sign
o Format
variable_name = expression;
o The expression can be a single variable or a literal, or it may
contain variables, literals and operators
Example :
num1 = 5;
num2 = 6;
num2 = num2 + 12;
num4 = (num1 + 2) / 2;
* Use C shortcut to simplify your assignment statement
Structure of C 23
Assignment Operator
int m = 3, n = 2;
double x, y, p = 2.0;
x = m / p;
y = m / n;
Structure of C 24
Assignment Operator
m n
x y
p 2.0
x 1.5 y 1.0
Structure of C 26
Operator in C
Arithmetic
o Arithmetic Operator
1. Unary
o Unary plus (+) & unary minus (-)
Eg : first = +x;
second = -x;
o Increment & Decrement (discuss later in slide 28)
2. Binary
o manipulate two operand (constants, variables or other arithmetic
expression )
o Use operator +, -, *, / and %
Arithmetic expression
Eg :
circe_area = PI *radius *radius;
constant variable
operand
Structure of C 27
Binary Arithmetic Operator
Example 1 : Assume x = 6 , y =2
Structure of C 28
Division and remainder for integer
number
3 / 15 = 0 3%5=3
15 / 3 = 5 4%5=4
16 / 3 = 5 5%5=0
17 / 3 = 5 6%5=1
18 / 3 = 6 7%5=2
0/4=0 8%5=3
4 / 0 ( undefined) 15 % 0 ( undefined)
Structure of C 29
Arithmetic
Example :
int a, b, c, d, e;
a = 20, b = 15, c = 10, d = 6;
e = a * (b c) % 30 + (c + d) ;
3 1 4 5 2
steps
Structure of C 30
Precedence &
Associatively
a. Parentheses rule: ( )
b. Operator precedence rule :
Rules
! ++ --
* / %
+ -
< <= > >=
== !=
&& || =
c. Associativity rule
right associativity unary operator usage will be evaluated
from right to left if located at the same level.
left associativity binary operator usage will be evaluated
from left to right if located at the same level.
Structure of C 31
Precedence &
Associatively
Eg : x * y * z + a / Rules
bc*d
What can we do to modify the above
expression ?
Structure of C 32
Precedence & Associatively
Rules
It would be better if the expression is added
with parentheses as below :
(x * y * z) + (a / b) (c * d)
Try this :
z (a + b / 2) + w * y
Structure of C 33
Operator in C
Equality, Relational & Logical
Standard Algebraic C Operator
Operator
Equality
= == Use to compare
not = != values forming
Relational relational expressions
> >
>= Example :
< <
a>b
c != 0
<=
Logical
AND &&
OR ||
NOT !
Structure of C 34
Operator in C
num = num + 1 ,
can be shorten to num++;
Structure of C 35
Operator in C
Cast
o Casting is an operation that converts value of one data type into
a value of another data type.
o
Syntax of cast operation : (type) variable/Expression
Example :
Structure of C 36
Casting operation
Structure of C 37
Wrting mathematic formula in
C Eg :
a = bc a = b * c;
m = y-b m = (y-b)/(x-a);
x-a
Try this : b-4ac
a+b-c
a+b
c+d
1
1+x
Structure of C 38
Example 1 :
#include <stdio.h>
#define STUDENT 5
void main( ) {
int total = 456;
double avg1, avg2;
Structure of C 39
Example 2:
#include <stdio.h>
void main( )
{ int a;
int b;
int c;
int jum;
jum = a + b + c;
printf(Jumlah adalah: %d\n\n, jum);
prinf(Terima kasih.\n);
}
Structure of C 40
Output for example 2 :
Selamat
Selamat Datang.
Datang.
Aturcara
Aturcara ini
ini menjumlahkan
menjumlahkan 33 nombor.
nombor.
Masukkan
Masukkan 33 nombor
nombor dalam
dalam bentuk:
bentuk: nnn
nnn nnn
nnn nnn
nnn
<enter>
<enter>
11
11 2222 33
33
Jumlah
Jumlah adalah:
adalah: 66
66
Terima
Terima kasih.
kasih.
Structure of C 41
Example 3 :
#include <stdio.h>
Nilai
Nilai pertama
pertama == 10
10
Nilai
Nilai kedua
kedua == 4.500000
4.500000
Structure of C 42
End of Chapter 2
Question?
Question?
Question?
Structure of C 43
Exercise :
Jumlah Besar ( )
jumlahBesar ( )
jumlah_besar ( )
8JumlahBesar ( )
Structure of C 44
Exercise :
int k = 5, m = 4, n;
double x =1.5, y = 2.1, z;
n = k + x;
z = k / m;
n = x * y;
Structure of C 45
Answer :
n = k + x; 5 + 1.5 = 6.5
z = k / m; 5 / 4 =1
n = x * y; 1.5 * 2.1 = 3.15
Structure of C 46