c programmig
***********
1. how many ways we can include a header file
1. <>
2. ""
Ex:
#include <stdio.h> //The compiler will search for this file in special directories (software installed path)
#include "stdio.h" //The compiler will search for this file in current folder, if not found then search in
special directories
2. What is a header file
It is also called libraries provided by compiler
stdio.h -> printf,scanf,FILE,....
conio.h -> getch()
stdlib.h -> malloc,calloc,realloc,free,....
string.h -> strcpy,strlen,strrev,strtok,strcat,...
math.h -> Mathematical related functions
3. C program execution starts from ?
main
4. What is the syntax of main function
int main()
int main(int argc,char *argv[])
5. what is the use of return 0 in main function
return 0; //informing the compiler that the program executed successfully
************
Types of operators in C language:
1. Arithmetic operators
2. relational operators
3. logical operators
4. bit-wise operators
5. increment and decrement operator
6. conditional operator
7. assignment operators
8. Hierarchy of operators
1. Arithmetic operators
2. relational operators
>
>=
<
<=
==
!= (not equal to)
3. logical operators
&& (logical and)
|| (logical or)
! (logical not)
4.bit-wise operator:
| (bitwise or)
& (bitwise and)
^ (bitwise xor)
>> (right shift)
<< (left shift)
5. increment and decrement operator
i=i+1
i++ (post increment)
++i (pre increment)
i-- (post decrement)
--i (pre decrement)
6.conditional operator (?)
exp1 ? exp 2: exp 3
a>b ? printf("a is big") : printf("b is big")
7.assignment operators
a=a+b
a+=b; (a=a+b)
a-=b; (a=a-b)
a*=b; (a=a*b)
a%=b; (a=a%b)
8. Hierarchy of operators (BODMAS)
5. Program to find greatest of three numbers
6. C Program to check if number is odd or even
7. C Program to find out the ASCII value of a character
8. C Program to find the size of int, float, double and char
9. C Program to check whether an alphabet is vowel or consonant
Control Statements:
-> if condition
-> switch case
if condition:
-> simple if
-> if ...else
-> nested if
-> Simple if:
if (i==10)
printf("I value is 10");
}
-> if ..else:
if (i==10)
printf("I value is 10");
else{
printf(" I value is not 10");
-> nested if
if (i<10)
if (i>5)
printf("I is greater than 5");
else
printf("I is not grear than 5");
else
printf("I value is grear than 10);
}
10. Write a C program explaining the use of relational operators
11. Write a C program explaining the use of:
-> simple if
-> if ...else
-> nested if
switch:
switch case
syntax:
switch (expr)
case 1:
break;
case 2:
break;
case 3:
break;
default:
nested switch:
int main()
int i,j;
printf("\n Enter the value of i,j");
scanf("%d%d",&i,&j);
switch(i)
case 1:
switch (j)
case 1:
printf("I am in inside switch case 1");
break;
case 2:
printf("I am in inside switch case 2");
break;
default:
printf("I am in default case");
break;
break;
case 2:
break;
default:
printf("Default");
return 0;
}
14.Write a C program to explain the use of switch case also
also explain the use of break
15. Write a C program to explain the use of switch case using a character also
loops:
repetetion of a logic
1. initial value
2. condition
3. increment/decrement (i=i+1)
ex:
print the values from 1 to 100
1,2,3,4,5
2,4,6,8,10,.....50
initial value: 2
condition : 50
increment : i=i+2
for
while
do ...while
1. for loop
for(initial value;condition;increment/decrement)
print the values from 1 to 100
for(i=1;i<100;i=i+1)
{
printf("\n %d",i);
Syntax 1. for loop
for(initial value;condition;increment/decrement)
print the values from 1 to 100
for(i=1;i<100;i=i+1)
printf("\n %d",i);
Syntax 2:
initial value
for(;condition;increment/decrement)
Syntax 3:
for(initial value;condition;)
increment/decrement;
}
Syntax 4:
initial value;
for(;condition;)
increment/decrement;
the loop will be executing infinite times (no limit), it will be keep on executing
While:
initial value
condition
increment/decrement
Syntax:
initial value;
while(condition)
increment;
========
do -- while
initial value;
condition
increment/decrement;
syntax
do
i=i+1;
while(condition);
control statements:
1. if conditions,
2. switch case
3. break; -> it will cut the loop execution
4. continue; -> it will skip the execution of the loop
5. goto
17.Write a C program to do following activities:
18. print the numbers from 100 to 200
19. print the numbers from 100 to 50
20. print the even numbers from 2 to 100
21. print the multiples of 5 from 40 to 80
22. print the multiples of 8 from 80 to 160
23. to show the different ways of using for loop syntax.
24.Write a C program to do following activities using while loop:
-> print the numbers from 100 to 200
-> print the numbers from 100 to 50
-> print the even numbers from 2 to 100
-> print the multiples of 5 from 40 to 80
-> print the multiples of 8 from 80 to 160
25. Write a C program to do following activities using do ..while loop:
-> print the numbers from 100 to 200
-> print the numbers from 100 to 50
-> print the even numbers from 2 to 100
There are 4 storage classes in C language:
1. auto
2. static
3. extern
4. register
1. auto:
By default all the variables are auto.
-> life time (with in a block/function)
-> what is the default value (garbage)
-> what is the scope (with in the block/function)
-> when it will die (once you come out of the block/function
6. Write a C program explaining the use of Auto Storage Class
2. Register:
We are telling the compiler that store this variable in CPU register memory.
Frequently used varibales should be store in CPU Register.
Syntax:
register int i;
-> life time (with in a block/function)
-> what is the default value (garbage)
-> what is the scope (with in the block/function)
-> when it will die (once you come out of the block/function
7. Write a C program explaining the use of register Storage Class
3.Static:
it will store the previous value.
-> life time (with in a program)
-> what is the default value (0)
-> what is the scope (with in the block/function)
-> when it will die (once you come out of the program)
int main()
static int i=1;
if(i>5)
{
exit(0);
else
printf("\n The value is %d",i);
i=i+1;
main();
return 0;
void display()
static int i=1;
printf("
The i value is %d",i);
i=i+1;
int main()
display();
display();
display();
return 0;
}
6. Write a C program explaining the use of Auto Storage Class
7. Write a C program explaining the use of register Storage Class
8. Write a C program explaining the use of static Storage Class
9. Write a C program explaining the use of extern Storage Class
Extern:
Step 1:
open a project - console application -> main.c
Step 2:
right click on the project -> add files -> right click on main.c -> paste ->
renamed the file (file1.c)
-> the file file1.c will be added to your project
-> double click on the file1.c file
-> remove every thing, and add
int i=100;
void display(){ printf("\n I am in display function");
-> save the file
Step 3:
-> open main.c file
-> extern i;
extern display();
-> in the main function
printf("\n The value of i is %d",i);
display();
Functions:
Piece of code, which can be used many times,
Reusable code.
syntax:
returntype functioname(parameters)
ex:
int addition();
int addition(int i,int j);
void display();
-> declaration of a function
-> definition of a function
Declaration:
we are telling the compiler that that
is the :
-> return type
-> name of the function
-> parameters/arguments
int addition();
int addition(int i,int j);
void display();
Defintion: Body of the function (logic)
ex:
int addition()
printf("xyz");
2.
int addition(int i,int j)
printf("The sum is %d",i+j);
Function Call:
we will call the function using the
name of the function itself along with
arguments.
ex:
display();
addition(10,20);
#include <stdio.h>
#include <stdlib.h>
void myfunction()
printf("\n My name is Sudhakar");
}
int main()
myfunction();
return 0;
10. Write a C program to display your name
inside a function.
11. Write a C program to add 2 integers using function
12. Write a C program for the following:
1. Addition
2. Subtraction
3. Multiplication
4. Divison
13. Write a C program to swap two integers
14. Write a C program to display numbers from 1 to 10
15. Write a C program to Print Multiplication Table of input Number in a function
16. Write a C Program to Check Whether a Number is Palindrome or Not Using While Loop and function.
17. Write a C program to Reverse a Number Using While Loop in a function
18. Write a C program for a Calculator using functions
19. Write a C program for Temperature Conversion Celcius to Fahrenheit and Vice Versa
20. Write a C program to Find LCM of Two Numbers Using While Loop
1. Write a C program explaining the use of nesting of functions
nesting of functions:
->calling a function with in another function.
-> we can't define a function within another function.
calling a function with arguments:
1. Call by value
2. Call by reference (Pointer concept)
2. Write a C program to call a function using arguments like below:
1. send one integer
2. send one character.
3. send one float
4. send one string
3. Write a C program to explain call by value in functions
Call by value:
When you call function using a parameter, then the function
receives the value, and another duplicate local variable
will be created in the function.
4. Write a C program to explain call by reference in Functions.
-> sending a variable to a function is call by value
-> sending the address of a variable to a function , it is called call by reference
Call by Reference: (Address -> *)
-> When you call a function using the address of the parameter,
and the function receives it as a pointer.
-> if you modify the variable inside a function, it will
effect the value in main function
-> here & is called Ambersent
-> here * this called astrik
in one line:
-> sending a variable to a function is call by value
-> sending the address of a variable to a function ,
it is called call by reference
5. Write a C program to swap two integers using call by reference
in functions
Pointer:
pointer is a variable which stores the address of another variable
type *var-name;
Ex:
int i=10;
int *j=&i;
Here j is a pointer which stores the address of variable i.
What is dangling pointer in C?
When a pointer is pointing to non-existing memory location is called dangling pointer.
What is null pointer in C?
A pointer that is assigned NULL is called a null pointer.
The NULL pointer is a constant with a value of zero defined in several standard libraries.
Null pointer is a pointer which is pointing to nothing. Null pointer points to empty location in memory.
Value of null pointer is 0.
int *p = NULL;
char *p = NULL;
Void pointer is a generic pointer that can be used to point another variable of any data type.
Void pointer can store the address of variable belonging to any of the data type
What is wild pointer in C?
Uninitialized pointers are called as wild pointers in C which points to arbitrary (random) memory
location
7. Write a C program to take a int pointer,assign the value, and print
8. Write a C program to take a float pointer,assign the value, and print
9. Write a C program to take a double pointer,assign the value, and print
#include <stdio.h>
#include <stdlib.h>
int main()
int i=10;
int *j=&i; //Single pointer
int **k=&j; //double pointer
int ***m=&k; //Thriple pointer
printf("%d -> %d -> %d -> %d",i,*j,**k,***m);
return 0;
-> Single pointer
ex:
int i;
int *j=&i;
-> Double pointer
int i;
int *j=&i;
int **k=&j; -> Double pointer
-> Triple pointer
int i;
int *j=&i;
int **k=&j; -> Double pointer
int ***m=&k; -> Triple pointer
10. Write a C program to declare
-> Single pointer
-> double pointer
-> Triple pointer
print the value of i using all the pointers
11.Write a program in C to find the square of any number using
the function, send the argument using call by reference.
12. Write a program in C to check a given number is even or odd
using the function, send the argument using call by reference.
13.Write a C program to Print Multiplication Table of input Number
in a function, send the argument by call by reference.
14.Write a C program to Reverse a Number Using While Loop in a
function, send the argument by call by reference.
15.Write a C program to explain the use of nested for loop like
display all its positions.
16. Write a C program to display
*****
*****
*****
*****
https://www.javatpoint.com/star-program-in-c
Arrays:
int i=10;
i=20;
i=30;
printf("%d",i);
This printf will print 30, because at a time we can store only
one value inside this variable i.
Is it possible to store multiple values inside a normal variable?
Nooooo, we can't store,
why?
because we have only one memory location, hence we can store
only one value.
if we want to store multiple values inside a single variable,then
we need to use Arrays.
Array:
An array is nothing but collection of homogenious(Similar) group
of values stored in a continuous memory.
or
we can say array is nothing but collection of similar values
10,20,30,40,50,60 -> group of integers
'a','b','c','d','e' -> group of characters
2.34,4.56,7.89,9.0 -> group of floats
Syntax:
int i; ->single variable , can store only one value at a time
int a[10]; -> group of 10 integers, also called integer array
float a[15] -> group of 15 floats, also called float array
char a[40] -> group of 40 characters,also called char array
double a[10] -> group of 10 doubles, also known as double array
Index -> position in the array, always starts from 0
ex:
int a[10];
here a is the name of the array, group of 10 integers.
its index starts from 0 and ends at 9
initialize an array:
int a[10]={100,200,300,400,500,600,650,700,750,800};
1. Write a C program to initialize a one dimensional array and
print the numbers from array
2. Write a Program to see the default values stored in array
3. Write a Program to take the elements of array from the
keyboard and print the values
4. Write a C Program to find the sum of all the elements
of a 1D array.
5. Write a C program to display the elements of an array in reverse order
6. Write a C program to display only even numbers from an array.
7. Write a C Program to find out the largest element in the array
8. Write a C program to find out the smallest element in the array
9. Write a program in C to copy the elements of one array into another array.
10. Write a C program to initialize the array without
array size and display the results.
11. Write a C program to initialize the array and copy
even numbers to even array.
12. Write a C program to initialize the array and copy
even numbers to even array.
13. Write a C program to initialize the array and copy
even numbers to even array and odd numbers to odd array.
14. Write a C program to reverse all the elements of an array.
15. Write a C program to display the values of an array using functions.
Important questions:
1. initialize one dimentional array
2. print all the elements of array
3. print all the elements of array in reverse order
1. for loop 2. initializing array 3. printing elements of arry
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[10]={100,200,300,400,500,600,650,700,750,800};
printf("\n Elements of array");
for(int i=0;i<10;i++)
printf("\n %d",a[i]);
return 0;
Linar search alogithm:
-> initialize the array
-> take the element to search
-> run a loop from 0th index to n-1 and
find if the element is found or not
-> if element is found , print element found
otherwise
print element not found
16. Write a C program for linear search.
Every alorithm is having
best case time complexity
worst case time complexity
avg case time complexity
Memory allocation:
memory allocation can happen in 2 ways in C Language:
-> Static memory allocation (Compile time)
-> Dynamic memory allocation (Run time)
bookmyshow
ex:
int a[5];
-> Dynamic memory allocation (Run time)
Memory allocation happens at run time (Execution time)
ex:
using pointers
memory allocation functions:
->malloc()
->calloc()
->realloc()
->free()
these 4 functions are available in below header files:
-> stdlib.h
-> malloc.h
malloc:
syntax:
pointer=(void *)malloc(noofblocks*sizeof(datatype));
ex:
to allocate memory for integer array.
1. int *a;
a=(int *)malloc(10*sizeof(int));
2. Allocate memory for float array.
float *a;
a=(float *)malloc(10*sizeof(float));
3. allocate memory for char array.
char *a;
a=(char *)malloc(10*sizeof(char));
4. allocate memory for double array.
double *a;
a=(double *)malloc(10*sizeof(double));
1. Write a C program to scanf integer array and print all the elements.
Use dynamic memory allocation.
2. Write a C program to take a int pointer,assign the value, and print
3. Write a C program to take a float pointer,assign the value, and print
4. Write a C program to take a double pointer,assign the value, and print
5. WCP to allocate memory for an array of 5 intergers ,take the inputs from keyboard, display the values
6. WCP to find the greatest element of an integer array(using dynamic memory allocation).
7. WCP to copy the contents of one array into another Array (using dynamic memory allocation).
8. WCP to print the elements of an array using dynamic memory allocation.
9. WCP to allocate memory for an array of 5 intergers ,take the inputs from keyboard, display the
values,udr calloc for dynamic allocation
10. WCP to copy the contents of one array into another Array (using dynamic memory allocation, calloc
function).
11.WCP to allocate memory for an array of 5 integers(using malloc/calloc), increase the size of the array
to 10 using realloc, print all the values.
12. Write a C program to allocate memory for 2 arrays, add them and display the results in a function
13. Write a C program to allocate memory for 1D array, find out of the sum of all the elements in a
function
for(int i=0;i<5;i++)
for(int j=0;j<5-i-1;j++)
if(a[j] < a[j+1])
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
16. WCP to search for an element in the array using linear search
17. WCP to search for an element in the array using binary search
Search:
to search for a given element in an array
We have 2 alogithms for searching
1. linar serach (sequential Search)
2. Binary Search
2 dimensional array (2d array):
how to declare:
int a[3][3]; //total sizeis 36
how to initialize 2D array:
2D is nothing but collection of 1D arrays:
int a[3][3]= {
{10,20,30},
{40,50,60},
{70,80,90}
};
how to print all the elements from 2D array:
for (int i=0;i<3;i++)
for(int j=0;j<3;j++)
printf("%d ",a[i][j]);
printf("\n");
}
2D array using dynamic memory allocation
2 steps:
1.allocate memory for entire block.
2.allocate memory for internaal 1d array.
int **p;
p=(int **)malloc(9*sizeof(int));
for(i=0;i<9;i++)
p[i]=(int *)malloc(3*sizeof(int)
16. Write a C program to Initialize 2D array and display the matrix
17. Write a C program to take the elements of 2D from keyboard , display the values
18. Write a C program to scan 2D array, search for an element.
19. Write a C program to scan 2D array and find out the sum of all the elements
20. Matrix addition
21. Matrix Multiplication
23. WCP to find out max and min element in 2D array.
24. Write a C program to allocate memory for 2D array, scan the elements and print them in matrix
format.
25. Write a C program to allocate memory for 2D array, scan the elements and print them in matrix
format.
26. Sort array of 2D strings using dynamic memory allocation
*********************************************************************************
Macros:
Functions:
-> push the return address to the stack
-> push all the arguments to the stack
-> go to the function definition
-> take out the parameters from stack
-> execute the function
-> pop the address
-> come back to the calling function
Preprocessor directives:
-> macros
-> file inclusions (<stdio.h>,"stdio.h")
-> conditional compilation
-> other directivies
Macro:
-> variables
-> Block (functions)
A macro is a piece of code in a program that is replaced by the value of the macro. Macro is defined by
#define directive.
Types Of Macros
1. Object-like Macros: (VARAIBALE TYPE)
// Macro definition
#define DATE 31
int main()
printf("Lockdown will be extended"
" upto %d-MAY-2020",
DATE);
return 0;
2.Chain Macros: Macros inside macros are termed as chain macros. In chain macros first of all parent
macro is expand then child macro is expanded
#define INSTAGRAM FOLLOWERS
#define FOLLOWERS 138
int main()
printf("Geeks for Geeks have %dK"
" followers on Instagram",
INSTAGRAM);
return 0;
}
Multi-line Macros: An object-like macro could have a multi-line. So to create a multi-line macro you have
to use backslash-newline.
#include <stdio.h>
#define ELE 1, \
5, \
int main()
int arr[] = { ELE };
printf("Elements of Array are:\n");
for (int i = 0; i < 3; i++) {
printf("%d ", arr[i]);
return 0;
4.Function-like Macro: These macros are the same as a function call. It replaces the entire code instead
of a function name.
#include <stdio.h>
#define min(a, b) (((a) < (b)) ? (a) : (b))
int main()
int a = 18;
int b = 76;
printf("Minimum value between %d and %d is %d\n",a, b, min(a, b));
return 0;}
compiler will paste the code where the macro is called.
syntax:
#define
Variable:
#define i 100
macro Block:
#define sum(i,j) i+j
1. Area of a rectangle using macros
2. C program explaining conditional compilation
3. undef a variable
conditional compilation:
#define i 100
#ifdef i
printf("I am good");
#else
printf("I am not good");
#endif
-> #ifdef
-> #else
-> #ifndef
-> #undef
File operations:
1. open a file
2. close a fille
3. open a file for reading
4. open a file for writing
5. Open a file for appending
6. create a new file
7. open a file, copy the content paste in another file.
When working with files, you need to declare a pointer of type file. This declaration is needed for
communication between the file and the program
1. FILE *ptr;
2. fopen:
ptr=fopen("filepath","r");
3. fgetc -> getting a character from a file
Syntax:
ch=fgetc(fptr)
4. fclose
r -> read mode
w-> write
a -> append
r+
#include <stdio.h>
#include <stdlib.h>
int main()
FILE *ptr;
char ch;
ptr=fopen("C://Users/Linus/Downloads/testing.txt","r");
if (ptr == NULL)
printf("Not able to open the file");
else
printf("Able to open the file");
while((ch=fgetc(ptr))!=EOF)
printf("%c",ch);
fclose(ptr);
return 0;
}
r+ : Opens a text file for both reading and writing.
r :Opens an existing text file for reading purpose.
w: Opens a text file for writing. If it does not exist, then a new file is created. Here your program will
start writing content from the beginning of the file.
a: Opens a text file for writing in appending mode. If it does not exist, then a new file is created. Here
your program will start appending content in the existing file content.
7. copy the contents of one file to another file
hint: open first file in read mode, second file in write mode
read the file line by line using fgets
write the data to second file using fputs
close first file
close second file
read write execute
--- 0
--x 1
-w- 2
-wx 3
r-- 4
r-x 5
rw- 6
rwx 7
-> chmod u=rwx file3.txt
chmod g=rx file3.txt
chmod o=x file3.txt
chmod u=rwx,g=rx,o=x file3.txt
Bitwise Operators:
Bitwise Or (|)
Bitwise and (&)
bitwise xor (or+not) ^
Bitwise not (~)
Left shift (<<)
shift right(>>)
ex: int i=6;
int j=4;
bitwise or : i|j (or gate)
bitwise and: i&j (and gate)
bitwise xor : i^j (xor gate)
bitwise not : ~i (not gate)
left sift : i<<3 (i will be left shifted 3 times) -> multiplication
right shift: i>>2 (i will be right shifted 2 times) -> divison
structure:
Structures (also called structs) are a way to group several related variables into one place. Each variable
in the structure is known as a member of the structure.
#include <stdlib.h>
struct student
int i;
float f;
};
int main()
struct student s;
s.i=100;
s.f=10.25;
printf("\n The size of the structure is %u",sizeof(s));
printf("\n The value are %d %f",s.i,s.f);
return 0;}
8. Definie a struct and declare a variable,initialize and print the values
Array of structures:
A student information -> single structure
multiple students information -> array of structures
9. WCP for array of structures
10. WCP for array of structures using dynamic memory allocation
struct student *s;
s= (struct student *)malloc(1*sizeof(struct student));
s->val1=100;
s->var2=10.25;
I have structure with one integer and one float;
i want to access these variables using
->
. (if it is a object of a structure)
-> (if it is a pointer of a structure)
Union:
it is similar to structure
union student
int i;
float f;
};
main()
{
union student s;
size of the union is the maximum size of the variable present inside that union
11. WCP to define a union and use that
12 WCP to define array of unions
enums: An enum is a special type that represents a group of constants (unchangeable values).
user definied data type.
it is mainly used for assign names to integral constatnts.
#include <stdio.h>
#include <stdlib.h>
enum week{Sunday,monday,tuesday,wednesday,thursday,friday,saturday};
int main()
enum week w;
for(int i=Sunday;i<=saturday;i++)
printf("\n %d",i);
return 0;
#include <stdio.h>
#include <stdlib.h>
enum week{Sunday=10,monday,tuesday,wednesday=5,thursday=5,friday,saturday};
int main()
enum week w;
w=wednesday;
printf("The value of w is %d",w);
return 0;
enum month{january,feb,mar,apr,may,june,july,aug,sept};
#include <stdio.h>
#include <stdlib.h>
enum week{Sunday=10,monday,tuesday,wednesday=5,thursday=5,friday,saturday};
int main()
enum week w;
w=wednesday;
printf("The value of w is %d",w);
return 0;
}
Printing the values of linked list in reverse order:
void displaylist(struct node *temp)
if(temp==NULL)
return;
else
displaylist(temp->next);
printf("%d -> ",temp->data);
printing the values of linked list in normal order:
void displaylist(struct node *temp)
if(temp==NULL)
return;
else
printf("%d -> ",temp->data);
displaylist(temp->next);
#include<stdio.h>
#include<stdlib.h>
struct node
int d;
struct node *next;
};
int main()
struct node *ptr,*temp,*head;
head=NULL;
for(int i=0;i<5;++i)
ptr=(struct node *)malloc(1*sizeof(struct node));
ptr->d=100+i;
if(head==NULL)
head=ptr;
temp=ptr;
else
temp->next=ptr;
temp=ptr;
}
temp=head;
while(temp!=NULL)
printf("%d->",temp->d);
temp=temp->next;
Linear Data Structures:
Stack
Queue
Arrays
Stack:
push (element)
pop()(removing element)
top - index
=====
top=-1
void push(int x)
if(top==size)
printf("Size is full or stack over flow");
}
else
top++;
a[top]=x;
int pop()
if(top==-1)
printf("Stack is empty");
else
x=a[top];
top--;
return x;
}
What is the difference between memcpy() & strcpy() functions in C?
memcpy() function is used to copy a specified number of bytes from one memory to another. Whereas,
strcpy() function is used to copy the contents of one string into another string.
memcpy() function acts on memory rather than value. Whereas, strcpy() function acts on value rather
than memory.
The strcpy ( ) function is designed to work exclusively with strings. It copies each byte of the source
string to the destination string and stops when the terminating null character (\0) has been moved. On
the other hand, the memcpy () function is designed to work with any type of data.
Because not all data ends with a null character, you must provide the memcpy ( ) function with the
number of bytes you want to copy from the source to the destination. The following program shows
examples of both the strcpy ( ) and the memcpy ( ) functions:
What is Macro? Why do we use macro?
Macro is a name which is given to a value or to a piece of code/block in a program. Instead of using the
value, we can use macro which will replace the value in a program.
What is dangling pointer in C?
When a pointer is pointing to non-existing memory location is called dangling pointer.
What is null pointer in C?
Null pointer is a pointer which is pointing to nothing. Null pointer points to empty location in memory.
Value of null pointer is 0.
int *p = NULL;
char *p = NULL;
Void pointer is a generic pointer that can be used to point another variable of any data type.
Void pointer can store the address of variable belonging to any of the data type
What is wild pointer in C?
Uninitialized pointers are called as wild pointers in C which points to arbitrary (random) memory
location
Memory leak occurs when programmers create a memory in heap and forget to delete it.
The consequences of memory leak is that it reduces the performance of the computer by reducing the
amount of available memory.
void f()
int *ptr = (int *) malloc(sizeof(int));
free(ptr);
return;
The memory leak occurs, when a piece of memory which was previously allocated by the programmer.
Then it is not deallocated properly by programmer.
Constant:
Constants refer to fixed values that the program may not alter during its execution. These fixed values
are also called literals.
Defining Constants
There are two simple ways in C to define constants −
Using #define preprocessor.
Using const keyword.
const int LENGTH = 10;
Typedef:
The C programming language provides a keyword called typedef, which you can use to give a type a new
name.
typedef unsigned char BYTE;
You can use typedef to give a name to your user defined data types as well.
typedef struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
} Book;