Programming in C and C++: Dr. M. Babul Islam
Programming in C and C++: Dr. M. Babul Islam
Main
Memory
Computer Programming
Language
High-level
language Interpreter/ Executable code
Object code Linker
program Compiler (machine code)
Library
C Tokens
• In a C program the smallest individual units are known as C tokens.
C Tokens
Keywords:
• All keywords have their fixed meanings and these meanings cannot
be changed.
• All keywords must be written in lowercase.
Identifiers:
• Identifiers refer to the names of variables, functions and arrays.
• Both uppercase and lowercase letters are permitted.
• First character must be an alphabet or underscore ( _ ).
• Must consist of only letters, digits or underscore.
• Cannot use a keyword.
• Must not contain white space.
Constants versus Variables
Constants:
• In C constants refer to fixed values that do not change during the
execution of a program.
Constants
Numeric Character
constants constants
• Documentation Section
• Link Section
• Definition Section
• Global Declaration Section
• main ( ) Function Section
{
declaration part
executable part
}
• Subprogram Section
• Declaration of Variables:
data-type variable names;
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increment and Decrement Operators
6. Conditional Operators
7. Bitwise Operators
8. Special Operators
Arithmetic Operators
Operator Meaning
+ Addition or unary plus
- Subtraction or unary minus
* Multiplication
/ Division
% Modulo division or remainder division
Ex: 13 % 3 = 1, 9 % 10 = 9
Relational Operators
Operator Meaning
< is less than
Ex: 4.5 < 4 False
12 < 19 True
<= is less than or equal to
Ex: 7 <= 6 False
12 <= 13 True
> is greater than
>= is greater than or equal to
== is equal to
Ex: a == b True (if a and b equals)
!= is not equal to
Ex: a != b True (if a is not equal to b)
Logical Operators
Operator Meaning
&& logical AND (a && b)
|| logical OR (a || b)
! Logical NOT (!a)
Assignment Operators
• ++
++m; or m++ ; (Equivalent to m = m+1)
• --
--m; or m-- ; (Equivalent to m = m-1)
• m = 5;
y = ++m;
In this case y = 6 and m = 6.
• m = 5;
y = m++;
True
False
Ex:
a = 10;
b = 15;
x = (a > b) ? a : b;
So, x will be 15.
Bitwise Operators
Operator Meaning
& bitwise AND (a & b)
| bitwise OR (a | b)
^ bitwise ex-OR (a^b)
<< shift left
>> shift right
Special Operators
• Decision-making statements
1. if statement
2. switch statement
3. Conditional operator (?:) statement
4. goto statement
False
Test expression
True
label:
.....
.....
goto label;
statement-x;
• Loop operations:
1. The while statement
2. The do-while statement
3. The for statement
while Statement