Course Learning Outcome (Clo) : CLO1 (Apply Knowledge of Basic Concepts and Fundamentals of
Course Learning Outcome (Clo) : CLO1 (Apply Knowledge of Basic Concepts and Fundamentals of
Course Learning Outcome (Clo) : CLO1 (Apply Knowledge of Basic Concepts and Fundamentals of
PROGRAMMING
DEC20012 :
CHAPTER 1
COURSE LEARNING OUTCOME (CLO) :
CLO1 (Apply knowledge of basic concepts and fundamentals of
structured programming in solving a variety of engineering and
scientific problems using a high level programming language.
The set of characters used to form words numbers and
expressions
The characters in C are grouped into the following two
2
Source Character Set
* ALPHABETS
Uppercase letters A-Z
* DIGITS
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
* WHITESPACE CHARACTERS
Space, Horizontal tab, Vertical Tab, Form Feed, New Line
num2 space
This declaration tells the compiler that the memory
7
CHARACTER
(char)
Keyword char is used to declare a character variables type.
8
CHARACTER
(char)
Variable characters store any print or non-printed
9
FLOATING POINT
(float/double)
Programme
16
2.2 Understand The Fundamentals Of C
Header File
Contain definition function and variables which
can be incorporated into any C program by
using the pre-processor #include statement
To use any of the standard function, the
appropriate file should be included
All header file have the extension of “.h” and
generally reside in the /usr/include
Programme
subdirectories
Example : #include<math.h>, #include<string.h>
17
2.2 Understand The Fundamentals Of C
Header File
The use of angle “< >” inform
the compiler to search
compiler include directories
for the specified files
Programme
18
2.2 Understand The Fundamentals Of C
Return Statement
The return statement terminates the
execution of a function and returns
control to the calling function.
Execution resumes in the calling
function at the point immediately
Programme
following the call
19
2.2 Understand The Fundamentals Of C
The Usage of Comments
Why use comment ?
Documentation of variables and
function of their usage
Explaining difficult section of code
Describe the program, author , date,
modification changes, revision,etc
Programme
Use /* close */ or // for single line
20
The symbolic name in functional and variable
Programme
Not the C reserved word (keyword)
21
2.2 Understand The Fundamentals Of C
Valid Invalid Reason?
polipd 2polipd
_bek meow?
rekod_201 rekod-301
Programme
22
2.2 Understand The Fundamentals Of C
Valid Invalid Reason?
polipd 2polipd Begin with
number
_bek meow? Illegal
character
rekod_201 rekod-301 Illegal
Programme
character
Gross_Pay Gross pay Illegal blank
23
2.2 Understand The Fundamentals Of C
The identifier name design
by the user for the purpose
of definition is :
1. variable
2. constant
3. function
Programme
24
2.2 Understand The Fundamentals Of C
Variable
A Variable is a name given to the memory
location where the actual data is stored
Variable name may have different data
types to identify the type of value stored.
Suppose we declare variable of type integer
then it can store only integer values.
Programme
25
Variable
Case sensitive
Example: AGE, Age, AgE, agE is a different variables
SAME AS IDENTIFIER
Cannot start with numbers
Example: 1NAMA
Programme
Keyword cannot be used as a variable names
Example: printf, while, loop
26
Variable
Programme
gross pay item 10
Hasil2 _KIRAJUMLAH
27
Direct Access Storage
Programme
printf(“%d”, no);
getch();
return 0;
}
DEC2012 - FUNDAMENTAL PROGRAMMING 28
Direct Access Storage
no1
Programme
printf("Purata 2 no ialah:%d”,purata);
getch();
return 0;
}
29
Variable Declaration
Programme
??? ??? ???
jum_pendapatan a b 30
Variable Assignment
Examples :
int i;
.
.
i = 0;
◦ Assigns initial values during declaration
Examples :
int i = 0;
Programme
int a, b = 0;
31
Variable Assignment
20 10
lebar tinggi
Programme
10 200 ???
20 10
lebar tinggi
Programme
10 200 2000
Programme
Floating point
Character
34
Integer Constant
Programme
Spaces between the digits are not allowed
Valid or Not ? -15 0179 1,756 +250 7500
0
35
Floating Point Constant
Programme
(‘E’ @ ‘e’ is exponential)
36
Floating Point Constant
double
long double
Programme
37
Character Constant
Programme
◦
38
Character Constant
Programme
39
Quiz
Constant
Programme
Enum – there is some values
Syntax - enum Color
Click the
{RED, BLUE, GREEN};
Quiz button on iSpring Pro
toolbar to edit your quiz
DEC2012 - FUNDAMENTAL PROGRAMMING 40
A numeric constant / string / a variable / value
Programme
41
2.2 Understand The Fundamentals Of C
Which one
#include <stdio.h> is the
#include <conio.h> Expression?
main()
{
int jumlah;
jumlah =4+9;
printf(“jumlah ialah:%d”,jumlah);
getch();
Programme
return 0;
}
42
Whenever and input/output function
#include<stdio.h>
Programme
43
The printf()Function
Coding
Programme
Output
44
The printf()Function
Coding
Programme
Here is an integer : 5
45
The printf()Function
Programme
%c – print a character
%s – print a string
46
The printf()Function
Programme
\r - move the cursor to the beginning of the current line
\t - move the cursor to the next horizontal tab position
\v - move the cursor to the next vertical tab position
47
Printing Integers Numbers(%d)
Output
The Integer Number is = 5
Programme
48
Printing Floating Numbers(%f)
Output
Programme
The Float Number is = 33.77
49
Printing Floating Numbers(%f)
12.3
Programme
12.35 The output is rounded to 2 decimal places
12.346
50
Printing Strings(%s)
Programme
Output
Computer
Computer 51
Printing Strings(%s)
Programme
Computer
Computer
52
Printing Characters(%c)
Output
Programme
JUNE
53
Printing Characters(%c)
Programme
JUNE
54
Displaying Several Values At Once
Programme
Output
Programme
input data to the variables (var1, var2, var3
…. )
56
The scanf()Function
Programme
57
The scanf()Function
Programme
58
The scanf()Function
Programme
An integer that will be
stored in variable num2
59
The scanf()Function
Output
Please enter a date:
Programme
17 8 1996
Day: 17 Month: 8 Year:1996
60
To perform a mathematical
Programme
Unary Operators
61
Assignment Operators (=)
Programme
Left-side value of Assignment Operator must be Variable.
62
Assignment Operators (=)
Programme
-=
operand and assign the result to left C - A
operand
Multiply AND assignment operator,
It multiplies right operand with the C *= A is equivalent to C = 63
*=
Assignment Operators (=)
Programme
to left operand
C <<= 2 is same as C = C <<
<<= Left shift AND assignment operator
2
Right shift AND assignment C >>= 2 is same as C = C >>
>>=
operator 2 64
Assignment Operators (=)
-= n -= 5 n = n - 5
*= n *= 5 n = n * 5
/= n /= 5 n = n / 5
Programme
%= n %= 5 n = n % 5
65
Assignment Operators (=)
#include <stdio.h>
Programme
printf(“After n %= 7 = “,n);
getch();
return 0;
}
66
Mathematical Operators
Programme
-- Decrement var--
+ Unary plus +var
– Unary minus -var 67
Mathematical Operators
Programme
68
Mathematical Operators
Priority Operato
(PRECEDENCE)
Level r
1 (High) ( )
2 * / %
Programme
3 (Low) + -
69
Mathematical Operators
(9–(3+2))*3
(9–5)*3
4*3
12
Programme
70
Mathematical Operators
16 – 8 16 – 8 / 2
2
15 + 5 + 7 ( 15 + 5 + 7 ) / 2
Programme
2
71
Mathematical Operators
4+2x3-(5÷ 4 + 2 * 3 – (5 /
2) 2)
Programme
X3 + 5 mod 2 X^3+5%2
DEC2012 - FUNDAMENTAL PROGRAMMING 72
Mathematical Operators
DIRECT CALCULATION EXAMPLE
Programme
}
Programme
return 0;
}
Programme
DEC2012 - FUNDAMENTAL PROGRAMMING 75
Relational Operators
Programme
Equal To
== Equal To 7 == 5 0
DEC2012 - FUNDAMENTAL PROGRAMMING 76
Relational Operators
Programme
<= Less than or equal to true ? x <= y
>= More than or equal tofalse ? x >= 5
DEC2012 - FUNDAMENTAL PROGRAMMING 77
Logical Operators
Programme
OR (||)
DEC2012 - FUNDAMENTAL PROGRAMMING 78
Logical Operators
Conditio NOT
n1
1 0
0 1
Programme
79
Logical Operators
Programme
0 0 0
80
Logical Operators
Condition Condition OR
1 2
1 1 1
1 0 1
0 1 1
Programme
0 0 0
81
Logical Operators
Programme
printf( "tak layak“);
getch();
return 0;
}
82
Unary Operators
Programme
83
Unary Operators : Increment &
Decrement
Programme
84
Unary Operators : Increment &
Decrement
Expressi Meaning
on
i++ Add the value of i after used in the
expression
Programme
++i Add the value of i before used in the
expression
i-- Minus the value of i after used in the 85
Quiz
Unary Operators : Increment &
Decrement
* Prefix Example :
i 5 j
2. i = 5;
Programme
j = ++i – 2; i 6 j
Click the i 6
Quiz button on iSpring Pro 4 j
toolbar to edit your quiz
DEC2012 - FUNDAMENTAL PROGRAMMING 86
3.3 Apply the fundamentals of C
Programming
87
1. Click Dev C++
icon
2. To create a source file
File New Source File
Programming
DEC2012 - FUNDAMENTAL PROGRAMMING 91
A memory location showing
Adding Two Integers : the name of variable
integer1 ?
integer2 ?
Programming
DEC2012 - FUNDAMENTAL PROGRAMMING 92
Adding Two Integers :
Output :
main( )
{
int a, b, c, jum ;
printf("Selamat Datang.");
printf("\nAturcara ini menjumlahkan 3 nombor.");
printf("\nMasukkan 3 nombor dalam bentuk: nnn nnn nnn
<enter>");
scanf("%d%d%d",&a,&b,&c);
jum = a + b + c;
printf("\nJumlah adalah:%d",jum);
printf("\nTerima kasih. ");
Programme
A memory location showing
getch(); the name of variable
return 0;
? ? ? ?
}
a b c jum
DEC2012 - FUNDAMENTAL PROGRAMMING 94
Selamat Datang._
main( )
{
int a, b, c, jum ;
printf("Selamat Datang.");
printf("\nAturcara ini menjumlahkan 3 nombor.");
printf("\nMasukkan 3 nombor dalam bentuk: nnn nnn nnn
<enter>");
scanf("%d%d%d",&a,&b,&c);
jum = a + b + c;
printf("\nJumlah adalah:%d",jum);
printf("\nTerima kasih. ");
Programme
A memory location showing
getch(); the name of variable
return 0;
? ? ? ?
}
a b c jum
DEC2012 - FUNDAMENTAL PROGRAMMING 95
Selamat Datang.
Aturcara ini menjumlahkan 3 nombor. _
main( )
{
int a, b, c, jum ;
printf("Selamat Datang.");
printf("\nAturcara ini menjumlahkan 3 nombor. ");
printf("\nMasukkan 3 nombor dalam bentuk: nnn nnn nnn
<enter>");
scanf("%d%d%d",&a,&b,&c);
jum = a + b + c;
printf("\nJumlah adalah:%d",jum);
printf("\nTerima kasih. ");
Programme
A memory location showing
getch(); the name of variable
return 0;
? ? ? ?
}
a b c jum
DEC2012 - FUNDAMENTAL PROGRAMMING 96
Selamat Datang.
Aturcara ini menjumlahkan 3 nombor.
Masukkan 3 nombor dalam bentuk: nnn nnn nnn <enter> _
main( )
{
int a, b, c, jum ;
printf("Selamat Datang.");
printf("\nAturcara ini menjumlahkan 3 nombor. ");
printf("\nMasukkan 3 nombor dalam bentuk: nnn nnn nnn
<enter>");
scanf("%d%d%d",&a,&b,&c);
jum = a + b + c;
printf("\nJumlah adalah:%d",jum);
printf("\nTerima kasih. ");
Programme
A memory location showing
getch(); the name of variable
return 0;
? ? ? ?
}
a b c jum
DEC2012 - FUNDAMENTAL PROGRAMMING 97
Selamat Datang.
Aturcara ini menjumlahkan 3 nombor.
Masukkan 3 nombor dalam bentuk: nnn nnn nnn <enter> 10 20 30 _
main( )
{
int a, b, c, jum ;
printf("Selamat Datang.");
printf("\nAturcara ini menjumlahkan 3 nombor. ");
printf("\nMasukkan 3 nombor dalam bentuk: nnn nnn nnn
<enter>");
scanf("%d%d%d",&a,&b,&c);
jum = a + b + c;
printf("\nJumlah adalah:%d",jum);
printf("\nTerima kasih. ");
Programme
A memory location showing
getch(); the name & value of variable
return 0;
10 20 30 ?
}
a b c jum
DEC2012 - FUNDAMENTAL PROGRAMMING 98
Selamat Datang.
Aturcara ini menjumlahkan 3 nombor.
Masukkan 3 nombor dalam bentuk: nnn nnn nnn <enter> 10 20 30 _
main( )
{
int a, b, c, jum ;
printf("Selamat Datang.");
printf("\nAturcara ini menjumlahkan 3 nombor.");
printf("\nMasukkan 3 nombor dalam bentuk: nnn nnn nnn
<enter>");
scanf("%d%d%d",&a,&b,&c);
jum = a + b + c;
printf("\nJumlah adalah:%d",jum);
printf("\nTerima kasih.");
Programme
A memory location showing
getch(); the name & value of variable
return 0;
10 20 30 60
}
a b c jum
DEC2012 - FUNDAMENTAL PROGRAMMING 99
Selamat Datang.
Aturcara ini menjumlahkan 3 nombor.
Masukkan 3 nombor dalam bentuk: nnn nnn nnn <enter> 10 20 30
Jumlah adalah: 60 _
main( )
{
int a, b, c, jum ;
printf("Selamat Datang.");
printf("\nAturcara ini menjumlahkan 3 nombor. ");
printf("\nMasukkan 3 nombor dalam bentuk: nnn nnn nnn
<enter>");
scanf("%d%d%d",&a,&b,&c);
jum = a + b + c;
printf("\nJumlah adalah:%d",jum);
printf("\nTerima kasih. ");
Programme
A memory location showing
getch(); the name & value of variable
return 0;
10 20 30 60
}
a b c jum
DEC2012 - FUNDAMENTAL PROGRAMMING 100
Selamat Datang.
Aturcara ini menjumlahkan 3 nombor.
Masukkan 3 nombor dalam bentuk: nnn nnn nnn <enter> 10 20 30
Jumlah adalah: 60
main( )
{
int a, b, c, jum ;
printf("Selamat Datang.");
printf("\nAturcara ini menjumlahkan 3 nombor. ");
printf("\nMasukkan 3 nombor dalam bentuk: nnn nnn nnn
<enter>");
scanf("%d%d%d",&a,&b,&c);
jum = a + b + c;
printf("\nJumlah adalah:%d",jum);
printf("\nTerima kasih. ");
Programme
A memory location showing
getch(); the name & value of variable
return 0;
10 20 30 60
}
a b c jum
DEC2012 - FUNDAMENTAL PROGRAMMING 101
Mathematic operations,
Programming
DEC2012 - FUNDAMENTAL PROGRAMMING 102
Example :
1. (a >= 1) && (b == 5)
(2 >= 1) && (5 == 5)
1 && 1
1
2. ! (c > a)
Programming
! (15 > 2)
! 1
0
103
Example :
Programming
4. ! ( (a < b) || (c > d ) )
! ( (2 < 5) || (15 > 17) )
!( 1 || 0 )
! 1
DEC2012 - FUNDAMENTAL PROGRAMMING 0 104