Module 1
Module 1
PROGRAMMING IN C
Prof. Sarju S
13 January 2025
Module 1
Page 2
Module 1
► Control Statements - if, if-else, nested if, switch, while, do-while, for, break & continue,
nested loops.
Page 3 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Module 1
Page 4 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
C Programming
Page 5 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
C Programming
Page 6 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
C Fundamentals
Page 7
Getting Started with C
Page 8 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Character Set
► The character set in C programming refers to the set of valid characters that can
be used in the source code.
Uppercase Alphabets A to Z A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z
Digits 0 to 9 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Special Characters – `~@!$#^*%&()[]{}<>+=_–|/\;:‘“,.?
Page 9 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Character Representation in C
Page 10 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Identifiers and Keywords in C
► In C, identifiers and keywords are fundamental concepts for defining and using
various elements of the language.
Page 11 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Identifiers
Page 12 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Identifiers
Page 13 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Keywords
► Keywords are predefined, reserved words in C that have specific meanings and
purposes. They cannot be used as identifiers.
► Characteristics of Keywords
► Reserved: Cannot be used for any other purpose.
► Lowercase Only: All C keywords are in lowercase.
► Fixed Meaning: The meaning of a keyword cannot be changed by the user.
Examples of Keywords
Category Keywords
Data Types int, float, char, double, void, long, short
Control Flow if, else, switch, case, default, for, while, do, break, continue, return
Modifiers signed, unsigned, const, volatile, static, auto, extern, register
Storage Classes static, extern, auto, register
Others sizeof, typedef, goto, enum, struct, union, typedef, inline
Page 14 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Differences Between Identifiers and Keywords
Page 15 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Constants in C
10 // Decimal
012 // Octal (Decimal 10)
0xA // Hexadecimal (Decimal 10)
3.14
2.71828
Page 16 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Constants in C
Types of Constants in C
► Character Constants: A single character enclosed in single quotes
'A'
► String Constants: A sequence of characters enclosed in double quotes.
"Hello, World!"
► Symbolic Constants: Defined using the #define preprocessor directive or the const keyword.
#define PI 3.14159
const int MAX = 100;
Page 17 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Variables in C
IT Park - 1
► An entity that may change its value during a
program execution.
► Why variables?
#453
► In computers, the data is stored in one or many
memory locations in the primary memory.
► Each memory location has a unique address. To
refer this data a name is assigned in a program. No 453 and IT Park – 1 refers to the same building
► This name assigned to a particular memory
location is called variable.
Page 18 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Variable Naming Conventions
► studentName
► rollNumber
► employeeName
► simpleInterest
► dateOfBirth
Page 19 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Data Types in C
► Data type determines the type of value that can be stored in variable
► C language has some predefined set of data types to handle various kinds of
data that we can use in our program.
► C language supports 2 different type of data types:
► Primary data types:
► These are fundamental data types in C namely integer(int), floating point(float),
character(char) and void.
► Derived data types:
► Derived data types are nothing but primary datatypes but a little twisted or grouped
together like array, structure, union and pointers. These are discussed in details later.
Page 20 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Integer Types (int)
► Integers are those numbers that do not contain a decimal point and
decimal digit.
► Examples : 17, 7890
► There are 3 integer data types supported by C language
► Short Integer(short)
► Integer(int)
► Long Integer(long)
► Each integer can be signed or unsigned
► Unsigned integer can store only positive values
Page 21 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Integer Types (int)
Page 22 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Integer Types (int)
► Examples
► short number1; unsigned long employeeId; int number2; unsigned int countPurchased;
Memory (RAM)
int a;
a =12 204
1210 = 1100 2
203 00001100
202 00000000
00000000 00000000 00000000 00001100 201 00000000
200 00000000
byte 4 byte 3 byte 2 byte 1
Page 23 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Floating-Point Types (float and double)
Page 24 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Floating-Point Types (float and double)
► Number that can have a fractional part
► Example
► 34.5, 7856.781
► In memory, a floating point number is stored as mantissa and exponent.
Sign – 1 bit
Exponent – 8 bit
Mantissa – 23 bit
Page 25 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Character Types (char)
Page 26 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
How character is stored in Computer
Page 27 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Escape Sequences
Page 28 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
The First C Program
Page 4
Do you Know?
Page 5 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
The First C Program
Page 6 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
File Header
Brief summary of what the code and file does, who created it, when etc….
Page 7 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Include Libraries
► A header file is a file with extension .h which contains C function declarations and
macro definitions to be shared between several source files
► You request to use a header file in your program by including it with the C
preprocessing directive #include
► stdio.h - file contains standard library functions for input and output operations
Page 8 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
The main function
► Although the function main with void main() works, it is a bad programing practice
and it should be used with the signature int main()
► At the end of the main program zeros should be returned if the program performed it’s job
successfully and non zero indicates error.
Page 9 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
The printf function
Page 10 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
How c program is compiled and executed
Text Editor Source Code Header Files
Execution
Pre-Processor
Loader
Intermediate Code
Compiler
Run time
Libraries
Developer
Object Files
(.obj)
Linker Executable Files
Page 11 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Basic Input And Output In C
Page 12
Basic Input and Output in C
Page 13 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Basic Input and Output in C
► Input is the process of taking data from the user and storing it in variables. The most
commonly used function for input is scanf().
► The scanf() function reads input from the standard input (keyboard).
► It uses format specifiers to determine the type of data to read.
► format_specifier: Specifies the type of input (e.g., %d for integers, %f for floats).
► &variable: The address-of operator (&) is used to store the input at the variable's memory location.
► Output is the process of displaying information to the user. The most commonly used function
for output is printf().
► Using printf()
► The printf() function writes output to the standard output (console).It also uses format specifiers
to specify the type of data to print.
Page 14 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Basic Input and Output in C
#include <stdio.h>
int main() { Format Descriptio Example Example
int age; Specifier n Input Output
float height; Integer
%d 42 42
(decimal)
printf("Enter your age: ");
Float/Dou
scanf("%d", &age); %f 3.14 3.140000
ble
printf("Enter your height: "); Float (2
%.2f 3.14 3.14
scanf("%f", &height); decimals)
%c Character A A
printf("You are %d years old and %.2f
meters tall.\n", age, height); %s String Hello Hello
Long
%ld 123456789 123456789
return 0; integer
} %lf Double 3.14159 3.141590
Page 15 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Basic Input and Output in C
#include <stdio.h>
► Reading and Writing Characters: For int main() {
character-level input and output, char ch;
getchar(), putchar(), gets(), and
puts() can be used. printf("Enter a character: ");
ch = getchar(); // Read a single character
► Using getchar() and putchar()
► getchar(): Reads a single character from printf("You entered: ");
the user. putchar(ch); // Print the character
► putchar(): Prints a single character to printf("\n");
the console.
return 0;
}
Page 16 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Basic Input and Output in C
return 0;
}
Page 17 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Expressions
Page 4 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Operators
► They are symbols that instruct the compiler to carry out specific computations or
manipulations.
► C programming language supports a rich set of operators that are classified as follows.
► Arithmetic Operators
► Relational Operators
► Logical Operators
► Increment & Decrement Operators
► Assignment Operators
► Bitwise Operators
► Conditional Operator
► Special Operators
Page 5 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Arithmetic Operators (+, -, *, /, %)
► The arithmetic operators are the symbols that are used to perform basic
mathematical operations like addition, subtraction, multiplication,
division and percentage modulo.
► The following table provides information about arithmetic operators.
Operator Meaning Example
+ Addition 10 + 5 = 15
- Subtraction 10 - 5 = 5
* Multiplication 10 * 5 = 50
/ Division 10 / 5 = 2
% Remainder of the Division 5 % 2 = 1
Page 6 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Arithmetic Operators (+, -, *, /, %)
Output
Program
Page 7 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Relational Operators (<, >, <=, >=, ==, !=)
► The relational operators are the symbols that are used to compare two values.
► That means the relational operators are used to check the relationship
between two values.
► Every relational operator has two results TRUE or FALSE.
Page 8 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Relational Operators (<, >, <=, >=, ==, !=)
Operator Meaning Example
Returns TRUE if the first value is smaller than
< 10 < 5 is FALSE
second value otherwise returns FALSE
Page 9 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Relational Operators (<, >, <=, >=, ==, !=)
Output
Page 10 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Logical Operators (&&, ||, !)
► The logical operators are the symbols that are used to combine multiple
conditions into one condition.
► The following table provides information about logical operators.
Page 11 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Increment & Decrement Operators(++,--)
► C programming has two operators increment ++ and decrement -- to change
the value of an operand (constant or variable) by 1.
► Increment ++ increases the value by 1 whereas decrement -- decreases the
value by 1.
► These two operators are unary operators, meaning they only operate on a
single operand.
Page 12 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Example : Increment and Decrement Operators
Page 13 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Increment & Decrement Operators(++,--)
► Pre-increment and Post-increment
► Pre-increment operator: A pre-increment operator is used to increment the
value of a variable before using it in a expression.
► In the Pre-Increment, value is first incremented and then used inside the
expression.
► Syntax: a = ++x;
► Here, if the value of ‘x’ is 10 then value of ‘a’ will be 11 because the value of
‘x’ gets modified before using it in the expression.
Page 14 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Increment & Decrement Operators(++,--)
► Pre-increment and Post-increment
► Post-increment operator: A post-increment operator is used to increment the value
of variable after executing expression completely in which post increment is used.
► In the Post-Increment, value is first used in a expression and then incremented.
► Syntax: a = x++;
► Here, suppose the value of ‘x’ is 10 then value of variable ‘b’ will be 10 because old
value of ‘x’ is used.
Page 15 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Assignment Operators (=)
► An assignment operator is used for assigning a value to a variable.
► The most common assignment operator is =
= a=b a=b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
Page 16 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Example : Assignment Operators
Page 17 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Bitwise Operators
► In arithmetic-logic unit (which is within the CPU), mathematical operations like:
addition, subtraction, multiplication and division are done in bit-level.
► To perform bit-level operations in C programming, bitwise operators are used.
| Bitwise OR
^ Bitwise XOR
~ Bitwise complement
Page 18 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Bitwise AND operator (&)
► The output of bitwise AND is 1 if the
corresponding bits of two operands is 1.
► If either bit of an operand is 0, the result of
corresponding bit is evaluated to 0.
► Let us suppose the bitwise AND operation of two
integers 12 and 25.
Page 19 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Bitwise OR operator (|)
► The output of bitwise OR is 1 if at least one
corresponding bit of two operands is 1.
► In C Programming, bitwise OR operator is
denoted by |.
Page 20 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Bitwise XOR (exclusive OR) operator (^)
► The result of bitwise XOR operator is 1 if the
corresponding bits of two operands are
opposite. It is denoted by ^.
Page 21 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Shift Operators
► There are two shift operators in C programming:
► Right shift operator
► Left shift operator.
► Right Shift Operator
► Right shift operator shifts all bits towards right by certain number of specified bits. It
is denoted by >>.
Page 22 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Shift Operators
► There are two shift operators in C programming:
► Right shift operator
► Left shift operator.
► Left Shift Operator
► Left shift operator shifts all bits towards left by a certain number of specified bits.
The bit positions that have been vacated by the left shift operator are filled with 0.
The symbol of the left shift operator is <<.
Page 23 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Conditional Operator in C ( ?: )
► The conditional statements are the decision-making statements that depend
upon the output of the expression.
► As a conditional operator works on three operands, so it is also known as the
ternary operator.
► It starts with a condition; hence it is called a conditional operator.
► Conditional operators return one value if condition is true and returns another
value is condition is false.
► Syntax
► expression1 ? expression2 : expression3;
► or for simplicity, we write it as condition ? true-statement : false-statement;
Page 24 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Conditional Operator ( ?: ) - Example
Page 25 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Conditional Operator in C ( ?: ) - Example
► Write a C Program to input a number from the keyboard and check whether number is
positive or negative using the conditional operator.
Page 26 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Conditional Operator ( ?: ) - Exercises
► Find the given number is odd or even using the conditional operator in C.
► Program to Find the minimum of two numbers using the conditional operator.
► Write a C Program to find largest among three numbers using the conditional
operator.
Page 27 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Special Operators
Comma Operator
► The comma operator in C allows multiple expressions to be evaluated in a single
statement.
► It evaluates the expressions from left to right and returns the value of the last
expression.
► The comma operator is primarily used to combine multiple expressions into one.
#include <stdio.h>
int main() {
int a, b, result;
return 0;
}
Page 28 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Special Operators
► The sizeof operator
► The sizeof is a unary operator that returns the size of data (constants,
variables, array, structure, etc).
Page 29 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Operators Precedence
x = 34 + 12 / 4 - 45
► Operator precedence determines the grouping of terms in an
expression and decides how an expression is evaluated.
► Certain operators have higher precedence than others
Page 30 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Operators Precedence table in C
Page 31 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Operators Precedence in C
Page 32 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Typecasting in C
► Typecasting is converting one data type into another one.
► It is best practice to convert lower data type to higher data type to avoid
data loss.
► Data will be truncated when the higher data type is converted to lower.
► For example, if a float is converted to int, data which is present after the decimal
point will be lost.
► C programming provides two types of type casting operations:
► Implicit type casting
► Explicit type casting
Page 33 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Implicit Type Conversion
► Also known as ‘automatic type
conversion’.
► Done by the compiler on its own,
without any external trigger from the
user.
► Generally, takes place when in an
expression more than one data type is
present.
► In such condition type conversion (type
promotion) takes place to avoid loss of
data.
► All the data types of the variables are
upgraded to the data type of the
variable with largest data type.
Page 34 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Implicit Type Conversion
Page 35 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Explicit type casting
► In implicit type conversion, the data type is converted automatically.
► There are some scenarios in which we may have to force type
conversion.
► Suppose we have a variable div that stores the division of two operands
which are declared as an int data type.
► int result, var1=10, var2=3;
► result=var1/var2;
► fraction part which is normally obtained in the division of two numbers
► To force the type conversion in such situations, we use explicit type
casting.
► Syntax
► (type-name) expression
Page 36 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Explicit type casting
Page 37 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Explicit type casting
Page 38 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Control Flow Statements
Page 4
Control Structures
► The way in which the programmer specifies the order of execution of the
statements.
► Different set of statements can be executed based on a condition.
► Sequential
► Selection
► Iteration (Repetition)
Page 5
Selection Control Structures
Page 6
Simple If Statement
if (Condition) { True
Condition
statement(s);
}
Next Statement; False Set of Statement(s)
Next Statement
Page 7
Simple If Statement - Example
if (duration>=3) {
rateOfInterest = 6.0;
}
Page 8
Simple If Statement
► Points to Remember:
► The if statement does not end with a semicolon
► Only statements follows the if, should end with a semicolon
Page 9
If…… Else Statement
if (Condition) {
True statement(s);
} False Statement(s) True Statement(s)
else{
False statement(s);
Next Statement
}
Next Statement;
Page 10
If…… Else Statement- Example
if (duration>=3) {
rateOfInterest = 6.0;
}
False Duration> True
else{ =3
rateOfInterest = 5.5;
} rateOfInterest = 6.0
rateOfInterest = 5.5
Next Statement
Page 11
Else… If Statement
► The else..if statement is to check for a if (test - expression 1) {
sequence of conditions. statement1;
is executed.
} else if (test - expression n){
Statement n;
} else {
default;
False False False
condition -1 condition -2 condition -n
}
Statement x;
True True
True
Page 12
Else… If Statement
Page 13
Else… If Statement
Page 14
Nested if
► If an ‘if’ statement embedded within another ‘if’ statement is called nested if
► Syntax
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
Page 15
switch case statement
► Switch statement is a control statement that switch(expression)
allows us to choose only one choice among {
Page 16
Rules for using switch statement
► The expression (after switch keyword) must yield an integer value i.e the
expression should be an integer or a variable or an expression that
evaluates to an integer.
► The case label values must be unique.
► The case label must end with a colon(:)
► The next line, after the case statement, can be any valid C statement.
Page 17
switch statement Example
Page 18
switch case statement
► Points to Remember:
► We don't use those expressions to evaluate switch case, which may
return floating point values or strings or characters.
► break statements are used to exit the switch block.
► It isn't necessary to use break after each block, but if you do not use it,
then all the consecutive blocks of code will get executed after the
matching block.
► default case is executed when none of the mentioned case matches
the switch expression. The default case can be placed anywhere in the
switch case. Even if we don't include the default case, switch statement
works.
Page 19
switch case statement
► Points to Remember:
► if statements can evaluate float conditions. switch statements cannot
evaluate float conditions.
► if statement can evaluate relational operators. switch statement cannot
evaluate relational operators i.e they are not allowed in switch
statement.
Page 20
switch statement
Page 21
Iteration Control Structures in C
► Iteration control structures are used to repeat certain statements for a specified
number of times.
► The statements are executed as long as the condition is true.
► These types of control structures are called loop control structures.
► There are 3 kinds of loop supported by C language
► while
► do while
► for
► The while loop is used when the number of iterations is not known
beforehand. It checks the condition before executing the loop body.
► Syntax
while (condition) {
statement(s);
} True
condition Set of statements
next Statement;
► Steps: False
True
print count
Count<=10
count = count+ 1
False
Next statements
► The do-while loop is similar to the while loop, but the condition is
checked after executing the loop body. This ensures the loop executes at
least once.
► It is also called an exit-controlled loop.
► Syntax Set of statements
do {
statement(s)
True
} while (condition); condition
next Statement;
► Steps: False
do {
printf(“Enter a number. Type 0(zero) to end the input”);
scanf(“%d”,&number);
sum =sum+number;
} while (number!=0);
Page 9
for loop
Page 10
for loop, while loop, do-while loop
Page 11
Controlling Loops
Output
for(int i = 1; i <= 5; i++) {
if(i == 3) break; 1
printf("%d\n", i); 2
}
Page 12
Controlling Loops
► Continue Statement: Skips the current iteration and moves to the next.
Output
for(int i = 1; i <= 5; i++) {
if(i == 3) continue; 1
printf("%d\n", i); 2
} 4
5
Page 13
Controlling Loops
#include <stdio.h>
int main() {
int i = 1;
loop:
if(i > 5) return 0;
printf("%d\n", i);
i++;
goto loop;
}
Page 14
Thank You
Prof. Sarju S
Department of Computer Science and Engineering
St. Joseph’s College of Engineering and Technology, Palai (Autonomous)
[email protected]
Page 15 Disclaimer - This document contains images/texts from various internet sources. Copyright belongs to the respective content creators.
Document is compiled exclusively for study purpose and shall not be used for commercial purpose.