CP Unit 1
CP Unit 1
Khan
Assistant Professor
Syllabus Semester I
Course Computer Programming
Course Type ESC Course Code AI24103
Credits 04 L-4, T-0, P – 0
Course Outcomes: On successful completion of the
course the learner will be able to:
CO Course Outcomes BT PO
Level
1 To learn the fundamentals of C Programming 1 1,2
2 To implement Control Statements in C Programming 3 1,2,4
3 To use derived data types as Arrays for problem solving 3 1,2
techniques
4 To utilize functions for Program Optimization 3 1,2,3
5 To apply the concept of pointers to perform various 3 1,3
operations.
6 To develop user defined types using structures and to 5 1,2,4
implement various file handling functions.
Unit No. Content
Introduction to C Programming:
Evolution of C programming, Fundamentals
of Algorithm, Fundamentals of C
Programming: Identifiers, Data Types,
Variables & Constants, Keywords; Program
1
structure: Preprocessor Directives, Basic
input and Output Functions, Format
Expressions, Operators, precedence &
Associativity, Simple C program and its
execution.
Chapter I
Oldest
Fastest
Famous
Important
Evolution of C programming
History of C:
The C programming language has a rich history,
dating back to the early 1970s:
• Early Development (1969-1973):
C was created by Dennis Ritchie at Bell Labs.
• Standardization (1978):
Brian Kernighan and Dennis Ritchie published the
book "The C Programming Language," often
referred to as K&R C. This book acted as the de
facto standard for C for many years.
Brian Kernighan
• C99 (1999):
The next major revision, C99, introduced new features
like variable-length arrays, inline functions, and improved
support for floating-point arithmetic.
• C11 (2011):
This version added features like type-generic expressions,
anonymous structures, and improved multi-threading
support.
• C17 (2018):
Primarily a bug-fix and clarification update to C11, with
no major new features.
Algorithm
• An algorithm is a step-by-step procedure for
solving a problem in a finite amount of time.
• Statements:
• Statement is a single action in a computer.
Input a=5,b=2
Process a+b
Result c=a+b
Output print
State:
Transition from one process to another process under
specified condition with in a time is called state.
• Control flow:
The process of executing the individual
statements in a given order is called control
flow.
The control can be executed in three ways
1. sequence
2. selection
3. iteration
Sequence
• All the instructions are executed one after
another is called sequence execution.
Example:
Add two numbers:
Step 1: Start
Step 2: get a,b
Step 3: calculate c=a+b
Step 4: Display c
Step 5: Stop
Selection:
• A selection statement causes the program control to
be transferred to a specific part of the program
based upon the condition.
Example
Write an algorithm to check whether he
is eligible to vote?
Step 1: Start
Step 2: Get age
Step 3: if age >= 18 print “Eligible to vote”
Step 4: else print “Not eligible to vote”
Step 6: Stop
Iteration:
• In some programs, certain set of statements are
executed again and again based upon conditional
test. i.e. executed more than one time. This type of
execution is called looping or iteration.
Example
Write an algorithm to print all natural numbers up to n
Step 1: Start
Step 2: get n value.
Step 3: initialize i=1
Step 4: if (i<=n) go to step 5 else go to step 7
Step 5: Print i value and increment i value by 1
Step 6: go to step 4
Step 7: Stop
Flow Chart
Flow chart is defined as graphical representation of the logic
for problem solving.
The purpose of flowchart is making the logic of the program
clear in a visual representation.
Notation
Rules for drawing a flowchart
x 5 x 3
• Rules for Constructing Variable Names
• A variable name is any combination of 1 to 31 alphabets, digits or
underscores. Some compilers allow variable names whose length
could be up to 247 characters. Still, it would be safer to stick to the
rule of 31 characters. Do not create unnecessarily long variable
names as it adds to your typing effort.
• The first character in the variable name must be an alphabet or
underscore.
• No commas or blanks are allowed within a variable name.
• No special symbol other than an underscore (as in gross_sal)
• can be used in a variable name.
• Ex.: si_int
m_hra
pop_e_89
How to declare Variable
• Syntax
Data type name_of_variable;
Assignment
name_of_variable = value;
C keywords
• Keywords are reserve words which can not
use as a variable .
• It is also called reserve word.
• Keywords are the words whose meaning has
already been explained to the C compiler (or
in a broad sense to the computer).
• There are only 32 keywords available in C.
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do If static while
1) Documentation section
/*
Overview of the code
*/
2) Preprocessor section
The preprocessor section contains all the header files used in a program. It
informs the system to link the header files to the system libraries. It is given by:
#include<stdio.h>
#include<conio.h>
3) Define section
The define section comprises of different constants declared using the define
keyword. It is given by:
#define a = 2
4) Global declaration
The global section comprises of all the global declarations in the program. It is
given by:
float num = 2.54;
int a = 5;
char ch ='z';
5) Main function( )
The program (basic or advanced) follows the same sections as listed above.
// C Program to illustrate how to use #define to
declare constants
#include <stdio.h>
// Defining macros with a constant value OUTPUT:
#define PI 3.14159265359 Area of Circle of radius
int main() 21: 1385
{ int radius = 21;
int area;
// Using macros to calculate the area of a circle
area = PI * radius * radius;
printf("Area of Circle of radius %d: %d", radius,
area);
return 0;
}
My First C Program
Comments are
ignored by
compiler
#include<stdio.h>
Each C program Must have
Void main(){ one main Function
int main()
{
// Integer value with positive data.
int a = 9;
return 0;
}
• Character Data Type
• Character data type allows its variable to store only a single
character. The size of the character is 1 byte. It is the most
basic data type in C. It stores a single character and requires
a single byte of memory in almost all compilers.
• Range: (-128 to 127) or (0 to 255)
• Size: 1 byte
• Format Specifier: %c
• Syntax of char
char var_name;
e.g char grade;
#include <stdio.h>
int main() {
char ch = 'A';
printf("The character is: %c\n",ch);
return 0;
}
• Float Data Type
• In C programming float data type is used to store
floating-point values. Float in C is used to store decimal
and exponential values. It is used to store decimal
numbers (numbers with floating point values) with
single precision.
• Range: 1.2E-38 to 3.4E+38
• Size: 4 bytes
• Format Specifier: %f
• Syntax of float
float var_name;
• #include <stdio.h>
int main() {
float number = 3.14159;
printf("The floating-point number is: %f\n",
number);
return 0;
}
• Double Data Type
• A Double data type in C is used to store decimal
numbers (numbers with floating point values) with
double precision. It is used to define numeric values
which hold numbers with decimal values in C.
• The double data type is basically a precision sort of
data type that is capable of holding 64 bits of decimal
numbers or floating points.
• Range: 1.7E-308 to 1.7E+308
• Size: 8 bytes
• Format Specifier: %lf
Void Data Type
• The void data type in C is used to specify that
no value is present. It does not provide a
result value to its caller. It has no values and
no operations. It is used to represent nothing.
Void is used in multiple ways as function
return type, function arguments as void,
and pointers to void.
• void name_function()
// C program to demonstrate
// use of void pointers
#include <stdio.h>
void main(){
}
Preprocessor Directives
• Before we talk about directives first should
understand about preprocessor.
• “Preprocessors are programs that process the
source code before compilation.”
Let take a look steps involved between writing and
execution of code
Preprocessor Directives in C
• Preprocessor programs provide preprocessor directives
that tell the compiler to preprocess the source code before
compiling.
• All of these preprocessor directives begin with a ‘#’ (hash)
symbol.
• The ‘#’ symbol indicates that whatever statement starts
with a ‘#’ will go to the preprocessor program to get
executed.
• We can place these preprocessor directives anywhere in
our program.
• Examples of some preprocessor directives
are: #include, #define, #ifndef, etc.
Basic Input and Output in C
• C language has standard libraries that allow input
and output in a program.
• The stdio.h or standard input output library in C
that has methods for input and output.
scanf()
• The scanf() method, in C, reads the value from
the console as per the type specified and store it
in the given address.
• Syntax:
• scanf("%X", &name_of_variable);
• where %X is the format specifier in C.
• It is a way to tell the compiler what type of
data is in a variable and & is the address
operator in C, which tells the compiler to
change the real value of name_of_variable,
stored at this address in the memory.
•
• printf()
• The printf() method, in C, prints the value passed
as the parameter to it, on the console screen.
• Syntax:
printf("%X", name_of_variable);
• where %X is the format specifier in C.
• It is a way to tell the compiler what type of data is
in a variable and name_of_variable is the
variable to be printed.
How to take input and output of basic types in C?
2) Formatted IO functions:
We use the formatted input and output functions in the C language for taking single
• getchar()
Syntax putchar(variablename);
a) gets()
• The gets() allows the user to enter the space-
separated strings.
• It returns the string entered by the user.
Syntax gets(variablename);
Syntax puts(variablename);
Subtracts right
2 – Minus operand from left
operand.
a–b
Multiply two
3 * Multiply
numeric values.
a*b
Returns the
remainder after
5 % Modulus diving the left
operand with the
a%b
right operand.
Returns true if
both the
1 && Logical AND
operands are
a && b
true.
Returns true if
both or any of
2 || Logical OR
the operand is
a || b
true.
Returns true if
3 ! Logical NOT the operand is !a
false.
4. Bitwise Operators in C
• The Bitwise operators are used to perform bit-
level operations on the operands.
• The operators are first converted to bit-level
and then the calculation is performed on the
operands.
• Mathematical operations such as addition,
subtraction, multiplication, etc. can be
performed at the bit level for faster
processing.
S. No. Symbol Operator Description Syntax
Performs bit-by-bit AND
1 & Bitwise AND operation and returns the a&b
result.
Performs bit-by-bit OR
2 | Bitwise OR operation and returns the a|b
result.
Parentheses (function
()
call)
Structure Pointer
->
Operator
Postfix increment,
++ , —
decrement
Prefix increment,
++ / —
decrement
Logical
!,~ NOT, Bitwise
complement
2 Right-to-Left
(type) Cast Operator
Dereference
*
Operator
Determine size in
sizeof
bytes
Multiplication,
3 *,/,% Left-to-Right
division, modulus
Addition,
4 +/- Left-to-Right
subtraction
Bitwise exclusive
9 ^ Left-to-Right
OR
12 || Logical OR Left-to-Right
14
= Assignment
Addition,
+= , -= subtraction
assignment
Multiplication,
*= , /=
division assignment
14 Right-to-Left
Modulus, bitwise
%= , &=
AND assignment
Bitwise exclusive,
^= , |= inclusive OR
assignment
comma (expression
15 , Left-to-Right
separator)