C-NOTES(2024-25)
C-NOTES(2024-25)
Computer Hardware
1. Input devices: These components are mostly to be used for recognized the
input data that are given by users for solving their problems. Input
instructions can be different types such as text, images, audio, and video.
4. Storage devices: The core task of the memory is storage and retrieval, this
depend on the Read/Write operation of all types of memory.
Introduction to C
There are two types of storage devices such as:
Computer Software:
Types of Software:
Introduction to C
Interpreter –
The translation of single statement of source program into machine code is done by
language processor and executes it immediately before moving on to the next line
is called an interpreter. If there is an error in the statement, the interpreter
terminates its translating process at that statement and displays an error message.
Introduction to C
The interpreter moves on to the next line for execution only after removal of the
error. Example: Perl, Python and MatLab.
Compiler –
The language processor that reads the complete source program written in high
level language as a whole in one go and translates it into an equivalent program in
machine language is called as a Compiler. The compiler specifies the errors at the
end of compilation with line numbers when there are any errors in the source code.
The errors must be removed before the compiler can successfully recompile the
source code again. Example: C, C++, C#, Java
Device drivers: Device driver or hardware driver is a group of files that enable
one or more hardware devices to communicate with the computer's operating
system. Without drivers, the computer would not be able to send and receive data
correctly to hardware devices, such as a printer.
Utility software helps to manage, maintain and control computer resources.
Operating systems typically contain the necessary tools for this, but separate utility
Introduction to C
programs can provide improved functionality. Examples of utility programs are
antivirus software, backup software etc..
Application software is commonly defined as any program or number of
programs designed for end-users.
General purpose application software is a type of application that can be used
for a variety of tasks. It is not limited to one particular function. ... Examples
of General purpose application software include: Word processors, Spreadsheet
and Presentation software.
Specialist application software performs a single very specific type of task.
Examples include programs to work out payroll, calculate accounts, deal with
stock control and handle appointments.
Customised software development is the process of designing, creating, deploying
and maintaining software for a specific set of users, functions or organizations.
Example includes salary component of an organization.
Introduction to C
Functional units of a computer:
A computer in its simplest form comprises five functional units namely input unit,
output unit ,memory unit, arithmetic & logic unit and control unit.
1. Input Unit: Computer accepts encoded information through input unit. The
standard input device is a keyboard. Whenever a key is pressed, keyboard
controller sends the code to CPU/Memory.
2. Memory Unit: Memory unit stores the program instructions (Code), data and
Secondary memories are non volatile in nature. These are used when large
amount of data and programs have to be stored.
3. Arithmetic and logic unit: ALU consist of necessary logic circuits like adder,
comparator etc., to perform operations of addition, multiplication, comparison of
two numbers etc.
4. Output Unit: Computer after computation returns the computed results, error
messages, etc. via output unit. The standard output device is a monitor, LCD/TFT
monitor. Other output devices are printers, projectors etc.
5. Control Unit: Control unit co-ordinates activities of all units by issuing control
signals. Control signals issued by control unit govern the data transfers and then
appropriate operations take place. Control unit interprets or decides the
operation/action to be performed.
1. Once the input is provided the raw data is processed by a suitable or selected
processing method. This is the most crucial step as it allows the processed
data in the form of output which will be used further.
2. Processing is usually done by CPU (Central Processing Unit) in a computer.
CPU is the crucial component for getting the operations done. The speed of
processing depends on the computing power of the processor.
1. This is the outcome, and the raw data provided in the first stage is now
“processed,” and the data is useful and provides information and no longer
called Data. This might be further used for data visualization.
2. This can be used as it is or used for further processing along with more data.
Output device can be a paper or in form of display screen such as monitors
or phone screen.
Storage can be done on various storage devices such as external hard disk,
inbuilt hard disk, pen drives, compact disks or even in registers.
Introduction to C
Introduction to C
Introduction to C
Execution
The program may ask user for inputs and generates output after processing the
inputs.
Debugging and Testing
Debugging is the discovery and correction of programming errors. When error
appears debugging is necessary.
Testing ensures that program performs correctly the required task.
Introduction to C
Introduction to C
Algorithm and flowchart
The algorithm and flowchart are two types of tools to explain the process of a
program. An algorithm is a step-by-step analysis of the process, while a flowchart
explains the steps of a program in a graphical way.
Definition of algorithm
To write a logical step-by-step method to solve the problem is called the algorithm;
in other words, an algorithm is a procedure for solving problems. In order to solve
a mathematical or computer problem, this is the first step in the process. An
algorithm includes calculations, reasoning, and data processing. Algorithms can be
presented by natural languages, pseudocode etc.
Algorithm has the following characteristics
Flowchart:
Introduction to C
2. Convert Temperature from Fahrenheit (F) to Celsius (C)
Algorithm:
Step 1: Start
Flowchart:
Introduction to C
3. largest of two numbers
Algorithm:
Step 1: Start
Step 2: Input a, b .
Step 3: If a>b then
Display “a is the largest number”.
Otherwise
Display “b is the largest number”.
Step 4: Stop.
Flowchart:
Introduction to C
4. To check the given number is even or odd.
Algorithm:
Step 1: Start
Step 2: Input Number .
Step 3: If Number%2==0 then
Display “Even number”. goto Step4
Otherwise
Display “odd number”.
Step 4: End.
Flowchart
Introduction to C
5. To check the given number is zero, positive or negative.
Algorithm
Step 1: Start
Step 2: Get n
Step 3: If n==0 then print “zero” goto step5
Step 4: if n>0 then print “positive” goto step5
Otherwise print “negative”
Step5: stop
Flowchart
Introduction to C
6. To find largest of three numbers
Algorithm
step 1 : start
step 2 : Read A,B,C
step 3 : if(A>B)then goto next step
otherwise goto step5
step 4 : if(A>C) then goto step6 otherwise step8
step 5 : if(B>C) goto step7 otherwise step8
step6:print “A is largest” then goto step9
step7:print “B is largest” then goto step9
step8:print “C is largest”
Step9:stop
Flowchart
Introduction to C
Overview of C language:
Introduction to C
▪ C is a procedural programming language.
▪ It was initially developed by Dennis Ritchie in the year 1972.
▪ It was mainly developed as a system programming language to write an operating system.
▪ It is machine-independent, structured programming language which is used extensively
in various applications.
▪ One can say, C is a base for the programming. If you know 'C,' you can easily grasp the
knowledge of the other programming languages that uses the concept of 'C‘.
Features/Characteristics of C
Documentation Section
This section consists of comment lines which include the name of the program, the author and
other details like time and date of writing the program. Documentation section helps anyone to
get an overview of the program.
Link Section
The link section consists of the header files of the functions that are used in the program. It
provides instructions to the compiler to link functions from the system library.
Definition Section
All the symbolic constants are written in definition section.
Global Declaration Section
The global variables that can be used anywhere in the program are declared in global declaration
Introduction to C
section. This section also declares the user defined functions.
main() Function Section
It is necessary have one main() function section in every C program. This section contains two
parts, declaration and executable part.
Subprogram Section
The subprogram section contains all the user defined functions that are used to perform a specific
task. These user defined functions are called in the main() function.
Introduction to C
Introduction to C
Process of compiling and running the program
Introduction to C
Important Points
✓ C program file (Source file) must be saved with .c extension.
✓ Input to the compiler is .c file and output from the compiler is .exe file, but it also
generates .obj file in this process.
✓ The compiler converts the file only if there are no errors in the source code.
✓ CPU places the result in User Screen window.
Escape Sequences
In C Programming, the word escape sequences means to escape the character from
the sequences of words and give special meaning to the escaped character.
Let us consider “Hello\n good morning". Clearly, n is the character which escapes from the
sequences of word, Hellogood morning and a special meaning is given to n i.e new line.
✓ An Escape sequences are non-printing characters.
✓ An Escape sequences are certain characters associated with \ (backslash).
✓ An Escape sequences always begin with \ (backslash) and is followed by one character.
✓ An Escape sequences are used in printf() statement.
Introduction to C
Character Set in C
Like every other language, 'C' also has its own character set.
A character set in 'C' is divided into,
➢ Letters: uppercase and lowercase letters
➢ Numbers:0-9
➢ Special characters: ; , . ( ) { } # % & / \ _ ? + - * etc..
➢ White spaces (blank spaces): Blank space,New line,Horizontal tab etc..
C Tokens
The compiler breaks a program into the smallest possible units and proceeds to the various stages
of the compilation, which is called token.
In C programs, each word and punctuation is referred to as a token. C Tokens are the smallest
building block or smallest unit of a C program.
Let suppose even we have a lot of words we can’t make a sentence without combining them, the
same way we can’t develop the application without using tokens in C language. So, we can say
that tokens in C language are the building block of C programming language.
Introduction to C
C Supports 6 Types of Tokens
✓ Keywords
✓ Identifiers
✓ Strings
✓ Operators
✓ Constants
✓ Special Symbols
Keywords
Keywords in C language are predefined or reserved keywords used to expose the behavior of the
data. There are 32 keywords in C.
Identifiers
Identifier in C language is used for naming functions, variables, structures, arrays, etc. The
identifier is user-defined words. An identifier is used to identify elements of a program.
Rules to construct identifiers is:
✓ The first character should be either alphabet or underscore and then followed by any
character, digit.
✓ Identifiers are case sensitive as there is A and a treated as different.
✓ Special symbols(except underscore) and white space characters are not allowed
Introduction to C
✓ Keywords can’t be used for identifiers.
✓ The length of the identifiers should not be more than 31 characters.
✓ The name must be meaningful.
Variable
A variable is an identifier which is used to store some value. Variables can change during the
execution of a program and update the value stored inside it. A single variable can be used at
multiple locations in a program. A variable name must be meaningful. A variable must be
declared first before it is used somewhere inside the program.
RULES FOR NAMING C VARIABLE:
➢ Variable name must begin with letter or underscore.
➢ Variables are case sensitive.
➢ They can be constructed with digits, letters.
➢ No special symbols and whitespaces are allowed other than underscore.
➢ A variable name should not consist of a keyword.
Note: You should always try to give meaningful names to variables
Introduction to C
Examples for variables
C Constants
C Constants is the most fundamental and essential part of the C programming language.
Constants in C are the fixed values that are used in a program, and its value remains the same
during the entire execution of the program.
✓ Constants are also called literals.
✓ Constants can be any of the data types.
✓ It is considered best practice to define constants using only upper-case names.
Types of constants
Introduction to C
Integer Constants
An integer constant is an integer quantity which contains a sequence of digits.It should not have
a decimal point. Blanks and commas are not allowed within an integer constant.An integer
constant can be either +ve or -ve.
An integer constant can be either Decimal, HexaDecimal or Octal.
A decimal constant can contain any combination of integers from 0 through 9.
An octal constant should begin with digit 0. It can take any digits from 0 through 7.
A hexadecimal constant should begin with 0X or 0x.
Ex:15, -265, 0, 99818, +25, 045, 0X6
Character Constant
A character constant is a character which is enclosed in single quotes.
Example: ‘A’ , ‘a’ , ‘ b’ , ‘8’
String Constant
– A string constant is a collection of characters enclosed in double quotations “ ”
– It may contain alphabets, digits, special characters and blank space.
Example: “bec” , “bgk2021”
Defining Constants:
In C program we can define constants in two ways :
✓ Using #define preprocessor directive
✓ Using a const keyword
Using #define preprocessor directive: We can use this to declare a constant as shown below:
#define identifierName value
identifierName: It is the name given to constant.
value: This refers to any value assigned to identifierName.
ex: #define PI 3.142
Using a const keyword: We can use this to declare a constant as shown b
elow:
Introduction to C
Data Types in C
Format specifiers in C
The format specifier is used during input and output. It is a way to tell the compiler what type of
data is in a variable during taking input using scanf() or printing using printf(). Some examples
are %c, %d, %f etc.
Format specifiers define the type of data to be printed on standard output. You need to use format
specifiers whether you're printing formatted output with printf() or accepting input with scanf().
% specifies the minimum field width.
Introduction to C
Variable Declaration in C
A variable declaration provides assurance to the compiler that there exists a variable with the
given type and name so that the compiler can proceed for further compilation without requiring
the complete detail about the variable. A variable definition has its meaning at the time of
compilation only, the compiler needs actual variable definition at the time of linking the
program.
Syntax: data_ type variable_list;
Example: int a,b;
float n1,n2;
Variables can be initialized (assigned an initial value) in their declaration. The initializer consists
of an equal sign followed by a constant expression as follows −
Syntax: data_type variable_name = value;
example: int a=10,b=20;
Operators in C
An operator is a symbol that tells the compiler to perform specific mathematical or logical
functions.
C language provides a rich set of operators. They may operate on single operand or two
operands.
They are used to perform basic arithmetic operations, comparisons, manipulations of bits and so
on.
Introduction to C
Introduction to C
Arithmetic operators
C supports all the basic arithmetic operators. The following table shows all the basic arithmetic
operators
Operator Description
% remainder of division
Relational operators
The following table shows all relation operators supported by C.
Operator Description
> Check if operand on the left is greater than operand on the right
Bitwise operators
Bitwise operators perform manipulations of data at bit level. These operators also
perform shifting of bits from right to left. Bitwise operators are not applicable to float or double
Operator Description
| Bitwise OR
^ Bitwise exclusive OR
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
Introduction to C
Assignment Operators
Assignment operators supported by C language are as follows.
= assigns values from right side operands to left side operand a=b
+= adds right operand to the left operand and assign the result to left a+=b is same as a=a+b
-= subtracts right operand from the left operand and assign the result to a-=b is same as a=a-b
left operand
*= mutiply left operand with the right operand and assign the result to left a*=b is same as a=a*b
operand
/= divides left operand with the right operand and assign the result to left a/=b is same as a=a/b
operand
%= calculate modulus using two operands and assign the result to left a%=b is same as a=a%b
operand
Conditional operator
The conditional operators in C language are known by two more names
1) Ternary Operator
2) ? : Operator
It is actually the if condition that we use in C language decision making, but using conditional
operator, we turn the if condition statement into a short and simple operator.
The syntax of a conditional operator is :
expression 1 ? expression 2: expression 3
Introduction to C
Example:
#include <stdio.h>
int main()
{ int a=5,b=10,c; // variable declaration
c = a > b ? a : b; // conditional operator
printf("The value of variable c is : %d",c);
return 0;
}
Increment and Decrement Operators
C has two special unary operators called increment (++) and decrement (--) operators. These
operators increment and decrement value of a variable by 1.
Increment/Decrement operators are of two types:
1. Prefix increment/decrement operator.
2. Postfix increment/decrement operator.
1. Prefix increment/decrement operator
The prefix increment/decrement operator immediately increases or decreases the current value of
the variable. This value is then used in the expression. Let's take an example:
y = ++x;
Here first, the current value of x is incremented by 1. The new value of x is then assigned to y.
Similarly, in the statement:
y = --x;
the current value of x is decremented by 1. The new value of x is then assigned to y.
2.Postfix Increment/Decrement operator
The postfix increment/decrement operator causes the current value of the variable to be used in
the expression, then the value is incremented or decremented.
For example:
y = x++;
Here first, the current value of x is assigned to y then x is incremented.
Similarly, in the statement:
y = x--;
the current value of x is assigned to y then x is decremented.
Introduction to C
Special Operators in C
✓ The Comma Operator
✓ sizeof operator
✓ Reference operator or Address Operator ("&")
✓ Dereference operator ("*") or Pointer Operator
✓ Dot Operator
Comma Operator
Comma Operator in C programming language has two contexts −
✓ As a Separator
✓ As an operator − The comma operator { , } is a binary operator that discards the first
expression (after evaluation) and then use the value of the second expression.
Each expression must be separated using the comma ( , ) and are evaluated from left to right. The
value of rightmost expression becomes the value of the overall expression
Ex: sum = (a=3, b=4, c=5, a+b+c)
Or a=3, b=4, c=5;
sum = a+b+c;
Sizeof() operator
The sizeof() is a unary operator used to determine the size of its operand.
The general form of sizeof operator is:
Syntax: sizeof(object)
where object can be data type keywords like int, float, double or expression or variable.
For example, sizeof(int) gives the size occupied by an int data type. The sizeof operator returns
size in bytes.
Introduction to C
Operators Precedence in C
Operator precedence determines the grouping of terms in an expression and decides how an
expression is evaluated. Certain operators have higher precedence than others; for example, the
multiplication operator has a higher precedence than the addition operator.
For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher
precedence than +, so it first gets multiplied with 3*2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those with the lowest
appear at the bottom. Within an expression, higher precedence operators will be evaluated first.
Implicit type casting means conversion of data types without losing its original meaning. This
type of typecasting is essential when you want to change data types without changing the
significance of the values stored inside the variable.
Implicit type conversion in C happens automatically when a value is copied to its compatible
data type. During conversion, strict rules for type conversion are applied. If the operands are of
two different data types, then an operand having lower data type is automatically converted into
a higher data type. This type of type conversion can be seen in the following example.
#include<stdio.h>
int main(){
char ch=’a’;
int b; //declaring int variable
b=a+1; //implicit type casting
printf("%d\n",b);
}
Output: 98
Explicit type conversion rules out the use of compiler for converting one data type to another
instead the user explicitly defines within the program the data type of the operands in the
expression.
Introduction to C
#include<stdio.h>
int main()
{
float a = 1.2;
//int b = a; //Compiler will throw an error for this
int b = (int)a + 1;
printf("Value of b is %d\n",b);
return 0;
}
Output:
Value of b is 2