0% found this document useful (0 votes)
72 views

C Programme Notes

This document provides a summary of 18 key C programming concepts: 1) Input/output functions like scanf() and printf(); 2) Variable types like int, char arrays, and constants; 3) Arithmetic, relational, logical, and bitwise operators; 4) if/else conditional statements and switch/case; 5) Loops like while, do-while, and for to repeat code.

Uploaded by

Hitendra Mahajan
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

C Programme Notes

This document provides a summary of 18 key C programming concepts: 1) Input/output functions like scanf() and printf(); 2) Variable types like int, char arrays, and constants; 3) Arithmetic, relational, logical, and bitwise operators; 4) if/else conditional statements and switch/case; 5) Loops like while, do-while, and for to repeat code.

Uploaded by

Hitendra Mahajan
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 20

#include <stdio.

h>

int main()
{

return 0;
}
NOTE : here for character like a,b,c use '%c' and for word or
string used '%s'
NOTE : for using string use 'char x [number of letters];

1) scanf = to enter the manual value. e.g - scanf("%d",&i) to enter


the value of i. we can use 2,3 value i.e. scanf("%d %d",&x,&y);

2) const = Ii is used to fix the value of assign to the variable


e.g.=> const int a =7; after that we cant change the value of a.
3) & = to adress the value ,usally use in scanf.

4) Arithmatic operations = addition


,substraction,multipication,dividing

5) Short hand operator => i= i+3 --> i+=3 ,i= i-3-->i-=3,i=i*3---


>i*3,i=i)/3-->i/ =3

6) Relational Operators = <,>,==,<=,>=,!>,!<. here '!' is used for


'not',i.e. not equal to .

7) Character Array ==> char str[10];


gets(str); ==> it works like scanf
printf("%s \n",str);
puts(str); ==> it works like printf
return 0;

8) Logical Operators => &&--> and oprator. T T ==> T ,TF ==> F


NOTE = if the first operation is false it will not check 2nd condition
|| --> or oprator. T T ==> T ,TF ==> T
IT CAN BE USED FOR MULTIPLE e.g. for vOwels and consonant
programm x= a||e||i||,,,etc.

9) Bitwise oprator => 1) & for 'and' oprator. e.g int x = 25&15
is 9
2) | for 'or' oprator. e.g int x = 25|15 is 31

10) if else if ==> e.g. == > int a=2,b=6,c=6


if(a>b && a>c)
printf("%d is greater",a);
else if (b>c)
printf("%d is greatre",b);
else
printf("%d is greater"),c;

11) Define preproccessor ==> The #define pre-processor directive is


used define pre-processor variable, constant or macro.
Macro operate much like functions. The #define
can use any basic data type. This pre-processor
directive can be used to replace a word with a
number globally. It acts as if an editor did a
global search-and-replace edit of the file.
Assign the value at the start. Define the value to
the variable ,which will use further in
programme
e.g.1) ==> #include <stdio.h>
#define x 5
int main()
{
int i = x; ===> IMP
printf("%d",i)
return 0;
}
ANS==> 5

e.g.2) ==> #define square(r) r*r


int main()
{
int r=4;
printf("%d",square(r)); ==> 16.
return 0;
}

12) Switch ,Break, Defualt. ==>


e.g ==> print number in word i)
#include <stdio.h>
int main()
{
int x;
printf("enter the number \n");
scanf("%d",&x);
switch(x) \\ here no ;
or : \\
{ \\ here note
bracket \\
case 0: \\ here used : \\
printf("zero");
break;
case 1:
printf("one");
break;
case 2:
printf("two");
break;
case 3:
printf("three");
break;
case 4:
printf("four");
break;
case 5:
printf("five");
break;
default: \\ here also :
used \\
printf("no match found ");
}
return 0;
}

13) Ternary oprator ==> It work like --> condition?exp1:exp2


e.g. ==> #include <stdio.h>
int main()
{
int x,y;
char even,odd;
printf("enter the value \n");
scanf("%d",&x);
y = x%2==0?1:2;
printf("%d",y);
return 0;
}

14) Swapping of two numbers ==> Exchanging two numbers.


e.g. ==> #include <stdio.h>
int main()
{
int x,y;
printf("enter the value \n");
scanf("%d %d",&x,&y);
x=x+y;
y=x-y;
x=x-y;
printf("%d %d",x,y);
return 0;
}

15) while Loop ==> When we want to print one word/number multiple
times rhen we can uses while loop.
NOTE: HERE WE DONT USE ; SYMBOLE AFTER WHILE()
e.g.==> #include <stdio.h>
int main()
{
int x,i;
printf("enter the value \n");
scanf("%d",&x);
i = 1;
//INITIALIZATION //
while(condition) while(i<=5) =====> HERE ';'
NOT USED IN 'WHILE LOOP' // CONDITION //
{
printf("%d\n",x);
i++; // INCREMENT OR
DECREMENT //
}
return 0;
}

16) Do While Loop ==> Whatever the condition in the loop it will
enter in the loop. when your condition is false it print the word or number at
least one time.
e.g.--> #include <stdio.h>
int main()
{
char x [100];
int i,y;
printf("enter the name \n");
scanf("%s",&x);
printf("how many times do u want it \n");
scanf("%d",&y);
i =
6; //INITIALIZATION //
do
{
printf("%s \n",x);
i+
+; //INCREMENT OR DECREMENT//
}
while(i<y); ======> HERE ';' USED
IN 'DO WHILE LOOP' //CONDITION//
return 0;
}

17) For Loop ==> it does same work as while loop but in one line
i.e. in programme => for(intialization;condition;increment or decrement)
e.g.=> #include <stdio.h>
int main()
{
char x [100];
int i,y;
printf("enter the name \n");
scanf("%s",&x);
printf("how many times do u want it \n");
scanf("%d",&y);
for(i=1;i<=y;i++)
{
printf(" %s\n",x);
}
return 0;
}

18) Nested For Loop ==> it is used for pattern n*n format.
e.g.==>{
int i,x,k,y,j;
printf("enter the name \n");
scanf("%d",&x);
printf("how many times do u want it
\n");
scanf("%d %d",&k,&y);
for (i=1;i<=k;i++)
---> in that 1st for we used 'i' and in 2nd for 'l' is used
{
for(j=1;j<=y;j++)
{
printf("%d ",x);
}
printf(" \n");
---> dont forget 1st for printf
}

19) Patterns using loops ==> pattern like ---> 1


1 1 1
Progrmme => int i,x,k,y,j; 1
1 ==> k,y = 4,4
printf("enter the name \n"); 1
1
scanf("%d",&x); 1
1 1 1
printf("n*n \n");
scanf("%d %d",&k,&y);
for(i=1;i<=k;i++)
{
for(j=1;j<=y;j++)
for another j=1;j=i;j++ ;; j=4;j>=i;j-- (mirror)
{
if ( i==1 || j==1 || i==4 ||
j==4)
printf("%d ",x);
else
printf(" ");
}
printf("\n");
}

20) Ascii value (american standard code for information interchage):


It has value of numbers from 0 to 127. To
cheak value of that number or ascii value programme is:
{
int i,k;
printf("enter the number");
scanf("%d",&k);
for(i=0;i<=k;i++)
{
printf("Ascii value:
%c\n",i);

}
return 0;
}

21) Array : It is collection of data of the same type.(marks of


student) 1d = [1,2,3,4];2d=[1,2,3,4],[5,6,7,8];3d=[6,5,4,7],[7,8,9,4],[1,2,3,4]
prodramme for it
i) for 1d array z[];
int main()
{
int i;
int z[4]={1,2,3,4,5,6};
for(i=0;i<=3;i++)
{
printf("%d ",z[i]);
}

return 0;
}
ii) for 2d ===> z[row][column];
{
int i,j;
int z[2][4]={ {1,2,3,4},
{7,8,9,4}}; ==> dont forget int bbefore assin brackets to z
for(i=0;i<2;i++)
{
for(j=0;j<4;j++)
printf("%d ",z[i][j]);
printf("\n");
}

return 0;
NOTE: same as the 2d only add row
and colum numbers in z[r][c].same we can do 4d,5d,6d.
int main()
{
int i,j;
int z[5][3]=
===> 'int'
{
{1,2,3},
===> used curly bracket
{5,6,7},
{9,10,11},
{13,14,15},
{17,18,19}
};
for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",z[i][j]);
}
printf("\n");
}

22) User Define Function ==> In user define function we can used our
own function.
There is im[portant three steps : i) Declaration
ii) Defination
iii) calling
ONLY VALUES TAKEN FROM MAIN FUNTION, REVRSE CAN
NOT DO.
e.g.==> #include <stdio.h>
void addition();
\\ declaration \\
int main()
{
addition(); \\
calling \\
return 0;
}
void addition() \\
defination \\
{
int i,j,k;
printf("enter the value of i and j\n");
scanf("%d %d",&i,&j);
k = i+j;
printf("the addition of 2 numbers is %d ",k);
}
II) void star(int n,int v) ===>mention variable
with decleration
{ value taken
form int main() function i.e star(n,v)
int i,j;
char c ='*';
for(i=1;i<=n;i++)
{
for(j=1;j<=v;j++)
{
if(i==1||j==1||i==4||j==4)
printf("%c ",c);
else
printf(" ");
}

printf("\n",c);
}
}
int main()
{
int a,b,d;
a=5,b=5;
d= a+b;;
star(4,4); ==> caliing
printf("%d\n",d);
return 0;
}
III) Recursive Function :
#include <stdio.h>
int factorial(int number)
{
if(number ==1||number==0)
{
return 1;
}
else
return(number*factorial(number-1));

}
int main()
{
int n;
printf("enter the number\n");
scanf("%d",&n);
printf("factorial of %d is %d",n,factorial(n));
return 0;
}

23) Pointer ==> It stores the address of another variable.Can be of


any type e.g. int,char,array,functionor any other pointer.* is dereference operator
and & is address of a variable
here *(asterisk symbole) is used to fetch the
value given to the number, but u have to mention its location to pointer e.g. p=
&i.
e.g.==> 1) char c[] = "hitendra';
char *ptr;
ptr = &c;
printf("%s",ptr); ==> Note : In 'int' or
'float' to print values in pointer we used '*ptr' here in 'char' we used the only
'ptr'.
e.g.--> 2) int i=5;
(IMP) int *p = &i;
printf("%d\n",&i); ==> 36 (BYTES)
printf("%d\n",&i+1); ==> 40 (4 bytes)
printf("%d\n",*p); ==> 5
printf("%d\n",&(*p)); ==> 36
printf("%d\n",&(*p)); ==> 40
24) Pointer using Array ==> e.g.-->
int i[4]={5,1,9,8};
(IMP) int *p = &i;
printf("adress of %d is
%d\n",i[0],&i[0]); ==>adress of 5 is 6422016
printf("sdress of %d is
%d\n",i[0],&i[0]+1); ==>adress of 5 is 6422020
printf("value is %d\n",*p+1);
==>value is 6
printf("value is %d\n",*(p+1));
==>value is 1
printf("value is %d\n",i[1]);
==>value is 1
printf("value is %d\n",&(*p));
==>adress 6422016 of pointer
e.g.--> int i[2][2]={{1,2},{3,4}};
int *p = &i;
printf("%d\n",i[0][1]); ==> 2
printf("%d\n",*(p+0)); ==> 1
return 0;
'ALSO ONE EXAMPLE IN RECURSIVE TOPIC'

25) Continue ==> 1) The continue statement is used inside loops in C


Language. When a continue statement is encountered inside the loop, control jumps
to the beginning
of the loop for next iteration, skipping the execution
of statements inside the body of loop after continue statement.
2) It is used to bring the control to the next iteration of
the loop.
3) The continue statement skips some code inside the loop
and continues with the next iteration.
4) It is mainly used for a condition so that we can skip
some lines of code for a particular condition.
(IMP) 5) It forces next iteration in loop i.e. as break
terminates the loop but continue forces the next iteration of the loop.

26) Typecasting ==> Typecasting can be defined as converting one data


type into another.
i) int a=5.6;
float b = 6.5;
printf("value is %f\n",(float)a); ==> 5.00
ii) int a=5;
float b = 6.5;
printf("value is %d\n",(int)b); ==> 6
iii) int a=5;
float b = (float)54/7;
printf("value is %f\n",b);

27) Goto ==> goto end;


end:

28) Recursive Function ==> i) Functtion are used to divide a large c


program into samller pieces.
ii) A function can be called mutiplle
times to provide reusability and modularity to the c program.Also called procedure
or subroutine.
iii) Process when FUNCTION Calls a copy of
itself to work on smaller problem.
e.g. ==> int array(int z[]) ==>
'INT' important here either error
{
for(int i=0;i<4;i++)
{
printf("%d\n",z[i]);
}
printf("\n\n");
return 1;
}
int pointer(int *p) ==> to
print array values
{
int a,b;
for(int i=0;i<4;i++)
{
printf("%d\n",*(p+i));
==> IMP
}
printf("\n\n");
return 2;
}
int twod(int *p)
{
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
printf("%d\n",*((p+i)+j));
==> IMP {i foe row and j for column} it must in bracket after *
}
printf("\n\n");
return 3;
}
int main()
{
int z[2][2]={{1,2},{3,4}};
array(z);
pointer(z);
twod(z);
return 0;
}

29) Call by value and Call by reference :


* Formal Parameter: Formal parameters are the local
variable which are assigned values from the arguments when the function is called.
In example given below 'a' and b are formal parametrs.
* Actual Parameter: When a function is called, the
values(expression) that are passed in the call are called arguments or actual
parameters.
In example given below 'x' and 'y' are actual parameters.

Call by value of each of the actual arguments in the


calling function is copied into corresponding formal arguments of the called
function. In this function call,
the changes made to the formal arguments in the called
function have no effect on the values of actual arguments in the calling function.
Actual and formal arguments
will be created in a different memory location. The following
program is the example of ‘Call by Value’.
e.g.==> #include <stdlib.h>
int add(int a,int b)
{
return(a+b);
}
int main()
{
int x,y;
printf("enter the value of x and y\n");
scanf("%d %d",&x,&y);
add(x,y);
printf("%d\n",add(x,y));
return 0;
}
Call by reference the addresses of actual arguments in the
calling function are copied into formal arguments of the called function. This
means that using these addresses
we could access the actual arguments and hence we would be
able to manipulate them. The changes that are made to the parameter affect the
argument. This is because the address
is used to access the actual argument. Formal and actual
arguments will be created in the same memory location. The following program is the
example of ‘Call by Reference’
e.g.==> int add(int *a,int *b)
{
return *a + *b;
}
int substr(int *a,int *b)
{
return *a - *b;
}

int main()
{
int x=5,y=5;
add(&x,&y);
substr(&x,&y);
printf("%d\n %d\n",add(&x,&y),substr(&x,&y));
return 0;
}

30) strcpy,strrev,strcmp :
#include<string.h> ==> (cheak the photo)
char s1[]="hite";
char s2[]="ndra";
char s3[]="mahajan";
//puts(strcat(s1,s2)); ==> strcat(x,y) = combine strings.
//printf("%d\n",strlen(s1));
//puts(strrev(s1)); ==> strrev(x,y) = opposits the
sequance.
//strcpy(s3,strcat(s1,s2)); ==> strcpy(x,y) = copy y in
x.
//puts(s3);
printf("%d",strcmp(s2,s1)); ==> strcmp(x,y) = campare x and y
and gives number by ASCII value.
BUT when the two strings are same
the return value is 0.

31) Structure in C ==> Structures are usually used when we want to


store dissimilar data together.For example, we want
to store data about a book. Book has its title,
author name, number of pages and price. All of the
book attributes belong to different data types. One
way to store the data is to construct individual
arrays, and another method is to use a structure
variable. Structure elements are always stored in contiguous
memory locations
#include <stdio.h>
#include <string.h>
struct student
{
char fullname[20];
float cgpa;
}hitendra,suyash,yash,harshal,shubham;
int main()
{
int a;
printf("enter the Roll Number or 0 to
exit\n");
scanf("%d",&a);
{
switch(a)
{
case 0:
printf("quitting the program....");
goto end;
break;
case 51:
strcpy(hitendra.fullname,"Hitendra
Mahajan");
hitendra.cgpa=7.12;
printf("NAME:%s\nCGPA:
%f",hitendra.fullname,hitendra.cgpa);
break;
case 50:
strcpy(suyash.fullname,"Suyash Bendale");
suyash.cgpa=7.2;
printf("NAME:%s\nCGPA:
%f\n",suyash.fullname,suyash.cgpa);
break;
case 49:
strcpy(yash.fullname,"Yash Badhe");
yash.cgpa=7.10;
printf("NAME:%s\nBRANCH:%s\nCGPA:
%f\n",yash.fullname,yash.cgpa);
break;
case 48:
strcpy(harshal.fullname,"Harshal Gurav");
harshal.cgpa=7.05;
printf("NAME:%s\nCGPA:
%f\n",harshal.fullname,harshal.cgpa);
break;
case 47:
strcpy(shubham.fullname,"Shubham Patil");
strcpy(shubham.branch,"Mechanical");
shubham.cgpa=8.12;
printf("NAME:%s\nCGPA:
%f\n",shubham.fullname,shubham.cgpa);
break;
default:
printf("Incorrrect Roll Number");
}
end:
return 0;

32) Typedef ==> A typedef is a keyword that is used to assign


alternative names to existing datatypes. We use typedef
with user defined datatypes, when names of the datatypes
become slightly complicated to use in programs.
#Typedefs can be used to:
I)Provide the clarity in the code.
II)It makes easier to change the underlying data types
that you use.
III)Typedefs makes the code more clear and easier to modify
**)FORMAT =>typedef <previous_name> <alias_name>
e.g.==>1) typedef float integer;
typedef char hits;
integer b = 5;
hits c[10]="hittendra";
printf("%f\n",b);
printf("%s is learning c",c);

(pointers) e.g.==>2) typedef int* intptr;


intptr a,b;
int c=5;
*a=&c;
*b=&c;

33) Unions : Just like Structures, the union is a user-defined data


type. All the members in union share the same memory
location. The union is a data type that allows different
data belong to different data types to be stored in
the same memory locations. One of the advantages of using a
union is that it provides an efficient way of reusing
the memory location, as only one of its members can be
accessed at a time. A union is used in the same way we
declare and use a structure.

34) Dynamic Memory Allocation ==> Dynamic memory allocation is the


process of allocation of memory space at the run time. We use
the concept of dynamic memory allocation to reduce the wastage of
memory, and it is the optimal way of memory allocation. To
#)For the allocation of memory using the heap, we have four
functions:
1) Malloc : Malloc stands for memory allocation. As can be
guessed by its name, it requests memory from the heap and
use : #include<stdlib.h> returns a pointer to the memory. The pointer is
of the void type, so that we can typecast it for any variables.
All the values at the allocation time are initialized
to garbage values.
Its syntax is : ptr = (ptr-type*)
malloc(n*size_in_bytes); ==> ptr = (int*) malloc (3* sizeof(int));
e.g.==> #include<stdlib.h>
printf("Enter the new number of n\n");
scanf("%d",&n);
ptr = (int *)malloc(n*sizeof(int));
==> malloc.
for(int i=1;i<=n;i++)
{
printf("Enter the %d value\n",i);
scanf("%d",&ptr[i]);
}
for(int i=1;i<=n;i++)
{
printf("The number of %d is :
%d\n",i,ptr[i]);
}
free(ptr); ==>
free()
return 0;
}
2) Calloc : Calloc stands for contiguous allocation. It also
requests memory from the heap and returns a pointer to the
memory and has the same functionality as malloc().In
calloc(), the values at the allocation time are initialized
to 0 instead of garbage value.
Its syntax is : ptr = (ptr-type*)
calloc(n,size_in_bytes); ==> ptr = (int*) malloc (10, sizeof(int));
e.g.==> int * ptr;
int n;
printf("Enter the number of n\n");
scanf("%d",&n);
ptr = (int *)calloc(n,sizeof(int)); +- ==>
calloc.
for(int i=1;i<=n;i++)
{
printf("Enter the %d value\n",i);
scanf("%d",&ptr[i]);
}
for(int i=1;i<=n;i++)
{
printf("The number of %d is : %d\n",i,ptr[i]);
}
free(ptr); ==>
free()
return 0;
3) Realloc : Realloc stands for reallocation. It is used in cases
where the dynamic memory is insufficient or wants
to increase the already allocated memory to store
more data.
Its syntax is : ptr = (ptr-type*)
realloc(ptr,new_size_in_bytes); ==> ptr = (int*) realloc (ptr, 5* sizeof(int));
e.g. is given below:
4) Free : We have to free up the allocated memory space manually
as there is no automatic procedure for that. So free is used to
free up the space occupied by the allocated memory.
Its syntax is : free(ptr);
e.g. ==> int * ptr;
int n;
printf("Enter the number of n\n");
scanf("%d",&n);
ptr = (int *)calloc(n,sizeof(int));
==> calloc.
for(int i=1;i<=n;i++)
{
printf("Enter the %d value\n",i);
scanf("%d",&ptr[i]);
}
for(int i=1;i<=n;i++)
{
printf("The number of %d is : %d\n",i,ptr[i]);
}
printf("\n\n");

printf("Enter the new number of n\n");


scanf("%d",&n);
ptr = (int *)realloc(ptr,n*sizeof(int));
==> realloc.
for(int i=1;i<=n;i++)
{
printf("Enter the %d value\n",i);
scanf("%d",&ptr[i]);
}
for(int i=1;i<=n;i++)
{
printf("The number of %d is :
%d\n",i,ptr[i]);
}
free(ptr); ==>
free()
return 0;

35) Storage Class :


1) Auto Storage Class : Variables being formed in a function and
whose storage class has not been defined initially
fall in this category automatically. Its scope
is minimum as it can only be accessed inside
the function it is initialized in. No other
function can access it. Until the variable has
been assigned some value, it stores garbage
value as default. Their lifetime depends upon the
function block's length as the lifetime is
until the function block's end.
int a;
auto int a;

2) External Storage Class : These sorts of variables are defined


outside the function, hence can be used inside any function
meaning that they can be used globally.
Their initial value is set to 0. As they can be used throughout
the program, so their lifetime equals
the lifetime of the program. Too many global variables in a
program can cause security issues and
also are not usually recommended,
Extern Keyword : Using the extern keyword, we inform
our compiler that the variable is already declared at
some other place. By doing so, we can
use the same variable with the same space, without allocating its new
memory and accessing the same
variable in some other file. Its syntax is simple as we have to use the extern
keyword, and it will automatically
access it from the other file.
e.g. ==> extern int a;
Programme : int b =1;
int a= 2;
int main()
{
extern int a,b;
int s;
s =a+b;
printf("%d\n",s);
return 0;
}

3) Static Storage Class : Static variables are a little bit


technical as their lifetime is throughout the program, but their scope is
limited to the function they are
initialized in. It comes in handy when we are changing their value in the program
as the program will store the new value,
overwriting the previous one. Their initial default value is 0, and their
syntax is very easy as we just have to use
the Static keyword during initialization.
e.g. ==> static int a;
Programme : int check()
{
static int a;
a++;
return a;
}
int main()
{
int b = 0;
printf("%d\n",check(b)); ==> 1
printf("%d\n",check(b)); ==> 2
printf("%d\n",check(b)); ==> 3
printf("%d\n",check(b)); ==> 4
printf("%d\n",check(b)); ==> 5
return 0;
}

4) Register Storage Class : It is very similar to the Auto


storage class as its scope is limited to the function it is defined in, the initial

default value is 0, and lifetime is till


the end of the function block. Now the major difference between it and the
others is that it requests the CPU's
register memory instead of the local memory for fast access. It is usually used
for the programs that need to be accessed
faster than the other or used frequently.
e.g. ==> register int a;

36) Void Pointers : Void pointers and their functionalities. As we are


already familiar, that void has no return type i.e., functions that are not
returning anything are given the type void. So, in case
of void pointers, they can be typecast into any data type whenever we want,
meaning we do not have to decide a type for the pointer
initially. In simple words, it is a general-purpose pointer variable.

e.g. ==> {void *ptr;


int a = 5;
float p = 5.3;
/*
ptr=&p;
printf("%f",*((float*)ptr));
*/
ptr=&a;
printf("%d",*((int*)ptr));

ptr =&c;
printf("%s",((char*)ptr)); --===> Note that first
'*' is not used in the case of 'char'
}

37) Null Pointers : The two are different as the Null pointer points to
the 0th memory location, which means that it does not occupy any memory location.
In contrast, an uninitialized pointer means that the
pointer occupies a garbage value. The garbage value can be any value the garbage
collector assigns to the pointer, which may point to
some memory location. So to be on the safe side, NULL pointers are preferred.
e.g. ==> int main()
int main()
{ {
int a=34;
char a[] ="hitendra";
int * ptr = NULL; ==> if *ptr = &a;
char*ptr = NULL;
if(ptr != NULL){
if(ptr != NULL){
printf("%d",ptr); ==> ans: 34.
printf("%s",ptr);
} }
else{ else{
printf("pointer is null we cannot be derefernce
it"); printf("pointer is null we cannot be derefernce it");
}
}
38) Dangling pointer : Dangling pointers are pointers that are pointing
to a memory location that has been freed or deleted.
Dangling pointers arise during object destruction, when
an object with an incoming reference is deleted or deallocated, without modifying
the value of the pointer, so that the pointer still
points to the memory location of the deallocated memory.The system may reallocate
the
previously deleted memory; the unpredicted result may
occur as the memory may now contain different data.
Dangling may occur by three ways : 1) De-allocating or
free variable memory 2) Function Call 3) Out of Scope
1) De-allocating or free variable memory : When memory is
deallocated, the pointer keeps pointing to freed space.
e.g. : int main()
{
int *ptrr=(int *)malloc(n*sizeof(int));
int x=80;
ptrr=&x;
free(ptr);
}
2) Function Call : int *myvalue() {
int a=5;
return &a;
}
int main()
{
int *ptr=myvalue();
printf("%d", *ptr);
}

39) Wild Pointer : It will store any garbage value in it, meaning it
will hold some arbitrary memory location. Due to the storage of some random
location, it can cause a
lot of bugs in the program, and sometimes the programmer
will not even be able to identify the cause.
int a =4354;
int *ptr; ==> This is a wild
pointer
// *ptr = 34; ==> This is not a good
thing to do
ptr = &a; ==> ptr is no
longer a wild pointer
printf("The value of a is %d\n", *ptr);

40) C Pre-processing : The C Pre-processor is not a part of the


compiler. The C Pre-processor is just a text substitution tool, and it instructs
the compiler to do required
pre-processing before the actual compilation process.
We refer to the C Pre-processor as CPP.
e.g. : #include<stdio.h> , #include<stdlib.h>
#include<string.h> , #include<time.h> ,#define,etc.

41) Predefined Macros & Other Pre-processor Directives :
1) #undef : As can be guessed by the name, it is used to
undefine a macro to eliminate its definition.
e.g, ==> #define E 5
#undef E
2) #ifdef : It is used to check whether a macro is defined
or not. If it is defined, then it executes the code.
3) #if : It checks whether the given condition is true or
not. If true, then it executes the code.
4) #else : If the condition of ‘if’ is false, then the else
is executed
5) #elif : It is used to insert more conditions between if
and else. If the “if statement” is true, then elif won’t be checked.
6) #pragma : Pragma is used to issue some special commands
to compiler. Let's discuss some pre-defined macros now. A pre-defined macro
is a macro that has already been defined or
understood by C preprocessor and does not need a definition.
7) __DATE__ : It prints the current date on to the screen.
The date format it follows is MMMDDYYY.
e.g. ==> int main()
{
printf(__DATE__);
}
8) __TIME__ : It prints the current time on to the screen.
The date format it follows is HH:MM:SS.
e.g. ==> int main()
{
printf(__TIME__);
}
9) __FILE__ : It prints the current file name on to the
screen. The name will be printed as a string literal.
e.g. ==> int main()
{
printf(__FILE__); ==>
E:\Hitendra\c\c08\main.c
}

10) __LINE__ : It prints the current line number on to


the screen. The number will be printed as a decimal constant.
e,g, ==> int main()
{
printf("%d",__TIME__);
}
11) __STDC__ : It is used to check whether our program is
being compiler using ANSI standard or not. It will return 1 if true.
e.g. ==> int main()
{
printf("%d",__STDC__);
}

42) Files in C : Files are an essential part of any program as we can


take input from and print output in files. We can also save a lot of program
space by accessing the file's data only when needed,
making the program more efficient and faster.
1) Purpose of files in C :
1) Files are used to store content hence reducing the
program's size.
2) We can read or access data from files.
3) The data in files remain stored even after the program's
execution is terminated.
There is two types of the files : 1) Text files 2) Binar
files.
2) Modes : Before discussing Files' functions, we have to learn
about different modes used along with these functions as a parameter. The following
are the modes:
1) r : opens a file for reading.
==> ptr = fopen(“file_location”,”mode”);
2) w : opens a file for writing. It can also create a new
file.
==>
3) a : opens a file for appending.
4) r+ : opens a file for both reading and writing but
cannot create a new file.
5) w+ : opens a file for both reading and writing.

43) atoi() : It is used to convert 'string' into the 'int'.

44) Function Pointer : In C programming, the pointer to a function is


known as function pointer in C.
Syntex : function_return_type(*Name_of_pointer)
(function arguments list)
e.g. ==> void (*fun)(int, char); ==> fun is a
pointer to a function taking two arguments, an integer and character, and that
returns void.
It's as if we are
declaring a function called "*fun", which takes an int and char, and returns void;
now, if *fun is a
function, then fun must be a pointer to a function. The following are the legal way
of declaring
function pointers.
int (*sum)( int); //legal declaration of
function pointer
int *sum(); //This is not a declaration
of function pointer
e.g. ==> int add(int a, int b)
{
return a+b;
}

int sub(int c,int v)


{
return c-v;
}
int main()
{
int q,w;
printf("Enter the numbers\n");
scanf("%d %d",&q,&w);
int(*fun)(int,int);
int(*lun)(int,int);
fun=add;
lun=sub;
int ans1=fun(q,w);
int ans2=lun(q,w);
printf("%d\n",ans1);
printf("%d",ans2);
return 0;
}

45) Callback Function : If a function's reference is passed to another


function as an argument to call it, it will be called a Callback function. This
allows the
programmer to write less code and do more things. A
callback function is a function that is called by using a function pointer.
Function
Pointer, as its name suggests, is a pointer to a
function.
* Syntex : void fileDownload (const char *file, void
(*callback_function)(int statusCode));
* Difference between Function Pointer and Callback
Functions :
1) A function pointer is a pointer that points to the
function. Whereas, callback functions are function pointers passed as parameters of
the
function, which can be called back when an event
occurs.
2) A function pointer is an address of the function.
Whereas, callback functions pass function pointer as an argument, and the caller
would
callback if something happens.
e.g. ==> int sum (int a, int b)
{
return a + b;
}
void greetHelloAndExecute(int (*fptr)(int,
int)){
printf("Hello user\n");
printf("The sum of 5 and 7 is %d\n",
fptr(5, 7));

}
void greetGmAndExecute(int (*fptr)(int, int))
{
printf("Good Morning User\n");
printf("The sum of 5 and 7 is %d\n",
fptr(5, 7));
}
int main()
{
int (*ptr)(int, int);
ptr = sum;
greetHelloAndExecute(ptr);
return 0;
}

46) Memory Leak : A memory leak is a poison for software because


software shows undefined behavior due to the memory leak. A memory leak occurs when
we
create a memory in a heap and forget to delete it. It
happens when we do not use dynamic memory properly. If the system has enough
memory, in that case also this slows down the performance
of the system. To avoid this problem, memory allocated on the heap should
always be freed when not needed.
* Memory leaks in C happen because of these three reasons:
1) we do not free the memory that is no longer needed
2) we do try to free the memory, but we do not have the
reference to a dangling pointer.
3) we try to free the memory using the wrong function.
* Two rules should be observed for any C program related to
dynamic memory management are:
1) Any memory that is dynamically reserved with malloc,
calloc, or realloc must be deallocated through using free().
2) When the program executes its last instruction and has
dynamic memory blocks that are not deallocated, it is considered incorrect.

You might also like