C Statements Overview
C Statements Overview
Notes on C Statements
1. What are C Statements?
C statements are the instructions written in a C program to perform specific actions. These instructions
tell the compiler what to do, such as perform calculations, manipulate variables, or control the flow of
execution.
2. Types of C Statements
a. Expression Statements
These are statements that produce a value or have an effect, such as assignment or arithmetic
operations.
Syntax: `<expression>;`
Examples:
x = 10; // Assignment
y = x + 5; // Arithmetic operation
b. Declaration Statements
c. Control Statements
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 1/59
Control Statements include:
1. Conditional Statements:
`if`, `if-else`, `switch`
Example:
if (x > 0) {
printf("Positive");
} else {
printf("Negative");
}
2. Looping Statements:
`for`, `while`, `do-while`
Example:
3. Jump Statements:
`break`, `continue`, `goto`, `return`
Example:
d. Compound Statements
{
int x = 10;
printf("%d", x);
}
e. Null Statements
; // Null statement
f. Input/Output Statements
int a;
printf("Enter a number: ");
scanf("%d", &a);
1. Semicolon Required:
Every statement ends with `;`.
Example: `int x = 5;`
2. Case-Sensitive:
Keywords like `if` and `IF` are different.
3. Whitespace Ignored:
Spaces or tabs between tokens are ignored.
Example:
a. Conditional Statement
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 3/59
c
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("Even");
} else {
printf("Odd");
}
return 0;
}
b. Looping Statement
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("Hello, World!\n");
}
return 0;
}
c. Jump Statement
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5) continue; // Skip 5
printf("%d ", i);
}
return 0;
}
if (x > 0) {
printf("Positive");
}
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 4/59
2. Commenting:
Add comments for clarity.
Example:
// This is a loop
for (int i = 0; i < 10; i++) {
printf("%d\n", i);
}
6. Summary
This covers the basics and essentials of C Statements! Let me know if you need a specific section
expanded. 😊
Here’s a comprehensive explanation of all the topics mentioned in your syllabus under C Statements:
C Statements
1. Selection Statements
Selection statements allow a program to make decisions based on conditions. These include:
a. `if` Statement
if (condition) {
// Code to execute if condition is true
}
Example:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 5/59
c
if (x > 0) {
printf("Positive number");
}
if (condition1) {
if (condition2) {
// Code to execute if both conditions are true
}
}
Example:
if (x > 0) {
if (x % 2 == 0) {
printf("Positive even number");
}
}
c. `if-else-if` Ladder
if (condition1) {
// Code for condition1
} else if (condition2) {
// Code for condition2
} else {
// Code if none of the conditions are true
}
Example:
Example:
e. `switch` Statement
switch (expression) {
case value1:
// Code for case 1
break;
case value2:
// Code for case 2
break;
default:
// Code if none of the cases match
}
Example:
switch (day) {
case 1: printf("Monday"); break;
case 2: printf("Tuesday"); break;
default: printf("Invalid day");
}
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 7/59
c
switch (category) {
case 1:
switch (type) {
case 1: printf("Electronics"); break;
case 2: printf("Clothing"); break;
}
break;
default: printf("Invalid category");
}
2. Iteration Statements
a. `for` Loop
Example:
Infinite Loop:
for (;;) {
printf("This runs forever");
}
Multiple Initializations/Increments:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 8/59
c
c. `while` Loop
while (condition) {
// Code to execute
}
Example:
int i = 0;
while (i < 5) {
printf("%d ", i);
i++;
}
d. `do-while` Loop
do {
// Code to execute
} while (condition);
Example:
int i = 0;
do {
printf("%d ", i);
i++;
} while (i < 5);
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 9/59
Variables can be declared within loop statements.
Example:
3. Jump Statements
a. `return` Statement
return 0;
b. `goto` Statement
goto label;
label: printf("Jumped here");
c. `break` Statement
d. `exit()` Function
exit(0);
e. `continue` Statement
Skips the rest of the code in the current iteration and moves to the next iteration.
Example:
4. Expression Statements
x = y + z; // Assignment
5. Block Statements
{
int x = 10;
printf("%d", x);
}
This detailed breakdown covers all topics mentioned in your syllabus related to C Statements. Let me
know if you need further explanations or examples for any specific topic!
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 11/59
Arrays in C
1. What is an Array?
An array is a collection of elements of the same data type, stored in contiguous memory locations.
Arrays allow you to store multiple values in a single variable, which can be accessed using an index.
For example, an array can store multiple integers, floating-point numbers, or characters.
2. Array Declaration
Declaring an array means allocating memory to store elements of the same data type.
Syntax:
data_type array_name[size];
`data_type`:
Type of elements (e.g., `int`, `float`, `char`).
`array_name`:
Name of the array.
`size`: Number of elements in the array.
Example:
3. Array Initialization
Syntax:
Example:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 12/59
b. Without specifying size
int arr[] = {1, 2, 3}; // Size is determined by the number of values (3 here).
c. Partial initialization
If fewer values are provided, the remaining elements are initialized to zero.
Example:
array_name[index]
Example:
Updating an element:
5. Two-Dimensional Arrays
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 13/59
Syntax:
data_type array_name[rows][columns];
Example:
int matrix[3][2] = {
{1, 2},
{3, 4},
{5, 6}
};
Accessing elements:
6. Multi-Dimensional Arrays
data_type array_name[size1][size2][size3]...[sizeN];
Example:
int arr[2][2][2] = {
{{1, 2}, {3, 4}},
{{5, 6}, {7, 8}}
};
Accessing elements:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 14/59
7. Passing an Array Element to a Function
void printElement(int x) {
printf("%d", x);
}
int main() {
int arr[3] = {10, 20, 30};
printElement(arr[1]); // Output: 20
return 0;
}
Example:
int main() {
int arr[3] = {10, 20, 30};
printArray(arr, 3); // Output: 10 20 30
return 0;
}
int arr[5];
Examples of Usage
1D Array Example
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
2D Array Example
#include <stdio.h>
int main() {
int matrix[2][2] = {{1, 2}, {3, 4}};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
This note comprehensively covers arrays, their declaration, initialization, and usage. Let me know if you
need further clarification or examples!
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 16/59
Here’s a detailed explanation of Pointers in C:
Pointers in C
1. Definition
Key Terms:
Example:
int x = 10;
int *ptr = &x; // Pointer ptr stores the address of x
2. Pointer Variables
data_type *pointer_name;
Example:
Usage Example:
int a = 5;
int *p = &a; // Pointer p stores the address of a
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 17/59
printf("Address of a: %p\n", p); // Prints address of a
printf("Value of a: %d\n", *p); // Prints value of a (dereference)
3. Pointer Expressions
Pointers can be used in expressions to perform operations on memory addresses and values.
Pointer Assignment
Dereferencing
Using the `*` operator to access the value at the pointer's address.
Example:
int x = 10;
int *p = &x;
printf("%d", *p); // Output: 10
4. Pointer Arithmetic
Rules:
Operations:
1. Increment (`++`):
Moves to the next memory location.
Example:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 18/59
p++; // Now points to the second element
2. Decrement (`--`):
Moves to the previous memory location.
3. Addition/Subtraction:
Adds/subtracts an integer to/from the pointer.
4. Difference between pointers:
Calculates the number of elements between two pointers.
Example:
Arrays and pointers are closely related. The name of an array acts as a pointer to its first element.
Pointer to an Array
6. Initializing Pointers
Pointers should be initialized before use; otherwise, they may cause undefined behavior.
Null Pointers
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 19/59
Example:
int *p = NULL;
if (p == NULL) {
printf("Pointer is null");
}
Void Pointers
void *ptr;
int a = 10;
ptr = &a;
int main() {
int x = 10;
update(&x);
printf("%d", x); // Output: 20
return 0;
}
Functions can return pointers, often used with dynamically allocated memory.
Example:
int* allocateMemory() {
int *p = (int *)malloc(sizeof(int));
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 20/59
return p;
}
Dangling Pointers:
Occur when a pointer points to a memory location that has been freed.
Example:
Uninitialized Pointers:
Using a pointer without initializing it can cause undefined behavior.
Memory Leaks:
Forgetting to free dynamically allocated memory.
Pointer Arithmetic Risks:
Incorrect arithmetic can lead to accessing unintended memory.
Examples
#include <stdio.h>
int main() {
int x = 10;
int *p = &x;
printf("Address of x: %p\n", p);
printf("Value of x: %d\n", *p);
return 0;
}
#include <stdio.h>
int main() {
int arr[3] = {10, 20, 30};
int *p = arr;
for (int i = 0; i < 3; i++) {
printf("%d ", *(p + i)); // Output: 10 20 30
}
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 21/59
return 0;
}
#include <stdio.h>
void update(int *p) {
*p = 50; // Modify the value at the pointer
}
int main() {
int x = 10;
update(&x);
printf("Updated value: %d", x); // Output: 50
return 0;
}
This detailed explanation covers pointers, pointer variables, arithmetic, arrays, initialization, functions,
and common problems. Let me know if you need further clarifications!
Functions in C
1. General Form of a Function
A function is a block of code designed to perform a specific task. Functions allow code reusability and
better organization.
Syntax:
return_type function_name(parameters) {
// Body of the function
return value; // Optional, depending on return_type
}
Example:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 22/59
2. Elements of a Function
1. Function Name:
Identifies the function uniquely.
Example: `add`, `calculate`.
2. Return Type:
Specifies the type of value the function returns.
Example: `int`, `float`, `void` (no return value).
3. Parameters:
Inputs to the function.
Example: `(int a, int b)`.
4. Function Body:
Contains the logic of the function.
Enclosed in `{}`.
5. Return Statement:
Specifies the value the function sends back to the caller.
Example: `return a + b;`.
3. Function Categories
4. Types of Functions
Functions can be categorized based on the presence of parameters and return types:
void greet() {
printf("Hello, World!");
}
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 23/59
c
int getValue() {
return 42;
}
5. Function Arguments
a. Call by Value
void modify(int x) {
x = 50; // Changes only the local copy
}
int main() {
int a = 10;
modify(a);
printf("%d", a); // Output: 10
return 0;
}
b. Call by Reference
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 24/59
c
int main() {
int a = 10;
modify(&a);
printf("%d", a); // Output: 50
return 0;
}
6. Return Statement
return expression;
Example:
int main() {
int result = square(5); // result = 25
printf("Square: %d", result);
return 0;
}
7. Uses of Functions
1. Code Reusability:
Functions can be called multiple times in a program.
2. Modularity:
Breaks a program into smaller, manageable parts.
3. Ease of Debugging:
Errors can be localized within specific functions.
4. Better Readability:
Well-defined functions improve program clarity.
5. Reduced Redundancy:
Eliminates duplicate code.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 25/59
Strings in C
1. What Are Strings?
Example:
char str[] = "Hello"; // Stored as {'H', 'e', 'l', 'l', 'o', '\0'}
2. String I/O
a. Input (`scanf`)
char str[20];
scanf("%s", str);
char str[20];
fgets(str, 20, stdin);
b. Output (`printf`)
To print a string:
printf("%s", str);
3. String Manipulation
C provides several library functions for string manipulation. These functions are declared in the
`<string.h>` header.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 26/59
Common Functions:
4. Example Programs
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello";
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 27/59
char str2[] = "World";
return 0;
}
#include <stdio.h>
int main() {
char name[50];
printf("Enter your name: ");
fgets(name, 50, stdin); // Reads input with spaces
printf("Hello, %s", name);
return 0;
}
This explanation provides a complete overview of functions and strings, including their definitions,
types, examples, and common use cases. Let me know if you need further clarifications!
Library Functions in C
Library functions are pre-defined functions provided by C to perform common tasks such as
input/output, mathematical calculations, string manipulation, and file handling. They are declared in
specific header files and save time by eliminating the need to write custom implementations for these
tasks.
The `<string.h>` header contains a set of functions for string manipulation and operations. These
functions make it easy to handle character arrays (strings) in C.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 28/59
2. String Copy (`strcpy`)
Description: Reverses the given string (may not be available in all compilers).
Prototype: `char *strrev(char *str);`
Example:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 29/59
c
char str[10];
memset(str, '*', 9);
str[9] = '\0';
printf("Filled String: %s\n", str); // Output: *********
This explanation provides an overview of string library functions and their usage. Let me know if you
need further clarifications or examples!
Structures in C
1. Definition
A structure is a user-defined data type in C that allows grouping of variables of different data types
under a single name. It helps organize related data and is particularly useful in modeling real-world
entities.
Syntax:
struct StructureName {
data_type member1;
data_type member2;
...
};
Example:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 32/59
c
struct Student {
int id;
char name[50];
float marks;
};
To access structure members, use the dot (`.`) operator with a structure variable.
Example:
3. Structure Assignments
You can directly assign one structure variable to another, provided they are of the same type.
Example:
4. Array of Structures
An array of structures allows you to store multiple records of the same structure type.
Example:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 33/59
for (int i = 0; i < 3; i++) {
printf("ID: %d, Name: %s, Marks: %.2f\n", students[i].id, students[i].name, students[i].
marks);
}
5. Passing Structures
Passing by Value:
Passing by Reference:
6. Structure Pointers
Example:
struct Student s;
struct Student *ptr = &s;
7. Uses of Structures
1. Organizing Complex Data: Useful for handling data related to real-world entities.
2. Modeling Records: E.g., student records, employee details.
3. Memory Efficiency: Provides a compact way to store related data.
Unions in C
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 34/59
1. Definition
A union is a user-defined data type similar to a structure, but all members share the same memory
location. The size of the union is determined by its largest member.
Syntax:
union UnionName {
data_type member1;
data_type member2;
...
};
Example:
union Data {
int i;
float f;
char str[20];
};
Access All members can be accessed at once. Only one member can hold a value at a time.
Usage Used for storing multiple variables. Used for saving memory in special cases.
3. `typedef` Keyword
The `typedef` keyword in C is used to create a new name (alias) for an existing data type.
Example:
typedef struct {
int id;
char name[50];
} Student;
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 35/59
Files in C
1. Introduction to Streams and Files
Stream: Represents a flow of data (input or output) between a program and a file/device.
File: A data storage unit used to store information persistently.
2. Basics of Files
3. File Pointer
A file pointer is a pointer to a structure of type `FILE`. It is used to access and manage file operations.
Declaration:
FILE *filePtr;
Opening a File
Modes:
`"r"`: Read
`"w"`: Write
`"a"`: Append
`"r+"`: Read and Write
`"w+"`: Write and Read (truncates existing file)
`"a+"`: Append and Read
Example:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 36/59
printf("Error opening file!");
}
Closing a File
fclose(filePtr);
6. File Functions
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 37/59
3. `fwrite`: Writes binary data to a file.
rewind(filePtr);
This detailed explanation covers Structures, Unions, and Files with examples and usage scenarios. Let
me know if you need further clarifications!
Introduction to Programming
Definition
Programming is the process of creating a set of instructions for a computer to execute. These
instructions, known as code, are written in a programming language to solve problems or perform tasks
efficiently.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 38/59
1. Algorithm
An algorithm is a step-by-step procedure or formula for solving a problem. It provides a clear sequence
of actions to achieve a desired outcome.
1. Start.
2. Read two numbers (`a` and `b`).
3. Compute their sum: `sum = a + b`.
4. Display `sum`.
5. Stop.
2. Flowchart
A flowchart is a visual representation of an algorithm. It uses symbols to represent different steps and
their flow.
Symbols:
Oval: Start/End.
Rectangle: Process/Instruction.
Diamond: Decision.
Arrow: Flow of control.
css
Introduction to C Programming
2. Character Set
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 39/59
The character set includes:
Identifiers: Names for variables, functions, etc. Must start with a letter or underscore and cannot
match keywords.
Example: `sum`, `_temp`.
Keywords: Reserved words in C (e.g., `int`, `return`, `if`).
4. Data Types
6. Operators
7. Constants
Integer: `10`
Float: `3.14`
Character: `'A'`
String: `"Hello"`
9. Storage Classes
11. C Preprocessor
Example:
#define PI 3.14
#include <stdio.h>
1. Preprocessor Directives
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 41/59
2. Main Function (`main()`)
3. Declarations and Statements
Example:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
int num;
scanf("%d", &num);
This comprehensive overview introduces the core concepts of programming and C, providing a
foundation for deeper exploration. Let me know if you need further details!
Here’s a detailed explanation of Introduction to Computer Hardware and Software and Data &
Number Systems:
1. Computer Generations
First Generation (1940-1956): Vacuum tube-based; used machine language; bulky and slow.
Second Generation (1956-1963): Transistor-based; used assembly language.
Third Generation (1964-1971): Integrated Circuits (IC); introduced high-level programming
languages.
Fourth Generation (1971-Present): Microprocessors; personal computers became popular.
Fifth Generation (Present and Beyond): AI, robotics, and advanced computing technologies.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 42/59
2. Computer Types
Supercomputer: Extremely fast; used for complex calculations (e.g., weather forecasting).
Mainframe: Handles large-scale processing tasks for businesses.
Minicomputer: Mid-sized; used in research and small industries.
Microcomputer: Personal computers like desktops and laptops.
Embedded Systems: Specialized systems within devices like washing machines, cars, etc.
4. Memory Types
Primary Memory:
RAM (Random Access Memory): Temporary storage; volatile.
ROM (Read-Only Memory): Permanent storage for boot processes.
Secondary Memory:
Hard Drives (HDD), Solid-State Drives (SSD): Persistent storage.
Optical Disks: CD/DVD.
USB Drives: Portable storage.
5. Input/Output Devices
2. Number Systems
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 43/59
Binary (Base 2): Digits are 0 and 1.
Used in computers because of two-state (on/off) circuits.
Octal (Base 8): Digits are 0-7.
Compact representation of binary numbers.
Hexadecimal (Base 16): Digits are 0-9 and A-F.
Used in memory addressing.
BCD (Binary-Coded Decimal): Represents decimal digits using 4 bits.
ASCII (American Standard Code for Information Interchange): Represents text using numbers.
3. Conversions
Binary to Decimal: Multiply each binary digit by 2n (where n is the position) and sum.
Example: 10102 = 1 ⋅ 23 + 0 ⋅ 22 + 1 ⋅ 21 + 0 ⋅ 20 = 1010 .
Binary to Octal: Group binary digits in threes (right to left) and convert each group.
Example: 1010112 = 538 .
Binary to Hexadecimal: Group binary digits in fours (right to left) and convert each group.
Example: 1010112 = 2B16 .
4. Complements
This detailed explanation provides foundational knowledge of computer hardware, software, and
number systems. Let me know if you’d like any specific topic explained further!
The execution of a C program involves multiple steps, from writing the code to producing the final
output. Here's a detailed description of each step:
What happens: The programmer writes the C code in a text editor (e.g., Visual Studio Code,
Notepad++).
File: The file is saved with a `.c` extension (e.g., `program.c`).
2. Preprocessing
What happens: The preprocessor handles preprocessor directives (`#include`, `#define`, etc.).
Tasks:
Includes header files (e.g., `stdio.h`).
Expands macros (`#define` constants or macros).
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 44/59
Removes comments.
Tool: C Preprocessor.
Output: A modified version of the source code, usually stored as a temporary file.
3. Compilation
What happens: The compiler translates the preprocessed code into assembly language.
Tasks:
Checks for syntax errors.
Optimizes the code.
Generates assembly instructions.
Tool: C Compiler (e.g., GCC).
Output: An assembly file (e.g., `program.s`).
4. Assembly
What happens: The assembler converts assembly language into machine code (binary
instructions).
Tasks:
Converts assembly instructions into binary opcodes.
Generates object code.
Tool: Assembler.
Output: An object file (e.g., `program.o`).
5. Linking
What happens: The linker combines object files and library files into a single executable file.
Tasks:
Resolves function calls and references.
Links required standard and user-defined libraries.
Produces a fully executable file.
Tool: Linker.
Output: An executable file (e.g., `program.exe` or `a.out`).
6. Loading
What happens: The operating system loads the executable file into memory for execution.
Tasks:
Allocates memory for the program.
Sets up the runtime environment.
Transfers control to the program’s entry point (`main()` function).
7. Execution
What happens: The program runs on the CPU, executing the instructions.
Steps:
Fetch: The CPU fetches the next instruction.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 45/59
Decode: The instruction is interpreted.
Execute: The operation specified by the instruction is performed.
Output: Displays results or interacts with the user/system.
8. Termination
This step-by-step explanation outlines how a C program transitions from source code to an executable
program and its execution. Let me know if you need more details on any step!
For an examination on C programming and computer fundamentals, you can expect a range of
question types that test both theoretical understanding and practical application. Here are some
common types of questions you might encounter:
1. Theoretical Questions
Example Questions:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 46/59
2. Syntax and Code Understanding
These questions will test your knowledge of C syntax and ability to analyze code.
Example Questions:
int a = 5, b = 10;
printf("%d", a + b);
int *p = &x;
These are coding problems where you need to write a C program to solve a specific problem.
Example Questions:
Example Questions:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 47/59
These questions may ask you to implement or understand basic data structures in C.
Example Questions:
Define a structure to represent a book with fields like title, author, and price.
Write a program that stores a list of student records using an array of structures.
Explain how you can pass an array to a function in C.
You may be asked to find errors in a given C program and fix them.
Example Questions:
int main() {
int a = 10;
if (a = 10)
printf("Correct");
else
printf("Incorrect");
return 0;
}
7. Conversion Questions
Example Questions:
These will test your ability to design algorithms and represent them as flowcharts.
Example Questions:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 48/59
Otherwise, print "b is greater".
9. Function-related Questions
Example Questions:
Explain the difference between "call by value" and "call by reference" with examples.
What is a function prototype?
Write a function that calculates the area of a circle, given the radius.
You might also get questions on other topics like file handling, bitwise operators, or using libraries.
Example Questions:
1. Understand C syntax and concepts: Make sure you can write and understand basic C programs
and concepts like loops, functions, and arrays.
2. Practice coding: Practice solving problems on platforms like LeetCode, HackerRank, or CodeChef.
3. Review key concepts: Pay attention to key concepts such as pointers, functions, memory
management, and file handling.
4. Work on sample problems: Write programs to solve common problems, especially those involving
arrays, strings, structures, and loops.
Let me know if you'd like help preparing for any specific topics!
In C programming, pre-defined functions are part of standard libraries, which help programmers
perform common operations without writing code from scratch. These functions are grouped based on
their functionality, and they are available through the inclusion of specific header files.
Here’s a detailed list of some of the most commonly used predefined functions in C, categorized by
their functionalities:
`printf()`
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 49/59
c
printf("Hello, World!\n");
`scanf()`
int x;
scanf("%d", &x);
`getchar()`
char ch = getchar();
`putchar()`
putchar('A');
Use: Reads a string from the user until a newline character is encountered.
Example:
char str[50];
gets(str);
`puts()`
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 50/59
Example:
puts("Hello, World!");
`strlen()`
`strcpy()`
char dest[20];
strcpy(dest, "Hello");
`strcat()`
`strcmp()`
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 51/59
c
`strchr()`
`strstr()`
`malloc()`
`calloc()`
Use: Allocates memory for an array and initializes all elements to zero.
Example:
`realloc()`
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 52/59
Use: Resizes a previously allocated memory block.
Example:
`free()`
free(ptr);
`abs()`
`sqrt()`
`pow()`
Use: Returns the value of one number raised to the power of another.
Example:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 53/59
`sin()`, `cos()`, `tan()`
Use: Computes the sine, cosine, and tangent of an angle (in radians).
Example:
`ceil()`
Use: Returns the smallest integer greater than or equal to the given number.
Example:
`floor()`
Use: Returns the largest integer less than or equal to the given number.
Example:
`fopen()`
`fclose()`
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 54/59
c
fclose(file);
`fread()`
`fwrite()`
`fgetc()`
char ch = fgetc(file);
`fputc()`
fputc('A', file);
`fprintf()`
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 55/59
c
`fscanf()`
`exit()`
`system()`
system("dir");
`atoi()`
int x = atoi("123");
`atof()`
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 56/59
Use: Converts a string to a floating-point number.
Example:
double x = atof("123.45");
`rand()`
int x = rand();
`srand()`
`time()`
Use: Returns the current time in seconds since the epoch (Jan 1, 1970).
Example:
`clock()`
clock_t t = clock();
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 57/59
`difftime()`
This list covers the most commonly used predefined functions in C and their purposes. There are many
more functions available in C libraries, but these are the ones you'll most likely encounter in day-to-day
programming.
Let me know if you'd like further clarification on any of these functions!
No, the `range()` function is not available in C. The `range()` function is typically found in Python,
where it is used to generate a sequence of numbers.
In C, if you need to generate a sequence of numbers (for example, for loops), you would do so manually
using loops. Here's an example of how you can achieve a similar result in C:
#include <stdio.h>
int main() {
int start = 1, end = 10;
return 0;
}
In this case, the `for` loop is used to generate a range from 1 to 10. You would need to handle ranges
manually by specifying the start and end points of the sequence within a loop.
If you need a function that mimics Python's `range()`, you can write your own in C. Here's an example:
#include <stdio.h>
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 58/59
}
}
int main() {
range(1, 10); // Prints numbers from 1 to 10
return 0;
}
This `range()` function behaves similarly to Python's `range()`, but in C, you must define such
functionality yourself using loops.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 59/59