0% found this document useful (0 votes)
1K views18 pages

FPL First Year Notes

All 5 units

Uploaded by

sanskarkale24
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)
1K views18 pages

FPL First Year Notes

All 5 units

Uploaded by

sanskarkale24
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/ 18

Unit 1

Introduction to Program Planning & C Programming

Algorithm
An algorithm is a step-by-step procedure or formula for solving a problem or
completing a
task. It consists of a finite sequence of well-defined instructions that can be
followed to
achieve a specific goal, such as performing calculations, processing data, or
making
decisions.
To make the program work properly, we must have to properly design the
algorithm.
Designing the algorithm helps to utilize the computing resources effectively.

Advantages of algorithm
o Algorithms are easy to write.
o There is a use of human-readable techniques to understand logic.
o For big problems, algorithms can be written with moderate steps.
Disadvantages of algorithm
o Algorithms are difficult to debug.
o It is hard to show branches and loops using an algorithm.
o Jumping (or goto statements) makes the algorithm hard to trace the problems.

Key Characteristics of Algorithms:


1. Finiteness: An algorithm must terminate after a finite number of steps.
2. Definiteness: Each step of the algorithm must be precisely defined and
unambiguous.
3. Input: An algorithm can accept zero or more inputs.
4. Output: It produces one or more outputs.
5. Effectiveness: The operations in the algorithm must be basic enough to be
carried out.

Flowchart
A flowchart is a graphical representation of an algorithm. Programmers often use
it
as a program-planning tool to solve a problem. It makes use of symbols that are
connected among them to indicate the flow of information and processing. The
process of drawing a flowchart for an algorithm is known as “flowcharting”.
Advantages of flowchart
1. Clear Understanding
- Flowcharts show processes visually, making it easier to understand complicated
steps
quickly.
2. Better Communication
-They help team members talk about ideas more clearly by using a simple visual
format.
3. Easier Problem Solving
- Flowcharts can highlight issues in a process, making it easier to find solutions.
4. Organized Approach
- They help you organize steps in a process, ensuring everything is in the right
order.
5. Helpful Documentation
- Flowcharts serve as a useful way to document how things work, which is great
for
training and reference.
6. Simplified Analysis
- They make it easier to analyze workflows and see the effects of changes.
7. Wide Use
- Flowcharts can be used in many areas, like business, education, and engineering.
8. Better Learning
- For students, flowcharts help visualize steps and information, making learning
easier.
9. Consistency
- They promote a standard way to show processes, making it simpler to share and
understand workflows.

Disadvantages of flowchart
1. Complexity for Large Processes
-For very complicated processes, flowcharts can become too detailed and hard to
read.
2. Time-Consuming to Create
-Designing a flowchart can take a lot of time, especially if the process has many
steps.
3. Limited Detail
- Flowcharts show the overall flow but might miss important details or nuances
in the
process.
4. Rigid Structure
- Once a flowchart is made, it can be hard to change if the process evolves or
needs
adjustments.
5. Misinterpretation
- If not created clearly, flowcharts can be misunderstood, leading to confusion
about the
process.
6. Over-Simplification
- Sometimes, flowcharts simplify things too much, missing out on essential
elements or
steps.
7. No Dynamic Interaction
- Flowcharts are static; they don’t show how processes change over time or under
different conditions.
8. Requires Training
- Not everyone is familiar with reading flowcharts, so some may need training to
understand them properly.

Difference between algorithm and flow chart:


Sr Algorithm Flowchart
No
1 An algorithm is a step-by-step A flowchart is a diagram
procedure to solve a problem. created with different
shapes to show the flow of
data.
2 The algorithm is complex to A flowchart is easy to
understand. A flowchart is understand.
easy to understand.

3 In the algorithm, plain text is In the flowchart,


used. symbols/shapes are used.
4 The algorithm is easy to A flowchart is hard to debug. .
debug.
5 The algorithm is difficult to A flowchart is simple to
construct construct.
6 The algorithm does not follow The flowchart follows rules to
any rules. be constructed.
7 The algorithm is the pseudo- A flowchart is just a graphical
code for representation of
the program. that logic.

Ooverview of C :
1.1 History of C
C is a general-purpose programming language created by Dennis Ritchie at the
AT&T (American Telephone & Telegraph) Laboratories in 1972. It is a very
popular language, despite being old. The main reason for its popularity is because
it is a fundamental language in the field of computer science.

Dennis Ritchie is known as the founder of the c language.

It was developed to overcome the problems of previous languages such as B,


BCPL, etc.

Language Year Developed By

Algol 1960 International Group

BCPL 1967 Martin Richard

B 1970 Ken Thompson

Traditional C 1972 Dennis Ritchie

K&RC 1978 Kernighan & Dennis Ritchie

ANSI C 1989 ANSI Committee

ANSI/ISO C 1990 ISO Committee

C99 1999 Standardization Committee

1.2 Importance of C

 It is one of the most popular programming languages in the world


 If you know C, you will have no problem learning other popular
programming languages such as Java, Python, C++, C#, etc, as the syntax
is similar
 C is very fast, compared to other programming languages,
like Java and Python due to its
 C is very versatile; it can be used in both applications and technologies
 It is robust language having rich set of built in functions and operators to
write any complex program.
 C is highly portable. This means that C program written for one computer
can be run on another with little or no modifications.
 C is well suited for structured programming. A problem is divided into no
of functions and modules.
 C is having ability to extend itself. It is having large set of built in functions
that are supported by C library.

1.3 Character set of C

In C programming, a character set consists of all valid characters that can be used
to form words, numbers, and expressions in the source code.

The character set is the fundamental building block of any programming


language. It is used to represent information just as natural languages do. A well-
defined character set is essential for creating effective programs, as it allows
programmers to express logic and operations clearly.

In C, a character set refers to a predefined collection of characters recognized by


the language.

This set typically includes the alphabet (both lowercase and uppercase), digits,
special symbols, and control characters. The most commonly used character set
in C is ASCII (American Standard Code for Information Interchange), which
assigns numeric codes to characters. These codes are used to represent characters
in memory and facilitate operations involving characters, such as input/output
operations, string manipulation, and comparison.

C programming language supports four types of characters:

 Alphabets

 Digits

 Special characters

 White space
Alphabets

C programming language supports four types of characters:


 Alphabets
 Digits
 Special characters
 White space
Alphabets
The C programming language allows for all upper- and lower-case alphabets in
its programs.
Lower Case alphabets: a-z (26 characters)
Upper case alphabets: A-Z (26 characters)
Digits
The C programming language also supports digits for building numeric
expressions.
Digits: 0-9 (10 characters)
Special Characters
Certain special characters are used in C programs in arithmetic expressions,
punctuation marks, conditional operators, etc. These characters can also be used
in defining the attributes of a program in a better way.
Special Characters: ` ~ @ ! $ # ^ * % & ( ) [ ] { } < > + = _ – | / \ ; : ‘ “ , . ?
White spaces
The white space characters in C refer to vertical and horizontal spaces in the
source code.
White spaces: Blank Spaces, Tab, Carriage Return, New Line, horizontal tab,
vertical tab, null

In C programming, special characters are often used to represent control


characters or to format output. Here are some commonly used special characters:
1. \n: Represents a newline character.
2. \t: Represents a tab character.
3. \b: Represents a backspace character.
4. \r: Represents a carriage return character.
5. \: Represents a backslash character.
6. ': Represents a single quote character.
7. ": Represents a double quote character.

1.4 Data Types in C:


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

Types Description

Primitive Data Primitive data types are the most basic data types
Types that are used for
representing simple values such as integers, float,
characters, etc.
User Defined The user-defined data types are defined by the user
Data Types himself.
Derived Types The data types that are derived from the primitive
or built-in
datatypes are referred to as Derived Data Types.
Integer Data Type
The integer datatype in C is used to store the integer numbers(any number
including positive,
negative and zero without decimal part). Octal values, hexadecimal values, and
decimal
values can be stored in int data type in C.
 Range: -2,147,483,648 to 2,147,483,647
 Size: 4 bytes
 Format Specifier: %d
Syntax of Integer:- int var_name;
Floating Data Types:
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;
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;
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. Since double has more
precision as compared to that float then it is much more obvious that it occupies
twice the memory occupied by the floating-point type. It can easily accommodate
about 16 to 17 digits after or before a decimal point.
 Range: 1.7E-308 to 1.7E+308
 Size: 8 bytes
 Format Specifier: %lf
Syntax of double :- double var_name;

C TOKEN
A token in C can be defined as the smallest individual element of the C
programming language that is meaningful to the compiler. It is the basic
component of a C program.
Types of Tokens in C
The tokens of C language can be classified into six types based on the functions
they are used to perform. The types of C tokens are as follows:
1. Keywords
2. Identifiers
3. Constants
4. Strings
5. Special Symbols
6. Operators

1. C Token – Keywords
The keywords are pre-defined or reserved words in a programming language.
Each keyword is meant to perform a specific function in a program. Since
keywords are referred names for a compiler, they can’t be used as variable names
because by doing so, we are trying to assign a new meaning to the keyword which
is not allowed. You cannot redefine keywords. C language supports 32 keywords
which are given below:
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

2. C Token – Identifiers
Identifiers are used as the general terminology for the naming of variables,
functions, and arrays. These are user-defined names consisting of an arbitrarily
long sequence of letters and digits with either a letter or the underscore(_) as a
first character. Identifier names must differ in spelling and case from any
keywords. You cannot use keywords as identifiers; they are reserved for special
use. Once declared, you can use the identifier in later program statements to refer
to the associated value.
Rules for Naming Identifiers
Certain rules should be followed while naming c identifiers which are as follows:
 They must begin with a letter or underscore().
 They must consist of only letters, digits, or underscore. No other special
character is allowed.
 It should not be a keyword.
 It must not contain white space.
 It should be up to 31 characters long as only the first 31 characters are
significant.

3. C Token – Constants
The constants refer to the variables with fixed values. They are like normal
variables but with the difference that their values can not be modified in the
program once they are defined.
Constants may belong to any of the data types.
There are different constant in C
 Integer constant
 Float constant
 Double constant
 Character constant

4. C Token – Strings
Strings are nothing but an array of characters ended with a null character
(‘\0’). This null character indicates the end of the string. Strings are always
enclosed in double quotes.
Example :
char string[20] = {‘g’, ’e’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’, ‘r’, ‘g’, ’e’, ‘e’, ‘k’, ‘s’, ‘\0’};
char string[20] = “Allen”;
char string [] = “Allen”;

5. C Token – Special Symbols


The following special symbols are used in C having some special meaning and
thus, cannot be used for some other purpose. Some of these are listed below:
 Brackets[]: Opening and closing brackets are used as array element
references. 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.
 Comma (, ): It is used to separate more than one statement like for
separating parameters in function calls.
 Colon(:): It is an operator that essentially invokes something called an
initialization list.
 Semicolon(;): It is known as a statement terminator. It indicates the end
of one logical entity. That’s why each individual statement must be ended
with a semicolon.
 Asterisk (*): It is used to create a pointer variable and for the
multiplication of variables.
 Assignment operator(=): It is used to assign values and for logical
operation validation.
 Pre-processor (#): The preprocessor is a macro processor that is used
automatically by the compiler to transform your program before actual
compilation.
 Period (.): Used to access members of a structure or union.
 Tilde(~): Bitwise One’s Complement Operator.

6. C Token – Operators
Operators are symbols that trigger an action when applied to C variables and
other objects. The data items on which operators act are called operands.
Depending on the number of operands that an operator can act upon, operators
can be classified as follows:
 Unary Operators: Those operators that require only a single operand to
act upon are known as unary operators.For Example increment and
decrement operators
 Binary Operators: Those operators that require two operands to act
upon are called binary operators. Binary operators can further are
classified into:
1. Arithmetic operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Bitwise Operator

Format Specifiers in C
The format specifier in C is used to tell the compiler about the type of data to be
printed or scanned in input and output operations. They always start with
a % symbol and are used in the formatted string in functions like printf(), scanf,
sprintf(), etc.
The C language provides a number of format specifiers that are associated with
the different data types such as %d for int, %c for char, etc.
List of Format Specifiers in C
The below table contains the most commonly used format specifiers in C

Format Specifier Description

%c For character type.

%d For signed integer type.

%f For float type.

%lf Double

%s String

 printf in C
In C language, printf() function is used to print formatted output to the
standard output stdout (which is generally the console screen). The printf
function is a part of the C standard library <stdio.h> and it can allow
formatting the output in numerous ways.
Syntax of printf
printf ( "formatted_string", arguments_list);
Parameters
 formatted_string: It is a string that specifies the data to be printed. It may
also contain a format specifier to print the value of any variable such as a
character and an integer.
 arguments_list: These are the variable names corresponding to the format
specifier.
Return Value
 printf() returns the number of characters printed after successful
execution.
 If an error occurs, a negative value is returned.
Example of printf
In this example, we are printing the string “Hello Geek!” in the output using
printf() function. In printf() function what we will write inside the double quotes
(” “) is printed in the output.

// C program to illustrate the use of printf function


#include <stdio.h>
int main()
{
// using printf to print "Hello World!"
printf("Hello World!");
return 0;
}

 scanf in C
In C programming language, scanf is a function that stands for Scan Formatted
String. It is used to read data from stdin (standard input stream i.e. usually
keyboard) and then writes the result into the given arguments.
 It accepts character, string, and numeric data from the user using standard
input.
 scanf also uses format specifiers like printf.
scanf Syntax
The syntax of scanf() in C is similar to the syntax of printf().
int var;
scanf(“%d”, &var);
The scanf will write the value input by the user into the integer variable var.
Why &?
While scanning the input, scanf needs to store that input data somewhere. To
store this input data, scanf needs to known the memory location of a variable.
And here comes the ampersand to rescue.
 & is also called as address of the operator.
 For example, &var is the address of var in memory.

Storage Classes in C
C Storage Classes are used to describe the features of a variable/function. These
features basically include the scope, visibility, and lifetime which help us to
trace the existence of a particular variable during the runtime of a program.
C language uses 4 storage classes :

1. auto
This is the default storage class for all the variables declared inside a function or
a block. Hence, the keyword auto is rarely used while writing programs in C
language. Auto variables can be only accessed within the block/function they
have been declared and not outside them.
2. extern
Extern storage class simply tells us that the variable is defined elsewhere and
not within the same block where it is used. Basically, the value is assigned to it
in a different block and this can be overwritten/changed in a different block as
well. So an extern variable is nothing but a global variable initialized with a
legal value where it is declared in order to be used elsewhere. It can be accessed
within any function/block.
Also, a normal global variable can be made extern as well by placing the
‘extern’ keyword before its declaration/definition in any function/block.
3. static
This storage class is used to declare static variables which are popularly used
while writing programs in C language. Static variables have the property of
preserving their value even after they are out of their scope! Hence, static
variables preserve the value of their last use in their scope. So we can say that
they are initialized only once and exist till the termination of the program. Thus,
no new memory is allocated because they are not re-declared.
Their scope is local to the function to which they were defined.
4. register
This storage class declares register variables that have the same functionality as
that of the auto variables. The only difference is that the compiler tries to store
these variables in the register of the microprocessor if a free register is
available. This makes the use of register variables to be much faster than that of
the variables stored in the memory during the runtime of the program.
If a free registration is not available, these are then stored in the memory only.
Usually, a few variables which are to be accessed very frequently in a program
are declared with the register keyword which improves the running time of the
program.

You might also like