Introduction To C Programming Notes Module 1
Introduction To C Programming Notes Module 1
MODULE 1
INTRODUCTION TO COMPUTING
Q1) Define computing and computer? With a neat block diagram explain the
components of a computer?
Figure below shows the layout of a CPU. The ALU contains circuits using which various
arithmetic and logical operations may be executed. These circuits include adders (for
addition), subtractors (for subtraction), comparators (for comparison of operands), etc.
The registers present inside the CPU may be considered to be a set of high-speed storage
devices serve as working memory for the ALU. Some of these registers are assigned the task
of holding special data or instructions. Such registers are known as special purpose registers,
whereas others are known as general purpose registers.
The units present inside the CPU interact with each other through an internal bus. Signals
generated by the CU also control the activities of the various components within the CPU.
The control bus carries control signals generated by the CU to the memory and all the
peripherals.
The address bus carries an address to locate a word in the memory. The data bus is used to
carry data and information to and from between the components of the computer. In addition,
CPUs are also equipped with high-speed, on-chip memory units known as the cache memory.
4. Memory: Memory / storage unit is the component of a computer that serves data storage
purposes. These units are of varying access speeds and volatility. Access speed refers to the
time taken to store / retrieve data from the memory. Volatility refers to the duration of time or
the conditions under which data is stored in the memory.
Based on the characteristic features, the computer‟s memory is categorized into different types.
The figure below shows the classification of the computer‟s memory.
The memory is broadly classified into primary memory and secondary memory. The primary
memory is also known as main memory, because the CPU is directly interact with it through
the data bus and the address bus.
The RAM is a volatile memory that stores instructions and data related to the program
currently being executed. The RAM is volatile, because it would lose all the information stored
in it as soon as the power to the computer is switched off. The RAM allows both the read and
write operations.
The ROM is a read-only, non-volatile memory. The ROM does not permit any write operation
and is non-volatile because the information stored in it would be retained even after the
computer has been switched off.
The ROM works as boot strap loader. The boot strap loader is a system software that is
responsible for loading the operating system from secondary memory into the RAM.
The secondary memory, also known as auxiliary memory, is a large depository of non-volatile
storage space that can store information permanently. The secondary memory is slower
compared to the access time of the primary memory, because primary memory is directly
connected to the CPU, whereas secondary memory is connected to CPU via primary memory.
The hard disk, flash drives, etc are some examples of secondary memory.
Q3) Define hardware and software. What are the different types of software?
Software is a collection of instructions, commands, programs, etc. that can make the system to
work.
Types of Software:
Depending upon the type of work a software is required to perform, it can be classified into
two categories.
i) System Software
ii) Application Software
System software is responsible for directly interacting with computer hardware and
managing its internal functions. It helps control hardware devices such as printers, storage
devices, and more.
Example: Operating Systems, Language Processors, Device Drivers.
Application software is designed to perform specific tasks for users. It operates on top of
system software and supports everyday computer functions.
Example: Word Processors, Spreadsheets, and more specialized software like graphic design
tools or database management systems.
i) Algorithm name
ii) Description
iii) Body of the algorithm
Algorithm Name: Each and every algorithm must be named. Example: Algorithm Add2
Description: We need to briefly explain what we are going to do in that algorithm. We must
also specify the name and type of input and output variables used.
Body of the algorithm: It includes the step by step solution for the given problem or program.
Algorithm ADD_2: This algorithm is used to add two integers. A and B are input integer
variables and C is the output integer variable.
Algorithm SUM_AVG_3: This algorithm is used to find sum and average of three integers.
A, B, C and SUM are input integer variables and AVG is the output floating point variable.
Example 2: Write a flowchart to find the sum and average of three integers.
The C programming language has a rich history rooted in the development of operating
systems. Its evolution can be traced through several key stages:
Origins in ALGOL and BCPL (1960s):
C's lineage can be traced back to ALGOL 58, a foundational language that introduced many
modern programming concepts. This led to CPL (Combined Programming Language) in
1960 and then BCPL (Basic Combined Programming Language) in 1967, developed by
Martin Richards.
The B Language (1970):
Ken Thompson at Bell Labs created B, a simplified version of BCPL, in 1970. B was
designed for system programming on the DEC PDP-7 and was used to develop early versions
of the Unix operating system.
The Birth of C (1972):
Dennis Ritchie, also at Bell Labs, developed C in 1972, building upon the principles of B.
Ritchie added new data types and a compiler, making C more powerful and versatile. C was
initially conceived as a system implementation language for the Unix operating system,
allowing for greater portability and efficiency than assembly language.
K&R C (1978):
Brian Kernighan and Dennis Ritchie published "The C Programming Language" in 1978 and
is often referred to as K&R C, defined the language's syntax and libraries.
ANSI C (C89/C90) (1989/1990):
The American National Standards Institute (ANSI) standardized C in 1989, resulting in
ANSI C (also known as C89). This was followed by ISO standardization in 1990 (ISO C or
C90). These standards provided a formal, consistent definition of the language, promoting
portability across different platforms.
C99 (1999):
A significant revision to the C standard was published in 1999, known as C99. It introduced
new features such as variable-length arrays, restrict pointer qualifier, and improved support
for numeric libraries.
The history and development of C language is illustrated in Figure below.
A C program may contain one or more sections. Basic structure of C program is as shown in
Figure below.
Documentation Section
Link Section (#include section)
Definition Section
Global Declaration Section
main()
{
Declaration Part
Execution Part
}
Sub Program Section
Function 1
Function 2 (User-defined Functions)
….
Function n
Figure: Basic structure of C program
• The documentation section consists of a set of comment lines giving the name of the
program, the author and other details.
• The link section provides instructions to the compiler to link functions from the system
library.
• The definition section defines all symbolic constants.
• There are some variables that are used in more than one function. Such variables are called
global variables and are declared in the global declaration section, that is outside of all the
functions. This section also declares all the user-defined functions.
• Every C program must have one main() function section. This section contains two parts,
declaration part and executable part. The declaration part declares all the variables used in
the executable part. There is at least one statement in the executable part. These two parts
must appear between the opening and the closing braces.
• All statements in the declaration and executable parts must end with a semicolon (;).
• The subprogram section contains all the user-defined functions that are called in the main()
function. User-defined functions are generally placed immediately after the main() function,
although they may appear in any order.
• All sections, except the main() function section may be absent when they are not required.
4) String Literals:
Sequences of characters enclosed in double quotes (e.g., "Hello World", "C Programming").
5) Operators:
These are symbols that perform specific operations on operands (data items). Examples
include:
Arithmetic Operators: +, -, *, /, %
Relational Operators: ==, !=, <, >, <=, >=
Logical Operators: &&, ||, !
Assignment Operators: =, +=, -=, etc.
Keywords in C are reserved words that have predefined meanings and cannot be used as
identifiers (like variable names, function names, etc.) by the programmer. They are
fundamental building blocks of the C language, used to define data types, control program
flow, manage memory, and more.
There are 32 keywords in the C language (as per the C99 standard):
Table: ANSI C Keyword
In the C programming language, identifiers are names used to uniquely identify various
program elements. These elements include:
Variables: Names given to memory locations storing data.
Functions: Names given to blocks of code that perform specific tasks.
Arrays: Names given to collections of elements of the same data type.
Structures and Unions: Names given to user-defined data types.
Labels: Names used with goto statements.
Q11) What are rules to be followed while naming an identifier. Give example.
Allowed Characters: Identifiers can consist of alphanumeric characters (A-Z, a-z, 0-9)
and the underscore symbol (_).
Starting Character: An identifier must begin with either an alphabet (uppercase or
lowercase) or an underscore. It cannot start with a digit.
Case Sensitivity: C is a case-sensitive language, meaning myVariable and MyVariable are
treated as distinct identifiers.
Keywords: C keywords (e.g., int, void, if, else, while) are reserved words and cannot be
used as identifiers.
No White Spaces: Identifiers cannot contain spaces or other special characters (e.g., !, @,
#, $).
Length: At least the first 31 characters of an identifier are significant, it is best practice to
keep identifiers reasonably concise and descriptive.
Example:
Valid identifiers Invalid identifiers
int n1, n2, n3; int 1n, 2n, 3n;
int n_1, n_2, n_3; int n 1, n 2, n 3;
int N1, N2, N3; int int;
int number1, number2, number3;
Q12) List and explain the different data types used in C language. Give example?
Data types in C classify the type of values a variable can hold and determine the operations
that can be performed on them. They are broadly categorized into three main types:
Primary (or Basic) Data Types:
These are the fundamental building blocks.
int: Stores whole numbers (integers). Can be modified with short, long, signed,
and unsigned. The size of integer data type is 2 bytes. e.g., 5, -100.
char: Stores single characters. Can be signed or unsigned. The size of character data
type is 1 byte for single character. e.g., 'a', '7', '$'.
float: Stores single-precision floating-point numbers (numbers with decimal points).
The size of float data type is 4 bytes. It displays 6 digits after the decimal point. e.g.,
3.141590, -0.500000.
double: Stores double-precision floating-point numbers, offering greater precision
than float. The size of double data type is 8 bytes. It displays 12 digits after the decimal
point.
void: A special type indicating the absence of a value, often used as a function return
type or for generic pointers.
Derived Data Types:
These are built upon the primary data types.
Arrays: Collections of elements of the same data type, accessed by an index,
e.g., int numbers[5].
Pointers: Variables that store memory addresses of other variables.
Functions: Blocks of code designed to perform a specific task, which can also be
considered a type in C.
User-Defined Data Types:
These are created by the programmer to define custom data structures.
Structures (struct): Allow grouping of different data types under a single name,
creating complex data structures.
Unions (union): Similar to structures but allow different members to share the same
memory location.
Enumerations (enum): Define a set of named integer constants, improving code
readability.
Operators in C are symbols that instruct the compiler to perform specific operations on
variables and data, known as operands. They are fundamental to manipulating data, performing
calculations, making decisions, and controlling program flow.
Here are the main categories of operators in C:
1. Arithmetic Operators: Used for basic mathematical calculations. + (Addition), -
(Subtraction), * (Multiplication), / (Division – Quotient after division), and % (Modulo -
remainder after division).
2. Relational Operators: Used to compare two operands and return a boolean result (true or
false).
== (Equal to)
!= (Not equal to)
> (Greater than)
< (Less than)
>= (Greater than or equal to)
<= (Less than or equal to)
3. Logical Operators: Used to combine or negate conditional expressions.
&& (Logical AND)
|| (Logical OR)
! (Logical NOT)
4. Assignment Operators: Used to assign values to variables.
= (Simple assignment)
+=, -=, *=, /=, %= (Compound assignment operators)
5. Increment/Decrement Operators: Used to increase or decrease the value of a variable by
1. ++ (Increment) and -- (Decrement).
6. Bitwise Operators: Used to perform operations on individual bits of an integer.
& (Bitwise AND), | (Bitwise OR), ^ (Bitwise XOR), ~ (Bitwise NOT), << (Left shift), and >>
(Right shift).
7. Conditional (Ternary) Operator: A shorthand for an if-else statement.
condition ? expression1 : expression2;
8. Special Operators:
sizeof (Returns the size of a variable or data type)
& (Address-of operator)
* (Pointer dereference operator)
, (Comma operator)
Q14) Explain the different unformatted input /output functions used in C language. Give
example.
Input / output functions used in C language are divided into two types.
1. Unformatted input / output functions
2. Formatted input / output functions.
Q16) List and explain the different escape characters used in C language:
In C programming, an escape character (or more accurately, an escape sequence) is a special
combination of characters that begins with a backslash (`\`) and is used to represent non-
printable characters or characters that have a special meaning within a string or character
literal. The common escape sequences used in C language are as follows:
Table: Backslash character constants (Escape characters)
Constant Meaning
„\a‟ Audible bell (alarm), produces a beep sound.
„\b‟ Backspace, moves the cursor back one position
„\f‟ Form feed (page break).
„\n‟ Newline character, moves the cursor to the beginning of the next line.
„\r‟ Carriage return, moves the cursor to the beginning of the current line.
„\t‟ Horizontal tab, inserts a tab space (8 blank spaces)..
„\v‟ Vertical tab.
Single quote, used to represent a literal single quote character within a
„\‟‟
character literal or string.
Double quote, used to represent a literal double quote character within
„\”‟
a string.
Question mark, used to represent a literal question mark, especially in
„\?‟
contexts where it might be misinterpreted as part of a trigraph.
„\\‟ Backslash, used to represent a literal backslash character.
„\0‟ Null character, used to terminate C strings.
Octal representation, where nnn is one to three octal digits
„\nnn‟
representing a character's ASCII value.
Hexadecimal representation, where hh is one or more hexadecimal
„\xhh‟
digits representing a character's ASCII value.
Note: Backslash character constants (Escape characters) must be used inside the printf()
statements only.
Q17) Explain the assignment operator and shorthand notation used in C language.
The assignment operator used in c language is used to assign a value to a variable. The variable
which is present on the left hand side of the assignment is called as left hand side variable and
it must be a single variable or an array variable with single index. Right hand side of the
assignment operator can be a constant, expression, variable, function call.
Example: Valid assignment statements are as follows.
A = 3;
B = 4;
C = A + B;
D = C;
E = sqrt(D);
G[0] = 10;
Example:
Statement with simple Statement with
assignment operator shorthand operator
A = A + 4; A += 4;
A = A – 5; A -= 5;
A = A * 7; A *= 7;
A = A / 10; A /= 10;
A = A % 2; A %= 2;
Q18) Explain the different increment and decrement operators used in C language. Give
example.
There are two types of increment operators.
i) Pre increment operator (++i)
ii) Post increment operator (i++)
i) Pre increment (++ i): Increment first and then assign. This is nothing but i = i + 1;. This
increments the value of i by 1.
Example:
int A = 4;
printf(“A = %d\n”,A);
printf(“A = %d\n”,++A);
printf(“A = %d\n”,A);
Output:
A=4
A=5
A=5
ii) Post increment (i ++): First assign and then increment. This is nothing but i = i + 1;. This
increments the value by 1.
Example:
int A = 4;
printf(“A = %d\n”,A);
printf(“A = %d\n”,A++);
printf(“A = %d\n”,A);
Output:
A=4
A=4
A=5
Example:
int A = 4;
printf(“A = %d\n”,A);
printf(“A = %d\n”,--A);
printf(“A = %d\n”,A);
Output:
A=4
A=3
A=3
ii) Post decrement (i --): First assign and then decrement. This is nothing but i = i - 1;. This
decrements the value by 1.
Example:
int A = 4;
printf(“A = %d\n”,A);
printf(“A = %d\n”,A--);
printf(“A = %d\n”,A);
Output:
A=4
A=4
A=3
Q19) Explain the conditional operator used in C language. Give example
The ?: in C programming language is called as conditional operator. It is also known as the
ternary operator, because it used 3 operands. It provides a concise way to express conditional
logic in a single line of code. It is a shorthand for simple if-else statements, particularly useful
for assigning values based on a condition.
The syntax is as follows:
Syntax: condition? true_statement:false_statement;
Example:
Using ternary operator Using if-else
main() main()
{ {
int a,b,c; int a,b,c;
a = 10; a = 10;
b = 15; b = 15;
clrscr(); clrscr();
c = (a > b)? a:b; if (a > b)
printf(“Largest = %d\n”,c); c = a;
getch(); else
} c = b;
Output: Largest = 15 printf(“Largest = %d\n”,c);
getch();
}
Output: Largest = 15
Q20) List and explain the special operators used in C language. Give example.
Special operators used in C language are as follows:
i) Comma operator
ii) Sizeof operator
iii) Pointer operator ( * and &).
iv) Membership operator (. and ->)
The comma operator can be used to link the related expressions together. A comma-linked list
of expressions are evaluated from left to right and the value of right-most expression is the
value of the combined expression.
Example:
value = (x = 15, y = 10, x + y);
First it assigns value 15 to x, 10 to y and finally assigns 25 (ie 15 + 10) to value. Since comma
operator has lowest precedence of all operators, so the parenthesis are necessary.
The sizeof() operator is a compile time operator. It is used to find the size of a variable, a
constant or a data type in bytes.
Example:
M = sizeof (int);
N = sizeof(a);
L = sizeof(235L);
Q21) Write the C equivalent expression for the given expressions.
Sl. No. Given Expression C Equivalent Expression
1 x = (-b – sqrt(b*b-4*a*c))/(2*a)
x = (-b + sqrt(b*b-4*a*c))/(2*a)
2 Area =√ s(s-a)(s-b)(s-c) Area = sqrt (s*(s-a)*(s-b)*(s-c))
3 x = (1 / pow(x,2) + pow(y,2)
4 y = 3x2 + 2x +1 y=3*x*x+2*x+1
5 Area = ∏r2 Area = 3.1415 * r * r
6 C = 2∏r C = 2 * 3.1415 * r
7 Area = ∏r2 +2∏rh Area = 3.1415*r*2 + 2*3.1415*r*h
8 Torque = ((2*m1*m2)/(m1+m2))*g
10 E = m(a * h + (pow(v,2)/2))
Q23) Define type casting? What are the different types of type casting? Give example.
Type casting in C programming is the process of converting a variable or value of one data
type into another.
There are 2 types of type casting.
1. Implicit type casting.
2. Explicit type casting
Implicit type casting: This occurs when the compiler automatically converts one data type to
another without any explicit instruction from the programmer. This typically happens in
situations where a "smaller" type is assigned to a "larger" type, or when performing operations
involving different data types where a common type is required. For example, assigning
an int to a float will implicitly convert the int to a float.
Example Program:
#include <stdio.h>
main()
{
int num_int = 10;
float num_float;
num_float = num_int; // Implicit conversion from int to float
printf("Implicit conversion= %f\n", num_float);
}
Output:
Implicit conversion=10.000000
Explicit type casting: This involves explicitly instructing the compiler to convert a value to a
specific data type using the cast operator (). This is necessary when the desired conversion
would not happen automatically, or when you want to force a specific conversion, potentially
leading to data loss if converting to a "smaller" type.
Note: C programming language uses the rule that, in all expressions except assignments, any
implicit type conversions are made from lower size type to a higher type and is as shown
below.
long double
double
float
unsigned long int
long int
unsigned int
Int
short char
Q24) Explain the working of bitwise operators in C language. Give example.
Bitwise operators in C are used to perform operations directly on the individual bits of integer
data types. They are fundamental for low-level programming, embedded systems, and tasks
requiring precise control over data at the binary level.
Bitwise OR ( | ):
• Performs a logical OR operation on corresponding bits of two operands.
• The resulting bit is 1 if at least one of the bits is 1; otherwise, it is 0.
Example:
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
int c = a | b; // Result: 0111 (7)
Bitwise XOR (^):
• Performs a logical Exclusive OR operation on corresponding bits of two operands.
• The resulting bit is 1 if the two bits are different; otherwise, it is 0.
Example:
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
int c = a ^ b; // Result: 0110 (6)
Bitwise NOT (~):
This is a unary operator that inverts all the bits of its single operand.
0s become 1s, and 1s become 0s.
Example:
int a = 5; // Binary: 00000101 (assuming 8-bit integer)
int b = ~a; // Result: 11111010 (depends on integer size and signedness)
Q25) Explain the use of precedence in solving the arithmetic expression. Give example.
In C programming, the precedence of arithmetic operators dictates the order in which
operations are performed within an expression. Operators with higher precedence are evaluated
before those with lower precedence.
The order of precedence for common arithmetic operators in C, from highest to lowest:
Parentheses ():
Parentheses can be used to explicitly control the order of evaluation, overriding default
precedence rules. Expressions within parentheses are always evaluated first.
Multiplication *, Division /, Modulus %:
These operators have equal precedence and are evaluated before addition and subtraction. If
multiple operators of this level are present, they are evaluated from left to right (associativity).
Addition +, Subtraction -:
These operators have the lowest precedence among the binary arithmetic operators. If multiple
operators of this level are present, they are evaluated from left to right (associativity).
Example: A= 10, B= 5, C= 2
Expression Result
A+B*C 20
(A + B) * C 30
A–B/C 8
(A – B) / C 2
Q26) Explain the usage of operator precedence and associativity in solving the
expression. Give example.
In C programming, operator precedence and associativity rules define the order in which
operators are evaluated within an expression.
Operator Precedence:
Operator precedence determines which operator is evaluated first when different operators are
present in an expression. Operators with higher precedence are evaluated before operators with
lower precedence.
Example: Multiplication (*) has higher precedence than addition (+), so in the expression 2 + 3
* 5, the multiplication 3 * 5 is performed first, resulting in 15, and then 2 is added to 15,
yielding 17.
Operator Associativity:
Operator associativity determines the order of evaluation when multiple operators of the same
precedence appear in an expression.
Associativity can be either left-to-right or right-to-left.
Left-to-right associativity:
Operators are evaluated from left to right.
Example: In the expression 8 / 4 * 5, both division (/) and multiplication (*) have the same
precedence and are left-associative. Therefore, 8 / 4 is evaluated first, resulting in 2, and then 2
* 5 is evaluated, giving 10.
Right-to-left associativity:
Operators are evaluated from right to left. Assignment operators (=, +=, -=, etc.) and the
conditional operator (?:) are examples of right-associative operators.
Example: For instance, in a = b = c, the assignment b = c is performed first, and then the result
is assigned to a.