[go: up one dir, main page]

0% found this document useful (0 votes)
60 views73 pages

Gxest204 CP Module I Handout

The document outlines the syllabus and course outcomes for the GXEST204 Programming in C module, prepared by Binoy K. P. at GEC Kozhikode. It covers fundamental concepts of C programming, including control statements, arrays, functions, pointers, and file handling. Additionally, it includes a mapping of course outcomes to program outcomes and references for further reading.

Uploaded by

aninsadath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views73 pages

Gxest204 CP Module I Handout

The document outlines the syllabus and course outcomes for the GXEST204 Programming in C module, prepared by Binoy K. P. at GEC Kozhikode. It covers fundamental concepts of C programming, including control statements, arrays, functions, pointers, and file handling. Additionally, it includes a mapping of course outcomes to program outcomes and references for further reading.

Uploaded by

aninsadath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 73

MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

PROGRAMMING IN C

GXEST204
PROGRAMMING IN C
MODULE I
PREPARED BY
BINOY K. P.
ASSISTANT PROFESSOR
DEPT OF AE&I ENGG.
GEC KOZHIKODE

COURSE OUTCOMES
 After the completion of the course the student will be able to
COs DESCRIPTION
Infer a computational problem and develop C programs from them using basic
CO1
constructs of C language including the control statements.
CO2 Develop C programs using arrays, matrices, and strings.

Utilize functions to find solution to the computational problems by dividing it


CO3
into a number of modules and abstract data types.

CO4 Develop C programs using pointers for dynamic data handling.

CO5 Use files in C to permanently store and manipulate data.


PREPARED BY 2
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE

GEC KOZHIKODE 1
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

CO-PO-PSO MAPPING

COs PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2 PSO3

CO1 3 3 3 3 3

CO2 3 3 3 3 3

CO3 3 3 3 3 3

CO4 3 3 3 3 3

CO5 3 3 3 3 3

PREPARED BY
3
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE

SYLLABUS
 MODULE I
C Fundamentals
 Character Set, Constants, Identifiers, Keywords, Basic Data types, Variables, Operators and
its precedence, Bit-wise operators,
 Expressions; Statements - Input and Output statements;
 Structure of a C program;
 Simple programs.
 Control Statements - if, if-else, nested if, switch, while, do-while, for, break & continue,
nested loops.

PREPARED BY 4
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE

GEC KOZHIKODE 2
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

SYLLABUS
 MODULE II
Arrays
 Single dimensional arrays, Defining an array, Array initialization, Accessing array elements;
 Enumerated data type; Type Definition;
 Two dimensional arrays – Defining a two-dimensional array;
 Programs for matrix processing; Programs for sequential search; Bubble sort;

 Strings
 Declaring a string variable, Reading and displaying strings, String related library functions
 Programs for string matching.

PREPARED BY
5
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE

SYLLABUS
 MODULE III
Functions
 Function definition, Function call, Function prototype, Parameter passing; Recursion;
Passing array to function; Macros – Defining and calling macros; Command line
Arguments.
Structures
 Defining a Structure variable, Accessing members, Array of structures, Passing structure to
function; Union.
Storage Class
 Storage Classes associated with variables: automatic, static, external and register.

PREPARED BY 6
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE

GEC KOZHIKODE 3
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

SYLLABUS
 MODULE IV
Pointers
 Declaration, Operations on pointers, Passing pointer to a function, Accessing array elements
using pointers, Processing strings using pointers, Pointer to pointer, Array of pointers,
Pointer to function, Pointer to structure, Dynamic Memory Allocation.

 Files
 Different types of files in C, Opening & Closing a file, Writing to and Reading from a file,
Processing files,
 Library functions related to file – fseek(), ftell(), fread(), fwrite().

PREPARED BY
7
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE

REFERENCES
1. Schaum Series, Gottfried B.S.,Tata McGraw Hill, Programming with C, 4th Edition July
2018
2. Brian W. Kernighan and Dennis M. Ritchie, Pearson, C Programming Language, 2nd
Edition January 2015
3. Herbert Schildt, C The Complete Reference, Mc Graw Hill Education, 4th Edition July
2017
4. E. Balagurusamy, Programming in ANSI C, Mcgraw Hill, 8th Edition March 2019
5. Yashavant P, Kanetkar, Let us C, BPB Publications, 19th Edition December 2022
6. Asok N Kamthane, Programming in C, Pearson, 3rd Edition January 2015
7. Rajaraman V, PHI, Computer Basics and Programming in C, 2nd July 2019

PREPARED BY 8
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE

GEC KOZHIKODE 4
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

MODULE I

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE 9

SYLLABUS
 MODULE I
C Fundamentals
 Character Set, Constants, Identifiers, Keywords, Basic Data types, Variables, Operators and
its precedence, Bit-wise operators,
 Expressions; Statements - Input and Output statements;
 Structure of a C program;
 Simple programs.
 Control Statements - if, if-else, nested if, switch, while, do-while, for, break & continue,
nested loops.

PREPARED BY 10
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE

GEC KOZHIKODE 5
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

1. BASICS OF C PROGRAMMING

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE 11

GENERAL ASPECT OF ‘C’


C was originally developed in the 1972, by Dennis Ritchie at Bell Telephone Laboratories,
Inc.
C is a High level, general purpose structured programming language.
Instructions of C consists of terms that are very closely same to algebraic expressions,
consisting of certain English keywords such as if, else, for ,do and while.
C contains certain additional features that allows it to be used at a lower level, acting as
bridge between machine language and the high level languages.
This allows C to be used for system programming as well as for applications programming

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
12

GEC KOZHIKODE 6
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

BASIC STRUCTURE OF
C PROGRAM

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE 13

THE C CHARACTER SET


The character set is the fundamental raw material of any language and are used to represent
information.
A character denotes any alphabet, digit or special symbol used to represent information.
The Character set of C consist of

Alphabets A to Z, a to z
Digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Special Characters ~ ‘ ! @ # % ^ & * ( ) _ - + = | \ { } [ ] : ; " ' < > , . ? /
White Spaces Space, back space, horizontal tab, new-line, vertical tab, form feed

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
14

GEC KOZHIKODE 7
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

THE C CHARACTER SET


 Special Characters are represented in the computer by numeric values. Widely used is
ASCII code.
 White Spaces are any character or series of characters that represent horizontal or vertical
space in typography.
 White Spaces does not correspond to a visible mark, but typically does occupy an area on
a page.
 The C language defines whitespace characters to be:
space horizontal tab - \t back space - \b
new-line - \n vertical tab - \v form feed - \f
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
15

THE C CHARACTER SET


 ESCAPE SEQUENCES
For using characters which cannot be typed or has special meaning in C programming
When a character is preceded by a backslash (\), it is called an escape sequence and it has a
special meaning to the compiler.
For example: \n is used for newline.
The backslash ( \) causes "escape" from the normal way the characters are interpreted by
the compiler

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
16

GEC KOZHIKODE 8
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

THE C CHARACTER SET


 ESCAPE SEQUENCES

\a alert(bell) character \\ backslash


\b backspace \? question mark
\n newline \’ single quote
\t horizontal tab \" double quote
\v vertical tab \ooo octal number
\f form feed \xhh hexa decimal number
\r carriage return

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
17

C TOKENS
 The words formed from the character set are building blocks of C and are known as
tokens.
 Tokens are the smallest building block or unit of a C program.
 These tokens represent the individual entity of language.
 The compiler breaks a program into smallest possible units and identifies them as tokens.

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
18

GEC KOZHIKODE 9
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

TYPES OF C TOKENS

 KEYWORDS

 IDENTIFIERS

 CONSTANTS

 OPERATORS

 SPECIAL CHARACTERS

 STRINGS

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
19

KEYWORDS
 Keywords are reserved words of the
auto double int struct
language.
 They have specific meaning in the break else long switch
language and cannot be used by the case enum register typedef
programmer as variable or constant
char extern return union
names.
 C is case sensitive, it means these must const float short unsigned
be used as it is. continue for signed void
 32 Keywords in C Programming
default goto sizeof volatile
do if static while

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
20

GEC KOZHIKODE 10
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

IDENTIFIERS
 Identifiers are user defined names for entities in a C program, such as variables, arrays,
functions, structures, unions, pointers and labels.

 Rules for identifier names


 Made up of letters and digits.
 Underscore(_) counts as letter.
 The first character must be a letter.
 So, we can begin identifier names with underscore, but it is better to avoid, since
library routines often use such names.
 Commas or blank spaces are not allowed within an identifier.
 Uppercase and lower case letters are distinct. Traditional approach is using lowercase
for variables and uppercase for constants.
 Keywords cannot be used as identifiers.
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
21

KEYWORD V/S IDENTIFIER


KEYWORD IDENTIFIER
Predefined-word User-defined word

Must be written in lowercase only Can written in lowercase and uppercase

Has fixed meaning Must be meaningful in the program


Whose meaning has already been explained to
Whose meaning not explained to the C compiler
the C compiler
Combination of alphabetic characters Combination of alphanumeric characters

Used only for its intended purpose Used for required purpose

Underscore character is not considered as a letter Underscore character is considered as a letter


PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
22

GEC KOZHIKODE 11
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

QUESTION?
 Tell whether the following variables names are valid or not?
 int name50;
 int 2nd;
 int _area;
 int a_1;
 int char;
 int CHAR;
 int var 1;
 int name.1;
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
23

ANSWER
 Tell whether the following variables names are valid or not?
 int name50;
 int 2nd;
 int _area;
 int a_1;
 int char;
 int CHAR;
 int var 1;
 int name.1;
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
24

GEC KOZHIKODE 12
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

printf & scanf


 The printf() function is used for output.
 It prints the given statement to the console.
 Syntax: printf(“format string”,argument_list);

 The scanf() function is used for input.


 It reads the input data from the console.
 Syntax: scanf(“format string”,argument_list);

 The format string can be %d (integer), %c (character), %s (string), %f (float) etc.


PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
25

printf & scanf


 Example – Cube of a number
& in scanf
#include<stdio.h> It is used to access the address of the
int main() variable used.
{ Eg:- scanf(“%d”, &a);
int number; we are reading into the address of a.
printf("enter a number:");
scanf("%d",&number);
printf("cube of number is:%d ", number*number*number);
return 0;
}

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
26

GEC KOZHIKODE 13
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

CONSTANTS
 A constant is an entity that doesn’t change its value during the execution of a program.
 Classification of C constants

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
27

CONSTANTS
 INTEGER CONSTANT
 Constants represented with whole numbers.
 Memory requirement- minimum of 2 bytes and maximum of 4 bytes.
 Rules for constructing integer constants:
An integer constant must have at least one digit.
It must not have a decimal point, fractional part or symbols.
It can be either positive or negative or may be zero.
If no sign precedes an integer constant it is assumed to be positive.
No commas or blanks are allowed within an integer constant.
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
28

GEC KOZHIKODE 14
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

CONSTANTS
 INTEGER CONSTANT
 For a 16 bit compiler, the allowable range for integer constants is -32768 to 32767.
 There are three types of integer constants in C programming:
 decimal constant(base 10)
 octal constant(base 8)
 hexadecimal constant(base 16)
 Octal numbers in C would be done with leading digit 0 and for hexadecimal with leading
0X.

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
29

OCTAL AND HEXADECIMAL NUMBERING


 Sample Program

#include<stdio.h>
void main ( )
{
int a , b , sum ;
a=070 ;
b=0X2A ;
sum=a+b ;
printf ( "\nSum is %d" , sum ) ;
}
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
30

GEC KOZHIKODE 15
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

CONSTANTS
 REAL CONSTANT
 Also known as floating point constants.
 Can be represented in fractional form or exponential form.
 Rules for constructing real constants in fractional form:
A real constant must have at least one digit.
Decimal point is permitted.
It could be either positive or negative.
No commas or blanks are allowed within a real constant.

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
31

CONSTANTS
 REAL CONSTANT
 Rules for constructing real constants in exponential form:
The mantissa part and the exponential part should be separated by a letter e.
The mantissa part may have a positive or negative sign.
The mantissa should be either a real number represented in decimal or integer form.
The exponent must have at least one digit, which must be a positive or negative
integer. Default sign is positive.

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
32

GEC KOZHIKODE 16
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

CONSTANTS
 CHARACTER CONSTANT
 Single Character Constants
 A character constant is a single alphabet, a single digit or a single
 Special symbol enclosed within single inverted commas.
 Length of the character is one character.
 Character constants have integer values known as ASCII values.
 Example : ’a’, ’A’

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
33

CONSTANTS
 CHARACTER CONSTANT
 String Constants
 String constants are a sequence of characters enclosed within
 Double quotes.
 String may be a combination of all kinds of characters.
 Example : "Hello", "Welcome to C", "45465"

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
34

GEC KOZHIKODE 17
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

VARIABLES
 A variable is a data name used to store a data value.
 A variable may take different values at different times during execution.
 Each variable in C has a specific type, which determines the size and layout of the
variable's memory; the range of values that can be stored within that memory; and the set
of operations that can be applied to the variable.
 A variable name can be chosen by the programmer in a meaningful way.
 Rules for giving variable names is same as that of identifiers.

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
35

VARIABLES
 Variable Declaration
 All variables must be declared before use.
 There are two purposes:
1. It tells the compiler what the variable name is.
2. It specifies what type of data the variable will hold.

 Syntax
 datatype variable_list;

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
36

GEC KOZHIKODE 18
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

VARIABLES
The following are the basic variable types

TYPE DESCRIPTION
char Typically a single octet(one byte). This is an integer type.
int The most natural size of integer for the machine.
float A single-precision floating point value.
double A double-precision floating point value.
void Represents the absence of type.

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
37

VARIABLES
 Example
int a, b, length;
float radius;
char c, flag;

 A variable may be initialized in its declaration.


int i=0;
char c=’A’;
float b=2*3.14;

 If it is not initialized its value is undefined (i.e. garbage).


PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
38

GEC KOZHIKODE 19
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

DATA TYPES IN C

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE 39

DATA TYPES IN C
 Data type specifies the type of data that a variable can store such as integer, floating,
character, etc.
 Data type constitute the characteristics of storage of data elements.
 Data types specify how we enter data into our programs and what type of data we enter.
 C language has some predefined set of data types to handle various kinds of data that we
can use in our program.
 They are expressed in the C syntax in form of declarations for memory locations or
variables.
 These datatypes have different storage capacities.
 Data types also determine the types of operations or methods of processing of data
elements.

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
40

GEC KOZHIKODE 20
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

DATA TYPES IN C

Classification of Data Types

Primary Data Types Derived Data Types User Defined Data Types
 int  Array  Structure
 char  Pointer,  Union
 float  Reference, etc.,  Enum
 double
 void

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
41

PRIMARY DATA TYPES


 Primary data types, also known as ‘primitive data type’ or ‘fundamental data type’, are
the built-in data types that are provided by the programming language.
 It defines the most basic data like int, char, float, etc.

 Primary data types could be of several types like an int can be unsigned int, short int,
unsigned long int, etc.
 With such a wide range of classification and variety, the programmer has got many
different data types to choose from as per the requirement and use it in their code along
with the advantage of the secondary data types.

 boolean, byte, char, short, int, long, float, double, void are the most commonly
used primary data type

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
42

GEC KOZHIKODE 21
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

PRIMARY DATA TYPES


 char
 Occupies single byte
 Capable of holding one character
 int
 An integer, occupies 2 bytes or 4 bytes.
 Used to store whole numbers.
 float
 Occupies 4 bytes, capable of holding one single-precision floating point number.
 Used to store real numbers.
 double
 Occupies 8 bytes, capable of holding one double-precision floating point number.
 Used to store real numbers.
 void
 void type means no value.
 This is usually used to specify the type of functions which returns nothing.
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
43

sizeof Operator
 To get the exact size of a type or a variable on a particular platform, you can use the
sizeof operator

 Syntax
sizeof (type) or sizeof (variable)

 Example: sizeof(int), sizeof(double), sizeof(x),...

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
44

GEC KOZHIKODE 22
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

sizeof Operator - Sample Program


#include <stdio.h>
void main ( )
{
int a = 16 ;
printf ( "Size of variable a : %d\n" , sizeof ( a ) ) ;
printf ( "Size of int data type : %d\n" , sizeof ( int ) ) ;
printf ( "Size of char data type : %d\n" , sizeof ( char ) ) ;
printf ( "Size of float data type : %d\n" , sizeof ( float ) ) ;
printf ( "Size of double data type : %d\n" , sizeof ( double ) ) ;
}

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
45

sizeof Operator - Sample Program


Output

Size of variable a : 4
Size of int data type : 4
Size of char data type : 1
Size of float data type : 4
Size of double data type : 8

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
46

GEC KOZHIKODE 23
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

QUALIFIERS IN C
 In addition to basic data types, there are a number of qualifiers
 A type qualifier is a keyword that is applied to a basic type, resulting in a qualified type.
 Qualifiers
 short and long - can be applied to int
 Eg:- short int a; long int b, c;
 or simply short a; long b, c;
 signed and unsigned - can be applied to char or any integer
 Eg:- unsigned char, unsigned int, signed long,...
 long can be applied to double to get extended precision floating point number
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
47

DATA TYPES
AND SIZES
 Characters and
Integers

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
48

GEC KOZHIKODE 24
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

DATA TYPES AND SIZES


 Floating Point

Type Storage Size Range Precision


float 4 bytes 1.2E-38 to 3.4E+38 6 decimal places
double 8 bytes 2.3E-308 to 1.7E+308 15 decimal places
long double 10 bytes 3.4E-4932 to 1.1E+4932 19 decimal places

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
49

DERIVED DATA TYPES


 Derived data types are basically derived from the primary data types.
 They ae also called Secondary data types.

 Array
 Arrays are sequences of data items having homogeneous values.
 They have adjacent memory locations to store.
 Array in C stores multiple values of the same data type.
 That means we can have an array of integers, chars, floats, doubles etc…

 Pointer
 A pointer is just a variable that stores the address of another variable.
 A pointer can store the address of variables of any data types.

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
50

GEC KOZHIKODE 25
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

USER DEFINED DATA TYPES


 The user-defined data type defines the data in the way that the programmer chooses.

 Commonly used user-defined data types are

 Structure
 It is a collection of variables of different data types represented by the same name.
 Store data of different data types under the same variable name.
 It is mostly used to form records where different specifications need to be stored
under the same name.
 The struct keyword is used to define a structure.

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
51

USER DEFINED DATA TYPES


 Union
 Another data type that is very similar to structures.
 It allows the programmer to store data of different data types in the same memory location.
 A union can have multiple members but it can store only one member at a particular time.
 The keyword union is used to define a Union.

 Enum
 Enumeration data types enhance the readability of the code.
 Enumeration Data Type (enum) is used to define variables that can only assign certain
discrete integer values throughout the program.
 The keyword enum is used to define the enumeration data type.
 Eg: enum identifier{element1,element2,…….,elementn};
 It assigns the value from 0 to n to the elements present inside the identifier sequentially.

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
52

GEC KOZHIKODE 26
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

FORMATTING THE OUTPUT - SPECIFIERS


 Used during input and output.
 It is a way to tell the compiler what
type of data is in a variable during
taking input or printing output.

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
53

FORMATTING THE OUTPUT - MODIFIERS


 Each specifier can be preceded by a modifier which determines how the value will be
printed.
 The most general modifier is of the form: flag width.precision

 width specifies the minimum number of characters used in total to display the value
 precision indicates the number of characters used after the decimal point

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
54

GEC KOZHIKODE 27
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

FORMATTING THE OUTPUT - MODIFIERS


%6d : Prints as decimal integer, at least 6 characters wide
%.2f : Print as floating point, 2 characters after decimal point
%6.2f : Print as floating point, at least 6 wide and 2 characters after decimal
%-10d : Will display an integer left justified in a ten character space
%#o : adds a leading 0 to the octal value
%#x : adds a leading 0x to the hex value
%#f or %#e : ensures decimal point is printed
%#g : displays trailing zeros

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
55

FORMATTING THE OUTPUT - MODIFIERS


 %s is used for string format
 If we use a width specifier then the string will be right justified within the space
 If we include a precision specifier then it print only that number of characters

 printf("%s","Hello") will print Hello


 printf("%25s","Hello") will print 25 characters with Hello right justified
 printf("%25.3s","Hello") will print Hel right justified in a group of 25 spaces

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
56

GEC KOZHIKODE 28
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

DATA TYPE
&
FORMAT

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
57

EXAMPLE - 1
#include <stdio.h>
void main( )
{
int a = 16 ;
printf ( "%d, %i, %o, %x\n" , a , a , a , a ) ;
printf ( "%o, %#o, %x, %#x\n" , a , a , a , a ) ;
printf ( "|%10d| |%-10d| |%010d| |%+10d|" , a , a , a , a ) ;
}

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
58

GEC KOZHIKODE 29
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

EXAMPLE - 1
Output :

16, 16, 20, 10


20, 020, 10, 0x10
| 16| |16 | |0000000016| | +16|

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
59

EXAMPLE - 2
#include <stdio.h>
void main( )
{
float b = 16 . 375 ;
printf ( "|%f| |%g| |%e| |%a|\n" , b , b , b , b ) ;
printf ( "|%3f| |%20f|\n" , b , b ) ;
printf ( "|%10.2f| |%.f|\n" , b , b ) ;
}

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
60

GEC KOZHIKODE 30
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

EXAMPLE - 2
Output :

|16.375000| |16.375| |1.637500e+001| |0x1.060000p+4|


|16.375000| | 16.375000|
| 16.38| |16|

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
61

CONSTANTS USING const KEYWORD


 const keyword can be used to declare constant variables.
 Constant variables are variables which when initialized, can’t change their value
 Syntax
const data_type var_name = var_value;
 Example
const float PI=3.14159;
const int MAX=100;

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
62

GEC KOZHIKODE 31
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

CONSTANTS USING #define PREPROCESSOR


 Syntax
#define name value;
 Examples
#define PI 3.14159;
#define MAX 100;
 Here PI and MAX are not variables.
 In preprocessing step name will be replaced with the value.
 So PI will be replaced with 3.14159 in the program.

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
63

OPERATORS AND EXPRESSIONS

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE 64

GEC KOZHIKODE 32
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

OPERATORS IN C
 An operator is a symbol which operates on a value or a variable.
Arithmetic Operators
Increment and Decrement Operators
Assignment Operators
Relational Operators
Logical Operators
Conditional Operators
Bitwise Operators

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
65

ARITHMETIC OPERATORS
The binary arithmetic operators are +, -, *, /, and the modulus operator %.

Operator Meaning Function Example


+ Addition or unary plus Returns the sum 20+10=30
- Subtraction unary minus Returns the difference 20-10=10
* Multiplication Returns the product 20*10=200
/ Division Returns the quotient 20/10=2
% Modulus Returns the remainder 20%10=0

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
66

GEC KOZHIKODE 33
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

ARITHMETIC OPERATORS
10/4=2 and 10.0/4.0=2.5
Integer division truncates any fractional part
The % operator cannot be applied to a float or double
Precedence of *, /, % are same, which is higher than the precedence of + and -.
Arithmetic operators associate left to right.
Eg:- 2+3-5/2+3*10%3 is

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
67

INCREMENT AND DECREMENT OPERATORS


Increment operator (++) is used to increase the value of the variable by one
Decrement operator (--) is used to decrease the value of the variable by one
Both can be used either as prefix operator or postfix operator

Pre-increment/Pre-decrement
++variable_name; & --variable_name;
Post-increment/Post-decrement
variable_name++; & variable_name--;

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
68

GEC KOZHIKODE 34
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

INCREMENT AND DECREMENT OPERATORS


 Example: ++n; n++; --n; n--;
Both ++n and n++ will increment the value of n by one
++n increments n before its value is used
n++ increments n after its value has been used

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
69

INCREMENT AND DECREMENT OPERATORS


#include <stdio.h>
int main ( )
{
int x=5 ;
printf( "%d\n" , x ) ;
printf( "%d\n" ,x++ ) ;
printf( "%d\n" , x ) ;
printf( "%d\n" ,++x ) ;
printf( "%d\n" , x ) ;
return 0 ;
}
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
70

GEC KOZHIKODE 35
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

INCREMENT AND DECREMENT OPERATORS


#include <stdio.h> Output :
int main ( ) 5
{ 5
int x=5 ; 6
printf( "%d\n" , x ) ; 7
printf( "%d\n" ,x++ ) ; 7
printf( "%d\n" , x ) ;
printf( "%d\n" ,++x ) ;
printf( "%d\n" , x ) ;
return 0 ;
}
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
71

ASSIGNMENT OPERATORS
 In C, values for the variables are assigned using assignment operators.
 There are 2 categories of assignment operators in C language.
1. Simple assignment operator (=)
2. Compound assignment operators (+=, -=, *=, /=, %=, &=, |=, ∧=, <<=, >>= )

 exp1+=exp2 is equivalent to exp1=(exp1)+(exp2)


 For Ex: x/=10; means x=x/10;
 x*=y+10; is equivalent to x=x*(y+10); not x=x*y+10;

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
72

GEC KOZHIKODE 36
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

ASSIGNMENT OPERATORS

Operator Example Same as


= 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
 Assignment operator makes the code easier to understand
 It may even help a compiler to produce efficient code.
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
73

RELATIONAL OPERATORS
A relational operator checks the relationship between two operands. If the relation is true, it
returns 1; if the relation is false, it returns value 0.
Relational operators are used in decision making and loops.
Operator Meaning Example
== Equal to 5 == 3 returns 0
< Less than 5 < 3 returns 0
> Greater than 5 > 3 returns 1
!= Not equal to 5 != 3 returns 1
<= Less than or equal to 5 <= 3 returns 0
>= Greater than or equal to 5 >= 3 returns 1
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
74

GEC KOZHIKODE 37
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

CONDITIONAL OPERATORS
 The conditional operator ? : is also called ternary operator since it take three arguments.
 Syntax: expr1 ? expr2 : expr3

 If expr1 is true (non-zero), then the value of ternary operator will be expr2, otherwise the
value will be expr3
 For example: To find maximum of two numbers, we can write max=(a>b)?a:b; instead of
if(a>b)
max=a;
else
max=b;
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
75

CONDITIONAL OPERATORS
 Precedence of ?: is very low, just above assignment operators
 Nesting of conditional expression is allowed
 Example : max=(a>b)?((a>c)?a:c):((b>c)?b:c);

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
76

GEC KOZHIKODE 38
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

LOGICAL OPERATORS

Operator Meaning Example

&& Logical AND True when both are true

|| Logical OR True when either one is true


Converts non zero operand into 0 and 0
! Unary Negation
operand into 1

 The precedence of && is higher than that of ||, and both are lower than relational operators

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
77

BITWISE OPERATORS
 To perform bit-level operations in C programming, bitwise operators are used.

Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise Exclusive-OR
<< Left Shift
>> Right Shift
~ One’s Complement
 Bitwise operations can be performed on any type of integer value in C but not on floating
point values
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
78

GEC KOZHIKODE 39
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

BITWISE OPERATORS
 Bitwise AND Operator (&)
When two values are bitwise ANDed in C, the binary a b a&b
representations of the values are compared bit by bit 0 0 0
The truth table of bitwise AND is shown. 0 1 0
1 0 0
The bitwise AND operator (&) is often used to mask off some
1 1 1
set of bits
Example
 n=n&3 will set all the bits of n to 0 except lower order 2 bits
 25 & 7 is 1
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
79

BITWISE OPERATORS
 Bitwise OR Operator (|)
When two values are bitwise ORed in C, the binary a b a|b
representations of the values are compared bit by bit 0 0 0
The truth table of bitwise OR is shown. 0 1 1
1 0 1
The bitwise OR operator (|) is often used to set some bits to
1 1 1
ON(1)
Example
 n=n|3 will set lower order 2 bits to 1

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
80

GEC KOZHIKODE 40
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

BITWISE OPERATORS
 Bitwise Exclusive-OR Operator (^)
Often called bitwise XOR operator. a b a^b
The truth table of bitwise XOR is shown. 0 0 0
Any value XORed with itself produces 0. 0 1 1
1 0 1
1 1 0

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
81

BITWISE OPERATORS
 One’s complement Operator (∼)
It is a unary operator. It converts each 1-bit into a 0-bit and vice versa
The truth table of one’s complement operator is shown. b ~b
Its use can help make a program more portable. 0 1
For Example: 1 0

 To set last 2 bits of a number(x) to zero we can write x=x&0xFFFD


 Here we assumes x is 16 bit
 Instead we can write x=x&∼3 which is independent of word length of x.

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
82

GEC KOZHIKODE 41
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

BITWISE OPERATORS
 Left Shift Operator (<<)
The left shift operator << perform left of its left operand by the number of bit positions
given by the right operand, which must be non-negative
x << 2 shifts the value of x by two bit positions, filling vacated bits with zero and bits that
are shifted out through the high-order bit of the data item are lost
Eg. 5 << 1 is 10, 5 << 2 is 20, ...
Left shifting by 1 is equivalent to multiplication by 2

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
83

BITWISE OPERATORS
 Right Shift Operator (>>)
The right shift operator >> perform right shifts of its left operand by the number of bit
positions given by the right operand, which must be non-negative
Right shift - bits shifted out of the low-order bit of the value are lost
Right shifting an unsigned quantity always fits the vacated bits with zero
Right shifting a signed quantity will fill with bit signs (“arithmetic shift”) on some
machines and with 0-bits (“logical shift”) on others.
 Eg: 20 >> 1 yields 10, 20 >> 2 yields 5, 20 >> 3 is 2
If the number is shifted more than the size of integer or if the second operand is -ve the
behavior is undefined
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
84

GEC KOZHIKODE 42
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

BITWISE OPERATORS

 What is 1 & 2 and 1 && 2 ?


1 & 2 is 0 and 1 && 2 is 1

 What is 25|5?
25|5 is 29

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
85

TYPE CONVERSIONS
 When an operator has operands of different types, they are converted to a common type
 The “lower” type is promoted to the “higher” type without losing information before the
operation proceeds
 long double > double > float > long long > long > int > short > char
 Floats in an expression are not automatically converted to double; this is a change from the
original definition
 Ex : 10/4 is 2 but 10/4.0 is 2.5
 Because in first expression both operands are ints, so it will do integer division
 In second expression one operand is int and the other is double, so it will convert int to double
and then do real division
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
86

GEC KOZHIKODE 43
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

TYPE CONVERSIONS
Conversions take place across assignments; the value of the right side is converted to the
type of the left
int i; float j; Then i=j and j=i both cause conversions, float to int causes truncation of any
fractional part
Example : if i=10; and j=3.75; Then i=j will assign 3 to i
When a double is converted to float, whether the value is rounded or truncated is
implementation dependent.
int x=9/5*2.5+32; Then x is 34
int x=2.5*9/5+32; Then x is 36

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
87

TYPE CASTING
Explicit type conversions can be forced in any expression, with a unary operator called a
cast
 Syntax
(type name)expression

The expression is converted to the named type by the conversion rules


Example:-
float x=(int)2.5*3; Then x is 6.0
float x=(float)9/5 is 1.8
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
88

GEC KOZHIKODE 44
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

OPERATOR PRECEDENCE
Priority
Operator Description Associativity
Rank
() Parentheses (function call)
[] Array subscript
1 . Structure and union member access left to right
-> Structure and union member access through pointer
++ -- Postfix increment and decrement
++ - - Prefix increment/decrement
+- Unary plus/minus
! ˜ Logical negation/bitwise complement
2 (type) Type cast right to left
* Indirection (dereference)
& Address (of operand)
sizeof Determine size in bytes
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
89

OPERATOR PRECEDENCE
Priority
Operator Description Associativity
Rank
3 * / % Multiplication, division, and remainder left to right
4 + - Addition and subtraction left to right
5 ≪ ≫ Bitwise left shift and right shift left to right
Less than, less than or equal to, greater than and greater
6 < <= > >= left to right
than or equal to
7 == != Equal to and not equal to left to right
8 & Bitwise AND left to right
9 ∧ Bitwise XOR (exclusive or) left to right
10 | Bitwise OR (inclusive or) left to right
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
90

GEC KOZHIKODE 45
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

OPERATOR PRECEDENCE
Priority
Operator Description Associativity
Rank
11 && Logical AND left to right
12 || Logical OR left to right
13 ?: Conditional Operator right to left
=Simple assignment
+= -= Assignment by sum and difference
14 *= /= %= Assignment by product, quotient, and remainder right to left
≪= ≫= Assignment by bitwise left shift and right shift
&= ˆ= |= Assignment by bitwise AND, XOR, and OR

15 , comma left to right


PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
91

CONTROL FLOW STATEMENTS

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE 92

GEC KOZHIKODE 46
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

CONTROL FLOW STATEMENTS


Selection Control Repetition Control Unconditional Control
 Decision Making  Loop Statements  Unconditional Branching
 Simple if statement  for loop  goto statement
 if else statement  while loop  break statement
 Nested if statement  do while loop  continue statement
 else if statement  return statement
 switch statement

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
93

DECISION MAKING/
SELECTION CONTROL/
CONDITIONAL BRANCHING

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE 94

GEC KOZHIKODE 47
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

SIMPLE if STATEMENT
 The if else statements is used to express decisions
 Syntax

if(expression)
statement1;

OR

if(expression)
{ statements }

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
95

if-else STATEMENT
 Syntax
if(expression)
statement1;
else
statement2;
OR
if(expression)
{ statements }
else
{ statements }
 else part is optional
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
96

GEC KOZHIKODE 48
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

NESTED if STATEMENT
 Nesting of if’s is allowed in C
 Since else part of an if-else is optional, there is an ambiguity when an else is omitted
from a nested if sequence.
 This is resolved by associating the else with the closest previous else-less if.
 For Example
CODE 1 CODE 2
if ( x>=0 ) if ( x>=0 )
if ( x>0 ) { if ( x>0 )
printf ( "x is +ve" ) ; printf ( "x is +ve" ) ; }
else else
printf ( "x is –ve" ) ; printf ( "x is -ve" ) ;
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
97

NESTED if STATEMENT
 In code 1 the else part is associated with the inner if (if(x > 0)).
 If that isn’t what you want, braces must be used to force the proper association as in
code 2.
 This kind of bug can be hard to find; it’s a good idea to use braces when there are nested
if’s.

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
98

GEC KOZHIKODE 49
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

NESTED if STATEMENT

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
99

else if LADDER
 The if-else statement is used to express decisions  Syntax
 else-if construction is used to express multi-way
if(expression)
decision. statement;
 The expressions are evaluated in order. If an else if(expression)
statement;
expression is true, the statement associated with it else if(expression)
is executed, and this terminates the whole chain. statement;

else
statement;

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
100

GEC KOZHIKODE 50
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

else if LADDER

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
101

switch STATEMENT
The switch statement is a multi-way decision that
tests whether an expression matches one of a number
of constant integer values, and branches accordingly
Switch statement is often faster than if ... else ...
If we check only the value of a single variable, it is
better to use switch statement

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
102

GEC KOZHIKODE 51
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

switch STATEMENT
 Syntax If a case matches the expression value,

switch (expression) execution starts at that case.


{ All case expressions must be different.
case const-expr: statements
case const-expr: statements default is executed if none of the other
... cases are satisfied, and it is optional
default: statements
} Cases (including default) can be in any
order

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
103

LOOPS / REPETITION

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE 104

GEC KOZHIKODE 52
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

LOOPS
 Loops in programs allow us to repeat blocks of code
 Useful for
 Counting
 Repetitive activities
 Programs that never end
 In C there are 3 looping statements
1. The while statement - Top-tested loop (pre-test)
2. The for statement - Counting loop– Counting
3. The do while statement - Bottom-tested loop (post-test)
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
105

while LOOP
 Syntax
while(test_expression) OR while(test_expression)
statement1 { statements }

 Test_expression is evaluated first


 If it is non zero(true), the program statement that immediately follows is executed.
 Then test_expression is reevaluated, if it is true the program statement executes once
again
 This cycle continues until the test_expression becomes zero, at which point the loop is
terminated.
 Execution of the program then continues with the statement that follows the program
statement.
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
106

GEC KOZHIKODE 53
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

while LOOP - FLOWCHART

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
107

while LOOP EXAMPLES


#include<stdio.h> #include<stdio.h>
int main ( ) int main ( )
{ { int i , j ;
int n ; i=0 ;
n=0 ;
while ( n<5 ) while (++i<3 ) ;
{ { j=0 ;
printf ( "%d " , n ) ; while (++j<3 )
n++; printf ( "%d %d\n" , i , j ) ;
} }
return 0 ; return 0 ;
} }
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
108

GEC KOZHIKODE 54
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

while LOOP EXAMPLES


#include<stdio.h> #include<stdio.h>
int main ( ) Output int main ( ) Output
{ 01234 { int i , j ; 31
int n ; i=0 ; 32
n=0 ;
while ( n<5 ) while (++i<3 ) ;
{ { j=0 ;
printf ( "%d " , n ) ; while (++j<3 )
n++; printf ( "%d %d\n" , i , j ) ;
} }
return 0 ; return 0 ;
} }
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
109

NESTED while EXAMPLE


#include<stdio.h>
int main ( ) Output
{ 11
int i , j ;
12
i=0 ;
while (++i<3 ) 21
{ j=0 ; 22
while (++j<3 )
printf ( "%d %d\n" , i , j ) ;
}
return 0 ;
}

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
110

GEC KOZHIKODE 55
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

for LOOP
 The while loop is pretty general
Anything that can be done using repetition can be done with a while loop
 Because counting is so common
There is a specialized construct called for loop
 for loop
Makes it easy to set up a counting loop

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
111

for LOOP
 for Loop Syntax

for(init_expression; test_expression; update_expression )


{ statement(s) }

which is equivalent to
 while loop
init_expression;
while(test_expression)
{ statement(s)
update_expression;
}
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
112

GEC KOZHIKODE 56
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

for LOOP - FLOWCHART

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
113

for LOOP - EXAMPLE

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
114

GEC KOZHIKODE 57
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

for LOOP - DIFFERENT WAYS

Form Comment

for(i=0; i<10; i++)


Loop with single statement
statement1;

for(i=0; i<10; i++)


{ statement1; Loop with multiple statements
statement2; …}

for(i=0; i<10; i++) { }


Loop with no body
for(i=0; i<10; i++);

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
115

for LOOP - DIFFERENT WAYS

Form Comment

for(i=0,j=0; i<10; i++,j+=2) Multiple initialization and multiple update statements


{ statements } separated by Comma

for(; i<10; i++)


Initialization not used
{ statements }

for(i=0; i<10;)
Update statement not used
{ statements }

for(; i<10; )
Both initialization & update statement not used
{ statements }

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
116

GEC KOZHIKODE 58
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

do while LOOP
 Bottom tested loop (post-test)
 One trip through loop is guaranteed
 Statement is executed at least once

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
117

do while LOOP - FLOWCHART

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
118

GEC KOZHIKODE 59
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

do while LOOP - EXAMPLE

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
119

UNCONDITIONAL BRANCHING

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE 120

GEC KOZHIKODE 60
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

break STATEMENT
 Syntax
break;

 Sometimes it becomes desirable to leave the loop as soon as a certain condition occurs
 break statement causes the innermost enclosing loop or switch to be exited immediately
 Subsequent statements in the loop are skipped, and execution of the loop is terminated

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
121

continue STATEMENT
 Syntax
continue;
 It is related to break, it is used for skipping part of loop
 It causes the next iteration of the enclosing for, while, or do-while loop to begin
 Any statements in the loop that appear after the continue statement are automatically
skipped
 In while and do-while, this means that the test part is executed immediately
 In for, control passes to the update statement

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
122

GEC KOZHIKODE 61
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

goto STATEMENT
 Syntax
goto label;
 Execution of a goto statement causes a direct branch to be made to a specified point in
the program
 To identify where in the program the branch is to be made, a label is needed
 A label is a name (same rules as variable names) followed by a colon (:)
 Using goto we can break out of two or more loops at once
 Good programmers will not use this statement (reason : programs become unreliable,
unreadable, and hard to debug). So as far as possible avoid the use of goto
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
123

SIMPLE PROGRAMS IN C

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE 124

GEC KOZHIKODE 62
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

QUESTIONS?
1. Write a program to check whether a number is odd or even.
2. Write a program to find the largest of two numbers.
3. Write a program to check whether a number is in between 10 & 100.
4. Write a program to assign grade (S:90-100, A:80-89, B:70-79, C:60-69, D:50-59, E:40-
49, F:0-39) for a student in a subject based on the mark obtained.
5. Write a program for Simple calculator using switch statement.
6. Write a program for Simple calculator using else if.
7. Write a program to swap two numbers without using 3rd variable

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
125

QUESTIONS?
8. Write a program to find factorial of a number factorial of n.
9. Write a program to print first n numbers using for and while loop.
10. Write a program to find sum of first n numbers using for and while loop
11. Write a program to print all numbers between 10 and 100 which are multiples of 7.
12. Write a Program to add numbers until user enters zero.
13. Write a Program to reverse the digits of a number.
14. Write a program to check whether a number is palindrome or not
15. Write a program to find GCD of two positive numbers m and n, m ≥ n

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
126

GEC KOZHIKODE 63
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

PROGRAM TO CHECK ODD OR EVEN


#include<stdio.h>
int main ( )
{ int n ;
printf ( "Enter the number:" ) ;
scanf ( "%d" ,&n ) ;
if ( n%2==0 )
printf ( "The given number is even\n" ) ;
else
printf ( "The given number is odd\n" ) ;
return 0 ;
}
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
127

PROGRAM TO FIND THE LARGEST OF TWO NUMBER

#include<stdio.h>
int main ( )
{ int a , b ;
printf ( "Enter the numbers" ) ;
scanf ( "%d %d" , &a , &b ) ;
if (a>b)
printf ( "%d is largest" , a ) ;
else
printf ( "%d is largest" , b ) ;
return 0 ;
}
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
128

GEC KOZHIKODE 64
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

Program to check whether a number is in between 10 & 100

#include<stdio.h>
void main ( )
{ int a ;
printf ( "Enter the number:" ) ;
scanf ( "%d" ,&a ) ;
if ( a>10 )
{ if ( a<100 )
printf ( "\n%d is in between 10 and 100\n" , a ) ;
else
printf ( "\n%d is not in between 10 and 100\n" , a ) ;
}
else
printf ( "\n%d is not in between 10 and 100\n" , a ) ;
PREPARED BY
}
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
129

Program to check whether a number is in between 10 & 100

#include<stdio.h>
void main ( )
{ int a ;
printf ( "Enter the number:" ) ;
scanf ( "%d" ,&a ) ;
if ( a>10 && a<100 )
printf ( "\n%d is in between 10 and 100\n" , a ) ;
else
printf ( "\n%d is not in between 10 and 100\n" , a ) ;
}

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
130

GEC KOZHIKODE 65
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

Program to assign Grade in a Subject


 Write a program to assign grade (S:90-100, A:80-89, B:70-79, C:60-69, D:50-59, E:40-49, F:0-
39) for a student in a subject based on the mark obtained

#include<stdio.h> else if ( a>=70 )


void main ( ) printf ( "B Grade \n" ) ;
{ else if ( a>=60 )
float a ; printf ( "C Grade \n" ) ;
printf ( "Enter the mark:" ) ; else if ( a>=50 )
scanf ( "%f" ,&a ) ; printf ( "D Grade \n" ) ;
if ( a>=90 ) else if ( a>=40 )
printf ( "S Grade \n" ) ; printf ( "E Grade \n" ) ;
else if ( a>=80 ) else
printf ( "A Grade \n" ) ; printf ( "F Grade \n" ) ; }

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
131

Simple calculator using switch


#include <stdio.h>
void main ( )
{ int num1 , num2 , result ;
char op ;
printf ( "Calculator: number1 operation number2\n" ) ;
scanf ( "%d %c %d" , &num1 , &op , &num2 ) ;
switch ( op )
{ case '+' : printf ( "Result is %d " , num1+num2 ) ; //break ;
case '-' : printf ( "Result is %d " , num1−num2 ) ; //break ;
case '*' : printf ( "Result is %d " , num1* num2 ) ; //break ;
case '/' : printf ( "Result is %d " , num1 / num2 ) ; // break ;
default : printf ( "Invalid Operation" ) ;
}
}
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
132

GEC KOZHIKODE 66
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

Simple calculator using else-if


#include <stdio.h>
void main ( )
{ int num1 , num2 , result ;
char op ;
printf ( "Calculator: number1 operation number2\n" ) ;
scanf ( "%d %c %d" , &num1 , &op , &num2 ) ;
if ( op=='+’ )
printf ( "Result is %d" , num1+num2 ) ;
else if ( op == '-’ )
printf ( "Result is %d" , num1−num2 ) ;
else if ( op == '*’ )
printf ( "Result is %d" , num1* num2 ) ;
else if ( op == '/’ )
printf ( "Result is %d" , num1 / num2 ) ;
else
printf ( "Invalid Operation" ) ; }
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
133

Program to swap the values in a and b


#include <stdio.h>
int main ( )
{ int a, b;
printf ( “Enter value of a=%d " ) ;
scanf ( "%d" , &a) ;
printf ( “Enter value of b=%d " ) ;
scanf ( "%d" , &b) ;
a=a^b ;
b=b^a ;
a=a^b ;
printf ( "a=%d \t b=%d \n" , a , b ) ;
return 0 ;
}
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
134

GEC KOZHIKODE 67
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

Program to check even or odd using bitwise operator


#include<stdio.h>
void main ( )
{ int a ;
printf ( "Enter the number:" ) ;
scanf ( "%d" ,&a ) ;
if ( a&1 ) // we can use this instead of if ( a&1!=0 )
printf ( "\nThe given number is odd\n" ) ;
else
printf ( "\nThe given number is even\n" ) ;
}

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
135

Program to find factorial of a number


#include<stdio.h>
int main ( )
{ int n , i=1 ;
long fact=1 ;
printf ( "Enter the number:" ) ;
scanf ( "%d" ,&n ) ;
while ( i<=n )
{
fact *=i ;
i++;
}
printf ( "Factorial of %d is %ld\n" , n , fact ) ;
return 0 ;}
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
136

GEC KOZHIKODE 68
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

Program to find factorial of a number


#include<stdio.h>
int main ( )
{ int n ;
long fact=1 ;
printf ( "Enter the number:" ) ;
scanf ( "%d" ,&n ) ;
while ( n>0 )
{ fact *=n ;
−−n ;
}
printf ( "Factorial is %ld\n" , fact ) ;
return 0 ;
}
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
137

Program to print first n numbers using for and while loop

#include <stdio.h> #include <stdio.h>


int main ( ) int main ( )
{ int n , i , sum=0 ; { int n , i , sum=0 ;
printf ( "Enter the number:" ) ; printf ( "Enter the number:" ) ;
scanf ( "%d" ,&n ) ; scanf ( "%d" ,&n ) ;
for ( i=1 ; i<=n ; i++) i=1 ;
printf ( "%d " , n ) ; while ( i<=n )
return 0; {
} printf ( "%d " , n ) ;
n++;
}
return 0;
}
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
138

GEC KOZHIKODE 69
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

Program to find sum of first n numbers using for and while loop

#include <stdio.h> #include <stdio.h>


void main ( ) void main ( )
{ int n , i , sum=0 ; { int n , i , sum=0 ;
printf ( "Enter the number:" ) ; printf ( "Enter the number:" ) ;
scanf ( "%d" ,&n ) ; scanf ( "%d" ,&n ) ;
for ( i=1 ; i<=n ; i++) i=1 ;
sum+=i ; while ( i<=n )
printf ( "Sum = %d\n" , sum ) ; {
} sum+=i ;
i++;
}
printf ( "Sum = %d\n" , sum ) ;
}
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
139

Program to print all numbers between 10 and 100 which are multiples of 7

#include <stdio.h> #include <stdio.h>


void main ( ) void main ( )
{ {
int n ; int n ;
for ( n=11 ; n<100 ; n++) for ( n=14 ; n<100 ; n+=7 )
if ( n%7==0 ) printf ( "%d " , n ) ;
printf ( "%d " , n ) ; }
}

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
140

GEC KOZHIKODE 70
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

Program to add numbers until user enters zero


//Using while loop //Using do while loop
#include <stdio.h> #include <stdio.h>
int main ( ) int main ( )
{ int number , sum = 0 ; { int number , sum = 0 ;
printf ( "Enter number:" ) ; do
scanf ( "%d" , &number ) ; { printf ( "Enter number:" ) ;
while ( number != 0 ) scanf ( "%d" , &number ) ;
{ sum += number ; sum += number ;
printf ( "Enter number:" ) ; } while ( number != 0 );
scanf ( "%d" , &number ) ; printf ( "Sum = %d" , sum ) ;
} return 0 ;}
printf ( "Sum = %d" , sum ) ;
return 0 ;}
PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
141

Program to reverse the digits of a number


#include <stdio.h> printf ( "\n The entered number is %d
int main ( ) and its reverse is %d" , temp , rev ) ;
{ return 0 ;
int num , temp , rev=0 ; }
printf ( "Enter your number.\n" ) ;
scanf ( "%d" , &num ) ;
temp=num ;
do
{
rev = rev *10+num % 10 ;
num = num / 10 ;
} while ( num !=0 ) ;

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
142

GEC KOZHIKODE 71
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

Program to check whether a number is palindrome or not


#include <stdio.h>
int main ( ) if ( temp==rev )
{ printf ( "\n%d is palindrome\n" , temp ) ;
int num , temp , rev=0 ; else
printf ( "Enter your number:\n" ) ; printf ( "\n%d is not palindrome\n" , temp ) ;
scanf ( "%d" , &num ) ; return 0 ;
temp=num ; }
while ( num>0 ) Rashid Ummer NT
{
rev = rev *10+num % 10 ;
num = num / 10 ;
}

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
143

Program to find GCD of two positive numbers

//Simple Solution
#include <stdio.h>
int main ( )
{ int m, n, k;
printf ( "Enter two numbers:" ) ;
scanf ( "%d%d" , &m, &n ) ;
k=n ; //Assumed n is smaller than m
while ( ! ( m%k==0 && n%k==0 ) ) //m and n not divisible by k
−−k ;
printf ( “GCD of %d and %d is %d" , m , n , k ) ;
return 0 ;
}

PREPARED BY
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
144

GEC KOZHIKODE 72
MODULE I - GXEST204 PROGRAMMING IN C 03-02-2025

Program to find GCD of two positive numbers


//Euclidean algorithm //Continue the loop until the remainder is zero
#include <stdio.h> while ( b !=0 )
int main ( ) { r=a%b ;
{ a=b ;
int a , b , temp , r ; b=r ;
printf ( "Enter the numbers:" ) ; }
scanf ( "%d %d" ,&a ,&b ) ; //Loop exits when remainder is zero , then
//Swap the numbers to make a>b GCD(a,0)=a
if ( a<b ) printf ( “GCD is %d\n" , a ) ;
{ return 0 ;
temp=a ; }
a=b ;
b=temp ;
PREPARED BY }
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
145

Program to print Fibonacci numbers between two given numbers

#include <stdio.h>
int main ( )
{ int t1=0 , t2=1 , next , lwr , upr ;
printf ( "Enter the range : " ) ;
scanf ( "%d %d" ,&lwr ,&upr ) ;
while ( t1<upr )
{
if ( t1>lwr )
printf ( "%d, " , t1 ) ;
next=t1+t2 ;
t1=t2 ;
t2=next ;
}
PREPARED BY return 0 ; }
BINOY K. P., ASSISTANT PROFESSOR, DEPARTMENT OF APPLIED ELECTRONICS & INSTRUMENTATION ENGINEERING, GOVERNMENT ENGINEERING COLLEGE KOZHIKODE
146

GEC KOZHIKODE 73

You might also like