Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
🧠 Introduction
This chapter focuses on three foundational pillars in C
programming:
Together, these components help organize, simplify, and
optimize your C programs.
🔧 Part 1: Functions in C
What is a Function?
A function is a self-contained block of code designed
to perform a specific task. Functions allow code reuse, readability,
modularity, and easier debugging.
🔹 Syntax:
return_type
function_name(parameters) {
// function body
return value;
}
✅ Example:
int
add(int a, int b) {
return a + b;
}
🔹 Calling a Function:
int
sum = add(5, 10);
🔸 Types of Functions
Type |
Description |
Library Function |
Built-in (e.g.,
printf(), scanf()) |
User-defined Function |
Custom
functions created by the programmer |
🔹 Function Categories
Based on Argument & Return
Category |
Example |
No argument, no
return |
void greet() |
Argument, no return |
void show(int
a) |
No argument, return |
int getVal() |
Argument and return |
int sum(int,
int) |
✅ Example: Argument + Return
int
square(int n) {
return n * n;
}
🔹 Function Declaration
(Prototype)
Defines the function signature before main():
int
add(int, int); // declaration
🔹 Recursive Functions
A function that calls itself.
int
factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
📊 Table: Function Concept
Summary
Feature |
Description |
void |
No return value |
Function prototype |
Declares function
signature |
Return statement |
Sends back output to
caller |
Recursion |
Function
calls itself |
📦 Part 2: Arrays in C
What is an Array?
An array is a collection of homogeneous data
elements stored in contiguous memory and accessed using indices.
🔹 Syntax:
data_type
array_name[size];
✅ Example:
int
marks[5] = {90, 85, 78, 92, 88};
🔹 Accessing Elements
printf("%d",
marks[2]); // prints 78
🔹 Declaring &
Initializing Arrays
int
arr[3]; // Declaration only
int
arr[3] = {1, 2, 3}; // Declaration + Initialization
🔹 Iterating Over Arrays
for
(int i = 0; i < 5; i++) {
printf("%d\n", marks[i]);
}
🔹 Multi-Dimensional
Arrays
Arrays with more than one index.
int
matrix[2][3] = {{1,2,3},{4,5,6}};
📊 Table: Array Concept
Summary
Type |
Example |
1D Array |
int a[5]; |
2D Array |
int a[3][4]; |
Character Array |
char name[10]; |
Access Element |
a[2], a[1][2] |
✨ Part 3: Strings in C
What is a String?
In C, a string is an array of characters ending with
a null character \0.
🔹 Declaring Strings
char
name[10] = "Alice";
OR
char
name[] = {'A','l','i','c','e','\0'};
🔹 Reading and Printing
Strings
✅ Using scanf and printf
char
name[20];
scanf("%s",
name);
printf("Hello,
%s", name);
✅ Using gets() and puts()
char
sentence[100];
gets(sentence); // Reads full line
puts(sentence); // Prints full line
⚠️ gets() is unsafe and not
recommended; use fgets() instead.
🔹 String Functions in
<string.h>
Function |
Use Case |
strlen(s) |
Returns length of
string |
strcpy(s1,s2) |
Copies s2
into s1 |
strcmp(s1,s2) |
Compares strings (0 if
equal) |
strcat(s1,s2) |
Appends s2 to
s1 |
strrev(s) |
Reverses string (not
standard) |
✅ Example:
#include
<string.h>
char
str1[20] = "Hello";
char
str2[20] = "World";
strcat(str1,
str2); // str1 = HelloWorld
🧪 Sample Program:
Function + Array + String
#include
<stdio.h>
#include
<string.h>
void
printReverse(char str[]) {
int len = strlen(str);
for (int i = len - 1; i >= 0; i--) {
printf("%c", str[i]);
}
}
int
main() {
char name[50];
printf("Enter your name: ");
scanf("%s", name);
printf("Reversed name: ");
printReverse(name);
return 0;
}
🧠 Real-World Applications
Component |
Use Case |
Function |
Reusability (login
checks, calculations, etc.) |
Array |
Storing
sensor readings, exam scores |
String |
Working with names,
inputs, messages, emails |
📋 Chapter Summary Table
Concept |
Description |
Function |
Reusable block with
arguments and return value |
Array |
Homogeneous
group of elements |
String |
Character array ending
with \0 |
Loop + Array |
Efficient
iteration over data |
A: Absolutely. C is widely used in systems programming, embedded systems, and performance-critical applications.
A: Just basic computer literacy. No prior programming knowledge is required.
A: Code::Blocks or VS Code with a C plugin is great for beginners.
A: C is a compiled language. It uses compilers like GCC or Clang.
A: With consistent practice, 4–8 weeks is sufficient to grasp core concepts.
A: Pointers and manual memory management can be tricky for beginners.
A: C is not typically used for web apps, but it can handle back-end processes or be integrated via CGI.
A: Each has its use. C is great for low-level control and speed, but C++ and Python offer more abstraction and ease of use.
A: Use a terminal/IDE to compile with gcc filename.c -o output and run with ./output.
A: GitHub, HackerRank, and open-source forums are great places to find beginner to advanced C projects.
Please log in to access this content. You will be redirected to the login page shortly.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Comments(0)