0% found this document useful (0 votes)
6 views55 pages

C Programming Problem Solving Guide

Programmes noted

Uploaded by

amxavier1975
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views55 pages

C Programming Problem Solving Guide

Programmes noted

Uploaded by

amxavier1975
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

CS25C01-Computer Programming: C

PART-I
Introduction to C
1)Problem Solving

Problem-solving in C programming involves a systematic approach to translate


real-world problems into executable code. The process can be summarized as follows:

1)Problem Definition and Analysis:

 Clearly understand the problem statement, inputs, expected outputs, and constraints.
 Break down complex problems into smaller, manageable sub-problems.

2)Algorithm Design:
 Develop a step-by-step procedure (algorithm) to solve the problem.
 Algorithms should be precise, unambiguous, and finite.
 Represent algorithms using pseudocode or flowcharts for clarity.

3)Coding (Implementation):
 Translate the designed algorithm into C code, adhering to C syntax and best practices.
 Utilize appropriate data types, control structures (if-else, loops), functions, and data
structures (arrays, pointers, structs) as needed.

4)Testing and Debugging:


 Test the program with various inputs, including edge cases, to ensure correctness.
 Identify and fix syntax errors (compiler errors) and logical errors (runtime errors or
incorrect output) through debugging.

5)Optimization and Refinement:


 Evaluate the program's efficiency and readability.
 Optimize the code for better performance or resource utilization if necessary.
 Add comments for clarity and maintainability.

Key Concepts in C for Problem Solving:

 Variables and Data Types: Storing and manipulating different types of data.
 Operators: Performing arithmetic, relational, logical, and bitwise operations.
 Control Flow Statements:
o Conditional Statements: if, else if, else, switch for decision-making.
o Looping Statements: for, while, do-while for repetitive tasks.
 Functions: Modularizing code for reusability and organization.
 Arrays: Storing collections of similar data.
 Pointers: Directly accessing memory locations and dynamic memory allocation.
 Structures: Grouping related data items of different types.
 File I/O: Reading from and writing to files for data persistence.

2) Problem Analysis Chart

A Problem Analysis Chart (PAC) in C programming, or any programming


language, is a tool used during the problem-solving phase to systematically break down
a problem into its core components before writing code. It helps in understanding the
problem, identifying inputs, desired outputs, and the processing steps required to
transform inputs into outputs.

Components of a Problem Analysis Chart:

 Input: What data or information is required for the program to operate?


 Processing: What calculations, logical operations, or transformations need to be
performed on the input to produce the desired output? This section often includes
formulas, conditions, and sequential steps.
 Output: What are the expected results or information that the program should
produce?
Example: Calculating the Area and Perimeter of a Rectangle
Let's illustrate with a simple example: creating a C program to calculate the area and
perimeter of a rectangle.

Problem Analysis Chart:

 Input:
o Length of the rectangle (e.g., float length;)
o Width of the rectangle (e.g., float width;)
 Processing:
o Read the value for length from the user.
o Read the value for width from the user.
o Calculate the Area: area = length * width;
o Calculate the Perimeter: perimeter = 2 * (length + width);
 Output:
o Calculated Area of the rectangle.
o Calculated Perimeter of the rectangle.
C Program Implementation (based on the PAC):

#include <stdio.h> // Include standard input/output library

int main() {
float length, width, area, perimeter; // Declare variables

// Input
printf("Enter the length of the rectangle: ");
scanf("%f", &length);

printf("Enter the width of the rectangle: ");


scanf("%f", &width);

// Processing
area = length * width;
perimeter = 2 * (length + width);

// Output
printf("Area of the rectangle: %.2f\n", area);
printf("Perimeter of the rectangle: %.2f\n", perimeter);

return 0; // Indicate successful execution


}

3) Developing an Algorithm

Developing an algorithm in C programming involves a systematic approach to


problem-solving, outlining the steps before translating them into code.

Algorithm Development Steps:

 Problem Definition:
Clearly understand the problem and what the algorithm should achieve. Identify
inputs and expected outputs.
 Algorithm Design (Pseudocode/Flowchart):
 Pseudocode: Write a step-by-step description of the algorithm using a simplified
language, closer to natural language than C code, but still structured.
 Flowchart: Create a visual representation of the algorithm's flow using standard
symbols.
 Variable Declaration:
Determine the necessary variables and their data types to store inputs, intermediate
results, and outputs.
 Logic Implementation:
Translate the designed steps into C code, using control structures (if-else, loops) and
operators as required.
 Testing and Debugging:
Verify the algorithm's correctness with various inputs and fix any errors.

Example: Algorithm to Find the Sum of Two Numbers


1. Problem Definition: Calculate the sum of two integers provided by the user.

2. Algorithm Design (Pseudocode):


Code
START
DECLARE integers num1, num2, sum
READ num1
READ num2
CALCULATE sum = num1 + num2
PRINT sum
STOP
3. C Implementation:
C
#include <stdio.h> // Required for input/output functions

int main() {
// Declare variables
int num1, num2, sum;

// Get input from the user


printf("Enter the first number: ");
scanf("%d", &num1);

printf("Enter the second number: ");


scanf("%d", &num2);

// Calculate the sum


sum = num1 + num2;

// Display the result


printf("The sum is: %d\n", sum);
return 0; // Indicate successful execution
}
Explanation of the C Code:

 #include <stdio.h>: This line includes the standard input/output library, providing
functions like printf (for printing to the console) and scanf (for reading input from the
console).
 int main(): This is the main function where program execution begins.
 int num1, num2, sum;: Declares three integer variables to store the two input numbers
and their sum.
 printf("Enter the first number: ");: Displays a message prompting the user for input.
 scanf("%d", &num1);: Reads an integer value entered by the user and stores it in
the num1 variable. The & symbol is used to pass the memory address of num1.
 sum = num1 + num2;: Performs the addition operation and assigns the result to
the sum variable.
 printf("The sum is: %d\n", sum);: Prints the calculated sum to the console. The %d is a
format specifier for integers.
 return 0;: Indicates that the program executed successfully.

4) Flowchart and Pseudocode

Flowchart
A flowchart is a graphical representation of an algorithm or process, using
standardized symbols to depict the sequence of steps, decisions, and data flow.

Common Flowchart Symbols:

 Terminal (Oval): Represents the start or end of a program.


 Input/Output (Parallelogram): Indicates data input from or output to a device.
 Process (Rectangle): Represents an action or operation, such as calculations or
assignments.
 Decision (Diamond): Represents a point where a decision is made, leading to different
paths based on a condition (e.g., true/false).
 Flow Lines (Arrows): Connect symbols and indicate the direction of flow.
Example: Flowchart for calculating the sum of two numbers.
Pseudocode
Pseudocode is an informal, high-level description of an algorithm or program
logic, using a combination of natural language and programming-like constructs. It is
not executable code but serves as a bridge between the algorithm and the actual
programming language.

Key Characteristics of Pseudocode:

 Readability: Uses plain English-like statements to describe actions.


 Structure: Incorporates common programming constructs like IF-THEN-
ELSE, WHILE, FOR, INPUT, OUTPUT.
 Independence: Not tied to any specific programming language syntax.
Example: Pseudocode for calculating the sum of two numbers.
Code
START
INPUT Number1
INPUT Number2
CALCULATE Sum = Number1 + Number2
DISPLAY Sum
END
5) program structure

The structure of a C program typically consists of several sections, although not


all are strictly mandatory for every program. Understanding these sections helps in
organizing code for readability, maintainability, and efficient execution.

1. Documentation Section:
This section is optional but highly recommended. It includes comments that
provide information about the program, such as its purpose, author, date of creation,
and any specific details relevant to its functionality.
Ex:
/*
* Program Name: MyFirstCProgram.c
* Author: John Doe
* Date: August 28, 2025
* Description: This program demonstrates the basic structure of a C program.
*/

2. Link Section (Preprocessor Directives):


This section includes preprocessor directives, primarily #include statements, which link
necessary header files (containing function declarations and macros) from the system
library or user-defined files.
Ex:
#include <stdio.h> // Includes standard input/output library for functions like printf,
scanf
#include <math.h> // Includes math library for mathematical functions

3. Definition Section:
This section is used to define symbolic constants using the #define directive. These
constants are replaced by their values during the pre-processing phase.
Ex:
#define PI 3.14159
#define MAX_SIZE 100

4. Global Declaration Section:


Variables and functions declared in this section have global scope, meaning they can be
accessed from anywhere within the program.
Ex:
int globalVariable = 10; // Global variable declaration
void myFunction(); // Function prototype declaration

5. main() Function:

This is the most crucial part of a C program, as execution always begins here. It contains
the core logic of the program and is divided into two parts:

 Declaration Part: Declares local variables used within the main function.
 Executable Part: Contains the statements and expressions that implement the
program's logic.
Ex:
int main() {
int localVariable = 5; // Local variable declaration
printf("Hello, World!\n"); // Executable statement
myFunction();
return 0; // Indicates successful execution
}

6. Subprogram Section (User-defined Functions):


This section contains the definitions of user-defined functions that are called from
the main() function or other user-defined functions. These functions encapsulate
specific tasks, promoting modularity and code reusability.
Ex:
void myFunction() {
printf("This is a user-defined function.\n");
}

6) Compilation & Execution process


The compilation and execution process in C programming involves several
distinct stages that transform human-readable source code into an executable program.

1. Compilation Process:

 Preprocessing:
The C preprocessor handles directives like #include (for including header files)
and #define (for macro expansion). It performs text substitutions and expands
macros, generating an expanded source file.
 Compiling:
The compiler takes the preprocessed code and translates it into assembly language. It
performs lexical analysis, parsing, semantic analysis, and code generation, identifying
syntax and semantic errors.
 Assembling:
The assembler converts the assembly code into machine-readable object code. This
object code is typically in a relocatable format, meaning it can be loaded at different
memory addresses during execution.
 Linking:
The linker combines the object code of your program with necessary library functions
(e.g., from stdio.h for printf) and other object files if your program is spread across
multiple source files. This process resolves external references and creates a single,
executable file.
2. Execution Process:

 Loading:
The operating system's loader loads the executable file into the computer's
memory. This involves allocating memory space for the program's code, data, and
stack.
 Execution:
The Central Processing Unit (CPU) begins executing the program's instructions
sequentially, starting from the main function. The CPU fetches instructions, decodes
them, and performs the specified operations, interacting with memory and I/O devices
as required by the program's logic.

7) Interactive and Script mode

In C programming, the concepts of "interactive mode" and "script mode" are not
directly analogous to how they are used in interpreted languages like Python. C is a
compiled language, meaning source code must be translated into machine-executable
code before it can be run.

Interactive Mode (C Programming Context):

While C doesn't have a true "interactive shell" like Python's REPL (Read-Eval-
Print Loop), "interactive mode" in C typically refers to scenarios where the compiled
program interacts with the user during its execution. This involves:

 User Input:
The program prompts the user for input (e.g., using scanf()) and responds based on
that input.
 Immediate Feedback:
The program provides output (e.g., using printf()) directly to the console in response
to user actions or processed data.
 Debugging:
Developers might use interactive debuggers (like GDB) to step through code, inspect
variables, and control execution flow in real-time, which offers an interactive
experience for code analysis.
Script Mode (C Programming Context):

"Script mode" in C refers to the standard way of developing and running C


programs. This involves:

 Source Code Files:


Writing C code in text files (e.g., .c files).
 Compilation:
Using a C compiler (like GCC) to translate the source code into an executable binary
file.
 Execution:
Running the compiled executable from the command line, where the program
executes its predefined logic without requiring continuous user input unless explicitly
programmed to do so.
 Batch Processing:
C programs in script mode are often used for batch processing, where they process
large amounts of data or perform complex computations without direct user
intervention during execution.
Key Differences Summarized:
Feature Interactive Mode (C Context) Script Mode (C Context)

Execution Program interacts with user during Compiled program runs


runtime; often for input/output. independently, following predefined
logic.

Purpose User interaction, real-time feedback, Application development, batch


debugging. processing, automated tasks.

Persistenc No inherent persistence of interactive Code is saved in source files and


e commands; code is in source file. compiled into executables.

Tools scanf(), printf(), interactive debuggers C compiler (GCC), text editor,


(GDB). command line for execution.

8) Comments
Comments in C programming are non-executable statements used to explain
code and improve readability. They are ignored by the compiler during program
execution.

Types of Comments:

 Single-line comments:
o Start with //.
o Comment extends to the end of the line.
o Example: int x = 10; // This declares and initializes variable x
 Multi-line comments (Block comments):
o Start with /* and end with */.
o Can span multiple lines.
o Example:
C
/*
This is a multi-line comment.
It can be used for longer explanations
or to temporarily comment out blocks of code.
*/

Purpose and Best Practices:

 Documentation: Explain the purpose of functions, variables, or complex logic.


 Readability: Make code easier to understand for yourself and others.
 Debugging: Temporarily disable code sections during testing.
 Clarity: Provide context and reasoning behind specific code choices.
 Conciseness: Keep comments clear and to the point.
 Relevance: Ensure comments accurately reflect the code they describe.

9) Indentation
Indentation in C programming refers to the practice of using whitespace (spaces
or tabs) at the beginning of lines of code to create a visual hierarchy and improve
readability. While C is a free-form language and indentation does not affect program
execution, it is crucial for writing clean, maintainable, and understandable code.

Key Points:

 Readability:
Indentation clearly shows the structure of control flow statements
(like if, else, for, while), functions, and blocks of code, making it easier to understand
which statements belong to which block.
 Maintainability:
Well-indented code is easier to debug and modify because the logical flow is visually
apparent.
 Consistency:
Adopting a consistent indentation style within a project or team is vital for
collaborative development. Common styles include K&R, Allman, and GNU.
 No Impact on Execution:
Unlike some languages (e.g., Python), C compilers ignore whitespace used for
indentation.
Example:
Consider the following C code snippet demonstrating the impact of indentation:
C
// Without indentation (poor readability)
int main() {
int x = 10;
if (x > 5) {
printf("x is greater than 5\n");
}
else {
printf("x is not greater than 5\n");
}
return 0;
}

// With indentation (improved readability)


int main() {
int x = 10;
if (x > 5) {
printf("x is greater than 5\n");
} else {
printf("x is not greater than 5\n");
}
return 0;
}
In the indented example, the if and else blocks are clearly distinguished, and the
statements within them are visually grouped, enhancing the code's clarity.
10) Error messages
Error messages in C programming indicate issues in your code, helping you identify and
fix problems. They are broadly categorized into:

1. Syntax Errors (Compile-time Errors):

These occur when your code violates the C language's grammatical rules. The compiler
detects these errors during compilation, preventing the program from generating an
executable.

 Example: Missing a semicolon (;) at the end of a statement.

#include <stdio.h>
int main() {
printf("Hello, World!") // Missing semicolon
return 0;
}

 Error Message (typical): error: expected ';' before 'return'


2. Runtime Errors:

These errors occur while the program is executing, often leading to crashes or
unexpected behavior.

 Example: Division by zero.

#include <stdio.h>
int main() {
int a = 10;
int b = 0;
int result = a / b; // Division by zero
printf("Result: %d\n", result);
return 0;
}

 Error Message (typical): Floating point exception (core dumped) or similar system-
level error.
3. Logical Errors:

Your code compiles and runs, but the output is incorrect because the program's logic is
flawed. The compiler or runtime environment typically do not flag these errors directly.

 Example: Incorrect loop condition.

#include <stdio.h>
int main() {
for (int i = 0; i <= 5; i++) { // Intended to print 0-4, but prints 0-5
printf("%d ", i);
}
return 0;
}

 Error Message: No error message, but the output is 0 1 2 3 4 5 instead of 0 1 2 3 4.

11) Primitive data types


Primitive data types in C are the fundamental building blocks for storing various
types of values. They are directly supported by the C language and serve as the basis for
all other data types.

Key Primitive Data Types:

 int (Integer):

Used to store whole numbers (positive, negative, or zero) without any decimal part.

 Example: int age = 30;


 char (Character):

Used to store a single character, enclosed in single quotes.


 Example: char grade = 'A';
 float (Floating-point):

Used to store numbers with decimal points, offering single-precision accuracy.

 Example: float price = 19.99f;


 double (Double Floating-point):

Used to store numbers with decimal points, offering double-precision accuracy,


suitable for larger or more precise decimal values.

 Example: double pi = 3.1415926535;


 void (Void):
Represents the absence of a value. It is typically used in function declarations to
indicate that a function does not return any value or takes no arguments.
 Example: void printMessage();
Qualifiers:

Primitive data types can be modified using qualifiers to control their size, range, and
whether they can hold negative values:

 short and long:

Modify int to create short int (smaller range) and long int (larger range). long
double also exists for extended precision floating-point numbers.

 signed and unsigned:


Modify integer types. signed allows both positive and negative values (default for int),
while unsigned only allows non-negative values, increasing the positive range.

12) Constants
Constants in C programming are fixed values that do not change during the
execution of a program. They are used to represent data that remains the same
throughout the program's runtime.

Key Characteristics:

 Fixed Value: Once a constant is defined and initialized with a value, that value cannot
be altered later in the program.

 Improves Readability: Using meaningful names for constants makes the code more
understandable.

 Enhances Maintainability: If a constant value needs to be changed, it only needs to be


modified in one place.

 Reduces Errors: Prevents accidental modification of important values.


Methods of Defining Constants:
Using the const keyword.
C
const int MAX_ATTEMPTS = 3;
const float PI = 3.14159;
This declares a variable as read-only, meaning its value cannot be changed after
initialization. using the #define preprocessor directive.
C
#define ARRAY_SIZE 10
#define GREETING "Hello, World!"
This defines a symbolic constant, where the preprocessor replaces every occurrence of
the identifier with its defined value before compilation.

Types of Constants:

 Integer Constants:

Whole numbers (e.g., 10, -5, 0). Can be decimal, octal (prefixed with 0), or hexadecimal
(prefixed with 0x).

 Floating-point Constants:

Numbers with a decimal point or in exponential form (e.g., 3.14, -2.5, 0.3E-5).

 Character Constants:

Single characters enclosed in single quotes (e.g., 'A', 'c', '\n'). Escape sequences
represent special characters.
 String Literals:
Sequences of characters enclosed in double quotes (e.g., "Hello", "C Programming").

13) Variables
In C programming, variables are named storage locations in memory used to
hold data during program execution.

Key Concepts:

 Definition: A variable definition tells the compiler about the variable's data type and
allocates memory for it.
C
int age; // Defines an integer variable named 'age'

 Declaration:
In C, variable declaration and definition often occur simultaneously. A declaration
specifies the name and type, while the definition allocates memory.

 Initialization:
Assigning an initial value to a variable at the time of its definition. This prevents the
variable from holding "garbage" data.
C
int count = 0; // Defines and initializes 'count' to 0

 Data Types:

Every variable must have a data type, which determines the size of memory allocated,
the range of values it can hold, and the operations that can be performed on it
(e.g., int, float, char, double). C is a strongly typed language, meaning a variable's type
cannot be changed after declaration.

 Rules for Naming Variables:

 Can contain letters (uppercase or lowercase), digits, and underscores (_).

 Must begin with a letter or an underscore. Cannot start with a digit.

 No white spaces are allowed within the variable name.

 Reserved keywords (e.g., int, if, while) cannot be used as variable names.

 Variable names are case-sensitive (e.g., age is different from Age).


 Scope:

Variables have a defined scope, which determines where they can be accessed in the
program (e.g., local variables within a function, global variables accessible throughout
the program).

 Memory Allocation:
Variables are typically stored in the main memory (RAM). The amount of memory
allocated depends on the variable's data type. For local variables, memory is often
allocated on the stack during function execution.

14) Reserved words

Reserved words, also known as keywords, in C programming are predefined


words that have special meanings to the C compiler. They are integral to the language's
syntax and cannot be used by the programmer for other purposes, such as naming
variables, functions, or structures. Attempting to use a reserved word as an identifier
will result in a compilation error.

Key characteristics of C reserved words:


 Predefined meaning: Each keyword has a specific, fixed meaning that the compiler
understands.

 Case-sensitive: C is case-sensitive, so keywords must be written in their exact


lowercase form (e.g., int is a keyword, INT is not).

 Cannot be redefined: Programmers cannot change the meaning or functionality of a


reserved word.

 Fundamental building blocks: They are used to define data types, control program
flow, declare storage classes, and perform other essential operations.
Examples of common C reserved words and their uses:

 Data Types:

 int: Integer data type.

 char: Character data type.

 float: Single-precision floating-point data type.

 double: Double-precision floating-point data type.

 void: Indicates no return value or a generic pointer.


 Control Flow:

 if, else: Conditional statements.

 for, while, do: Looping constructs.

 switch, case, default: Multi-way branching.

 break, continue: Used within loops and switch statements to alter flow.

 return: Returns a value from a function.

 goto: Unconditional jump statement.


 Storage Classes:

 auto: Default storage class for local variables.

 register: Suggests storing a variable in a CPU register for faster access.

 static: Retains value between function calls or limits scope to a single file.

 extern: Declares a variable or function defined in another file.


 Other:
 const: Declares a constant value.

 sizeof: Unary operator to determine the size of a data type or variable.

 struct: Defines a structure, a collection of variables.


 union: Defines a union, which allows different data types to share the same memory
location.

 typedef: Creates an alias for an existing data type.

 enum: Defines an enumeration, a set of named integer constants.

 volatile: Indicates a variable whose value can be changed by external factors.

15) Arithmetic, Relational, Logical, Bitwise, Assignment, Conditional operators


1. Arithmetic Operators:

These operators perform mathematical calculations.

 + (Addition): Adds two operands.

 - (Subtraction): Subtracts the second operand from the first.

 * (Multiplication): Multiplies two operands.

 / (Division): Divides the first operand by the second.

 % (Modulus): Returns the remainder of an integer division.


2. Relational Operators:

These operators compare two operands and return a Boolean result (true/false,
represented as 1/0 in C).

 == (Equal to): Checks if two operands are equal.

 != (Not equal to): Checks if two operands are not equal.

 > (Greater than): Checks if the first operand is greater than the second.

 < (Less than): Checks if the first operand is less than the second.

 >= (Greater than or equal to): Checks if the first operand is greater than or equal to the
second.

 <= (Less than or equal to): Checks if the first operand is less than or equal to the second.
3. Logical Operators:

These operators combine or negate logical expressions and return a Boolean result
(1/0).

 && (Logical AND): Returns true if both operands are true.

 || (Logical OR): Returns true if at least one operand is true.

 ! (Logical NOT): Reverses the logical state of an operand.


4. Bitwise Operators:

These operators perform operations on individual bits of integer operands.

 & (Bitwise AND): Performs a bitwise AND operation.

 | (Bitwise OR): Performs a bitwise OR operation.

 ^ (Bitwise XOR): Performs a bitwise XOR operation.

 ~ (Bitwise NOT/One's Complement): Inverts all bits of an operand.

 << (Left Shift): Shifts bits to the left.

 >> (Right Shift): Shifts bits to the right.


5. Assignment Operators:

These operators assign a value to a variable.

 = (Simple Assignment): Assigns the value of the right operand to the left operand.

 +=, -=, *=, /=, %=: Compound assignment operators that combine an arithmetic
operation with assignment (e.g., a += b is equivalent to a = a + b).

 &=, |=, ^=, <<=, >>=: Compound assignment operators that combine a bitwise operation
with assignment.
6. Conditional (Ternary) Operator:

This operator provides a concise way to write an if-else statement.

 condition ? expression1 : expression2;


If condition is true, expression1 is evaluated and its result is
returned. Otherwise, expression2 is evaluated and its result is returned.

16) Input/ Output Functions


In C programming, input/output (I/O) functions facilitate communication between a
program and the outside world, including the user (via console) and files. These
functions are primarily found in the standard input/output library, accessed by
including the <stdio.h> header file.

1. Formatted I/O Functions: These functions handle data in a specific format using
format specifiers (e.g., %d for integer, %f for float, %s for string).

 printf(): Used for displaying formatted output to the console (standard output, stdout).
C
printf("Hello, %s! Your age is %d.\n", "Alice", 30);

 scanf(): Used for reading formatted input from the console (standard input, stdin).
C
int age;
char name[20];
printf("Enter your name and age: ");
scanf("%s %d", name, &age);

 fprintf(): Used for writing formatted output to a file.

 fscanf(): Used for reading formatted input from a file.


2. Unformatted I/O Functions: These functions handle data without specific formatting,
typically for single characters or strings.

 getchar(): Reads a single character from the console.

 putchar(): Writes a single character to the console.

 gets() (Deprecated/Unsafe): Reads a string from the console. It is unsafe due to buffer
overflow risks; fgets() is the recommended alternative.

 puts(): Writes a string to the console, followed by a newline character.

 fgetc(): Reads a single character from a file.

 fputc(): Writes a single character to a file.

 fgets(): Reads a line of text (string) from a file or standard input, allowing for buffer size
specification to prevent overflows.

 fputs(): Writes a string to a file.


3. File I/O Functions: These functions manage operations on files.

 fopen(): Opens a file, returning a file pointer.

 fclose(): Closes an opened file.

 fread(): Reads a block of data from a file.

 fwrite(): Writes a block of data to a file.


Key Concepts:

 Standard Streams:

stdin (standard input, typically keyboard), stdout (standard output, typically screen),
and stderr (standard error, typically screen).

 Format Specifiers:

Characters used with printf() and scanf() to define the type of data being input or
output.

 File Pointers:
Variables of type FILE* that point to a file and are used to interact with it.

17) Built-in Functions.

Built-in functions in C, also known as standard library functions or predefined


functions, are functions provided by the C programming language or its standard
libraries to perform common tasks. These functions are readily available for use by
including the appropriate header files in your C program.

Characteristics:

 Predefined: Their definitions are already available in the C standard library.

 Accessible via Header Files: To use them, you must include the relevant header file
using the #include directive (e.g., <stdio.h>, <math.h>, <string.h>).

 Perform Standard Operations: They handle tasks like input/output, mathematical


calculations, string manipulations, and memory management.
Examples of Common Built-in Functions:

 Input/Output Functions (from <stdio.h>):

 printf(): Prints formatted output to the console.

 scanf(): Reads formatted input from the console.

 gets(): Reads a line of text from standard input.

 puts(): Writes a string to standard output.


 Mathematical Functions (from <math.h>):

 sqrt(): Calculates the square root of a number.

 pow(): Calculates the power of a number.

 abs(): Returns the absolute value of an integer.

 sin(), cos(), tan(): Trigonometric functions.


 String Manipulation Functions (from <string.h>):
 strlen(): Returns the length of a string.

 strcpy(): Copies one string to another.

 strcmp(): Compares two strings.

 strcat(): Concatenates two strings.


How to Use Built-in Functions:

 Include the Header File: Add #include <header_file_name.h> at the beginning of your C
file.
 Call the Function: Use the function name followed by parentheses containing any
required arguments.
Example:
C
#include <stdio.h> // For printf()
#include <math.h> // For sqrt()

int main() {
double number = 25.0;
double result = sqrt(number); // Calling the built-in sqrt() function
printf("The square root of %.2f is %.2f\n", number, result);
return 0;
}
PART-II
Control Structures
Here's an explanation of common control flow statements in C programming,
including syntax and examples with output:

1. if Statement

 Explanation: Executes a block of code if a specified condition is true.

 Syntax:
C
if (condition) {
// Code to execute if condition is true
}
example.
C
#include <stdio.h>
int main() {
int x = 10;
if (x > 5) {
printf("x is greater than 5\n");
}
return 0;
}
Output.
Code
x is greater than 5

2. if-else Statement

 Explanation: Executes one block of code if the condition is true, and another block if
the condition is false.

 Syntax:
C
if (condition) {
// Code if condition is true
} else {
// Code if condition is false
}
example.
C
#include <stdio.h>
int main() {
int age = 17;
if (age >= 18) {
printf("Eligible to vote\n");
} else {
printf("Not eligible to vote\n");
}
return 0;
}
Output.
Code
Not eligible to vote

3. Nested if Statement

 Explanation: An if statement placed inside another if or else block, allowing for more
complex conditional logic.

 Syntax:
C
if (condition1) {
if (condition2) {
// Code if both condition1 and condition2 are true
}
}
example.
C
#include <stdio.h>
int main() {
int num = 15;
if (num > 10) {
if (num < 20) {
printf("Number is between 10 and 20\n");
}
}
return 0;
}
Output.
Code
Number is between 10 and 20

4. switch-case Statement

 Explanation: Provides a way to execute different blocks of code based on the value of a
single variable or expression.

 Syntax:
C
switch (expression) {
case value1:
// Code for value1
break;
case value2:
// Code for value2
break;
default:
// Code if no case matches
}
example.
C
#include <stdio.h>
int main() {
char grade = 'B';
switch (grade) {
case 'A':
printf("Excellent!\n");
break;
case 'B':
printf("Very good!\n");
break;
default:
printf("Needs improvement\n");
}
return 0;
}
Output.
Code
Very good!

5. while Loop

 Explanation: Repeats a block of code as long as a specified condition remains true. The
condition is checked before each iteration.

 Syntax:
C
while (condition) {
// Code to repeat
}
example.
C
#include <stdio.h>
int main() {
int i = 1;
while (i <= 3) {
printf("Count: %d\n", i);
i++;
}
return 0;
}
Output.
Code
Count: 1
Count: 2
Count: 3

6. do-while Loop

 Explanation: Similar to while, but guarantees that the loop body executes at least once,
as the condition is checked after each iteration.

 Syntax:
C
do {
// Code to repeat
} while (condition);
example.
C
#include <stdio.h>
int main() {
int i = 5;
do {
printf("Value: %d\n", i);
i++;
} while (i < 5); // Condition is false, but loop runs once
return 0;
}
Output.
Code
Value: 5

7. for Loop

 Explanation: Used when the number of iterations is known or can be easily


determined. It combines initialization, condition checking, and iteration update in one
line.

 Syntax:
C
for (initialization; condition; update) {
// Code to repeat
}
example.
C
#include <stdio.h>
int main() {
for (int i = 0; i < 3; i++) {
printf("Iteration %d\n", i);
}
return 0;
}
Output.
Code
Iteration 0
Iteration 1
Iteration 2

8. Nested Loops

 Explanation: A loop placed inside another loop. Used for tasks requiring iteration over
multiple dimensions, like processing 2D arrays or printing patterns.

 Syntax (example with for loops):


C
for (outer_loop_init; outer_loop_condition; outer_loop_update) {
for (inner_loop_init; inner_loop_condition; inner_loop_update) {
// Code for inner loop
}
// Code for outer loop
}
example.
C
#include <stdio.h>
int main() {
for (int i = 1; i <= 2; i++) {
for (int j = 1; j <= 3; j++) {
printf("%d%d ", i, j);
}
printf("\n");
}
return 0;
}
Output.
Code
11 12 13
21 22 23

9. Jump Statements (break, continue, goto)

 Explanation: Alter the normal flow of control within loops or switch statements.
o break: Terminates the innermost loop or switch statement and transfers control to the
statement immediately following it.
o continue: Skips the rest of the current iteration of a loop and proceeds to the next
iteration.
o goto: Unconditionally transfers control to a specified labeled statement within the same
function. (Use sparingly, as it can lead to unstructured code.)
 Syntax (examples):
C
// break
while (condition) {
if (another_condition) {
break; // Exit the loop
}
}

// continue
for (initialization; condition; update) {
if (another_condition) {
continue; // Skip to next iteration
}
}

// goto
goto label_name;
// ...
label_name:
// Code here
Example (break).
C
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break; // Exit loop when i is 3
}
printf("%d ", i);
}
printf("\n");
return 0;
}
Output.
Code
12
Example (continue).
C
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skip printing 3
}
printf("%d ", i);
}
printf("\n");
return 0;
}
Output.
Code
1245
PART-III
Functions
Function Declaration, Definition and Calling, Function Parameters and Return
Types, Call by Value and Call by Reference, Recursive Functions, Scope and
Lifetime of Variables, Header files and Modular Programming.

Function Declaration, Definition, and Calling

 Function Declaration (Prototype): Informs the compiler about a function's name,


return type, and the types and order of its parameters. It does not contain the function's
body.
C
return_type function_name(parameter_type1, parameter_type2, ...);

 Function Definition: Provides the actual implementation of the function, including its
body which contains the executable code.
C
return_type function_name(parameter_type1 parameter1, parameter_type2
parameter2, ...) {
// Function body (statements)
return value; // If return_type is not void
}
 Function Calling: Invokes the function to execute its code. Arguments are passed to the
function during the call.
C
function_name(argument1, argument2, ...);

Function Parameters and Return Types

 Parameters: Variables declared in the function definition that receive values passed
during the function call.

 Arguments: The actual values passed to a function when it is called.

 Return Type: Specifies the data type of the value that the function returns. void is used
if the function does not return any value.
Call by Value and Call by Reference

 Call by Value:

A copy of the argument's value is passed to the function. Changes to the parameter
within the function do not affect the original argument.

 Call by Reference:
The memory address of the argument is passed to the function. This allows the
function to directly modify the original argument using pointers.
Recursive Functions

 A function that calls itself, either directly or indirectly.

 Requires a base case to terminate the recursion and prevent infinite loops.
Scope and Lifetime of Variables

 Scope:

The region of the program where a variable can be accessed.

 Local Scope: Variables declared inside a function; accessible only within that function.

 Global Scope: Variables declared outside any function; accessible throughout the
program.
 Lifetime:
The period during which a variable exists in memory.
 Automatic Lifetime: Local variables, created on function entry and destroyed on
function exit.

 Static Lifetime: Global variables and static local variables, exist throughout the
program's execution.
Header Files and Modular Programming
 Header Files (.h):

Contain function declarations, macro definitions, and global variable


declarations. They are included in source files using #include directives.

 Modular Programming:
The practice of breaking down a large program into smaller, independent modules
(often functions and related data) that can be developed and tested separately, and
then combined to form the complete program. Header files facilitate modularity by
providing interfaces for these modules.

1) Function Declaration
A function declaration in C, also known as a function prototype, informs the compiler
about a function's existence, its return type, and the types of its parameters before the
function is actually defined. This allows the compiler to perform type-checking during
function calls and ensure correct usage.

Syntax:
C
return_type function_name(parameter_list);

 return_type: The data type of the value the function returns (e.g., int, float, void).

 function_name: The unique identifier for the function.

 parameter_list: A comma-separated list of the data types of the parameters the function
accepts. Parameter names are optional in the declaration.
Example:
C
#include <stdio.h>

// Function declaration (prototype)


int add(int num1, int num2);

int main() {
int result;
result = add(5, 3); // Function call
printf("The sum is: %d\n", result);
return 0;
}

// Function definition
int add(int num1, int num2) {
return num1 + num2;
}
Example Output:
Code
The sum is: 8

Key Points:

 Function declarations are crucial when the function's definition appears after its call in
the code (e.g., after main()).

 They ensure the compiler knows how to handle function calls correctly, preventing
errors related to incorrect return types or parameter types.

 Parameter names are optional in the declaration; only their data types are
necessary. For example, int add(int, int); is also a valid declaration for the add function.

2) Function Definition and Calling

1. Function Definition:
A function definition provides the actual body of the function, including the code that
executes when the function is called. It specifies the return type, function name,
parameters (if any), and the function body enclosed in curly braces.
C
return_type function_name(parameter_list) {
// Function body (statements to be executed)
// Optional: return value;
}

 return_type: The data type of the value the function returns. Use void if the function
does not return any value.

 function_name: A unique identifier for the function.

 parameter_list: A comma-separated list of declarations for the values passed to the


function. Empty parentheses () indicate no parameters.
2. Function Calling:
To execute the code within a function, you "call" it from another part of your program
(e.g., from main() or another function). When a function is called, program control
transfers to the called function, and after its execution, control returns to the point
where it was called.
C
// Calling a function without arguments or return value
function_name();
// Calling a function with arguments and storing the return value
return_value_variable = function_name(argument1, argument2);

Example:
C
#include <stdio.h>

// Function Definition: Adds two integers and returns the sum


int add(int a, int b) {
int sum = a + b;
return sum; // Returns the calculated sum
}

// Function Definition: Prints a greeting (no return value, no parameters)


void greet() {
printf("Hello from the greet function!\n");
}

int main() {
// Function Calling: Call greet()
greet();

// Function Calling: Call add() with arguments and store the result
int num1 = 10;
int num2 = 5;
int result = add(num1, num2); // Call add, pass num1 and num2

printf("The sum of %d and %d is: %d\n", num1, num2, result);

return 0;
}

Example Output:
Code
Hello from the greet function!
The sum of 10 and 5 is: 15
3)Function Parameters and Return Types
In C programming, functions can accept input values through parameters and can
produce an output value through their return type.

Function Parameters

Parameters are variables declared in the function definition that receive values passed
to the function during a function call. They allow functions to operate on different data
without needing to be rewritten.

 Syntax: return_type function_name(type1 param1, type2 param2, ...)


 Example: In int add(int a, int b), a and b are parameters of type int.
Function Return Types

The return type specifies the data type of the value that a function sends back to the
calling code after its execution.

 Syntax: return_type function_name(parameters)

 void return type: If a function does not return any value, its return type is void.

 return statement: Used within the function body to send a value back to the caller. The
type of the returned value must match the declared return type.
Example with Output
C
#include <stdio.h>

// Function with parameters and a return type


int multiply(int x, int y) {
int product = x * y;
return product; // Returns an integer value
}

// Function with parameters and no return type (void)


void greet(char name[]) {
printf("Hello, %s!\n", name);
// No return statement needed for void functions, or 'return;' can be used.
}

int main() {
int num1 = 5, num2 = 3;
char myName[] = "Alice";

// Calling multiply function and storing the returned value


int result = multiply(num1, num2);
printf("The product of %d and %d is: %d\n", num1, num2, result);

// Calling greet function (no return value to store)


greet(myName);

return 0;
}

Output:
Code
The product of 5 and 3 is: 15
Hello, Alice!
4) Call by Value and Call by Reference
Call by Value in C
Concept: In call by value, a copy of the actual argument's value is passed to the
function's formal parameter. Any modifications made to the formal parameter inside
the function do not affect the original variable in the calling function because the
function operates on a separate copy.

Example:
C
#include <stdio.h>

void modifyValue(int num) {


num = num + 10; // Modifies the copy
printf("Inside function: num = %d\n", num);
}

int main() {
int x = 5;
printf("Before function call: x = %d\n", x);
modifyValue(x); // Pass by value
printf("After function call: x = %d\n", x); // Original x remains unchanged
return 0;
}

Output:
Code
Before function call: x = 5
Inside function: num = 15
After function call: x = 5

Call by Reference in C
Concept: In call by reference (simulated using pointers in C), the address of the actual
argument is passed to the function. The function receives a pointer to the original
variable, allowing it to directly access and modify the original data in the calling
function's memory location.

Example:
C
#include <stdio.h>

void modifyValue(int *ptr_num) {


*ptr_num = *ptr_num + 10; // Modifies the original variable via its address
printf("Inside function: *ptr_num = %d\n", *ptr_num);
}

int main() {
int x = 5;
printf("Before function call: x = %d\n", x);
modifyValue(&x); // Pass the address of x
printf("After function call: x = %d\n", x); // Original x is modified
return 0;
}

Output:
Code
Before function call: x = 5
Inside function: *ptr_num = 15
After function call: x = 15

5) Recursive Functions

Recursive Functions in C: Short Notes


Recursive functions in C are functions that call themselves directly or indirectly to solve
a problem. This technique is particularly useful for problems that can be broken down
into smaller, self-similar subproblems.

Key Components:

 Base Case:

A condition that stops the recursion and prevents an infinite loop. When the base case
is met, the function returns a value without making further recursive calls.

 Recursive Step:
The part of the function where it calls itself, typically with modified arguments that
move closer to the base case.
Working Principle:
Each recursive call creates a new stack frame, storing local variables and
parameters. When the base case is reached, the function returns, and the stack frames
are cleared in reverse order of their creation, allowing the results of subproblems to be
combined.

Example (Factorial Calculation):


C
#include <stdio.h>

// Recursive function to calculate factorial


long long factorial(int n) {
// Base case: factorial of 0 or 1 is 1
if (n == 0 || n == 1) {
return 1;
}
// Recursive step: n * factorial(n-1)
else {
return n * factorial(n - 1);
}
}

int main() {
int num = 5;
printf("Factorial of %d is %lld\n", num, factorial(num));
return 0;
}

Example Output:
Code
Factorial of 5 is 120

6) Scope and Lifetime of Variables


In C programming, the scope of a variable defines the region of the program where it
can be accessed, while its lifetime determines how long it exists in memory.

1. Scope:

 Local Scope (Block Scope): Variables declared inside a function or a block {} are
local. They are only accessible within that specific function or block.
C
#include <stdio.h>

void myFunction() {
int localVar = 10; // localVar has local scope
printf("Inside function: %d\n", localVar);
}

int main() {
// printf("%d", localVar); // Error: localVar is not accessible here
myFunction();
return 0;
}

Output:
Code
Inside function: 10

 Global Scope (File Scope): Variables declared outside all functions, at the top level of a
file, have global scope. They are accessible from any function within that file.
C
#include <stdio.h>

int globalVar = 20; // globalVar has global scope

void anotherFunction() {
printf("Inside another function: %d\n", globalVar);
}

int main() {
printf("Inside main: %d\n", globalVar);
anotherFunction();
return 0;
}

Output:
Code
Inside main: 20
Inside another function: 20

2. Lifetime:

 Automatic Lifetime: Most local variables have automatic lifetime. They are created
when their block or function is entered and destroyed when the block or function
exits. Their values are not preserved between multiple calls to the same function.
C
#include <stdio.h>

void countCalls() {
int autoVar = 0; // autoVar has automatic lifetime
autoVar++;
printf("autoVar: %d\n", autoVar);
}

int main() {
countCalls(); // autoVar is 1
countCalls(); // autoVar is 1 again (re-initialized)
return 0;
}

Output:
Code
autoVar: 1
autoVar: 1

 Static Lifetime: Variables declared with the static keyword (local or global) have static
lifetime. They are created once when the program starts and exist until the program
terminates, retaining their value even after their scope is exited.
C
#include <stdio.h>

void staticCountCalls() {
static int staticVar = 0; // staticVar has static lifetime
staticVar++;
printf("staticVar: %d\n", staticVar);
}

int main() {
staticCountCalls(); // staticVar is 1
staticCountCalls(); // staticVar is 2 (retains value)
return 0;
}

Output:
Code
staticVar: 1
staticVar: 2

 Dynamic Lifetime: Memory allocated using functions like malloc() or calloc() has
dynamic lifetime. This memory persists until explicitly deallocated using free().
C
#include <stdio.h>
#include <stdlib.h>

int main() {
int *dynamicVar = (int *)malloc(sizeof(int)); // Dynamic lifetime
if (dynamicVar != NULL) {
*dynamicVar = 30;
printf("Dynamic variable: %d\n", *dynamicVar);
free(dynamicVar); // Deallocate memory
}
return 0;
}

Output:
Code
Dynamic variable: 30
7) Header files and Modular Programming.
Header files and modular programming in C facilitate code organization, reusability, and
maintainability.

Header Files (.h)

 Contain function prototypes, macro definitions, and type definitions (e.g., structs,
enums) that are shared across multiple source files.
 Act as an interface, declaring what functions and data structures are available in a
module without revealing their implementation details.

 Included in source files using the #include preprocessor directive.


o #include <filename.h> for standard library headers.

o #include "filename.h" for user-defined headers.


 Include Guards: #ifndef, #define, #endif prevent multiple inclusions of the same
header, avoiding redefinition errors.
Modular Programming

 Breaks down a large program into smaller, self-contained units called modules.

 Each module focuses on a specific task or set of related functionalities.

 Promotes:
o Encapsulation: Hiding implementation details within a module.

o Reusability: Modules can be reused in different parts of the same program or in other
projects.
o Maintainability: Easier to debug and update individual modules without affecting the
entire program.
o Collaboration: Multiple developers can work on different modules simultaneously.
Example
Consider a program that calculates the area of various shapes.

1. shapes.h (Header File)


C
#ifndef SHAPES_H
#define SHAPES_H

// Function prototypes
double calculateCircleArea(double radius);
double calculateRectangleArea(double length, double width);

#endif // SHAPES_H

2. shapes.c (Module Implementation)


C
#include "shapes.h"
#include <math.h> // For M_PI

double calculateCircleArea(double radius) {


return M_PI * radius * radius;
}

double calculateRectangleArea(double length, double width) {


return length * width;
}

3. main.c (Main Program)


C
#include <stdio.h>
#include "shapes.h" // Include the custom header

int main() {
double circleRadius = 5.0;
double rectangleLength = 4.0;
double rectangleWidth = 6.0;

double circleArea = calculateCircleArea(circleRadius);


double rectangleArea = calculateRectangleArea(rectangleLength, rectangleWidth);

printf("Area of circle with radius %.2f: %.2f\n", circleRadius, circleArea);


printf("Area of rectangle with length %.2f and width %.2f: %.2f\n", rectangleLength,
rectangleWidth, rectangleArea);

return 0;
}

Compilation and Output


To compile this modular program, you would typically compile each .c file separately
and then link them:
Code
gcc -c shapes.c -o shapes.o
gcc -c main.c -o main.o
gcc shapes.o main.o -o myprogram -lm # -lm links the math library
./myprogram

Output:
Code
Area of circle with radius 5.00: 78.54
Area of rectangle with length 4.00 and width 6.00: 24.00

PART-IV
Strings & Pointers
1) One-dimensional and Multi-Dimensional Arrays

Arrays in C Programming: One-Dimensional vs. Multi-Dimensional


Arrays in C are collections of elements of the same data type stored in contiguous
memory locations. They provide an efficient way to store and manipulate a fixed
number of similar data items.

One-Dimensional Arrays
Definition: A one-dimensional array is a linear collection of elements, accessed using a
single index. Think of it as a single row of data.

Declaration:
C
data_type array_name[size];

 data_type: The type of elements the array will store (e.g., int, char, float).

 array_name: The name given to the array.

 size: The number of elements the array can hold.


Initialization:
C
int numbers[5] = {10, 20, 30, 40, 50}; // Initializing at declaration
or
C
int numbers[5];
numbers[0] = 10; // Assigning values individually
numbers[1] = 20;
// ... and so on
Accessing Elements: Elements are accessed using their index, which starts from 0 for
the first element and goes up to size - 1.
C
int first_element = numbers[0]; // Accesses the first element (10)

Example:
C
#include <stdio.h>

int main() {
int scores[3] = {85, 92, 78}; // Declare and initialize a 1D array
printf("Score 1: %d\n", scores[0]);
printf("Score 2: %d\n", scores[1]);
printf("Score 3: %d\n", scores[2]);
return 0;
}

Output:
Code
Score 1: 85
Score 2: 92
Score 3: 78

Multi-Dimensional Arrays
Definition: Multi-dimensional arrays are arrays of arrays. They are used to store data in
a tabular or grid-like structure, requiring multiple indices to access elements. The most
common type is a two-dimensional array (matrix).

Declaration (Two-Dimensional Array):


C
data_type array_name[rows][columns];

 rows: The number of rows in the array.

 columns: The number of columns in the array.


Initialization (Two-Dimensional Array):
C
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}}; // Initializing at declaration
or
C
int matrix[2][3];
matrix[0][0] = 1; // Assigning values individually
matrix[0][1] = 2;
// ... and so on
Accessing Elements (Two-Dimensional Array): Elements are accessed using two indices:
one for the row and one for the column.
C
int element = matrix[0][1]; // Accesses the element at row 0, column 1 (2)

Example (Two-Dimensional Array):


C
#include <stdio.h>

int main() {
int matrix[2][3] = {{10, 20, 30}, {40, 50, 60}}; // Declare and initialize a 2D array

printf("Matrix elements:\n");
for (int i = 0; i < 2; i++) { // Loop through rows
for (int j = 0; j < 3; j++) { // Loop through columns
printf("%d ", matrix[i][j]);
}
printf("\n"); // New line after each row
}
return 0;
}

Output:
Code
Matrix elements:
10 20 30
40 50 60
2) Array operations and traversals
Arrays in C programming are collections of elements of the same data type stored in
contiguous memory locations. They are accessed using an index, which starts from 0.

Array Traversal
Array traversal involves visiting each element of an array, usually for processing or
displaying its contents. This is typically done using a loop.

Example of Traversal:
C
#include <stdio.h>

int main() {
int numbers[] = {10, 20, 30, 40, 50}; // Declare and initialize an array
int size = sizeof(numbers) / sizeof(numbers[0]); // Calculate the size of the array

printf("Array elements: ");


for (int i = 0; i < size; i++) { // Loop through each element
printf("%d ", numbers[i]); // Access and print each element
}
printf("\n");

return 0;
}

Output:
Code
Array elements: 10 20 30 40 50

Common Array Operations

Beyond traversal, other common array operations include:


 Insertion: Adding an element at a specific index. This often requires shifting existing
elements.
C
// Example: Inserting 25 at index 2
// Assuming 'numbers' array from above, and enough space
// Shift elements to the right from index 2
for (int i = size; i > 2; i--) {
numbers[i] = numbers[i-1];
}
numbers[2] = 25; // Insert the new element
size++; // Increment the size

 Deletion: Removing an element from a specific index. This often requires shifting
subsequent elements to the left.
C
// Example: Deleting element at index 2
// Assuming 'numbers' array from above
// Shift elements to the left from index 3
for (int i = 2; i < size - 1; i++) {
numbers[i] = numbers[i+1];
}
size--; // Decrement the size

 Searching: Finding a specific element within the array. This can be done using linear
search (checking each element sequentially) or binary search (for sorted arrays).
C
// Example: Linear search for 30
int target = 30;
int found_index = -1;
for (int i = 0; i < size; i++) {
if (numbers[i] == target) {
found_index = i;
break; // Element found, exit loop
}
}
// If found_index is not -1, the element was found at that index.

 Updating: Changing the value of an element at a specific index.


C
// Example: Updating element at index 0 to 5
numbers[0] = 5;
Note: Insertion and deletion in static arrays (like those declared in C) are less efficient
as they require shifting elements. Dynamic memory allocation
(using malloc and realloc) can offer more flexibility for resizing arrays.
3) String Handling: String declaration, input/output, string library
functions

C String Handling: Declaration, Input/Output, and Library Functions


1. String Declaration:
In C, strings are essentially arrays of characters terminated by a null character \0.
C
char str_name[size]; // Declares a character array named str_name with a maximum
size

Example:
C
char greeting[10] = "Hello"; // Declares and initializes a string
char city[] = {'N', 'e', 'w', ' ', 'Y', 'o', 'r', 'k', '\0'}; // Another way to initialize

2. String Input/Output:

 printf() and scanf(): For basic string output and input (stops at whitespace).
C
#include <stdio.h>

int main() {
char name[20];
printf("Enter your first name: ");
scanf("%s", name); // Reads a string until whitespace
printf("Hello, %s!\n", name);
return 0;
}

Output:
Code
Enter your first name: John Doe
Hello, John!

 gets() and puts(): For reading and displaying entire lines of text (including
spaces). Note: gets() is generally discouraged due to buffer overflow risks.
C
#include <stdio.h>

int main() {
char message[50];
printf("Enter a message: ");
gets(message); // Reads a whole line of text
printf("Your message: ");
puts(message); // Displays the string
return 0;
}

Output:
Code
Enter a message: This is a test message.
Your message: This is a test message.

 fgets(): A safer alternative to gets(), allowing you to specify the buffer size.
C
#include <stdio.h>

int main() {
char sentence[100];
printf("Enter a sentence: ");
fgets(sentence, sizeof(sentence), stdin); // Reads a line, limiting input to buffer size
printf("You entered: %s", sentence); // Note: fgets includes the newline character
return 0;
}

3. String Library Functions (string.h):

Include <string.h> to use these functions.

 strlen(char *str): Returns the length of the string (excluding \0).


C
#include <stdio.h>
#include <string.h>

int main() {
char myString[] = "Programming";
int length = strlen(myString);
printf("Length of \"%s\": %d\n", myString, length); // Output: Length of
"Programming": 11
return 0;
}

 strcpy(char *dest, const char *src): Copies the src string to dest.
C
#include <stdio.h>
#include <string.h>

int main() {
char source[] = "Copy me!";
char destination[20];
strcpy(destination, source);
printf("Copied string: %s\n", destination); // Output: Copied string: Copy me!
return 0;
}

 strcat(char *dest, const char *src): Concatenates src to the end of dest.
C
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello, ";
char str2[] = "World!";
strcat(str1, str2);
printf("Concatenated string: %s\n", str1); // Output: Concatenated string: Hello,
World!
return 0;
}

 strcmp(const char *str1, const char *str2): Compares two strings


lexicographically. Returns 0 if equal, a negative value if str1 < str2, and a positive value
if str1 > str2.
C
#include <stdio.h>
#include <string.h>

int main() {
char s1[] = "apple";
char s2[] = "banana";
char s3[] = "apple";

printf("strcmp(\"apple\", \"banana\"): %d\n", strcmp(s1, s2)); // Output: a negative


value
printf("strcmp(\"apple\", \"apple\"): %d\n", strcmp(s1, s3)); // Output: 0
return 0;
}
4)Pointer arithmetic
Pointer arithmetic in C involves performing arithmetic operations on
memory addresses stored in pointers. Unlike general arithmetic, pointer
arithmetic is scaled by the size of the data type the pointer points to.

Key Concepts:

 Scaling by Data Type Size:

When you increment a pointer (e.g., ptr++), it advances by sizeof(data_type) bytes, not
just 1 byte. Similarly, decrementing a pointer moves it backward
by sizeof(data_type) bytes.

 Valid Operations:

 Adding an integer to a pointer (ptr + n): Moves the pointer forward by n *


sizeof(data_type) bytes.

 Subtracting an integer from a pointer (ptr - n): Moves the pointer backward by n *
sizeof(data_type) bytes.
 Subtracting two pointers of the same type (ptr1 - ptr2): Results in the number of
elements between them.

 Comparison of pointers (==, !=, <, >): Useful for checking relative positions in memory,
especially within arrays.
 Invalid Operations:
 Adding two pointers.

 Multiplying or dividing pointers.

 Performing arithmetic on void pointers (they must be cast to a specific type first).
Example:
Consider an integer array and a pointer pointing to its first element.
C
#include <stdio.h>

int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr; // ptr points to the first element (arr[0])

printf("Value at ptr: %d\n", *ptr); // Output: 10


printf("Address of ptr: %p\n", (void*)ptr); // Output: Address of arr[0]

ptr++; // Increment ptr


printf("Value at ptr after increment: %d\n", *ptr); // Output: 20
printf("Address of ptr after increment: %p\n", (void*)ptr); // Output: Address of
arr[1] (address of arr[0] + sizeof(int))

ptr = ptr + 2; // Add 2 to ptr


printf("Value at ptr after adding 2: %d\n", *ptr); // Output: 40
printf("Address of ptr after adding 2: %p\n", (void*)ptr); // Output: Address of arr[3]
(address of arr[1] + 2 * sizeof(int))

return 0;
}

Outputs (example on a system where sizeof(int) is 4 bytes):


Code
Value at ptr: 10
Address of ptr: 0x7ffc7b4e2010
Value at ptr after increment: 20
Address of ptr after increment: 0x7ffc7b4e2014
Value at ptr after adding 2: 40
Address of ptr after adding 2: 0x7ffc7b4e201c

Explanation of Outputs:
 Initially, ptr points to arr[0], so *ptr is 10. The address shown is the memory location
of arr[0].

 After ptr++, the pointer moves to the next int location, which is arr[1]. The address
increases by sizeof(int) (e.g., 4 bytes), and *ptr becomes 20.

 After ptr = ptr + 2, the pointer moves forward by two int elements from its current
position (which was arr[1]). Thus, it points to arr[3], the address increases by 2 *
sizeof(int) (e.g., 8 bytes), and *ptr becomes 40.

5)Pointers and Arrays

Pointers in C
A pointer is a variable that stores the memory address of another variable. It
"points" to the location in memory where a value is stored.
Declaration: datatype *pointer_name; (e.g., int *ptr;)
Initialization: ptr = &variable_name; (assigns the address of variable_name to ptr)
Dereferencing: *ptr (accesses the value at the memory address stored in ptr)

Example:
C
#include <stdio.h>

int main() {
int num = 10;
int *ptr; // Declare an integer pointer

ptr = &num; // Store the address of 'num' in 'ptr'

printf("Value of num: %d\n", num);


printf("Address of num: %p\n", &num);
printf("Value stored in ptr (address of num): %p\n", ptr);
printf("Value pointed to by ptr: %d\n", *ptr);

*ptr = 20; // Change the value of num using the pointer


printf("New value of num: %d\n", num);

return 0;
}

Output:
Code
Value of num: 10
Address of num: 0x7ffee1d2c9ac // (Actual address may vary)
Value stored in ptr (address of num): 0x7ffee1d2c9ac // (Actual address may vary)
Value pointed to by ptr: 10
New value of num: 20

Arrays in C
An array is a collection of elements of the same data type stored in contiguous memory
locations. The array name itself acts as a pointer to its first element.
Declaration: datatype array_name[size]; (e.g., int arr[5];)
Initialization: int arr[] = {1, 2, 3, 4, 5}; (or int arr[5] = {1, 2, 3, 4, 5};)
Accessing elements: array_name[index] (e.g., arr[0])

Example:
C
#include <stdio.h>

int main() {
int numbers[] = {10, 20, 30, 40, 50}; // Declare and initialize an array

printf("First element: %d\n", numbers[0]);


printf("Address of first element: %p\n", &numbers[0]);
printf("Array name (address of first element): %p\n", numbers);

// Accessing elements using pointer arithmetic


printf("Second element using pointer arithmetic: %d\n", *(numbers + 1));

return 0;
}

Output:
Code
First element: 10
Address of first element: 0x7ffee1d2c9b0 // (Actual address may vary)
Array name (address of first element): 0x7ffee1d2c9b0 // (Actual address may vary)
Second element using pointer arithmetic: 20

Relationship between Pointers and Arrays:


The name of an array in C essentially behaves as a constant pointer to its first
element. This means array_name is equivalent to &array_name[0]. Consequently,
elements can be accessed using both array indexing (array_name[i]) and pointer
arithmetic (*(array_name + i)). Pointers offer a more flexible way to traverse and
manipulate array elements, particularly in scenarios involving dynamic memory
allocation or passing arrays to functions.

6)Pointers to function
Pointers to Functions in C

Short Notes:

 Definition:

A function pointer is a variable that stores the memory address of a function. This
allows you to call the function indirectly through the pointer.

 Declaration:
The syntax for declaring a function pointer must match the signature (return type and
parameter list) of the function it will point to.
C
return_type (*pointer_name)(parameter_list);

For example, int (*funcPtr)(int, int); declares a pointer funcPtr to a function that returns
an int and takes two int arguments.

 Initialization: You can assign the address of a function to a function pointer. The
function name itself represents its address.
C
funcPtr = function_name; // or funcPtr = &function_name;

 Calling the Function:

You can call the function through the pointer using either (*funcPtr)(args) or
simply funcPtr(args).

 Use Cases:
Function pointers are useful for implementing callback mechanisms, creating arrays of
functions, and achieving polymorphism in C.
Example:
C
#include <stdio.h>

// A simple function to add two integers


int add(int a, int b) {
return a + b;
}

// Another function to subtract two integers


int subtract(int a, int b) {
return a - b;
}

int main() {
// Declare a function pointer that points to a function
// returning int and taking two int arguments
int (*operation)(int, int);

// Assign the address of the 'add' function to the pointer


operation = add;

// Call the 'add' function using the function pointer


int result_add = operation(10, 5);
printf("Result of addition: %d\n", result_add); // Expected: 15

// Assign the address of the 'subtract' function to the pointer


operation = subtract;

// Call the 'subtract' function using the function pointer


int result_subtract = operation(10, 5);
printf("Result of subtraction: %d\n", result_subtract); // Expected: 5

return 0;
}

Output:
Code
Result of addition: 15
Result of subtraction: 5

7) Dynamic memory allocation


Dynamic memory allocation in C allows programs to manage memory during
runtime, rather than at compile time. This is particularly useful for handling data
structures whose size is not known beforehand or can change during program
execution. The memory is allocated from the heap section of the system memory.

Key Functions (defined in <stdlib.h>):

 malloc() (memory allocation): Allocates a single block of contiguous memory of a


specified size in bytes. It returns a void pointer to the beginning of the allocated block,
or NULL if allocation fails. The allocated memory is uninitialized and may contain
garbage values.
C
void* malloc(size_t size);

 calloc() (contiguous allocation): Allocates multiple blocks of memory, each of a specified


size, and initializes all bytes to zero. It returns a void pointer to the first byte of the
allocated memory, or NULL if allocation fails.
C
void* calloc(size_t num, size_t size);
 realloc() (reallocation): Changes the size of a previously allocated memory block. It can
expand or shrink the block. It returns a void pointer to the new memory block,
or NULL if reallocation fails. If the original block cannot be resized in place, a new block
is allocated, and the contents are copied.
C
void* realloc(void* ptr, size_t new_size);

 free(): Deallocates memory previously allocated by malloc(), calloc(), or realloc(),


returning it to the system heap. This prevents memory leaks.
C
void free(void* ptr);

Example:
C
#include <stdio.h>
#include <stdlib.h>

int main() {
int n, i;
int *arr;

printf("Enter the number of elements: ");


scanf("%d", &n);

// Allocate memory using malloc()


arr = (int *)malloc(n * sizeof(int));

// Check if malloc was successful


if (arr == NULL) {
printf("Memory allocation failed!\n");
return 1; // Indicate an error
}

printf("Enter %d integer elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Elements entered are: ");


for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

// Deallocate the memory


free(arr);
arr = NULL; // Good practice to set the pointer to NULL after freeing
return 0;
}

Example Output:
Code
Enter the number of elements: 3
Enter 3 integer elements:
10
20
30
Elements entered are: 10 20 30

You might also like