0% found this document useful (0 votes)
43 views25 pages

Unit-1 Introduction To Program Planing - C Programming

This document provides an introduction to program planning and C programming, covering essential concepts such as algorithms, flowcharts, pseudocode, and the history and importance of the C language. It details the character set, tokens, data types, and the rules for naming identifiers and variables in C. Additionally, it discusses derived data types, including structures, unions, enums, and classes, along with examples for clarity.

Uploaded by

anshulwaghmare10
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)
43 views25 pages

Unit-1 Introduction To Program Planing - C Programming

This document provides an introduction to program planning and C programming, covering essential concepts such as algorithms, flowcharts, pseudocode, and the history and importance of the C language. It details the character set, tokens, data types, and the rules for naming identifiers and variables in C. Additionally, it discusses derived data types, including structures, unions, enums, and classes, along with examples for clarity.

Uploaded by

anshulwaghmare10
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/ 25

UNIT I

INTRODUCTION TO PROGRAM PLANNING & C PROGRAMMING

14 Marks END SEM EXAM

Program Design Tools: Art of Programming through Algorithms, Flowcharts

Algorithm:
An algorithm is a step-by-step method for solving some problem
An algorithm refers to a set of instructions that define the execution of work to get the expected
results.
1. To make the program work properly, we must have to properly design the algorithm.
2. Designing the algorithm helps to utilize the computing resources effectively.
3. It uses plain text, which is written in plain English language.
4. The algorithm is easy to debug.
5. There are no rules employed for algorithms.
6. Algorithms are the program's pseudo code

Example: Write an algorithm for Addition of two Number

1. Start
2. Read two number a and b
3. Add the number c= a+b
4. Display Result c
5. Stop

Flowcharts:
1. Flowchart is a graphical representation of an algorithm.
2. Diagrammatic representation of an algorithm is called flow chart.
3. A flowchart can be helpful for both writing programs and explaining the program to
others.
4. A flowchart is a diagram created with different shapes to show the flow of data.
5. It uses various symbols to show the operations and decisions to be followed in a Program
The process of drawing a flowchart for an algorithm is known as “flowcharting

MET BKC IOE IT


Symbols used in flowchart are mentioned below –

Name Symbol Purpose

Terminal Start/stop/begin/end Of flow


chart

Oval

Input/output Input/output of data

Parallelogram

Process Any processing to be


performed can be represented

Rectangle

Decision box Decision operation that


determines which of the
alternative paths to be
followed

Diamond

Connector Used to connect different


parts of the flowchart

Circle

MET BKC IOE IT


Name Symbol Purpose

Flow Join 2 symbols and also


represent flow of execution

Arrows

Pre-defined Module (or) subroutines


process specified elsewhere

Double
Sided Rectangle

Page connector Used to connect flowcharts in


2 different pages

Pentagon

For loop shows initialization,


symbol condition, and increment of
the loop variable

Hexagon

Document Shows the data that is ready


for printout

Printout

MET BKC IOE IT


Q. Write an algortihm and draw the flowchart of to find the sum of two number

MET BKC IOE IT


pseudocode
 Pseudocode is a simple way of writing programming code in English
 Pseudocode is not actual programming language. It uses short phrases to write code for
programs before you actually create it in a specific language
 Once you know what the program is about and how it will function, then you can use
pseudocode to create statements to achieve the required results for your program.
 Pseudocode does not have a specific syntax
 Writing pseudocode allows you to get those brilliant ideas in your head in front of you
without having to worry about the syntax of the programming language.

MET BKC IOE IT


Overview of C: History

1. C programming language was developed in 1972 by Dennis Ritchie at bell laboratories of


AT&T (American Telephone & Telegraph), located in the U.S.A.
2. Dennis Ritchie is known as the founder of the c language
3. It was developed to overcome the problems of previous languages such as B, BCPL, etc.
4. C language was developed to be used in UNIX operating system
5. The C Language is developed for creating system applications that directly interact with the
hardware devices such as drivers, kernels, etc.
6. C programming is considered as the base for other programming languages, that is why it is
known as mother language

Importance C
The C programming language is immensely important for several reasons:
1. Foundation of Modern Programming: C is often referred to as a "mother language"
because it has influenced many other programming languages, including C++, Java, and
Python.
2. Performance and Efficiency: C is known for its high performance and efficiency
because it allows for low-level manipulation of hardware and memory.
3. Portability: Programs written in C can be compiled and run on different platforms with
minimal modifications, promoting portability across diverse systems.
4. System Programming: C is widely used in developing operating systems (like UNIX),
embedded systems, and system-level applications due to its ability to interact closely with
hardware.
5. Compatibility: C code can be easily integrated with code written in other languages like
C++, Java, and Python.

MET BKC IOE IT


Application of C Programming Language
C programming language finds its application in various domains, including:
1. Operating systems: C is extensively used in developing operating systems like Windows,
Linux, and macOS.
2. Embedded systems: C is the language of choice for developing firmware and software for
various embedded systems, such as microcontrollers and IoT devices.
3. System Utilities: Many system utilities, such as text editors, compilers, and interpreters, are
written in C.
4. Game development: C is used to develop games due to its ability to handle complex
calculations and graphics.
5. Database systems: The core components of databases and their management systems are
often built using C.
6. Networking: C is used for developing network protocols and applications

Character Set
1. C program contains statements. These statements are constructed using words and
these words are constructed using characters from C character set.
2. In C, a "character set" refers to the set of characters that can be used in the source code
[Program].
3. C language character set contains the following set of characters.
4. Alphabets -
a. lower case letters - a to z
b. UPPER CASE LETTERS - A to Z
5. Digits -
a. C language supports 10 digits which are used to construct numerical values in C
language.
b. Digits - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
6. Special Symbols-
a. Special Symbols - ~ @ # $ % ^ & * ( ) _ - + = { } [ ] ; : ' " / ? . > , < \ |
b. tab newline space NULL bell backspace vertical tab
Every character in C language has its equivalent ASCII (American Standard Code for
Information Interchange) value.

MET BKC IOE IT


C Tokens
In C, Tokens are the smallest meaningful elements used to create a program.

1. It is the basic component of a C program.


2. They include keywords, identifiers, constants, string literals, operators, punctuation
marks, and special symbols.
3. When a C program is compiled, it is broken down into these tokens, enabling the compiler to
analyse and understand the program's structure.
1. Keywords
2. Identifiers
3. Constants
4. Strings
5. Special Symbols
6. Operators

1. Keywords
The keywords are pre-defined or reserved words in a programming language.
1. Each keyword is meant to perform a specific function in a program
2. C language supports 32 keywords which are given below:

MET BKC IOE IT


2. Identifiers

These tokens are user-defined names given to variables, functions, or entities.

Example
int a; // here a variable is identifier

Example
void add() // here add function is identifier

Rules for Naming Identifiers


Certain rules should be followed while naming c identifiers which are as follows:
1. They must begin with a letter or underscore(_).
2. They must consist of only letters, digits, or underscore. No other special character is allowed.
3. It should not be a keyword.
4. It must not contain white space.
5. It should be up to 31 characters long as only the first 31 characters are significant

3. Constants
1. Constants represent fixed values that cannot be altered during program execution.
2. Constants may belong to any of the data types [int, float , char].
3. The constants in C are the read-only variables whose values cannot be modified once they
are declared in the C program.
4. The type of constant can be an integer constant, a floating constant, a string constant, or a
character constant.
5. In C language, the const keyword is used to define the constants.

Examples of Constants in C
const float pi = 3.14 // pi is an identifier and '3.14' is a constant value

const int MAX_LIMIT = 100; // 'MAX_LIMIT' is an identifier and '100' is a constant

MET BKC IOE IT


4. String
String: A sequence of characters enclosed in single or double quotation marks.

For example, "C programming" is a string litera

char x= ‟A‟;
char string[20] = “Hello”;
char string [] = “METIOE”;

5. Special Symbols
In C programming language, generally, the special symbols have some special meaning and
they cannot be used for other purposes.

 Brackets [] − Opening and closing of brackets are used for array element reference,
which indicate single and multidimensional subscripts.
 Parentheses () − These special symbols are used for function calls and function
parameters (Read: Functions in C).
 Braces {} − Opening and closing of curly braces indicates the start and end of a block of
code which contains more than one executable statement.
 Comma (, ) − It is used to separate more than one statements like separating parameters
in function calls.
 Colon (:) − It is an operator which essentially invokes something called as an
initialization list.
 Semicolon (;) − It is called as a statement terminator that indicates the end of one logical
entity. That is the reason each individual statement must be ended with a semicolon.
 Asterisk (*) − It is used to create a pointer variable.
 Assignment operato r(=) − It is used for assigning values.
 Pre-processor (#) − The pre-processor called as a macro processor is used by the
compiler to transform your program before the actual compilation starts.

MET BKC IOE IT


6. Operators
Operators in C are special symbols used to perform specific functions, and data items on
which they are applied upon are known as operands (Data).

1. Unary Operators: Those operators that require only a single Data to act upon are
known as unary operators.
a. For Example increment (++)
b. Decrement (--) operators.
2. Binary Operators: Those operators that require two data to act upon are called
binary operators. Binary operators can further are classified into:
a. Arithmetic operators + ,-, *. /
b. Relational Operators < > , <= ,>=
c. Logical Operators && , || , !
d. Assignment Operators =
e. Bitwise Operator << , >>
3. Ternary Operator: The operator that requires three data to act upon is called the
ternary operator.
a. Conditional Operator (?) is also called the ternary operator.

Data types
1. It specifies the type of data that the variable can store
2. Each variable in C has an associated data type. It specifies the type of data that the
variable can store like integer, character, floating, double, etc.

MET BKC IOE IT


Primitive data types in C
Data Type Size Format Specifiers Example
4 bytes used to store the integer
int %d numbers (any number
including positive,
negative and zero without
decimal part).
Example
int a=10;
char 1 bytes Character data type allows
%c its variable to store only a
single character.
Example
char a = 'a';
float 4 bytes used to store floating-point
%f values
Example
float b = 2.5;
double 8 bytes C is used to store decimal
numbers (numbers with
%lf floating point values) with
double precision
Example
double a = 123123123.00;
unsigned int 4 byte C is used to store the data
values from zero to
%u positive numbers but it
can’t store negative
values like signed int.
Example
int a=10;
short int 2 byte It is lesser in size than the
%hd int by 2 bytes so can only
store values from -32,768
to 32,767.
Example
int b=10;
long int 8 byte Larger version of the int
%ld datatype so can store
values greater than int
Example
int a=10;

MET BKC IOE IT


Derived Data type
The derived data types are those data types that are created by combining primitive data types
and other derived data types

1. Functions
A function is called a C language construct which consists of a function-body associated with a
function-name.
In every program in C language, execution begins from the main function, which gets
terminated after completing some operations which may include invoking other functions.

Example:

void add()

2. Arrays
Array in C is a fixed-size collection of similar data items stored in contiguous memory
locations.
An array is capable of storing the collection of data of primitive, derived, and user-defined
data types.

Example:

int arr[3] = { 18, 36, 54 };

3. Pointer

A pointer in C language is a data type that stores the address where data is stored.
Pointers store memory addresses of variables, functions, and even other pointers.
Example:

Int *myPtr;

int var = 20;

myPtr = &var;

MET BKC IOE IT


4. User Defined Data Type
1. Structure
2. Union
3. Enum
4. Class

1. Structure

Structures are used to group items of different types into a single type.
The “struct” keyword is used to define a structure.

Example

struct Person
{
char company[50];
int lifespan;
};

2 Union

Unions are similar to structures in many ways. What makes a union different is that all the
members in the union are stored in the same memory location resulting in only one member
containing data at the same time.

Example

union Data {
int i;
float f;
char str[20];
};

3 Enumeration (enums)

Enum is short for “Enumeration”. It allows the user to create custom data types with a set of
named integer constants.

enum Level { LOW, MEDIUM, HIGH };

MET BKC IOE IT


4 class
It user defined data type contain the data type and function

Example

class student
{
public:
int roll_no;
void display();
};

Declaration of variables
1. Variable is basically nothing but the name of a memory location that we use for storing data.
2. Variables are containers for storing data values.

Declaring (Creating) Variables


To create a variable, specify the type and assign it a value:

data_type variable_name = value; // defining single variable or


data_type variable_name1 , variable_name2; // defining multiple variable

Example
int var; // integer variable
char a; // character variable
float b; // float variables

Rules for Naming Variables in C


1. A variable name must only contain alphabets, digits, and underscore.
2. A variable name must start with an alphabet or an underscore only. It cannot start with a
digit.
3. No white space is allowed within the variable name.
4. A variable name must not be any reserved word or keyword.

MET BKC IOE IT


Assigning Values to variables

To assign a value to a variable in C programming, you can use the assignment operator, which is
the equal sign (=)

The equal sign = is used to assign a value to the variable.

 Syntax:
data_type variable Name = value
 Example:
int i = 9 ;

here assigns the value 9 to the variable i

Exmple

void main ()
{

int a; /* variable definition: */


float b, c; /* local variable, defining multiple variables */
a = 5; /* initialization with assignment statement*/
b = 1.5;
c = 2.5;
}

Storage Class
1. Storage class determines the scope, visibility and lifetime of a variable
2. The scope of a variable says where the variable is accessible and where it is not in the
program

There are 4 types of storage class:


1. automatic
2. external
3. static
4. register

1. Automatic
1. The variables declared inside a block are automatic or local variables.
2. The local variables exist only inside the block in which it is declared
3. The „local,‟ variables defined with auto are accessible within the function boundary or
the block in which they are defined.

MET BKC IOE IT


4. All auto variables must be defined inside a block or function body.

Syntax to define an auto variable:

auto int max; // Explicit declaration


int min; // By default auto

Example :

#include <stdio.h>
int main()
{
auto int min; // Explicit declaration local variable
printf(“Enter Value ");
scanf(“%d“,&min);
printf(“%d”,min);
}

2. External [global Variable]

1. Variables that are declared outside of all functions are known as external or global
variables.
2. They are accessible from any function inside the program.

Example

#include <stdio.h>

int n = 5; // global variable OR External

int main()
{
n++;
printf("n = %d", n);
return 0;
}

Output
n=6

MET BKC IOE IT


3. Static
1. A static variable possesses the property of preserving its actual value even after it is
out of its scope.
2. Once a static variable is defined it lives until the end of the program.
3. A static variable is declared by using the static keyword.

Example;
#include <stdio.h>

void display();

int main()
{
display();
display();
}
void display()
{
static int c = 1;
c += 5;
printf("\n%d ",c);
}

Output
6

11

During the first function call, the value of c is initialized to 1. Its value is increased by 5.
Now, the value of c is 6, which is printed on the screen.
During the second function call, c is not initialized to 1 again. It's because c is a static
variable. The value c is increased by 5. Now, its value will be 11, which is printed on the
screen.

MET BKC IOE IT


4. register
1. The register keyword is used to declare register variables.
2. Register variables were supposed to be faster than local variables.
3. The main difference is that variables of register storage are initialized inside a CPU
register.

register int x;

#include <stdio.h>
int main()
{
register int x=10;
printf("%d",x);

Defining Symbolic Constants:


1. The C language's "#define" command is used to declare symbolic constants.
2. A symbolic constant is a name that represents a fixed value that doesn't change during a
program's execution
Example :

#define PI 3.141592
#define GOLDENRATIO 1.6
#define MAX 500

Here PI , GOLDENRATIO, MAX are symbolic constants.

#include <stdio.h>

#define PI 3.141592

void main()
{
printf("%f",PI);
}

Output
3.141592

MET BKC IOE IT


Declaring a Variable as Constant
1. The constants in C are the read-only variables whose values cannot be modified once
they are declared in the C program.
2. We define a constant in C language using the const keyword
3. The const keyword is placed at the start of the variable declaration to declare that variable
as a constant.
Example

const int roll_no = 100;

Example

#include <stdio.h>
int main()
{
const float PI= 3.14;
float area;
int r=5;
area =PI *r*r;
printf(" Area of Circle %f",area);
}

Output
Area of Circle 78.500000

Properties of Constant in C
1. Initialization with Declaration
We can only initialize the constant variable in C at the time of its declaration.
Otherwise, it will store the garbage value.
2. Immutability
The constant variables in c are immutable after its definition, i.e., they can be initialized
only once in the whole program.
After that, we cannot modify the value stored inside that variable.

Types of Constants in C
The type of the constant is the same as the data type of the variables.
Following is the list of the types of constants
 Integer Constant : cons int x=10;
 Character Constant : const char x=’A’
 Floating Point Constant : float val=3.14;
 Double Precision Floating Point Constant : const double c=99.99;

MET BKC IOE IT


Declaring a Variable as Volatile
1. The volatile keyword in C is used to inform the compiler that a variable's value may

be changed by something outside the normal program flow, such as hardware, an

interrupt service routine, or a different thread of execution.

2. This prevents the compiler from applying certain optimizations that could lead

to unexpected behavior.

3. The volatile keyword can be applied to variables of any type, including int, float,

double, and even struct and union types.

4. The volatile keyword is often used in embedded systems programming, where

hardware devices may modify the value of memory-mapped registers at any time.

5. The volatile keyword is a powerful tool in C programming that allows for safe
interaction with variables that can change outside of the program's control.

Example
volatile int x = 0; // declare a volatile integer variable

MET BKC IOE IT


MET BKC IOE IT
MET BKC IOE IT
MET BKC IOE IT
MET BKC IOE IT

You might also like