CHAPTER 3:
FUNDAMENTALS
OF C LANGUAGE
DEC2012
Sesi Dis 2014
CHAPTER 3: FUNDAMENTALS OF C
LANGUAGE
3.1. Know the Variables, Constants and Data Types.
3.1.1. List the items in C Character set.
3.1.2. Identify the data types available in C.
3.1.3. Recognize keywords in C.
3.2. Understand the fundamentals of C Programme.
3.2.1. Explain the structure of C Programs.
3.2.2. Explain the structure of a function in the main function.
3.2.3. Explain valid identifiers.
3.2.4. Differentiate constants and variables in C.
3.2.5. Explain the input/output statement.
3.2.6. Explain the following types of Operators.
a. Assignment operators.
b. Mathematical operators.
c. Relational operators.
d. Logical operators.
e. Unary operators.
Sesi Dis 2014
CHAPTER 3: FUNDAMENTALS OF C
LANGUAGE
3.3. Apply the fundamental of C Programming.
3.3.1. Construct a simple C Program.
3.3.2. Compile and execute programs.
3.3.3. Use input statements in C Program.
3.3.4. Apply output statements in simple C Program.
3.3.5. Display the output in the specified format.
3.3.6. Apply calculations by using operators and expressions.
3.3.7. Implements mathematical calculations in simple C Program.
3.3.8. Implements mathematical calculations using the function in the main function.
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
• Variables
– A variable is just a named area of storage
that can hold a single value (numeric or
character).
– they represent some unknown.
– Programming language C has two main
variable types
• Local Variables
• Global Variables
Sesi Dis 2014
Example: Local vs. Global
#include<stdio.h>
void print_number(void);
p is declared outside of all
int p; functions. So, it is a global
variable.
void main (void)
{
int q = 5; q is declared inside the
printf(“q=%d”, q); function main. So, it is a
local
p=10;
variable to the function.
print_number();
}
p can be used anywhere
void print_number(void)
{
printf(“%d”,p); Error! q can only be used in
the function main, because it
q = q + 5;
is a local variable 5
}
3.1. Know the Variables, Constants and Data Types.
• Variables a variable must
be declared
before it can be
used !!
This is
Variable.!!
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
• Constants:
– the values that never change,
– Constants can be very useful in C
programming whenever you have any value
that is repeated in your program.
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
• Variables and Constants
– name/identifier can be declared
with constant,
This is
Variable.!!
and it can be
declared with
constant…
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
• Rules for Variables & Constants
– may be given representations containing
multiple characters. But there are rules for
these representations..
• May only consist of letters, digits, and
underscores
• May be as long as you like, but only the first 31
characters are significant
• May not begin with a number
• May not be a C reserved word (keyword)
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
• Rules for Variables & Constants
• May only consist combination of letters,
digits, and underscores
• May be as long as you like, but only the first 31
characters are significant
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
• Rules for Variables & Constants
• May not begin with a number
• May not be a C reserved word (keyword)
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
• Rules for Variables & Constants
– To declare a variable means to create a
memory space for the variable depending
on the data type used and associate the
memory location with the variable name.
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
• Rules for Variables & Constants
– The shortest variable name is a letter of
the alphabet.
– Variables are typically in lowercase. (All of C is
lowercase for the most part.) They can
contain letters and numbers.
– AGAIN!!..You should not begin a variable
name with a number. They can contain
numbers, but you begin it with a letter.
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
• Variables & Constants understanding..
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
• Variables & Constants understanding..
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
• Variables & Constants understanding
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
• Variables & Constants understanding..
If all value is integer??
If all value is floating point??
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
• Variables & Constants understanding..
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
• refers to where variables can be
referenced.
• Global Scope
o outside any functions.
o can be used by later
blocks of code:
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
• Local Scope
o inside any functions.
o can be used only in blocks of code:
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
• Variables & Constants understanding..
If all value is integer??
If all value is floating point??
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
• Variables & Constants understanding..
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
• Keywords in C Programming Language :
o Keywords are those words whose meaning is
already defined by Compiler
o Cannot be used as Variable Name
o There are 32 Keywords in C
o C Keywords are also called as Reserved
words
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
• Use keywords in programmes.
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
Exercise: determine the variables &
constants
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
• List the basic data types in C.
• A program usually contains different types
of data types (integer, float, character etc.)
and need to store the values being used in
the program.
• C language is rich of data types. A C
programmer has to employ proper data type
as per his/her requirements. C language
provides various data types for holding
different kinds of values.
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
• C language provides various data types
for holding different kinds of values.
• There are several integer data types, a
character data type, floating point
data types for holding real numbers and
more.
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
• C has a concept of 'data types' which are used to
define a variable before its use. The definition of a
variable will assign storage for the variable and define
the type of data that will be held in the location.
• C has different data types for different types of data and
can be broadly classified as:
Primary Data Types
Secondary Data Types
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
Primary Data Types
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
Secondary Data Types
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
Integer types
•C provides several standard integer types,
from small magnitude to large magnitude
numbers: char, short int, int, long int, long
long int.
•Each type can be signed or unsigned.
Signed types can represent positive and
negative numbers while unsigned can
represent zero and positive numbers.
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
Integer types
• C provides several standard integer types,
from small magnitude to large magnitude
numbers:
•short int; int; long int; long long int;
1 Byte = 8 Bits 1 Byte
1 1 1 1 1 1 1 1
Sesi Dis 2014 8 Bits
3.1. Know the Variables, Constants and Data Types.
Numeric: Integer types
Generally an integer (int) occupies 2 bytes
memory space and its value range limited to
-32768 to +32767 (that is, -215 to +215-1). A
signed integer use one bit for storing sign
and rest 15 bits for number.
2 Bytes memory
Signed FFFF HEX = -32768 to 32767 DECIMAL
Unsigned FFF HEX = 0 - 65535 DECIMAL
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
Integer types
signed short int
short int
long short int
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
Integer types
Syntax:
int <variable name>;
int num1;
short int num2;
long int num3;
Example: 5, 6, 100, 2500.
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
Numeric: Floating point types
•The float data type is used to store fractional
numbers (real numbers) with 6 digits of
precision. Floating point numbers are denoted by
the keyword float.(eg. 0.000001)
•When the accuracy of the floating point number is
insufficient, we can use the double to define the
number. The double is same as float but with
longer precision and takes double space (8 bytes)
than float.
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
Numeric: Floating point types
•To extend the precision further we can use
long double which occupies 10 bytes of
memory space.
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
Numeric: Floating point types
Syntax: float <variable name>; like
float num1;
double num2;
long double num3;
Example: 9.125, 3.1254.
Floating Point Data Type Memory Allocation
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
Character:
•Character type variable can hold a single
character. As there are singed and unsigned
int (either short or long), in the same way
there are signed and unsigned chars;
•Both occupy 1 byte each, but having
different ranges. Unsigned characters have
values between 0 and 255, signed
characters have values from –128 to 127.
Sesi Dis 2014
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
Character:
Syntax: char <variable name>; like
char ch = ‘a’;
Example: a, b, g, S, j.
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
String:
•A string in C is an array of char values
terminated by a special null character value
'\0'. For example, here is a statically
declared string that is initialized to “bye":
char str[4]; // need space for chars in str, plus for
terminating '\0' char
str[0] = ‘b';
str[1] = ‘y';
str[2] = ‘e';
str[3] = '\0';
printf("%s\n", str); // prints bye to stdout
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
Exercise:
Determine what types of data to used if the given number is?
Question: Question:
Number is Answer is
234 int
23.122 float
2 int
‘a’ char
“Dad” char/string
2314.1121231 double
Sesi Dis 2014
3.1. Know the Variables, Constants and Data Types.
Exercise 1
Fahrul has 3 packets of sugar weighing 2.6 kg each. If she gives
2.6 kg to her mother and 1.1 kg to her brother, how much sugar
does she have left?
What is the data types? float
Exercise 2
A box contains 108 oranges. SMK Pulau Nyior receives 8 such
boxes to be distributed evenly to 16 classes. How many oranges
would each class get?
What is the data types? int
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
• Evolved by Ritchie from two
previous programming
languages, BCPL and B
• Used to develop UNIX
• Used to write modern
operating systems
• Hardware independent
(portable)
• By late 1970's C had evolved
to "Traditional C"
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
Six (6) phase of C
development
environment:
1. Editor
2. Preprocessor
3. Compiler
4. Linker
5. Loader
6. CPU
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
Six (6) phase of C development environment:
1. Editor - Software packages for the C/C++ integrated program
development environments such as Microsoft Visual Studio have
editors that are integrated into the programming environment.
C program file names should end with the .c extension.
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
Six (6) phase of C development environment:
2. Preprocessor
• In a C system, a preprocessor program executes
automatically before the compiler’s translation phase
begins.
• commands called preprocessor directives, which
indicate that certain manipulations are to be
performed on the program before compilation.
3. Compiler
• the compiler translates the C program into machine-
language code. (High language → Machine language)
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
Six (6) phase of C development environment:
4. Linker
• contain references to functions defined elsewhere,
such as in the standard libraries (e.g <stdio.h>) or in
the private libraries of groups of programmers
working on a particular project.
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
Six (6) phase of C development environment:
5. Loading
• Before a program can be executed, the program
must first be placed in memory. This is done by
the loader, which takes the executable ‘image’ from
disk and transfers it to memory. Additional
components from shared libraries that support the
program are also loaded (if).
6. CPU
• Finally, the computer, under the control of its CPU,
executes the program one instruction at a time.
(*.exe)
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
preprocessor
header file
main function
user program
inside {….}
user defined
function (if any)
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
1 /* Fig. 2.1: fig02_01.c
2 A first program in C */
3 #include <stdio.h>
4
5 /* function main begins program execution */
6 int main()
7 {
8 printf( "Welcome to C!\n" );
9
10 return 0; /* indicate that program ended successfully */
11
12 } // end function main.
Welcome to C!
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
1 /* Fig. 2.1: fig02_01.c
2 A first program in C */
3 #include <stdio.h>
4
5 /* function main begins program execution */
6 int main()
7 {
8 printf( "Welcome to C!\n" );
9
10 return 0; /* indicate that program ended successfully */
11
12 } // end function main.
Welcome to C!
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
1 /* Fig. 2.1: fig02_01.c
2 A first program in C */
3 #include <stdio.h>
4
5 /* function main begins program execution */
6 int main()
7 {
8 printf( "Welcome to C!\n" );
9
10 return 0; /* indicate that program ended successfully */
11
12 } // end function main.
Welcome to C!
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
put \n
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
try to change \n → \a..observe what happen
• Notice that the characters \n were not
printed on the screen. The backslash (\) is
called an escape character..
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
int is refer to
the integer
try to change this..
number_3 %d
Value change %d change
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
%d just for integer only!!
try to change this..
number_3 %d
Value change %d change
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
• Preprocessor directives.
– Text must be start with #include<header.h>
– #define is one of the preprocessor directives.
1 /* Fig. 2.1: fig02_01.c
2 A first program in C */
3 #include <stdio.h>
4
5 /* function main begins program execution */
6 int main()
7 {
8 printf( "Welcome to C!\n" );
9
10 return 0; /* indicate that program ended successfully */
11
12 } // end function main.
Welcome to C!
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
• Preprocessor directives.
– #define is one of the preprocessor directives.
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
• The C preprocessor (cpp) is the
preprocessor for the C and C++ computer
programming languages.
• The preprocessor handles directives for
– source file inclusion (#include)
– macro definitions (#define), and
– conditional inclusion (#if).
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
• Header file is a file that allows programmers
to separate certain elements of a program's
source code into reusable files.
• These are collectively known as the standard
libraries and include:
– string.h : for string handling
– stdlib.h : for some miscellaneous functions
– stdio.h : standardized input and output
– math.h : mathematical functions
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
Compile
& Run
under header stdlib.h
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
• These are collectively known as the standard
libraries and include:
– ctype.h : for character handling
– conio.h : library functions for performing
"console input and output" from a program
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
• Tells computer to load contents of a
certain file;
– <stdio.h> allows standard input/output
operations
stdio.h (standard input output header) example.
printf prints formatted byte/wchar_t
output to stdout,
scanf reads a byte string from stdin
puts writes a byte string to stdout
gets reads a byte string from stdin
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
• conio.h is a C header file used in old MS-DOS
compilers to create text user interfaces.;
– <conio.h> allows console input/output
operations
conio.h (console input output header) example.
getch Reads a character directly from the console without
buffer
putch Writes a character directly to the console
cscanf Reads formatted values directly from the console
cprintf Formats values and writes them directly to the
console.
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
• Defines numeric conversion functions, pseudo-random
numbers generation functions, memory allocation,
process control functions;
– <stdlib.h> allows standard input/output
operations
stdlib.h (standard library header) example.
system Execute system command (function )
rand Generate random number (function)
abort Abort current process (function)
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
1 /* Fig. 2.1: fig02_01.c
2 A first program in C */
3 #include <stdio.h>
4
5 /* function main begins program execution */
6 int main()
7 {
8 printf( "Welcome to C!\n" );
9
10 return 0; /* indicate that program ended successfully */
11
12 } // end function main.
Welcome to C!
• Block {…}
– Text surrounded by braces {...}.
– See line no.7 and no.12.
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
• Brace { }
– A left brace, {, begins the body of every
function (line 7). A corresponding right brace
ends each function (line 11).
1 /* Fig. 2.1: fig02_01.c
2 A first program in C */
3 #include <stdio.h>
4
5 /* function main begins program execution */
6 int main()
7 {
8 printf( "Welcome to C!\n" );
9
10 return 0; /* indicate that program ended successfully */
11
12 } // end function main.
Welcome to C!
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
1 /* Fig. 2.1: fig02_01.c
2 A first program in C */
3 #include <stdio.h>
4
5 /* function main begins program execution */
6 int main()
7 {
8 printf( "Welcome to C!\n" );
9
10 return 0; /* indicate that program ended successfully */
11
12 } // end function main.
Welcome to C!
• return 0; statement
– in line no.10, there are the statement return 0; which is indicate
that the function is successful normally terminated.
– Why return 0, bcoz main function should return integer data
type.
– void main() does no need to return 0.
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
• Comments
– Text surrounded by /* and */ is ignored by
computer, also by //. See line no.1 & 2
– Used to describe program
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
1 /* Fig. 2.1: fig02_01.c
2 A first program in C */
3 #include <stdio.h>
4
5 /* function main begins program execution */
6 int main()
7 {
8 printf( "Welcome to C!\n" );
9
10 return 0; /* indicate that program ended successfully */
11
12 } // end function main.
Welcome to C!
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
Operators
Understand and
Expressions
•Operators are symbols which take one or more operands
or expressions and perform arithmetic or logical
computations.
•Types of operators available in C are as follows:
– Arithmetic
– Assignment/relational
– Logical
– Boolean operator/Bitwise
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
• Refresh Back!
– Variable(Identifier)
– Constant
– Expression
variable(operand)
expression
Y = A+B×2
constant
OPERATOR
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
• Arithmetic Operator
– All the basic arithmetic operations can be carried out in C.
– Both unary and binary operations are available in C language.
*Unary operations operate on a singe operand, therefore the number 5 when
operated by unary – will have the value –5.
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
Arithmetic Operator
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
• Arithmetic Operator (Do it Yourself!!)
algebraic expression c expression
Y = mX² + d ÷ m ×k Y =m*X*X+d/m*k;
s = s³ + d - w²
v = ½V + a × t
E = mc²
Z = kD - bV² + m
o = 0.23m – 0.11k²
Area = ½(P × L)
Formula C Expression
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
• Arithmetic Operator
– Both unary and binary operations are available in C.
+a Positive a -b Negative b
int a; int b;
a=10; b=10;
m=a; m=-b;
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
• Arithmetic Operator
– Both unary and binary operations are available in C
prefix ++a prefix --a
a+1 a-1
postfix a++ postfix a--
int a; int a;
a=10; a=10;
m=a++; m=a--;
m=11 is the answer m=9 is the answer
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
Arithmetic Operational
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
• Relational Operator
– C supports the following relational operators.
– Often it is required to compare the relationship between
operands and bring out a decision and program accordingly.
– Example, we might make a decision in a program, for example,
to determine if a person’s grade on an exam is greater than
or equal to 60 and if it is to print the message
“Congratulations! You passed.”
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
• Relational Operator
algebraic expression c expression
X is greater than 46 X>46
y is lower than 423
221 is lower than m
A is greater than or equal
to 257
A is greater than or equal
to s
W is not equal to K
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
Relational Operator
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
Relational Operator
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
• Logical Operator
– So far we have studied only simple conditions, such as counter
<= 10, total > 1000, and number != Value.
– C provides logical operators that may be used to form more
complex conditions by combining simple conditions.
– The logical operators are
• && (logical AND),
• || (logical OR) and
• ! (logical NOT also called logical negation).
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
• Logical Operator (&& - and)
– Variable1&&Variable2
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
• Logical Operator(Bitwise) (&& - and)
– Variable1&&Variable2
algebraic expression c expression result
0 and with 1 0&&1 0 (false)
0 and with 0
1 and with 1
0 and with 124
12 and with 111
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
• Logical Operator (|| - or)
– Variable1||Variable2
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
• Logical Operator (|| - or)
– Variable1||Variable2
algebraic expression c expression result
0 or with 1 0||1 1 (true)
0 or with 0
1 or with 1
0 or with 124
12 or with 111
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
• Logical Operator(Bitwise) (! - not)
– !(Variable1)
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
• Logical Operator(Bitwise) (! - not)
– !(Variable1)
algebraic expression c expression result
not 0 !0 1 (true)
not 1
not (1 or 1)
not (0 &&124)
(not 12) or 111
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
Logical Operational
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
• Boolean Operator(Bitwise)
– The bitwise operators perform bitwise-AND (&), bitwise-
exclusive-OR (^), and bitwise-inclusive-OR (|) operations.
– & - The bitwise-AND operator compares each bit of its first
operand to the corresponding bit of its second operand. If both
bits are 1, the corresponding result bit is set to 1. Otherwise, the
corresponding result bit is set to 0.
0 0 1 1 1 0 1 1
& 0 1 0 1 0 1 1 1
0 0 0 1 0 0 1 1
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
• Boolean Operator(Bitwise)
– The bitwise operators perform bitwise-AND (&), bitwise-
exclusive-OR (^), and bitwise-inclusive-OR (|) operations.
– ^ - The bitwise-exclusive-OR operator compares each bit of its
first operand to the corresponding bit of its second operand. If
one bit is 0 and the other bit is 1, the corresponding result bit is
set to 1. Otherwise, the corresponding result bit is set to 0.
0 0 1 1 1 0 1 1
^ 0 1 0 1 0 1 1 1
0 1 1 0 1 1 0 0
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
• Boolean Operator(Bitwise)
– The bitwise operators perform bitwise-AND (&), bitwise-
exclusive-OR (^), and bitwise-inclusive-OR (|) operations.
– |- The bitwise-inclusive-OR operator compares each bit of its first
operand to the corresponding bit of its second operand. If either
bit is 1, the corresponding result bit is set to 1. Otherwise, the
corresponding result bit is set to 0.
0 0 1 1 1 0 1 1
| 0 1 0 1 0 1 1 1
0 1 1 1 1 1 1 1
Sesi Dis 2014
OR X-OR I-OR
0 0 0 0 0
0 1 1 1 1
1 0 1 1 1
1 1 1 0 1
X-OR i-OR
Klu kedua2 sama Klu salah satu adalah 1
=0 =1
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
Example
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
• Exercise . Bitwise ~ & | ^
C Expression result
4&3
4A & 2B (assume in hex)
2|4
2&6|3
(8^3) & 6
(9 ^ 3) | (3 & 6)
2 & 3 & 5 | 10 (assume in decimal)
4+4&4
5-2&5
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
Boolean(Bitwise) Operational
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
• Assignment
– often just called the "assignment operator", is a special case of
assignment operator where the source (right-hand side) and
destination (left-hand side) are of the same class type.
eg: a=a+b; mean a(new) = a(old) + b;
int a,b;
a=3; b=5; a=a+b; a+=b;
a=a+b;
d=d-b; d-=b;
e=e×b;
m=m÷k;
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
Compound(assignment) Operational
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
3.2. Understand the fundamentals of C Programme.
100 5 9
&1000
0000
0+5>=9;
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
Prompt number
0 to 100
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
Modify the program to print as follow
0 2 4 6……………
...........200
Modify the program to print as follow
0
2
4
Modify the program to print as follow 6
:
0 0.1 0.2 0.3…… :
...........20 :
200
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
Modify the program to print as follow
No. x2 x3
--------------------------
0 0 0
1 2 3
2 4 6
3 6 9
4 8 12
5 10 15
: : :
: : :
20 40 60
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
extra notes
• In this example, if x is equal to either w, y, or z, the
second argument to the printf function evaluates to
true and the value 1 is printed. Otherwise, it
evaluates to false and the value 0 is printed. As
soon as one of the conditions evaluates to true,
evaluation ceases.
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
• The following examples illustrate the logical
extra notes
operators:
• In this example, the printf function is called to print a
message if x is less than y and y is less than z. If x
is greater than y, the second operand (y < z) is not
evaluated and nothing is printed.
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
Data type printf conversion scanf conversion
specification specification
long double %Lf %Lf
double %f %lf
float %f %f
unsigned long int %lu %lu
long int %ld %ld
unsigned int %u %u
int %d %d
unsigned short %hu %hu
short %hd %hd
Sesi Dis 2014
char %c %c
3.2. Understand the fundamentals of C Programme.
extra notes
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
extra notes
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
extra notes
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
extra notes
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
Sesi Dis 2014
3.2. Understand the fundamentals of C Programme.
Sesi Dis 2014
3.3. Apply the fundamental of C Programming.
Observe what the output..
A program to key-in
and to view number:
Sesi Dis 2014
3.3. Apply the fundamental of C Programming.
Observe what the output..
Try make a program to
key-in and view
number:
a)23 & 24
b) 33, 155 & 112
hint.: just add more
variable
Eg: int a;
int b;
int c;
Sesi Dis 2014
3.3. Apply the fundamental of C Programming.
Observe what the output..
Program to add 2 integers
have 3 variable,
a for first number..
b for second number..
..and…..
c for the total..
Sesi Dis 2014
3.3. Apply the fundamental of C Programming.
Observe what the output..
Try make a program to key-in and add this number:
a)23 & 24
b)33, 155 & 112
hint.: just add more variable
Eg: int a;
int b;
int c;
Sesi Dis 2014
3.3. Apply the fundamental of C Programming.
Observe what the output..
Sesi Dis 2014
3.3. Apply the fundamental of C Programming.
Create C Program..
Sesi Dis 2014
3.3. Apply the fundamental of C Programming.
Create C Program
to display 1 to 100..
Sesi Dis 2014