C Language Tutorial for Beginners (With Notes) | CodeWithHarry
C Language Tutorial for Beginners (With Notes) | CodeWithHarry
PDF download links for all the chapters are at the end of the
</>page
CodeWithHarry Menu Login
Chapter 0: Introduction
What is Programming?
Computer programming is a medium for us to communicate with
computers, just like we use Hindi or English to communicate with each
other. Programming is a way for us to deliver our instructions to the
computer.
What is C?
C is a programming language. C is one of the oldest and finest
programming languages. C was developed by Dennis Ritchie in 1972.
Uses of C
C is a language that is used to program a wide variety of systems. Some
of the uses of C are as follows:
1. Major parts of Windows, Linux, and other operating systems are written
in C.
2. C is used to write driver programs for devices like Tablets, Printers, etc.
3. C language is used to program embedded systems where programs
need to run faster in limited memory.
4. C is used to develop games, an area where latency is very important,
i.e., a computer has to react quickly to user input.
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 2 of 53
:
b is
b = 4.7 assigned
“4.7”
c is
c = 'A'
assigned “A”
Rules for naming variables in c:
1. The first character must be an alphabet or underscore(_).
2. No commas or blanks are allowed.
3. No special symbol other than underscore is allowed
4. Variable names are case sensitive
Constants
An entity whose value doesn’t change is called a constant.
Types of constant
Primarily there are 3 types of constant:
1. Integer
-1,6,7,9
Constant
2. Real
-322.1,2.5,7.0
Constant
3. Character ‘a’,’$’,’@’(must be enclosed within
Constant single inverted commas)
Keywords
These are reserved words whose meaning is already known to the
compiler. There are 32 keywords available in c:
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 3 of 53
:
continue signed for void
do static if while
Our first C program
#include<stdio.h>
int main() {
File : first.c
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 4 of 53
:
Comments in a C program are not executed and ignored.
Compilation and execution
printf(“This is %d”,i);
// %d for integers
// %c for characters
Types of variables
Integer
int a=3;
variables
Real int a=7.7 (wrong as 7.7 is
variables real) ; float a=7.7;
Character
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 5 of 53
:
variables char a=’B’;
& is the “address of” operator, and it means that the supplied value should
be copied to the address which is indicated by variable i.
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 6 of 53
:
Type of declaration instruction:
int a;
float b;
other variations:
int a,b,c,d;
Arithmetic Instructions
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 7 of 53
:
Note:
1.No operator is assumed to be present
int i=ab ( Invalid )
int i=a*b ( valid )
2.There is no operator to perform exponentiation in c however we can use
pow(x,y) from <math.h>(More later).
Type conversion
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 8 of 53
:
float and float ==> float
NOTE:
int a = 3.5; //In this case, 3.5 (float) will be denoted to a 3 (in
Operator associativity
When operators of equal priority are present in an expression, the tie is
taken care of by associativity
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 9 of 53
:
x * y / z => (x * y) / z
x / y * z => (x / y) * z
Control instructions
Determines the flow of control in a program.
Four types of control instruction in C are:
1. Sequence Control Instruction
2. Decision Control Instruction
3. Loop Control Instruction
4. Case-Control Instruction
1. int a; b=a;
2. int v=3^3;
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 10 of 53
:
Sometimes we want to watch comedy videos on youtube if the day is
Sunday. Sometimes we order junk food if it is our friend's birthday in the
hostel. You might want to buy an umbrella if it's raining and you have the
money. You order the meal if dal or your favorite bhindi is listed on the
menu.
All these are decisions that depend on conditions being met.
In ‘C’ language, too, we must be able to execute instructions on a
condition(s) being met.
Decision-making instructions in C
If-else statement
Switch statement
If-else statement
The syntax of an if-else statement in c looks like this:
if ( condition to be checked) {
Statements-if-condition-true ;
else{
statements-if-condition-false ;
Code Example
int a=23;
if (a>18){
printf(“you can drive\n”);
}
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 11 of 53
:
Relational Operators in C
Relational operators are used to evaluate conditions (true or false) inside
the if statements. Some examples of relational operators are:
== equals to
greater than or
>=
equal to
> greater than
< less than
less than or
<=
equal to
!= not equal to
Important Note: '=' is used for an assignment, whereas '==' is used for an
equality check.
The condition can be any valid expression. In C, a non-zero value is
considered to be true.
Logical Operators
&&, ||, and ! are the three logical operators in C. These are read as “and,””
or,” and “not.” They are used to provide logic to our c programs.
Use of logical operators:
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 12 of 53
:
!(3==3) evaluates to false
if {
// statements ;
else if { //statements;
else { //statements;
Using if-else if-else reduces indents. The last “else” is optional. Also, there
can be any number of “else if.”
Last else is executed only if all conditions fail.
Operator Precedence
Priority Operator
1st !
2nd *,/,%
3rd +,-
4th <>,<=,>=
5th ==,!=
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 13 of 53
:
6th &&
7th ||
8th =
Conditional operators
A shorthand “if-else” can be written using conditional or ternary operators.
Switch(integer-expression)
Case c1:
Code;
Case c2: //
Code;
Case c3:
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 14 of 53
:
90-
A
100
80-
B
90
70-
C
80
60-
D
70
<70 F
Important notes
We can use switch case statements even by writing in any order of our
choice
Char values are allowed as they can be easily evaluated to an integer
A switch can occur within another, but in practice, this is rarely done
int a=10;
if(a=11)
printf(“I am 11”);
else
printf(“I am not 11”);
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 15 of 53
:
Slab
2.5L-5.0L 5%
5.0L-10.0L 20%
Above
30%
10.0L
Note that there is no tax below 2.5L. Take income amount as an input from
the user.
4. Write a program to find whether a year entered by the user is a leap
year or not. Take the year as input from the user.
5. Write a program to determine whether a character entered by the user
is lowercase or not.
6. Write a program to find the greatest of four numbers entered by the
user.
While(condition is true) {
// Code
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 16 of 53
:
}
An example:
int i=0;
while (i<10){
printf(“The value of i is %d”,i); i++;
}
Note:
If the condition never becomes false, the while loop keeps getting
executed. Such a loop is known as an infinite loop.
Quick Quiz: Write a program to print natural numbers from 10 to 20 when
the initial loop counter i is initialized to 0.
The loop counter need not be int, it can be a float as well.
i++ (i is increased by 1)
i-- (i is decreased by 1)
printf(“—i=%d”,--i);
printf(“i--=%d”,i--);
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 17 of 53
:
do {
//code;
//code;
}while(condition)
For Loop
The syntax of for loop looks like this:
//code;
//code;
for(i=0;i<3;i++)
{
printf(“%d”,i);
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 18 of 53
:
printf(“\n”);
}
Output:
0
1
2
Quick Quiz: Write a program to print first n natural numbers using for loop.
A case of Decrementing for loop
for(i=5; i; i--)
printf(“%d\n”,i);
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 19 of 53
:
break;
}
}
int skip=5;
int i=0;
while(i<10){
if(i != skip)
continue;
else
printf(%d”,i);
}
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 20 of 53
:
At most once
4. What can be done using one type of loop can also be done using the
other two types of loops – True or False.
5. Write a program to sum the first ten natural numbers using a while
loop.
6. Write a program to implement program 5 using for and do-while loop.
7. Write a program to calculate the sum of the numbers occurring in the
multiplication table of 8.(Consider 8x1 to 8x10)
8. Write a program to calculate the factorial of a given number using for
loop.
9. Repeat 8 using a while loop.
10. Write a program to check whether a given number is prime or not
using loops.
11. Implement 10 using other types of loops.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 21 of 53
:
int main(){
int number, guess, nguesses=1;
srand(time(0));
number = rand()%100 + 1; // Generates a random number between 1
// printf("The number is %d\n", number);
// Keep running the loop until the number is guessed
do{
printf("Guess the number between 1 to 100\n");
scanf("%d", &guess);
if(guess>number){
#include<stdio.h>
void display(); // Function prototype
int main(){
int a;
display(); // Function call
return(0);
}
Function prototype:
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 22 of 53
:
Function prototype is a way to tell the compiler about the function we are
going to define in the program.
Here void indicates that the function returns nothing.
Function call:
Function call is a way to tell the compiler to execute the function body at
the time the call is made.
Note that the program execution starts from the main function in the
sequence the instructions are written.
Function definition:
This part contains the exact set of instructions that are executed during the
function call. When a function is called from main(), the main function falls
asleep and gets temporarily suspended. During this time, the control goes
to the function being called when the function body is done executing
main() resumes.
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 23 of 53
:
To keep track of what we are doing in a program
To test and check logic independently
Passing values to functions:
We can pass values to a function and can get a value in return from a
function
int sum(int a, int b)
The above prototype means that sum is a function which takes values a(of
type int) and b(of type int) and returns a value of type int
Function definition of sum can be:
c=a+b;
return c;
Now we can call sum(2,3) [here 2 and 3 are arguments]; from main to get
5 in return.
Note:
1. Parameters are the values or variable placeholders in the function
definition. Ex: a & b
2. Arguments are the actual values passed to the function to make a call.
Ex: 2 & 3
3. A function can return only one value at a time.
4. If the passed variable is changed inside the function, the function call
doesn’t change the value in the calling function.
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 24 of 53
:
int change(int a){
return 0;
change is a function which changes a to 77. No, if we call it from main like
this.
int b=22;
Recursion
A function defined in C can call itself. This is called recursion.
A function calling itself is also called a recursive function.
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 25 of 53
:
Example of Recursion:
A very good example of recursion is factorial
factorial(n) = 1x 2 x 3...........x n
factorial(n)= 1 x 2 x 3...........n-1 x n
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 26 of 53
:
Important Notes:
1. Recursion is sometimes the most direct way to code an algorithm
2. The condition which doesn’t call the function any further in a recursive
function is called the base condition.
3. Sometimes, due to a mistake made by the programmer, a recursive
function can keep running without returning, resulting in a memory error.
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 27 of 53
:
lines):
***
*****
Chapter 6 - Pointers
A pointer is a variable that stores the address of another variable.
j is a pointer
j points to i.
The "address of" (&) operator
The address of operator is used to obtain the address of a given variable
If you refer to the diagrams above
&i=> 87994
&j=>87998
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 28 of 53
:
The value at address or * operator is used to obtain the value present at a
given memory address. It is denoted by *
*(&i) = 72
*(&j) = 87994
Just like pointer type integer, we also have pointers to char, float, etc.
#include<stdio.h>
int main()
{
int i=8;
int *j;
j=&i;
printf(“Add i=%u\n”,&i);
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 29 of 53
:
printf(“Add i=%u\n”,j);
printf(“Add j=%u\n”,&j);
printf(“Value i=%d\n”,i);
printf(“Value i=%d\n”,*(&i));
printf(“Value i=%d\n”,*j);
return 0;
}
Output:
Add i=87994
Add i=87994
Add j=87998
Value i=8
Value i=8
Value i=8
This program sums it all. If you understand it, you have got the idea of
pointers.
Pointers to a pointer:
Just like j is pointing to i or storing the address of i, we can have another
variable, k which can store the address of j. What will be the type of k?
int **k;
k= &j;
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 30 of 53
:
We can even go further one level and create a variable l of type int*** to
store the address of k. We mostly use int* and int** sometimes in real-
world programs.
If sum is defined as sum(int a, int b), the values 3 and 4 are copied to a
and b. Now even if we change a and b, nothing happens to the variables x
and y.
This is call by value.
In C, we usually make a call by value.
Call by reference:
Here the address of the variable is passed to the function as arguments.
Now since the addresses are passed to the function, the function can now
modify the value of a variable in calling function using * and & operators.
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 31 of 53
:
Example:
This function is capable of swapping the values passed to it. If a=3 and
b=4 before a call to swap(a,b), a=4 and b=3 after calling swap.
int main()
{
int a=3; // a is 3 and b is 4
int b=4;
swap(a,b)
return 0; // now a is 4 and b is 3
}
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 32 of 53
:
5. Write a program to print the value of a variable i by using the "pointer to
pointer" type of variable.
6. Try problem 3 using call by value and verify that it doesn’t change the
value of the said variable.
Chapter 7 - Arrays
An array is a collection of similar elements.
One variable => Capable of storing multiple values
Syntax,
The syntax of declaring an array looks like this:
marks[0]=33;
marks[1]=12;
Note: It is very important to note that the array index starts with 0.
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 33 of 53
:
Accessing Elements
Elements of an array can be accessed using:
float marks[]={33,40}
Arrays in memory
Consider this array:
This will reserve 4x3=12 bytes in memory. 4 bytes for each integer.
1 2 3
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 34 of 53
:
62302 62306 62310 {Array in Memory}
Pointer Arithmetic
A pointer can be incremented to point to the next memory location of that
type.
int i = 32;
int *a = &i; ==> a =87994 (address = 87994)
a++; ==> now a = 87998
char a = 'A';
char *b = &a; ==> b = 87994
b++; ==> now b = 87995
float i = 1.7;
float*a = &i; ==> Address of i or a = 87994
a++; ==> Now a = 87998
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 35 of 53
:
If ptr points to index 0, ptr++ will point to index 1 & so on...
This way we can have an integer pointer pointing to the first element of the
array like this:
ptr++;
or
Multidimensional arrays
An array can be of 2 dimension / 3 dimension / n dimensions.
A 2-dimensional array can be defined as:
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 36 of 53
:
int arr [3][2] ={
{1,4}
{7,9}
{11;22}
};
We can access the elements of this array as arr [0] [0], arr [0] [1] & so on...
At arr [0] [0] value would be 1 and at arr [0] [1] value would be 4.
2-D arrays in Memory
A 2-d array like a 1-d array is stored in contiguous memory blocks like
this:
Quick Quiz: Create a 2-d array by taking input from the user. Write a
display function to print the content of this 2-d array on the screen.
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 37 of 53
:
Depends
3. Write a program to create an array of 10 integers and store a
multiplication table of 5 in it.
4. Repeat problem 3 for a general input provided by the user using scanf()
5. Write a program containing a function that reverses the array passed to
it.
6. Write a program containing functions that counts the number of positive
integers in an array.
7. Create an array of size 3x10 containing multiplication tables of the
numbers 2,7 and 9, respectively.
8. Repeat problem 7 for a custom input given by the user.
9. Create a three-dimensional array and print the address of its elements
in increasing order.
Chapter 8 - Strings
A string is a 1-d character array terminated by a null(‘\0’) => {this is null
character}
The null character is used to denote string termination, characters are
stored in contiguous memory locations.
Initializing Strings
Since string is an array of characters, it can be initialized as follows:
char s[]={‘H’,’A’,’R’,’R’,’Y’,’\0’}
Strings in memory
A string is sorted just like an array in the memory as shown below
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 38 of 53
:
Quick Quiz: Create a string using " " and print its content using a loop.
Printing Strings
A string can be printed character by character using printf and %c.
But there is another convenient way to print strings in C.
char st[50];
scanf(“%s”,&st);
scanf automatically adds the null character when the enter key is pressed.
Note:
1. The string should be short enough to fit into the array.
2. scanf cannot be used to input multi-word strings with spaces.
gets() and puts()
gets() is a function that can be used to receive a multi-word string.
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 39 of 53
:
char st[30];
This tells the compilers to store the string in the memory and the assigned
address is stored in a char pointer.
Note:
1. Once a string is defined using char st[]= ”harry”, it cannot be initialized
to something else.
2. A string defined using pointers can be reinitialized. => ptr=”rohan”;
int length=strlen(st);
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 40 of 53
:
char target[30];
Target string should have enough capacity to store the source string.
strcat() - This function is used to concatenate two strings
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 41 of 53
:
4. Write a function slice() to slice a string. It should change the original
string such that it is now the sliced strings. Take m and n as the start
and ending position for slice.
5. Write your own version of strcpy function from <string.h>
6. Write a program to encrypt a string by adding 1 to the ASCII value of its
characters.
7. Write a program to decrypt the string encrypted using the encrypt
function in problem 6.
8. Write a program to count the occurrence of a given character in a
string.
9. Write a program to check whether a given character is present in a
string or not.
Chapter 9 - Structures
Arrays and Strings => Similar data (int, float, char)
Structures can hold => dissimilar data
Syntax for creating Structures
A C Structure can be created as follows:
struct employee{
float salary;
char name[10];
}; • semicolon is impo
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 42 of 53
:
e1.code=100;
e1.salary=71.22;
facebook[0].code=100;
facebook[1].code=101;
..........and so on.
Initializing structures
Structures can also be initialized as follows:
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 43 of 53
:
Structures in memory
Structures are stored in contiguous memory locations for the structures e1
of type struct employee, memory layout looks like this:
ptr=&e1;
printf(“%d”,*(ptr).code);
Arrow operator
Instead of writing *(ptr).code, we can use an arrow operator to access
structure properties as follows
*(ptr).code or ptr->code
Here -> is known as an arrow operator.
Passing Structure to a function
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 44 of 53
:
A structure can be passed to a function just like any other data type.
void show(struct employee e); =>Function prototype
Quick Quiz: Complete this show function to display the content of the
employee.
Typedef keyword
We can use the typedef keyword to create an alias name for data types in
c.
typedef is more commonly used with structures.
struct complex{
float real; // struct complex c1,c2; for def
float img;
};
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 45 of 53
:
fields did you use and why?
18. Write a structure capable of storing date. Write a function to compare
those dates.
19. Solve problem 9 for time using typedef keyword.
File pointer
The “File” is a structure that needs to be created for opening the file. A file
pointer is a pointer to this structure of the file.
File pointer is needed for communication between the file and the
program.
A file pointer can be created as follows:
FILE *ptr;
ptr=fopen(“filename.ext”,”mode”);
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 46 of 53
:
Following modes are primarily used in c File I/O
Types of Files
There are two types of files:
1. Text files(.txt, .c)
2. Binary files(.jpg, .dat)
Reading a file
A file can be opened for reading as follows:
FILE *ptr;
ptr=fopen(“Harry.txt”,”r”);
int num;
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 47 of 53
:
associated resources could be freed.
Writing to a file
We can write to a file in a very similar manner as we read the file
FILE *fptr;
fptr=fopen(“Harry.txt”,”w”);
int num=432;
fprintf(fptr,”%d”,num);
fclose(fptr);
while(1){
ch=fgetc(ptr); // When all the content of a file has been read,
if(ch==EOF){
break;
}
//code
}
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 48 of 53
:
3. Write a program to read a text file character by character and write its
content twice in a separate file.
4. Take name and salary of two employees as input from the user and
write them to a text file in the following format:
name1, 3300
name2, 7700
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 49 of 53
:
}
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 50 of 53
:
Syntax:
If the space is not sufficient, memory allocation fails and a NULL pointer is
returned.
ptr = realloc(ptr,newSize);
ptr = realloc(ptr, 3* sizeof(int)) //ptr now points to this new blo
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 51 of 53
:
3. Solve problem 1 using calloc().
4. Create an array dynamically capable of storing 5 integers. Now use
realloc so that it can now store 10 integers.
5. Create an array of the multiplication table of 7 up to 10 (7x10=70). Use
realloc to make it store 15 numbers(from 7x1 to 7x15).
6. Attempt problem 4 using calloc().
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 52 of 53
:
Click here to download Chapter 8 - Practice Set.
Click here to download the notes For Chapter 9 - Structures.
Click here to download Chapter 9 - Practice Set.
Click here to download the notes For Chapter 10 - File I/O
Click here to download the notes For Chapter 10 - Practice Set.
Click here to download the notes For Project 2 - Snake, Water, Gun.
Click here to download the notes For Chapter 11 - Dynamic Memory
Allocation.
Click here to download the notes For Chapter 11 - Practice Set
CodeWithHarry
https://www.codewithharry.com/videos/c-tutorial-in-hindi-with-notes/ 17/11/24, 10 27 PM
Page 53 of 53
: