BCA I Sem - C Programming-1
BCA I Sem - C Programming-1
PROBLEM-SOLVING TECHNIQUES
(UNIT - I, II, III)
BOOKS:
1. The Art of programming through flowcharts & algorithm by Anil B. Chaudhari Firewall Media,
Laxmi publication, New Publication.
2. Programming in C by E. Balagurusamy TMH Publications
INTRODUCTION
A computer program is a sequential set of instructions written in some computer language that is
used to direct the computer to perform some specific tasks of computation.
UNIT I
PROGRAMMING STRUCTURES
Introduction:
In any programming language, the logical structure of a program is vital for ensuring clarity and
functionality. The four fundamental programming structures are:
1. Sequence
2. Selection
3. Iteration
4. Modular
These structures allow a program to handle various tasks, make decisions, repeat operations, and
break down complex tasks into smaller parts. Let’s go over each concept in detail with examples.
1. Sequence:
Sequence is the most basic structure in programming, where instructions are executed one after the
other in a specific order. Every statement in the program runs in the order they appear unless a
control structure alters this flow.
Example:
Selection allows programs to make decisions and choose different paths based on conditions. It is
implemented using if, else, and elif statements (in Python). This allows the program to execute a
block of code only if a certain condition is true.
Example:
temperature = 30
if(temperature > 25)
printf("It's a hot day!");
else
printf("It's a cool day!");
3. Iteration (Looping):
Iteration allows a program to repeat a block of code multiple times until a specific condition is met.
Common looping structures include for loops and while loops.
4. Modular (Functions):
Modular programming is a way of organizing programs into smaller, manageable, and reusable
blocks of code known as functions or modules. Each function performs a specific task and can be
called upon when needed.
Function Example:
void greet_user(name) {
printf("Hello, %s", name)
}
greet_user("Alice");
greet_user("Bob");
Advantages of Modular Programming:
● Reusability: Functions can be reused in different parts of the program.
● Maintainability: Breaking the code into smaller parts makes it easier to maintain.
● Debugging: It simplifies debugging as you can test individual functions separately.
When starting to program, we need a clear way to plan and organize our thoughts before writing
actual code. This is where development tools come into play. Three common tools used for planning
a program are:
● Pseudocode
● Flowcharts
● Algorithms
These tools help us design the structure of a program logically and systematically.
1. Pseudocode
Definition:
Pseudocode is a simple way to describe a program’s logic without using a specific programming
language. It uses plain, everyday language or a mix of English and common programming
constructs like loops or conditionals.
Characteristics of Pseudocode:
● Language-friendly: It doesn't follow any syntax rules of a programming language. It's easy
to understand for anyone, regardless of their technical knowledge.
● Easy to write and read: Focuses on the logic and steps rather than precise syntax.
● Helps transition to actual code: Developers can use pseudocode as a stepping stone to
writing code in any programming language.
● No strict formatting: Unlike code, pseudocode is flexible in formatting, as its main purpose
is clarity.
Example of Pseudocode:
START
INPUT two numbers (A and B)
IF A > B THEN
PRINT "A is greater than B"
ELSE
PRINT "B is greater than or equal to A"
ENDIF
END
Advantages:
● Clarifies thinking before coding.
● Provides a high-level view of the solution without worrying about syntax.
2. Flowcharts
Definition:
A flowchart is a visual diagram that represents the flow of a program or process. It uses different
symbols to represent different types of actions or decisions in a program.
Characteristics of Flowcharts:
● Visual representation: Uses shapes like rectangles, diamonds, and arrows to represent
processes, decisions, and the flow of control.
● Structured and clear: Helps illustrate complex processes in an easy-to-understand way.
● Widely used in problem-solving: Helps identify logical errors or inefficiencies in the
process early.
Advantages:
● Visual clarity: Flowcharts make it easy to see how different parts of a program are
connected.
● Detects issues early: Helps in identifying errors or logical gaps before coding begins.
3. Algorithms
Definition:
An algorithm is a step-by-step procedure or formula for solving a problem. In programming,
algorithms define how a program processes data to achieve a desired outcome.
Characteristics of Algorithms:
1. Finite: An algorithm must terminate after a finite number of steps.
2. Definite: Each step must be clearly defined without any ambiguity.
3. Input: The algorithm should take some input.
4. Output: It should produce at least one output.
5. Effectiveness: Every step of the algorithm should be basic enough to be performed, even by
hand.
6. Language-independent: Algorithms are not tied to any specific programming language.
Example of an Algorithm:
Problem 2: Construct a flowchart to show how to obtain the volume of a rectangular box
Problem: Draw a flowchart to find the greatest among three numbers.
Step 1. INPUT TO A, B, C
Step 2. IF A > B
IF B > C
THEN PRINT B “is greater”
ELSE
PRINT C “is greater”
END-IF
ELSE
IF A > C
THEN PRINT A “is greater”
ELSE
PRINT “C is greater”
END-IF
END-IF
Step 3: STOP
Sample Program
main()
{
/* printing begins */
printf(“Hello World!”);
/* printing begins */
}
C CHARACTER SET
The characters that can be used to form words, numbers and expressions depend upon the
computer on which the program is run. The characters in C are grouped into the following
categories:
1. Letters
2. Digits
3. Special characters
4. White spaces
Letters Digits
Special Characters
& ampersand
, comma ^ caret
. period * asterisk
; semicolon - minus sign
: colon + plus sign
? question mark < opening angle bracket (or less than sign)
' apostrophe > closing angle bracket (or greater than sign)
" quotation mark ( left parenthesis
! exclamation mark ) right parenthesis
| vertical bar [ left bracket
/ slash ] right bracket
\ backslash { left brace
~ tilde } right brace
_ underscore # number sign
$ dollar sign
% percent sign
White Spaces
Blank space
Horizontal tab
Carriage return
New line
Form feed
C TOKENS
In a passage of text, individual words and punctuation marks are called tokens. Similarly, in a C
program, the smallest individual units are known as C tokens. C has six types of tokens as shown in
below figure. C programs are written using these tokens and the syntax of the language.
KEYWORDS
Every C word is classified as either a keyword or an identifier. All keywords have fixed meanings
and these meanings cannot be changed. Keywords serve as basic building blocks for program
statements. The list of all keywords of ANSI C are listed in below table. All keywords must be written
in lowercase.
auto break case char
const continue default do
double else enum extern
float for goto if
int long register return
short signed sizeof static
struct switch typedef union
unsigned void volatile while
IDENTIFIER
Identifiers refer to the names of variables, functions and arrays. These are user-defined names and
consist of a sequence of letters and digits, with a letter as a first character.
Both uppercase and lowercase letters are permitted. The underscore character is also permitted in
identifiers. It is usually used as a link between two words in long identifiers.
VARIABLES
A variable is a data name that may be used to store a data value. A variable may take different
values at different times during execution.
A variable name can be chosen by the programmer in a meaningful way so as to reflect its function
or nature in the program.
DATA TYPES
Data types are the foundation of any programming language, and in C, they allow you to define the
type of data you’re working with. They help the compiler understand the size and format of data
stored in variables.
The basic data types in C are used to store simple values, such as integers and characters. They
include:
1. integer (int),
2. floating point (float),
3. double-precision floating point (double)
4. character (char),
5. void.
Many of them also offer extended data types such as long int and long double.
void
Example:
long int total;
unsigned int marks;
In C programming, user-defined data types are types created by the programmer, building on C’s
basic types.
These types allow for more complex data structures to be represented within a program. The main
user-defined data types in C are:
typedef
● Simplifies complex data types by giving them custom names, making code more readable.
● Use typedef to create an alias for existing data types, so you can use shorter names.
Example:
typedef unsigned long int ULI;
ULI number = 1000000;
QUALIFIERS
Qualifiers in C modify the behavior or storage of a variable. They don’t change the data type
itself but instead add certain characteristics. The main qualifiers are:
1. const
● Tells the compiler that the value of a variable may change at any time.
● Example: volatile int sensorValue;
OPERATORS AND EXPRESSIONS
OPERATORS
Operators are symbols that perform operations on variables and values. In C, operators are
grouped into several categories:
Examples:
int a = 10, b = 3;
int sum = a + b; // Addition: 13
int diff = a - b; // Subtraction: 7
int prod = a * b; // Multiplication: 30
int quot = a / b; // Division: 3
int rem = a % b; // Modulus: 1
Examples:
int x = 5, y = 8;
int isEqual = (x == y); // Equal to: 0 (false)
int isNotEqual = (x != y;// Not equal to: 1 (true)
int isGreater = (x > y);// Greater than: 0 (false)
int isLess = (x < y);// Less than: 1 (true)
int isGreaterEqual = (x >= y);// Greater than or equal to: 0
(false)
int isLessEqual = (x <= y); // Less than or equal to: 1 (true)
3. Logical Operators: Used to combine multiple conditions:
● && (Logical AND)
● || (Logical OR)
● ! (Logical NOT)
Examples:
int p = 1, q = 0;
int andResult = (p && q); // Logical AND: 0(false)
int orResult = (p || q); // Logical OR: 1 (true)
int notResult = (!p); // Logical NOT: 0 (false)
Examples:
int a = 5;
a += 3; // Add and assign: a = 8
a -= 2; // Subtract and assign: a = 6
a *= 4; // Multiply and assign: a = 24
a /= 6; // Divide and assign: a = 4
a %= 3; // Modulus and assign: a = 1
Pre-Increment ++num;
Post-Increment: num++
Examples:
int m = 5, n = 3; // In binary: m = 0101, n = 0011
int andOp = m & n; // Bitwise AND: 0001 (1 in decimal)
int orOp = m | n; // Bitwise OR: 0111 (7 in decimal)
int xorOp = m ^ n; // Bitwise XOR: 0110 (6 in decimal)
int notOp = ~m; // Bitwise NOT: 1010 (-6 in decimal for 2's complement)
int leftShift = m << 1; // Left shift: 1010 (10 in decimal)
int rightShift = m >> 1; // Right shift: 0010 (2 in decimal)
EXPRESSIONS
An expression is a combination of variables, constants, and operators that produces a value. For
example:
int a = 10, b = 5;
int sum = a + b; // sum is an expression
int result = (a * b) + (a - b); // complex expression
CONSTANTS
In C programming, constants are fixed values that cannot be altered by the program during
execution. They are classified into various types, including numeric constants and character
constants.
1. Numeric Constants
Numeric constants represent numbers that remain constant throughout the program. They are
further divided into two categories:
i. Integer Constants
Integer constants are whole numbers without any fractional part or decimal point.
#include <stdio.h>
int main() {
int decimal = 100; // Decimal constant
int octal = 012; // Octal constant (12 in octal is 10 in
decimal)
int hexadecimal = 0x1A; // Hexadecimal constant (1A in hexadecimal is
26 in decimal)
return 0;
}
Examples:
#include <stdio.h>
int main() {
float pi = 3.14; // Fixed-point notation
double large = 2.5e6; // Scientific notation (2.5 × 10^6)
return 0;
}
2. Character Constants
#include <stdio.h>
int main() {
char letter = 'A'; // Single character constant
char digit = '5'; // Single digit constant
char special = '$'; // Special character constant
printf("Letter: %c\n", letter);
printf("Digit: %c\n", digit);
printf("Special: %c\n", special);
return 0;
}
String constants are sequences of characters enclosed in double quotes (" ").
#include <stdio.h>
int main() {
char name[] = "John Doe"; // String constant
char greeting[] = "Hello, World!";
return 0;
}
#include <stdio.h>
int main() {
printf("Hello\nWorld!\tTabbed\n");
printf("Path: C:\\Program Files\\MyApp\n");
return 0;
}
In C programming, symbolic constants are identifiers that represent constant values. Symbolic
constants are defined with meaningful names, improving code readability and maintainability. Once
defined, their values cannot be altered throughout the program.
Features of #define:
● No memory is allocated.
● Can define both numbers and strings.
● Typically written in uppercase for clarity.
● Does not terminate with a semicolon.
int main() {
float radius = 5.0;
float area = PI * radius * radius;
return 0;
}
int main() {
printf("%s\n", GREETING);
return 0;
}
Advantages of #define:
● No additional memory usage.
● Ideal for defining global constants.
Limitations of #define:
● No type checking (e.g., treating numbers as integers or floats).
● Difficult to debug if errors occur in preprocessor substitution.
2. Using const Keyword
The const keyword allows you to declare typed constants. These are variables whose values cannot
be modified after initialization.
Syntax
const data_type variable_name = value;
Features of const:
● Provides type safety (e.g., distinguishing between int and float).
● Allows memory allocation, enabling pointer and scope usage.
● Can be local or global.
int main() {
const float GRAVITY = 9.8; // Define a constant using const
float weight = 75.0; // Mass in kg
float force = weight * GRAVITY;
return 0;
}
int main() {
const char MESSAGE[] = "Welcome to C Programming!"; // String constant
printf("%s\n", MESSAGE);
return 0;
}
Advantages of const:
● Enforces type checking, reducing errors.
● Scopes can be limited (local or global).
Limitations of const:
● Uses memory (unlike #define).
In C, the enumerated data type (enum) is a user-defined data type that assigns names to integral
constants. It is used to represent a set of related constants, making code more readable and easier
to manage.
enum EnumName {
CONSTANT1,
CONSTANT2,
CONSTANT3,
...
};
Example:
#include <stdio.h>
// Declare an enumeration
enum Color {
RED, // 0
GREEN, // 1
BLUE // 2
};
int main() {
enum Color myColor = GREEN; // Assign the constant GREEN
printf("My color value: %d\n", myColor); // Output: 1
return 0;
}
Specifying Values for Constants
You can assign specific integer values to constants. Constants without assigned values increment
from the last specified value.
#include <stdio.h>
enum Weekday {
MONDAY = 1, // Start from 1
TUESDAY, // 2 (incremented automatically)
WEDNESDAY = 5, // Set specific value
THURSDAY, // 6 (incremented automatically)
FRIDAY // 7
};
int main() {
printf("Monday: %d\n", MONDAY); // Output: 1
printf("Wednesday: %d\n", WEDNESDAY); // Output: 5
printf("Friday: %d\n", FRIDAY); // Output: 7
return 0;
}
Operator precedence and associativity determine how expressions are evaluated. This is essential
for understanding the behavior of complex expressions involving multiple operators.
int result = 10 + 5 * 2;
The multiplication (*) operator has higher precedence than addition (+), so 5 * 2 is evaluated first,
and then 10 + 10 is computed.
● Left-to-right (e.g., +, -, *, /)
● Right-to-left (e.g., assignment operators like =, +=, and -=)
#include <stdio.h>
int main() {
int x = 5, y = 10, z = 20;
int result = x + y * z; // Multiplication (*) has higher precedence than
addition (+).
printf("Result: %d\n", result); // Output: 205
return 0;
}
Example 2: Associativity
#include <stdio.h>
int main() {
int a = 10, b = 20, c = 30;
int result = a = b = c; // Assignment (=) is right-to-left associative.
printf("Result: a=%d, b=%d, c=%d\n", a, b, c); // Output: a=30, b=30,
c=30
return 0;
}
#include <math.h>
List of Common Math Functions in <math.h>
Function Description Syntax
abs() Returns the absolute value of an integer int abs(int x)
int main() {
double number = 16.0;
double result = sqrt(number);
The C programming language provides a variety of string-handling functions through the <string.h>
header file. These functions are used to manipulate and process strings, such as copying,
concatenating, comparing, and finding the length of strings.
What is a String in C?
Include the <string.h> header at the beginning of your program to access string-handling functions:
#include <string.h>
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
printf("Length of the string: %zu\n", strlen(str)); // Output: 13
return 0;
}
int main() {
char src[] = "Hello, C!";
char dest[20];
return 0;
}
Example: Concatenating Strings (strcat and strncat)
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello, ";
char str2[] = "World!";
return 0;
}
int main() {
char str1[] = "Apple";
char str2[] = "Banana";
return 0;
}
int main() {
char str[] = "Learning C is fun!";
char *ptr = strstr(str, "C");
if (ptr)
printf("Substring found: %s\n", ptr); // Output: C is fun!
else
printf("Substring not found\n");
return 0;
}
COMPOUND STATEMENTS
The use of compound statements is essential when you need to execute multiple instructions where
only one is allowed syntactically, such as within an if statement, for loop, or while loop.
Syntax
{
statement1;
statement2;
...
statementN;
}
int main()
{
int a = 5;
{
int b = 10;
printf("Value of b: %d", b);
}
return 0;
}
SELECTION STATEMENTS IN C
fSelection statements in C control the flow of execution based on certain conditions. These
conditions evaluate to either true (non-zero) or false (zero).
1. if Statement
The if statement executes a block of code only if a specified condition is true.
Syntax:
if (condition) {
// Block of code to execute if the condition is true
}
Example:
#include <stdio.h>
int main() {
int number = 10;
if (number > 0) {
printf("The number is positive.\n");
}
return 0;
}
2. if-else Statement
The if-else statement provides an alternative block of code if the condition evaluates to false.
Syntax:
if (condition) {
// Block of code to execute if the condition is true
} else {
// Block of code to execute if the condition is false
}
Example:
#include <stdio.h>
int main() {
int number = -5;
if (number >= 0) {
printf("The number is non-negative.\n");
} else {
printf("The number is negative.\n");
}
return 0;
}
Example:
#include <stdio.h>
int main() {
int age = 25;
int hasLicense = 1; // 1 = true, 0 = false
return 0;
}
3. Nested if
if else if ladder in C programming is used to test a series of conditions sequentially.
Furthermore, if a condition is tested only when all previous if conditions in the if-else ladder are
false. If any of the conditional expressions are evaluated to be true, the appropriate code block will
be executed, and the entire if-else ladder will be terminated.
Syntax:
if (condition1) {
if (condition2) {
// Block of code if both condition1 and condition2 are true
}
}
Example:
#include <stdio.h>
int main() {
int age = 20;
int hasID = 1; // 1 means true, 0 means false
return 0;
}
int main() {
int marks;
return 0;
}
4. switch Statement
The switch statement allows you to test a variable against multiple values (cases) and execute the
corresponding block of code.
In C, the switch case statement is used for executing one condition from multiple conditions. It is
similar to an if-else-if ladder.
Syntax:
switch (expression) {
case value1:
// Block of code for value1
break;
case value2:
// Block of code for value2
break;
...
default:
// Block of code if no case matches
}
Example:
#include <stdio.h>
int main() {
int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
default:
printf("Invalid day\n");
}
return 0;
}
Example:
#include <stdio.h>
int main() {
int grade = 2;
switch (grade) {
case 1:
case 2:
case 3:
printf("You are in primary school.\n");
break;
case 4:
case 5:
printf("You are in middle school.\n");
break;
default:
printf("Invalid grade.\n");
}
return 0;
}
ITERATION STATEMENTS IN C
Iteration statements in C, also known as loops, are used to execute a block of code repeatedly as
long as a specified condition is true. C provides three main types of loops:
● for loop
● while loop
● do-while loop
Each loop serves a distinct purpose and is chosen based on the specific requirement of the
program. Let's explore each of these in detail.
1. for Loop
The for loop is used when the number of iterations is known beforehand. It is commonly used for
counting iterations.
Syntax:
Components:
Example:
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("Iteration %d\n", i);
}
return 0;
}
Output:
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
2. while Loop
The while loop is used when the number of iterations is not known in advance and depends on a
condition being true.
Syntax:
while (condition) {
// Body of the loop
}
How It Works:
Example:
#include <stdio.h>
int main() {
int count = 1;
while (count <= 5) {
printf("Count: %d\n", count);
count++;
}
return 0;
}
Output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
3. do-while Loop
The do-while loop is similar to the while loop, but it guarantees that the loop body executes at least
once because the condition is evaluated after the loop body.
Syntax:
do {
// Body of the loop
} while (condition);
Example:
#include <stdio.h>
int main() {
int num = 1;
do {
printf("Number: %d\n", num);
num++;
} while (num <= 5);
return 0;
}
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
4. Infinite Loops
An infinite loop runs indefinitely because the terminating condition is never met. They are commonly
used in event-driven programs or where the program needs to wait for user input.
Example:
#include <stdio.h>
int main() {
while (1) {
printf("This will run forever. Press Ctrl+C to stop.\n");
}
return 0;
}
5. Controlling Loops
break Statement:
The break statement is used to terminate the loop immediately and transfer control to the
statement following the loop.
Example:
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
printf("%d\n", i);
}
return 0;
}
Output:
1
2
3
4
continue Statement:
The continue statement skips the current iteration of the loop and moves to the next iteration.
Example:
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
printf("%d\n", i);
}
return 0;
}
Output:
1
2
4
5
6. Nested Loops
Loops can be nested, meaning one loop inside another. This is often used for working with
multidimensional data like matrices.
Example:
#include <stdio.h>
int main() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
printf("(%d, %d)\n", i, j);
}
}
return 0;
}
Output:
(1, 1)
(1, 2)
(1, 3)
(2, 1)
(2, 2)
(2, 3)
(3, 1)
(3, 2)
(3, 3)
goto in C
The goto statement in C is a control flow statement that allows you to transfer control to a labeled
statement within the same function. While it can simplify certain tasks, improper use can lead to
difficult-to-maintain code.
Syntax of goto
The syntax of goto consists of two parts:
label_name:
// code to execute
2. Using goto: The goto statement uses the label name to transfer control.
goto label_name;
Example:
#include <stdio.h>
int main() {
printf("Start of program.\n");
goto skip;
printf("This line will be skipped.\n");
skip:
printf("This line is executed after the goto.\n");
return 0;
}
Output:
Start of program.
This line is executed after the goto.
Advantages of goto
1. Simplifies certain control flows: When used judiciously, goto can simplify breaking out of
complex structures like nested loops.
2. Improves error handling in legacy code: It can be used for cleanups in older C programs
without modern exception handling.
Disadvantages of goto
1. Readability issues: Code with multiple goto statements can become hard to follow, often
called "spaghetti code."
2. Debugging complexity: Debugging becomes harder because the control flow is less
predictable.
3. Structured alternatives: Modern languages and practices provide alternatives like break,
continue, and functions, making goto less relevant.
UNIT III
ARRAYS
Arrays are one of the most fundamental and commonly used data structures in C programming.
They allow the storage and management of multiple values of the same data type in a single
variable, enabling efficient handling of large datasets.
1. Large Number of Variables: Suppose you need to store marks of 100 students. Without
arrays:
2. Difficult to Process Data: Iterating through these variables for calculations (e.g., finding the
average) requires individual handling for each variable, leading to long and error-prone code.
3. Dynamic Operations Are Tedious: Without arrays, performing operations like sorting,
searching, or modifying elements in bulk is cumbersome.
Arrays provide a systematic way to handle multiple elements of the same type. With arrays:
What is an Array?
An array is a collection of elements of the same data type, stored in contiguous memory locations
and accessed using an index.
1. Declaring an Array:
data_type array_name[size];
● data_type: The type of data stored in the array (e.g., int, float, char).
● array_name: The name of the array.
● size: The number of elements the array can hold.
Example:
2. Initializing an Array:
Alternatively, you can omit the size if you provide values during initialization:
int marks[] = {90, 85, 88, 92, 87};
Array elements are accessed using their index, starting from 0. The syntax is:
array_name[index]
Example:
Traversing an Array
Example:
#include <stdio.h>
int main() {
int marks[5] = {90, 85, 88, 92, 87};
Output:
Mark 1: 90
Mark 2: 85
Mark 3: 88
Mark 4: 92
Mark 5: 87
TYPES OF ARRAYS
1. One-Dimensional Arrays:
A one-dimensional array is like a list.
Declaration:
int arr[10]; // An array of 10 integers
2. Two-Dimensional Arrays:
A Two-dimensional array is like a table or a matrix where we can access element by two indices.
Declaration:
Initialization:
int matrix[2][2] = {{1, 2}, {3, 4}};
Accessing elements:
We can access elements from two dimensions array by givingan index.
Character arrays are used to store strings where each element has a single character.
Example:
Advantages of Arrays
1. Efficient Data Access: Allows direct access to elements using their index.
2. Compact Memory Storage: Stores data contiguously in memory.
3. Ease of Iteration: Loop constructs make it simple to process array elements.
Limitations of Arrays
1. Fixed Size: Arrays have a fixed size, which must be declared at compile time.
2. Homogeneous Data: Only stores elements of the same data type.
3. No Built-In Operations: Requires manual implementation for tasks like resizing or inserting.
Functions in C
1. Function Components
A function in C is a self-contained block of code that performs a specific task. Functions provide
modularity and reusability, allowing programmers to break down complex problems into smaller,
manageable parts.
3. Function Call:
4. Return Statement:
Advantages of Functions:
1. Void:
○ Indicates the function does not return any value.
○ Syntax: void display();
Example:
void display() {
printf("Hello, World!\n");
}
2. Basic Data Types:
○ Functions can return data types like int, float, char, etc.
Example:
int getNumber() {
return 42;
}
3. Rules for Return Values:
● The return type must match the data type specified in the function prototype.
● A function returning a non-void type must use a return statement.
● If no return type is specified, int is assumed by default (not recommended).
3. Parameter Passing
Parameters allow functions to accept input values, which influence the function's behavior. In C,
there are two primary ways to pass parameters:
1. Pass by Value:
Example:
void increment(int x) {
x = x + 1;
}
int main() {
int a = 10;
increment(a);
printf("a = %d", a); // Output: a = 10
2. Pass by Reference:
● The address of the variable is passed, allowing the function to modify the original value.
● Uses pointers to achieve this behavior.
Example:
4. Recursive Functions
A recursive function is a function that calls itself to solve smaller instances of a problem. Recursion
is commonly used for problems that can be divided into smaller, similar sub-problems.
int factorial(int n) {
if (n == 0) { // Base case
return 1;
} else {
return n * factorial(n - 1); // Recursive case
}
}
int main() {
printf("Factorial of 5 = %d", factorial(5));
return 0;
}
Advantages of Recursion:
● Simplifies code for problems like tree traversal, factorial calculation, and Fibonacci series.
Disadvantages of Recursion:
When an array is passed to a function, a pointer to the first element of the array is passed. This
allows the function to access and modify the original array elements.
int main() {
int numbers[] = {1, 2, 3, 4, 5};
printArray(numbers, 5);
return 0;
}
Modifying Arrays in Functions
Since arrays are passed by reference, modifications made to the array within the function affect the
original array.
int main() {
int numbers[] = {1, 2, 3, 4, 5};
doubleArray(numbers, 5);
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
return 0;
}
Multidimensional Arrays
Multidimensional arrays, such as matrices, can also be passed to functions. However, the size of all
dimensions except the first must be specified.
Syntax:
int main() {
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
printMatrix(matrix, 2);
return 0;
}
● Efficiency: Functions can process large datasets without copying the entire array.
● Modularity: Simplifies complex tasks by breaking them into reusable functions.
1. Passing the Array Size: Always pass the size of the array to avoid out-of-bounds errors.
2. Returning Local Arrays: Avoid returning local arrays as their memory is deallocated after
the function ends.
STORAGE CLASSES IN C
Storage classes in C determine the scope, visibility, lifetime, and default initial value of
variables. They control where a variable is stored, how long it persists in memory, and who can
access it during program execution.
Characteristics:
Syntax:
auto int x;
Example:
#include <stdio.h>
void autoExample() {
auto int a = 10; // Explicit use of 'auto' (optional)
int b = 20; // Implicitly 'auto'
printf("a = %d, b = %d\n", a, b);
}
int main() {
autoExample();
return 0;
}
// Output:
// a = 10, b = 20
Characteristics:
Key Points:
● The extern keyword allows access to a global variable or function defined in another file.
● Useful for sharing variables between multiple files in a project.
Syntax:
extern int x;
File1.c:
#include <stdio.h>
void printX() {
printf("Value of x: %d\n", x);
}
File2.c:
#include <stdio.h>
int main() {
printX(); // Accesses 'x' from File1.c
return 0;
}
Compilation:
// Value of x: 42
3. Static Storage Class (static)
Characteristics:
● Scope:
○ Local static variables: Scope is limited to the block in which it is declared.
○ Global static variables: Scope is limited to the file in which it is defined.
● Lifetime: Exists throughout the program's lifetime but retains the value between function
calls.
● Default Initial Value: Zero.
● Storage: Stored in global memory.
Key Points:
Syntax:
static int x;
#include <stdio.h>
void staticExample() {
static int counter = 0; // Retains value between calls
counter++;
printf("Counter = %d\n", counter);
}
int main() {
staticExample();
staticExample();
staticExample();
return 0;
}
// Output
Counter = 1
Counter = 2
Counter = 3
4. Register Storage Class (register)
Characteristics:
Key Points:
● The register keyword suggests that the variable be stored in a CPU register.
● Used for frequently accessed variables, such as counters in loops.
● Cannot take the address of a register variable using the & operator.
Syntax:
register int x;
Example:
#include <stdio.h>
void registerExample() {
register int i;
for (i = 0; i < 5; i++) {
printf("i = %d\n", i);
}
}
int main() {
registerExample();
return 0;
}
// Output
i = 0
i = 1
i = 2
i = 3
i = 4
UNIT IV
STRUCTURE
What is a Structure in C?
A structure in C is a user-defined data type that allows grouping of variables of different types under
a single name. It is especially useful when dealing with complex data where grouping related
information together makes the code more readable and manageable.
Unlike arrays, which can only store multiple elements of the same type, structures enable you to
combine variables of various data types, making it possible to represent real-world entities like a
student, an employee, or a book.
Declaration of a Structure
To define a structure, the struct keyword is used. The declaration defines the structure's blueprint
but does not allocate memory for any variables.
Syntax:
struct StructureName {
data_type member1;
data_type member2;
...
};
Here, StructureName is the name of the structure, and member1, member2, etc., are the
variables (or members) inside the structure.
Example:
struct Student {
int id;
char name[50];
float marks;
};
This declares a structure named Student with three members: an integer id, a string (character
array) name, and a float marks.
Definition of Structure Variables
After declaring a structure, you can create variables of that structure type. These variables hold the
actual data.
Syntax:
Example:
struct Student {
int id;
char name[50];
float marks;
} s1, s2;
Syntax:
variable_name.member_name;
Example:
#include <stdio.h>
struct Student {
int id;
char name[50];
float marks;
};
int main() {
struct Student s1;
return 0;
}
Output:
ID: 101
Name: John Doe
Marks: 95.50
Initialization of Structures
You can initialize a structure variable at the time of definition by assigning values in a
comma-separated list inside curly braces.
Syntax:
Example:
If you initialize fewer members than declared, the remaining members will be initialized to zero (for
numeric types) or NULL (for pointers).
Nesting of Structures
Structures can be nested, meaning one structure can contain another structure as a member. This
allows you to model complex data relationships.
Example:
#include <stdio.h>
struct Address {
char city[50];
char state[50];
int zip;
};
struct Student {
int id;
char name[50];
float marks;
struct Address addr; // Nested structure
};
int main() {
struct Student s1;
return 0;
}
Output:
ID: 101
Name: John Doe
Marks: 95.50
City: New York
State: NY
ZIP: 10001
What is a Union in C?
A union in C is a user-defined data type, similar to a structure, but with a key difference: all
members of a union share the same memory location. This means that while multiple members can
be defined, only one member can store a value at any given time. A union is useful when you need
to work with different types of data in the same memory space, optimizing memory usage.
Declaration of a Union
A union is declared using the union keyword. The declaration defines the blueprint of the union but
does not allocate memory for any variables.
Syntax:
union UnionName {
data_type member1;
data_type member2;
...
};
Here, UnionName is the name of the union, and member1, member2, etc., are the members inside
the union.
Example:
union Data {
int i;
float f;
char str[20];
};
This declares a union named Data with three members: an integer i, a float f, and a string
(character array) str.
To use a union, you need to define variables of the union type. Memory is allocated only for the
largest member of the union, ensuring efficient memory usage.
Syntax:
union Data {
int i;
float f;
char str[20];
} d1, d2;
Union members are accessed using the dot (.) operator for regular variables and the arrow (->)
operator for pointers. Since all members share the same memory, modifying one member affects
the values of other members.
Syntax:
variable_name.member_name;
Example:
#include <stdio.h>
union Data {
int i;
float f;
char str[20];
};
int main() {
union Data d1;
return 0;
}
Output:
Integer: 10
Float: 220.50
String: Hello, Union
Note: Assigning a new value to one member overwrites the memory, so the previous value
becomes invalid.
Initialization of Unions
Unions can be initialized at the time of definition, but only one member can be initialized.
Syntax:
Example:
If you initialize more than one member, only the first member's value will be considered, as all
members share the same memory.
While structures and unions seem similar in syntax and usage, they differ significantly in how they
allocate and use memory.
Feature Structure Union
Memory Allocates separate memory for Allocates shared memory for all
Allocation each member. members.
Size Size is the sum of the sizes of Size is equal to the size of the
all members. largest member.
Access All members can hold Only one member can hold a value
independent values at a time.
simultaneously.
Usage Used when you need to store Used when you need to store and
and access all data members access one member at a time to
simultaneously. save memory.
Pointers
Pointers are one of the most powerful and versatile features of the C programming language. They
allow developers to directly manipulate memory, making them essential for dynamic memory
allocation, efficient array handling, and building data structures like linked lists. In this tutorial, we
will explore pointers in detail, covering their concepts, operators, and advanced usage.
1. Introduction to Pointers
A pointer is a variable that stores the memory address of another variable. Instead of holding a
value directly, pointers "point" to the location in memory where the value is stored. Understanding
pointers is crucial because:
For example:
The address operator (&) is used to retrieve the memory address of a variable. It is the foundation
for working with pointers.
Example:
int x = 5;
int *p;
p = &x; // `p` now holds the address of `x`
Key Points:
● The & operator can only be applied to variables, not constants or expressions.
● It is often used to pass arguments by reference to functions.
3. Pointer Variables
A pointer variable is a variable designed to hold the address of another variable. When declaring a
pointer, you must specify the type of data it points to.
Syntax:
data_type *pointer_name;
● data_type specifies the type of the variable the pointer will point to.
● * indicates that the variable is a pointer.
Example:
int x = 42;
int *p = &x; // Declare a pointer to an integer and assign it the
address of `x`
Explanation:
Dereferencing:
To access the value stored at the memory address a pointer holds, you use the dereference
operator (*).
4. Void Pointers
A void pointer is a special type of pointer that can store the address of any data type. Since it has
no associated data type, you cannot dereference it directly.
Syntax:
void *ptr;
Example:
int x = 10;
float y = 20.5;
void *ptr;
Key Points:
● Void pointers are often used in generic functions like malloc and free.
● To dereference a void pointer, you must cast it to the appropriate type.
5. Pointer Arithmetic
Pointer arithmetic allows you to perform operations on pointers to traverse arrays and manipulate
memory. These operations depend on the size of the data type the pointer is pointing to.
Operations:
1. Increment (ptr++): Moves the pointer to the next memory location based on the size of the
data type.
2. Decrement (ptr--): Moves the pointer to the previous memory location.
3. Addition (ptr + n): Moves the pointer n elements forward.
4. Subtraction (ptr - n): Moves the pointer n elements backward.
Example:
Key Points:
6. Pointers to Pointers
A pointer to a pointer is a pointer that stores the address of another pointer. This is useful in
scenarios where you need to modify a pointer's value indirectly or work with multi-level data
structures.
Syntax:
data_type **ptr;
Example:
int x = 5;
int *p = &x; // Pointer to x
int **pp = &p; // Pointer to the pointer `p`
Explanation:
Applications: