0% found this document useful (0 votes)
78 views15 pages

FPL Unit 1 Notes

Uploaded by

alokchavan7082
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)
78 views15 pages

FPL Unit 1 Notes

Uploaded by

alokchavan7082
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

Unit I - Introduction to Program Planning & C Programming

Unit I - Introduction to Program Planning & C Programming


CONTENTS
Program Design Tools: Art of Programming through Algorithms, Flowcharts. Overview of C:
History and importance C, Character Set, C Tokens, Keywords and Identifiers, Constants,
Variables, Data types, Declaration of variables, Storage Class, Assigning Values to variables,
Defining Symbolic Constants, declaring a Variable as Constant, Declaring a Variable as
Volatile

Program Design Tools


Algorithm- It is a sequence of steps that need to be followed in order to achieve a certain
task or solve a problem
OR

A process or step-by-step set of instructions designed to solve a problem.

It is the foundation for writing programs, because every program is essentially an


implementation of an algorithm.

Characteristics of an Algorithm

1. Input: May take zero or more inputs.


2. Output: Produces at least one result.
3. Definiteness (Clear and Unambiguous): Each step must be clearly defined and
unambiguous, so it can be understood and executed precisely.
4. Finiteness: Must terminate after a finite number of steps.
5. Effectiveness: Each step must be practical and executable.

Benefits of an Algorithm

1. Clarity in Problem Solving:


o Algorithms provide a step-by-step procedure, making it easier to understand
and solve a problem systematically.
2. Reusability:
o Once an algorithm is designed, it can be used multiple times for similar
problems or tasks.
3. Efficiency:
o Helps in identifying the most efficient way to solve a problem, saving time
and resources.
4. Simplifies Programming:
o Algorithms form the foundation of programs, making coding easier and
more organized.
5. Debugging and Testing Made Easier:
o Clear step-by-step instructions make it easier to identify and correct errors
in programs.

1
Unit I - Introduction to Program Planning & C Programming

6. Independence from Programming Language:


o An algorithm can be designed without worrying about a specific
programming language, making it flexible and universal.
7. Improves Problem-Solving Skills:
o Thinking in terms of algorithms enhances logical and analytical thinking.

Simple Example of an Algorithm


Problem: Find the sum of two numbers.

Algorithm:
Step 1: Start
Step 2: Read the first number (A)
Step 3: Read the second number (B)
Step 4: Calculate SUM = A + B
Step 5: Print the SUM
Step 6: Stop

Problem: Add 10 and 20 and print the sum

Algorithm:
Step 1: Start
Step 2: Initialize the first number A = 10
Step 3: Initialize the second number B = 20
Step 4: Calculate SUM = A + B
Step 5: Print SUM
Step 6: Stop

Problem: Print “Hello World”

Algorithm:
Step 1: Start
Step 2: Write the instruction to print “Hello World”
Step 3: Execute the print instruction
Step 4: Stop

Problem: Multiply 10, 20, and 30 and print the product

Algorithm:
Step 1: Start
Step 2: first number A = 10
Step 3: second number B = 20
Step 4: third number C = 30
Step 5: Calculate PRODUCT = A × B × C
Step 6: Print PRODUCT
Step 7: Stop

2
Unit I - Introduction to Program Planning & C Programming

Problem: Compute and print the quotient and remainder of two numbers

Algorithm:

Step 1: Start
Step 2: Read the first number (Dividend) → let it be A
Step 3: Read the second number (Divisor) → let it be B
Step 4: Calculate Quotient = A ÷ B
Step 5: Calculate Remainder = A mod B
Step 6: Print Quotient and Remainder
Step 7: Stop

Problem: Divide two numbers

Algorithm:

Step 1: Start
Step 2: Read the first number (Dividend) A
Step 3: Read the second number (Divisor) B
Step 4: Calculate Quotient = A ÷ B
Step 5: Print the Quotient
Step 6: Stop

Problem: Find the average of three numbers

Algorithm:
Step 1: Start
Step 2: Read the first number A
Step 3: Read the second number B
Step 4: Read the third number C
Step 5: Calculate SUM = A + B + C
Step 6: Calculate AVERAGE = SUM ÷ 3
Step 7: Print AVERAGE
Step 8: Stop

Advantages of Algorithm

1. Clarity: Algorithms provide a clear and step-by-step procedure, making it easier to


solve problems systematically.
2. Reusability: Once designed, an algorithm can be used multiple times for similar
problems or tasks.

Disadvantages of Algorithm

1. Time-consuming: Designing an algorithm for complex problems can be time-


consuming and requires careful thought.
2. Not language-specific: An algorithm itself cannot be executed directly; it needs to
be implemented in a programming language.

3
Unit I - Introduction to Program Planning & C Programming

Flowcharts

Definition of Flowchart

A flowchart is a graphical representation of an algorithm, process, or workflow.


It uses standard symbols to show the sequence of steps and the flow of control in a process.

Symbol Name Meaning / Use


Start / Stop
Shows where the flowchart begins or ends
(oval shape) (Terminal)
Used for any operation or calculation (e.g., “Add two
Processing
(rectangle) numbers”)
Used for input (like entering data) or output (like
Input / Output
displaying results)
(parallelogram)
Used to ask a question or check a condition (e.g.,
Decision
Yes/No or True/False)
(diamond)
Used to connect different parts of a flowchart on the
Connector
same page
(small circle)

Flow line Shows the direction of flow between symbols


(arrow)

History of C Language

C is a general-purpose, structured, and powerful programming language.


It was mainly developed to write system software, especially the UNIX operating system

Development History

 1960 → ALGOL language developed at Cambridge University.


 1963 → CPL (Combined Programming Language) developed.
 1967 → BCPL (Basic CPL) developed by Martin Richards.
 1970 → B language developed by Ken Thompson at AT&T Bell Labs.
 1972 → C language developed by Dennis Ritchie at AT&T Bell Labs, USA.
 1978 → The first C book published by Kernighan and Ritchie (K&R C).
 1989 → ANSI standardizes C (known as ANSI C / C89).
 1990 → ISO internationally standardizes C (known as ISO C).
 1999 → C99 version released (added new features).
 2011 → C11 version released (added multithreading).
 2017 / 2018 → C17 / C18 versions released (minor updates).
 2023 → C23 version released (latest standard).

4
Unit I - Introduction to Program Planning & C Programming

Features and Importance of the C Language

C language is one of the most powerful, efficient, and widely used programming languages.
It plays a key role in the development of modern computer science and software systems

1. Foundation of Computer Science Concepts – Builds strong basics in programming,


data structures, and algorithms.
2. Portability – C programs can run on different machines without modification.
3. Efficiency and Performance – Executes fast and uses memory efficiently.
4. Foundation of Other Languages – Forms the base for languages like C++, Java, and
Python.
5. Extensible Language – Allows adding new features and functions easily.
6. Rich Library – Offers many built-in functions for faster coding.
7. Wide Availability – Supported by all major operating systems and compilers.
8. Simple and Easy Language – Easy to learn and understand for beginners.

Character Set in C

Definition: A character set in C is the collection of valid characters that can be


used to write C programs.
These characters are used to form tokens such as keywords, identifiers, constants,
operators, and symbols.

Categories of Character Set

1. Letters (Alphabets)

 Uppercase letters: A to Z
 Lowercase letters: a to z
 Use: For keywords, identifiers, and user-defined names.
 Examples: int, total, main

2. Digits

 Digits: 0 1 2 3 4 5 6 7 8 9
 Use: In numeric constants and identifiers (but identifiers cannot start with a digit).
 Examples: 123, price1

3. White Spaces

 Includes: Blank space, tab, newline, carriage return, form feed


 Use: To separate tokens and improve readability.
 Note: Ignored by the compiler (except inside strings).
 Example int age = 20; // spaces between tokens

4. Special Characters

 Use: As part of syntax or to form operators.


 Examples:

5
Unit I - Introduction to Program Planning & C Programming

o Punctuation: , ; : . #
o Brackets: ( ) { } [ ]
o Others: \ " ' % & ^ | ~

5. Operators

 Definition: Special symbols used to perform operations.


 Examples:
+ - * / % = < > ! && ||

6. Escape Sequences (Backslash Characters)

 Definition: Special character combinations starting with a backslash ( \) that have


specific meanings in C.

Escape Sequence Meaning


\n New line
\t Horizontal tab
\\ Backslash
\" Double quote
\' Single quote

The character set in C provides all symbols needed to write programs including letters,
digits, special symbols, and escape sequences making C powerful, readable, and structured.

C Tokens

Tokens are the smallest individual elements of a C program that the compiler can recognise
and process.

Types of Tokens in C

1. Keywords
o Predefined, reserved words in C with special meaning to the compiler.
o Cannot be used as variable or function names.
o Examples: int, float, return, if, else, for, while, switch, break
2. Identifiers
o Names given by the programmer to variables, functions, arrays, etc.
o Rules for identifiers:
1. Must start with a letter or underscore.
2. Can include letters, digits, and underscores.
3. Cannot contain spaces or special characters.
4. Cannot be a keyword.
5. Case-sensitive (Total ≠ total).
o Examples: age, totalMarks, main, printf
3. Constants
o Fixed values that do not change during program execution.
o Types:

6
Unit I - Introduction to Program Planning & C Programming

 Integer constants: 10, -45


 Floating-point constants: 3.14, -0.99
 Character constants: 'A', '5'
4. Strings
o Sequence of characters enclosed in double quotes.
o Examples: "Hello World", "C Language"
5. Operators
o Symbols used to perform operations on data.
o Types:
 Arithmetic: +, -, *, /, %
 Relational: ==, !=, <, >
 Logical: &&, ||, !
 Assignment: =, +=, -=
6. Special Symbols (Separators)
o Symbols with a specific meaning in C.
o Examples:
 () → Parentheses (function calls)
 {} → Braces (block of code)
 ; → Statement terminator
 # → Preprocessor directive symbol
7. Punctuators
o Sometimes grouped with special symbols; used to separate statements and
expressions.
o Examples: ;, ,, {}, ()

Keywords in C

Definition:
Keywords are predefined, reserved words in C that have special meaning to the compiler.
They cannot be used as identifiers (variable or function names).

Key Points:

 There are 32 keywords in C (ANSI C standard).


 All keywords are written in lowercase.
 Each keyword serves a specific

7
Unit I - Introduction to Program Planning & C Programming

32 Keywords:

Example
int age = 25; // 'int' is a keyword

Identifiers
Definition: Identifiers are names given by the programmer to elements like variables,
functions, arrays,
structures, etc.
Rules for Identifiers
1. Can include letters (A–Z, a–z), digits (0–9), and underscore (_).

2. Must not begin with a digit (e.g., 123abc ❌).

3. No spaces or special characters allowed (e.g., my-variable ❌).


4. Cannot be a keyword (e.g., you cannot name a variable int).
5. Case-sensitive → Total and total are different identifiers.
6. Should be meaningful for better readability.
Examples of Identifiers:

age, marks, totalMarks, calculateArea, student_name


Example in Code:

int marks = 90; // 'marks' is an identifier

Constants in C

Definition:
A constant is a fixed value that does not change during the execution of a C program.

8
Unit I - Introduction to Program Planning & C Programming

Constants are used to represent data that remains the same throughout the program,
improving readability and reducing errors.

Types of Constants in C

1. Integer Constants
o Whole numbers without a fractional part.
o Can be positive or negative.
o Examples: 0, 10, -25, 1000

Types of Integer Constants:

o Decimal (Base 10): 45, 0, -89


o Octal (Base 8): Begins with 0 → 012, 077
o Hexadecimal (Base 16): Begins with 0x or 0X → 0xA, 0X1F

2. Floating-Point Constants (Real Constants)

 Numbers with a decimal point or exponent.


 Examples: 3.14, -0.45, 6.02e23
 Used to represent fractional values or very large/small numbers in scientific notation.

3. Character Constants
o A single character enclosed in single quotes.
o Examples: 'A', 'z', '5', '%'
o Internally stored as an integer ASCII value, e.g., 'A' = 65.
o
4. String Constants (Literals)
o A sequence of characters enclosed in double quotes.
o Examples: "Hello", "123", "C Language"
o Used to represent text or messages in a program.

Data Types in C

9
Unit I - Introduction to Program Planning & C Programming

Definition:
A datatype in C is a keyword that specifies the type of data a variable can store. It defines:

1. The kind of values (integer, character, decimal, etc.),


2. The amount of memory to be allocated, and
3. The operations that can be performed on the data.

Data Size (depends on


Description Example
Type compiler/system)

Integer (whole %d
int 2 or 4 bytes int a = 10;
numbers)

Single-precision %f
float 4 bytes float b = 3.14;
decimal numbers

Double-precision %lf double c =


double 8 bytes
decimal numbers 3.141592;

char %c char d = 'A';


1 byte Single character

Represents no void
void — function();
value / empty

Format specifiers are the symbols that are used for printing and scanning values of given
data types.

 Integer Data Type Range: -2,147,483,648 to 2,147,483,647


 Character Data Type Range: (-128 to 127) or (0 to 255)
 Float Data Type Range: 1.2E-38 to 3.4E+38
 Double Data Type Range: 1.7E-308 to 1.7E+308
Void Data Type The void data type in C is used to indicate the absence of a value.
Variables of void data type are not allowed. It can only be used for pointers and function
return type, and parameters.

Variables in C

Definition:
A variable in C is a named storage location in memory that holds a value, which can be
changed during program execution.

It acts like a container to store data.

Rules for Naming Variables

1. Must begin with a letter or underscore (_).


2. Can contain letters, digits, and underscore (e.g., age1, _sum).
3. Cannot use keywords (int, float, etc.).

10
Unit I - Introduction to Program Planning & C Programming

4. Case-sensitive (Age and age are different).


5. Should be meaningful for readability.

Declaration and Initialization of variables

 Declaration: Telling the compiler about the variable and its type.
 int age; // declaration
 int age = 20; // declaration + initialization
 int age;
 age = 20;

Example

int rollNo = 5; // integer variable


float marks = 85.5; // float variable
char grade = 'A'; // character variable

Defining Symbolic Constants


DEFINITION: In C programming, symbolic constants are names that represent fixed
values. Instead of using literal values directly in your program, you give them meaningful
names.
This improves readability, makes code easier to maintain, and reduces errors.
Ways to Define Symbolic Constants in C

1. Using #define (Preprocessor Directive)

The most common way.

 Syntax: #define NAME value

EXAMPLE

Here, PI and MAX are symbolic constants.

11
Unit I - Introduction to Program Planning & C Programming

OUTPUT:

Declaring a Variable as Constant in C


2. Using const Keyword
 Declares a variable whose value cannot be changed after initialization.
Example:

Here, SIZE and PI act as symbolic constants.

OUTPUT:

Symbolic constants in C (defined using #define or const) make programs more


readable, reliable, and easy to maintain.

12
Unit I - Introduction to Program Planning & C Programming

Storage Class
In C programming, storage classes define the scope (visibility), lifetime (existence), and
storage location of variables or functions. They tell the compiler how and where a variable
will be stored, how long it will exist, and where it can be accessed.

There are four main storage classes in C:

Example of an auto storage class

13
Unit I - Introduction to Program Planning & C Programming

Example of an extern storage class

Example of static

14
Unit I - Introduction to Program Planning & C Programming

Example of a register storage class

15

You might also like