C Programming - Unit 1 Final
C Programming - Unit 1 Final
1956
Approved by AICTE, COA and BCI, New Delhi
B22CI0104
Programming with C
➢ Illustrate the use of iterative statements and conditional Statements for solving
➢ Discuss the use of structures, unions, and file operations for solving the real
world problems.
HISTORY OF C
C_Pgm_Unit_I 6
Purpose of writing an algorithm
• It is written using English language so that it is easily understandable even by non-
programmers.
• Basically an algorithm is a finite steps that must be followed to solve any problem.
C_Pgm_Unit_I 7
Rules for writing an algorithm
✓ It should terminate after a finite time.
✓ It should be deterministic means giving the same output for the same input case.
✓ Every step in the algorithm must be effective i.e. every step should do some work.
C_Pgm_Unit_I 8
Add two numbers entered by the user
Step 1: Start
Step 3: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Stop
C_Pgm_Unit_I 9
Algorithm to calculate simple interest
Step 1: Start
si←p × t × r / 100
Step 4: Display si
Step 5: Stop
C_Pgm_Unit_I 10
Algorithm to calculate area of triangle
Step 1: Start
area←0.5 × b × h
Step 5: Stop
C_Pgm_Unit_I 11
Algorithm to calculate area of circle
Step 1: Start
area←3.1412 × r × r
Step 5: Stop
C_Pgm_Unit_I 12
Find the largest number among three numbers
Step 1: Start
Step 2: Declare variables a,b and c.
Step 3: Read variables a,b and c.
Step 4: If (a > b && a > c )
Display a is the largest number.
else if ( b > a && b>c)
Display b is the largest number.
else
Display c is the largest number.
Step 5: Stop
C_Pgm_Unit_I 13
Flow chart
✓ They are widely used in multiple fields to document, study, plan, improve and
communicate often complex processes in clear, easy-to-understand diagrams.
C_Pgm_Unit_I 14
Notations used to write a flow chart
C_Pgm_Unit_I 15
Add two numbers
C_Pgm_Unit_I 16
Calculate profit and loss
C_Pgm_Unit_I 17
Largest among three different numbers
C_Pgm_Unit_I 18
Advantage of writing algorithm
✓ It is easy to understand.
✓ In Algorithm the problem is broken down into smaller pieces or steps hence, it is
C_Pgm_Unit_I 19
Advantages of using flowcharts
✓ Communication: Flowcharts are better way of communicating the logic of a system
✓ Effective analysis: With the help of flowchart, problem can be analysed in more
documentation, which is needed for various purposes, making things more efficient.
✓ Efficient Coding: The flowcharts act as a guide or blueprint during the systems
flowchart becomes complex and clumsy. This will become a pain for the user,
✓ Alterations and Modifications: If alterations are required the flowchart may require re-
✓ Time consuming
C_Pgm_Unit_I 21
Difference between Flow Chart & Algorithm
C_Pgm_Unit_I 22
Structure of C program
comments function 1 definition
preprocessor directives
Global variables Declaration
{
int main ()
{ }
Local variables Declaration function 2 definition
program statements
call function 1
{
call function 2
} }
1. DOCUMENTATION / COMMENTS
• This section consists of the description of the program, the name of the program,
and the creation date and time of the program.
• It is represented as follows
Or
2.Multi-line comment:
• Operations like declaration and execution are performed inside the open and close
curly braces of the main program.
• The return type of the main() function can be int as well as void too. void() main
tells the compiler that the program will not return any value.
• The int main() tells the compiler that the program will return an integer value.
Example:
int add(int p, int q)
{
int r;
r = p + q;
return r;
}
//Program: To find addtion of 2 int add (int p, int q)
numbers {
//Author: Dennis Ritchie int r;
//Date: 09/03/2024 r = p + q;
# include <stdio.h> return r;
int a, b, c; }
int main ()
{
int a, b, c;
int add (int, int);
a = 10;
b = 20;
c = add (a, b);
printf ("%d", c);
return 0;
}
C TOKENS
C_Pgm_Unit_I 34
CONSTANTS:
An entity whose value does not change during the execution of program. The
classification of constants in C is shown below:
CONSTANTS:
• Integer Constant: Integer constants are whole numbers without any fractional
part. Ex: 10, -5, 24, 0
• Floating Point Constant: A number with decimal point is called floating point
constant.
Ex: 10.5, -5.6, 12.3
Note: 2.3*10^67 can be written as 2.3e67 or 2.3E67
• Brackets []: Opening and closing brackets are used as array element reference.
These indicate single and multidimensional subscripts.
• Parentheses (): These special symbols are used to indicate function calls and
function parameters.
• Braces {}: These opening and ending curly braces mark the start and end of a
block of code containing more than one executable statement.
• Pre-processor (#): The preprocessor is a macro processor that is used
automatically by the compiler to transform your program before actual
compilation.
p, t, r and si are variables
12.5000 23.40000
# include <stdio.h> P t
00 0
int main ()
{ 32.500 95.06250 si
float p, t, r, si; r 000 0
datatype
printf ("Enter values for p, t and r \n");
scanf ("%f%f%f", &p, &t, &r);
si = p * t * r / 100; Enter values for p, t and r
printf ("Simple interest = %f\n", si); 12.5
return 0; 23.4
} 32.5
Simple interest = 95.062500
100 is a constant
Variables
• Variable is a name given to memory location, its value can change during
the execution of the program. In the above program p, t, r and si are
variable names
Rules for constructing variable names:
1. Variable name can be constructed using alphabets, digits and underscore
2. First character should be alphabet or underscore
3. Keywords cannot be used as variable names in the program
4. No white spaces allowed within variable name
Note: C is a case sensitive language
REVA and Reva both are different according to C language
si int
2si $abc
_si simple
p sb_acc
t
abc_123
abc-123
Difference b/w variable declaration and definition / Variable initialization :
Note: {
} Block
TYPES OF VARIABLES IN C CONT..
2. Register variable
o A variable that is declared and used inside the function or block with the
keyword register is called register variable.
o A variable that retains its value between multiple function calls is known as a
static variable.
Derived / Non Primitive data types: arrays, functions, structures and pointers.
1. Void Data types
✓ It is the library function defined in stdio.h header file. This function takes all
types of values (numeric, character, string) as input.
• %d → int
• %f → float
• %lf → double
• %c → char
• %s → string
Input and Output Functions cont..
Formatted Input Function: scanf()
Input and Output Functions cont..
Formatted Output Function: printf()
• It is a library function defined in stdio.h header file which will take data stored at
given location and prints it on monitor.
• This function prints all types of values (numeric, character, string) as input.
• getchar(): It reads a character from the keyboard and store this into a memory
location. We have to press ENTER key after typing the character.
These functions allow us to take input or These functions do not allow to take input
1 display output in the user’s desired format. or display output in user desired format.
printf(), scanf() are examples of these getch(), getche(), gets() and puts(), are
5 functions. some examples of these functions.
SAMPLE PROGRAM FOR INT DATATYPE
#include<stdio.h> Output
int main() Integer value with positive data: 9
Integer value with negative data: -9
{
Integer value with an unsigned int data: 89
int a = 9; Integer value with an long int data: 99998
int b = -9;
int c = 89U;
long int d = 99998L;
printf("Integer value with positive data: %d\n", a);
printf("Integer value with negative data: %d\n", b);
printf("Integer value with an unsigned int data: %u\n", c);
printf("Integer value with an long int data: %ld", d);
return 0;
}
C Program for arithmetic operation
#include <stdio.h>
#include<conio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
getch();
}
C_Pgm_Unit_I 67
C Program for arithmetic operation
#include<stdio.h>
#include<conio.h>
int main()
{
int a = 12, b = 3;
int addition, subtraction, multiplication, division, modulus;
7. Libraries with rich Functions: Robust libraries and functions in C help even a
beginner coder to code with ease.
10. Easy to Extend: Programs written in C language can be extended means when a
program is already written in it then some more features and operations can be added to
it.
Introduction to
GitHub
C_Pgm_Unit_I 74
THANK YOU
C_Pgm_Unit_I 75