FPL First Year Notes
FPL First Year Notes
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.
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.
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.
1.2 Importance 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.
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.
Alphabets
Digits
Special characters
White space
Alphabets
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”;
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
%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.
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.