FUNDAMENTALS OF PROGRAMMING LANGUAGES
Unit I Introduction to Program Planning & C Programming
*Program Design Tools: Art of Programming through Algorithms, Flowcharts.
[Link]-
Step by step procedure for solving any problem is known as algorithm.
*Developing an Algorithm-
The algorithm creation is a logical task.
We will follow the rules given below to write the algorithm-
i. The algorithm should always begin with the statement
“START” and end with the statement “STOP”
ii. To accept input from the user, we will use “INPUT” or the
“READ” statement.
iii. To display output on the monitor, we will use “PRINT” or
“Display” statement.
*Example-
[Link] an algorithm to display a statement “Hello World”
Step I: Start
Step II: Print “Hello World”
Step III: Stop.
[Link] an algorithm to calculate the addition of two given numbers.
Step I: Start
Step II: Read two numbers as A & B
Step III: Add numbers A and B & store results as C.
Step IV: Display C.
Step V: Stop.
OR
1
Step I: Start
Step II: Input A & B
Step III: C=A+B
Step IV: Print C.
Step V: Stop.
[Link] an algorithm to accept a number and display its square.
Step I: Start
Step II: Print “Enter the value of A”
Step III: Input A
Step IV: C=A*A
Step V: Print C.
Step VI: Stop.
[Link] an algorithm to calculate area of circle
Step I: Start
Step II: Input r
Step III: area=3.14*r*r
Step IV: Print area.
Step V: Stop.
[Link] an algorithm to swap the contents of two variables
Step I: Start
Step II: Input A & B
Step III: C=A
Step IV: A=B
Step V: B=C
Step VI: Print A, B
Step VII: Stop
2
Properties of Algorithm:
[Link]-ambiguity-
The steps used in algorithm must be clear & precise
ii. Range of Input-
The range of input for which the algorithm works is also to be
compulsorily mentioned in the algorithm.
There should be clear indication for the range of inputs for which the
algorithm may fail.
[Link]-
The algorithm can be written in English language or by the flowchart or the
pseudo code algorithm.
[Link]-
The algorithm should give the answer quickly,using less time & resources.
[Link]-
The algorithm must end after a certain number of steps; it can not run
forever.
3
[Link] :
A Flowchart is a diagrammatic representation of the steps of an
algorithm.
OR
A Pictorial representation of an algorithm is called a flowchart.
*Symbols used in flowchart-
Name Symbol Use in flowchart
Oval / Terminal Used for start or stop of an
(Start / Stop) algorithm
Flow Line Used to denote the
direction of flow
Parallelogram Used to perform input or
(Input / Output) output i.e. take input from
keyboard or give output to
monitor
Rectangle Used to indicate operation
(Processing) is to be performed, for
[Link]
Diamond This symbol is used to
(Decision) check any condition
It is used to connect or join
Connector flow lines
4
Example:
1. Draw a flowchart to add two numbers.
START S
INPUT A & B
C=A+B
PRINT C
STOP
[Link] a flowchart to swap the contents of two variables.
START
INPUT A & B
C=A
A=B
B=C
INPUT A & B
STOP
5
Overview of C: History and importance C
• Overview of C:
• It is a general-purpose, procedure-oriented programming language.
• It is both machine-independent and structured. C is a high-level programming
language developed by Dennis Ritchie in the early 1970s.
• It is now one of the most popular and influential programming languages
worldwide.
• History of C
Importance of C :
[Link] of many languages-
C is the base for many popular languages like c++, Java & Python learning C
makes it easier to learn these other languages.
[Link] & powerful-
Program written in C run very fast & can handle complex tasks efficiently.
[Link]
The same C program can run on different types of computers with little or no
change.
[Link] in system programming-
Operating systems like Unix, Linux & parts of windows are written in C
6
Character Set :
C uses the upper case letters A-Z, the lower case letters a-z, The digits 0 to 9 and
certain special characters as building blocks to form a basic C program elements such as
constants, variables, operators, expressions etc.
OR
The character set is the collection of letters, digits, & symbols you can use to write
the program.
Types Character set
Letters Uppercase(A-Z)
Lowercaes(a-z)
Digits 0-9
Special symbols , . < > / ? { } [ ] ! ( ) @ #
$ % ^ & * „ “ /
Other special character Blank space, Enter key, Tab,
New line
7
C Tokens-
It is a smallest individual element of the C programming language that is
meaningful to the compiler.
Tokens can be classified as follows-
[Link]
[Link]
[Link]
[Link]
[Link] Symbols
[Link].
Tokens in C Description Example
Keywords These are reserved words that have special Int, float, if, else
meaning to the C compiler
Identifiers Names given by the programmer to identify A
variables, functions , arrays, structure etc. sum
Constant It is a value that remains constant throughout 5
the program and cannot be changed during 1.5
program execution.. „c‟
“student”
“a”
Strings A set of characters enclosed in double quoetes “program”
“1956”
Special Punctuations such as semicolon, comma, ; , :(){}[]
symbols colon, dot, different types of brackets.
Operators It is used to perform operations such as + - / * &&
Arithmetic, logic, conditional, etc.
8
Keywords-
Keywords in C are reserved words that have predefined meaning
in C language.
We can not use keyword as identifiers.
All keywords are in lowercase letters.
9
Identifiers-
Identifier is a user-defined name of an entity to identify it uniquely during the program
execution.
OR
These are unique names that are assigned to variables, structs, functions, and other
entities.
OR
It is names given by the programmer to define a variables, functions , arrays,
structure etc.
Examples of valid & invalid identifiers
Valid Identifier Name Invalid Identifier Name
n a.5
x num!2
_id #number
num 123east
Student_name +add
Rules to defining Identifiers-
[Link] must begin with letters or underscore( _ )
[Link] cannot be a keyword.
[Link] is case sensitive.
[Link] cannot contain any special symbol except underscore( _ )
[Link] spaces are not allowed.
[Link] cannot start with a digit.
10
Constants-
It is a value that remains constant throughout the program & cannot
be changed during program execution.
There are 4 basic types of constants-
[Link] constants
[Link] constants
[Link] constants
[Link] constants
[Link] constants-
It is fixed whole number
It has no decimal point.
It can be positive & negative & zero
Example-0,12,37,-80
[Link] constants-
It is numbers decimal point.
It can be positive & negative & zero
Example-0, 1.65, -3.2, 2.5
[Link] constants-
It is a single character written in single quotes.
Example- „A‟ „;‟ „5‟ „x‟ „<‟
[Link] constants-
It is a set of character written in double quotes.
Example- “A” “students” “1987”
11
Data Types-
It specifies the type of data that the variable can store like integer, character,
floating, double, etc.
Data Types in C
Basic Derived User-defind
1. integer 1. Array 1. Structure
2. character 2. String 2. union
3. float 3. Pointer
4. double
Data Size Format Description Example
Type Specifiers
int 2 or 4 %i Stores whole numbers without 10
bytes decimals
Stores fractional numbers containing
float 4 bytes %f one or more decimals. 1.99
Sufficient for storing 6-7 decimal
digits.
Stores fractional numbers containing
double 8 bytes %lf one or more decimals. 1.99
Sufficient for storing 15 decimal digits.
character 1 bytes %c Stores a single character or letter or „A‟
number
12
Variables-
It is the name associated with some memory location to store data of different
types.
OR
Variables are created by us, the programmer to store values during a program
OR
Variable is a name given to a memory location where we can store different
values of the same data type during the program execution.
Example-
int a;
Here, int = data type
a = variable name
Declaration of variables:
1. In C , a variable must be declared before it can be used, Variable can be
declared at the start of any block of code, but most are found at the start
of each function.
2. The main purpose of variable declaration is to store the required data in
the memory location in the form of variables so that we can use them in
our program to perform any operation or task.
C Variable Syntax-
data_type variable_name ;
or
data_type variable_name1, variable_name2;
Example-
1 int number ;
The above declaration,
int= data type
number = Variable Name
2 float area ;
The above declaration,
float = data type
area = Variable Name
13
Rules for declaration of variables-
[Link] must begin with letters or underscore( _ )
[Link] cannot be a keyword.
[Link] is case sensitive.
[Link] cannot contain any special symbol except underscore( _ )
[Link] spaces are not allowed.
[Link] cannot start with a digit.
Assigning values to variable-
Assigning values to a variables means writing values to the variable.
This process involves four entities-
1. data Type
2. Variable
3. The assignment operator( = )
4. The value
Syntax-
data_type variable_name = value;
Example-
int a = 10 ;
Here,
int= data type
a = variable name
“ = ” is the assignment operator
10 = value
Defining symbolic constants-
A symbolic constant is just a name for a fixed value, created using #define.
OR
Labels/names that represent fixed values that never change during the course
of a program.
Syntax-
#define symbolic_constant_name value_of_the_constant
Example-
#define PI 3.141592
Here, PI is the symbolic constants.
14
Declaring a variable as constant-
The const keyword in c is used to define constant values that cannot be
changed at runtime
Syntax-
const data_type variable_name = value;
Example-
const int x = 5 ;
Here,
• const = keyword
• int = data type
• x = name of variable
• 5 = value
Declaring a variable as a volatile-
When a variable is declared as volatile, it indicates to the compiler that the
variables value may change at any time without any action being taken by the
code the compiler finds nearby.
Syntax-
volatile data_type variable_name ;
Example-
volatile int x ;
15
Storage classes in C-
Storage classes in C are used to determine the lifetime, visibility (scope),
memory location & initial value of a variable.
There are four types of storage classes in C-
[Link]
[Link]
[Link]
[Link]
[Link] Storage Storage Default Value Scope Life time
specifier
1 auto memory Garbage value local End of block
2 extern memory Zero ( 0 ) global Whole program
3 static memory Zero ( 0 ) local Whole program
4 register register Garbage value local End of block
16
Difference between identifier & keyword:
Feature Identifier Keyword
Definition User-defined name Reserved word in language
Names variables, functions, Language constructs (e.g.,
Usage
etc. loops)
Can programmer
Yes No
define?
Examples myVar, calculate, data1 if, while, for, return
Comparison between constants and variables :
Feature Constants Variables
Fixed values that do not change Values that can change during
Definition
during program execution. program execution.
Value Assigned once and cannot be modified Can be assigned and modified
assignment later. multiple times.
To represent fixed data like PI, max To store data that can change, like
Purpose
size, etc. user input, counters, etc.
Syntax const int MAX = 100; int count = 0;
(example)
Stored in a fixed memory location or Stored in a memory location that can
Memory
optimized by compiler. be updated.
Example PI = 3.14159 age = 25; age = 26;
Error if Compiler or runtime error if you try to
No error, changes allowed.
changed change it.
17
Questions-
Que
Questions
No.
1 What is an algorithm explain with the example? Explain the properties of an algorithm.
2 Explain various symbols used in flowcharts
3 State importance of c.
4 Explain the character set in c.
5 What is variable? How to declare variable in c? How to assign values to the variable.
6 Write short note on external storage class.
7 write an algorithm to accept a number & display its square.
8 Write an algorithm to calculate the area of a circle.
9 Compare Constants and Variables
10 Write a C Program for addition of Two Numbers.
11 Define Keywords, Identifiers and Strings with example.
12 What is an data type? Explain character data type.
13 Define symbolic constants with example
14 Write flowchart & algorithm to find area of triangle
15 Write short note on automatic storage class.
16 Write an algorithm & flowchart to calculate the area of a circle.
17 what is variable?List the rules to declaring a variable.
18 What is variable? How to declare & initialize variable in c?
19 What are identifiers?List the rules to define identifiers.
20 Write notes on storage classes in c
21 What are constants?Describe different types of constant in c with example.
22 Describe Various data types in c.
23 What are the constants in C?
24 Enlist all the data types in C language with example.
25 Write History and importance C language.
26 Write an algorithm and draw a flowchart to calculate the factorial of a number.
27 What are the different tokens of C? Explain it.
28 Differentiate between keywords and identifier
18
29 Write short note on register storage class
30 Write an algorithm and draw a flowchart to display the first 'n' natural numbers.
31 Define an algorithm and write an algorithm for addition of two numbers.
32 Explain different storage classes in C.
33 How can we define variable as volatile and Defining symbolic constants.
34 What is variable?How can we define variable as constant
19