BCSC 1102 Introduction to Programming Lecture 2 Notes
BCSC 1102 Introduction to Programming Lecture 2 Notes
Contents
1 Basic Structure of a C Program 3
1.1 Preprocessor Directives . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Global Declarations . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 The main() Function . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 Variable Declarations . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.5 Function Definitions . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.6 Statements and Expressions . . . . . . . . . . . . . . . . . . . . . 4
1.7 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.8 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.9 Explanation of the Example . . . . . . . . . . . . . . . . . . . . . 5
2 Data Types 6
2.1 Basic Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1.1 Integer Types . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1.2 Floating-Point Types . . . . . . . . . . . . . . . . . . . . . 7
2.1.3 Character Type . . . . . . . . . . . . . . . . . . . . . . . . 8
2.2 Derived Data Types . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.3 User-Defined Data Types . . . . . . . . . . . . . . . . . . . . . . 8
3 Variables in C 8
3.1 Variable Declaration . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.2 Variable Initialization . . . . . . . . . . . . . . . . . . . . . . . . 9
3.3 Variable Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.4 Local Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.5 Function Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.6 Block Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.7 Global Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.8 Static Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.8.1 Static Local Variables . . . . . . . . . . . . . . . . . . . . 11
3.8.2 Static Global Variables . . . . . . . . . . . . . . . . . . . 11
3.9 Register Variables . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.10 Extern Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1
4 Data Type Modifiers 13
4.1 Signed and Unsigned Modifiers . . . . . . . . . . . . . . . . . . . 13
4.1.1 Signed Integers . . . . . . . . . . . . . . . . . . . . . . . . 13
4.1.2 Unsigned Integers . . . . . . . . . . . . . . . . . . . . . . 13
4.2 Short and Long Modifiers . . . . . . . . . . . . . . . . . . . . . . 14
4.2.1 Short Integers . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.2.2 Long Integers . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.3 Combining Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.4 Storage Size and Value Ranges . . . . . . . . . . . . . . . . . . . 15
6 Operators 20
6.1 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . 20
6.1.1 List of Arithmetic Operators . . . . . . . . . . . . . . . . 20
6.2 Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . 21
6.3 List of Relational Operators . . . . . . . . . . . . . . . . . . . . . 21
6.4 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
6.4.1 List of Logical Operators . . . . . . . . . . . . . . . . . . 22
6.5 Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . 22
6.5.1 List of Bitwise Operators . . . . . . . . . . . . . . . . . . 22
6.6 Operator Precedence and Associativity . . . . . . . . . . . . . . . 23
6.7 Precedence Table . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2
1 Basic Structure of a C Program
A C program follows a specific structure that includes several essential compo-
nents. Understanding the basic structure is crucial for writing well-organized
and functional C programs. Here are the primary elements of a C program:
3
1.4 Variable Declarations
Variables must be declared before they are used. Declarations can be within
the main() function or other functions. Variable declarations specify the type
and name of the variable.
Example:
1 int main () {
2 int a , b , sum ; // Variable declaration
3 return 0;
4 }
1.7 Comments
Comments are non-executable parts of the code that provide explanations or an-
notations. They are ignored by the compiler. There are two types of comments
in C:
• Single-line comments (//)
• Multi-line comments (/* ... */)
Example:
4
1 // This is a single - line comment
2 /*
3 This is a
4 multi - line comment
5 */
1.8 Example
Here is a complete example of a basic C program that incorporates all the above
elements:
Code:
1 # include < stdio .h > // Include standard I / O library
2
6 // Function declaration
7 int add ( int x , int y ) ;
8
9 int main () {
10 // Local variable declarations
11 int a = 5 , b = 15;
12 int sum ;
13
14 // Function call
15 sum = add (a , b ) ;
16
20 // Return statement
21 return 0;
22 }
23
24 // Function definition
25 int add ( int x , int y ) {
26 return x + y ;
27 }
5
This line includes the Standard Input Output header file necessary for the
printf function.
2. Global Variable:
1 int globalVariable = 10;
This declares a function named add that takes two integers as parameters
and returns an integer.
4. main() Function:
1 int main () {
2 int a = 5 , b = 15;
3 int sum ;
4 sum = add (a , b ) ;
5 printf (" Sum : % d \ n " , sum ) ;
6 return 0;
7 }
This is the entry point of the program where variables are declared, the
add function is called, and the result is printed.
5. Function Definition:
1 int add ( int x , int y ) {
2 return x + y ;
3 }
This defines the add function which adds two integers and returns the
result.
2 Data Types
Understanding data types and variables is fundamental to programming in C.
Data types specify the type of data that a variable can hold, while variables are
used to store data that can be manipulated throughout the program.
C provides several built-in data types, which can be categorized into basic,
derived, and user-defined types.
6
2.1 Basic Data Types
Basic data types in C include:
• int: Used to store integers.
Example:
1 short a ;
2 int b ;
3 long c ;
4 long long d ;
Example:
1 float x ;
2 double y ;
3 long double z ;
7
2.1.3 Character Type
Character type is used to store single characters.
• char: Typically 1 byte.
Example:
1 char ch = ’A ’;
3 Variables in C
Variables are used to store data that can be manipulated throughout the pro-
gram. A variable declaration specifies the type of data it can hold and allocates
memory for it.
8
3.2 Variable Initialization
Variables can be initialized at the time of declaration.
1 int number = 10;
2 float salary = 5000.50;
3 char grade = ’A ’;
3 void function () {
4 int localVar = 10; // Local variable
5 printf (" Local Variable in function : % d \ n " ,
localVar ) ;
6 }
7
8 int main () {
9 function () ;
10 // printf (" Local Variable in main : % d \ n " , localVar
) ; // This will cause an error
11 return 0;
12 }
9
3.6 Block Scope
Local variables can also be declared within a block, which is a set of statements
enclosed in curly braces ‘‘. Variables declared within a block are accessible only
within that block.
Example:
1 # include < stdio .h >
2
3 int main () {
4 int a = 10;
5
6 if ( a > 5) {
7 int b = 20; // Block scope
8 printf (" Block Variable : % d \ n " , b ) ;
9 }
10
13 return 0;
14 }
5 void function () {
6 printf (" Global Variable in function : % d \ n " ,
globalVar ) ;
7 }
8
9 int main () {
10 function () ;
11 printf (" Global Variable in main : % d \ n " , globalVar )
;
12 return 0;
13 }
10
3.8 Static Variables
Static variables retain their value between function calls. They can be declared
with local or global scope, but they are stored in the static memory area, which
is shared across multiple function calls.
3 void function () {
4 static int count = 0; // Static local variable
5 count ++;
6 printf (" Count : % d \ n " , count ) ;
7 }
8
9 int main () {
10 function () ; // Output : Count : 1
11 function () ; // Output : Count : 2
12 function () ; // Output : Count : 3
13 return 0;
14 }
5 void function () {
6 printf (" Global Variable in function : % d \ n " ,
globalVar ) ;
7 }
8
9 int main () {
10 function () ;
11 printf (" Global Variable in main : % d \ n " , globalVar )
;
12 return 0;
11
13 }
3 void function () {
4 register int i ;
5 for ( i = 0; i < 10; i ++) {
6 printf ("% d " , i ) ;
7 }
8 printf ("\ n ") ;
9 }
10
11 int main () {
12 function () ;
13 return 0;
14 }
5 void function () {
6 printf (" Global Variable in function : % d \ n " ,
globalVar ) ;
7 }
// File2.c
1 # include < stdio .h >
2
12
4
5 int main () {
6 function () ;
7 printf (" Global Variable in main : % d \ n " , globalVar )
;
8 return 0;
9 }
13
3 unsigned long c = 100000 UL ; // Unsigned long integer
5 double x = 3.14;
6 long double y = 3 . 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 L ; // Long double
for higher precision
Example Combinations
14
4.4 Storage Size and Value Ranges
The storage size and value ranges of the modified data types depend on the
system architecture (32-bit vs. 64-bit) and the compiler. Here is a general
overview of the typical sizes and ranges:
3 int main () {
4 unsigned int a = 3000000000 U ;
5 printf (" Unsigned int : % u \ n " , a ) ;
6 return 0;
7 }
3 int main () {
4 long double pi = 3 . 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 L ;
5 printf (" Long double : %.20 Lf \ n " , pi ) ;
6 return 0;
7 }
15
Example 3: Combining Modifiers
3 int main () {
4 unsigned long long int largeNumber =
1 8 4 4 6 7 4 4 0 7 3 7 0 9 5 5 1 6 1 5 ULL ;
5 printf (" Unsigned long long int : % llu \ n " ,
largeNumber ) ;
6 return 0;
7 }
Data type modifiers in C provide additional control over the storage size and
value range of variables. By using signed, unsigned, short, and long modifiers,
programmers can optimize memory usage and ensure that variables can hold the
required range of values. Understanding and utilizing these modifiers effectively
is crucial for writing robust and efficient C programs.
5.1 Constants
Constants are variables whose value cannot be changed once defined. They are
used to represent fixed values that remain the same throughout the program.
16
1 # define MAX_SIZE 100
2 # define PI 3.14
5.1.3 Enumerations
Enumerations (enum) are user-defined data types that consist of named integer
constants. They are used to assign names to integral constants, making the
code more readable.
Example:
1 enum Days { SUNDAY , MONDAY , TUESDAY , WEDNESDAY ,
THURSDAY , FRIDAY , SATURDAY };
5.2 Literals
Literals are fixed values used directly in the code. They can be of various types,
including integer, floating-point, character, and string literals.
Example:
1 int decimal = 100;
2 int octal = 0144; // Octal representation of 100
3 int hexadecimal = 0 x64 ; // Hexadecimal representation
of 100
17
5.2.2 Floating-Point Literals
Floating-point literals represent real numbers with a fractional part. They can
be written in either decimal form or exponential (scientific) notation.
Example:
1 float decimalFloat = 3.14;
2 double scientificFloat = 1.23 e4 ; // 1.23 * 10^4
18
1 # include < stdio .h >
2
3 # define PI 3.14
4
5 int main () {
6 const int radius = 5;
7 float area ;
8
12 return 0;
13 }
3 int main () {
4 int decimal = 100;
5 int octal = 0144;
6 int hexadecimal = 0 x64 ;
7 float decimalFloat = 3.14;
8 double scientificFloat = 1.23 e4 ;
9 char letter = ’A ’;
10 char greeting [] = " Hello , World !";
11 bool flag = true ;
12
22 return 0;
23 }
Constants and literals are essential for defining fixed values in C programs.
Constants provide a way to define immutable values, improving code readability
19
and maintainability. Literals represent fixed values directly in the code and come
in various forms such as integers, floating-point numbers, characters, and strings.
Understanding and using constants and literals effectively is fundamental to
writing robust and efficient C programs.
6 Operators
Operators are special symbols in C that perform operations on variables and
values. They are used to manipulate data and variables and form the backbone
of any programming language. In C, operators are classified into several types,
including arithmetic, relational, logical, and bitwise operators.
Examples
1 int a = 10 , b = 5 , result ;
2
20
6.2 Relational Operators
Relational operators are used to compare two values. They return either true
(1) or false (0).
• Greater than (¿): Checks if the left operand is greater than the right
operand.
• Less than (¡): Checks if the left operand is less than the right operand.
• Greater than or equal to (¿=): Checks if the left operand is greater
than or equal to the right operand.
• Less than or equal to (¡=): Checks if the left operand is less than or
equal to the right operand.
Examples
1 int a = 10 , b = 5;
2 int result ;
3
21
6.4.1 List of Logical Operators
• Logical AND (&&): Returns true if both operands are true.
• Logical OR (——): Returns true if at least one of the operands is true.
• Logical NOT (!): Returns true if the operand is false and vice versa.
Examples
1 int a = 1 , b = 0;
2 int result ;
3
Examples
22
6 result = a ^ b ; // Bitwise XOR : result is 6 (0110 in
binary )
7 result = ~ a ; // Bitwise NOT : result is -6
(1111...1010 in binary )
8 result = a << 1; // Left shift : result is 10 (1010 in
binary )
9 result = a >> 1; // Right shift : result is 2 (0010 in
binary )
• Multiplicative: * / %
• Additive: + -
• Shift: << >>
• Relational: < <= > >=
• Equality: == !=
• Bitwise AND: &
• Bitwise XOR: ^
• Bitwise OR: |
• Logical AND: &&
• Logical OR: ||
• Conditional: ?:
23
Examples
1 int a = 10 , b = 5 , c = 2;
2 int result ;
3
24