C Programme Notes
C Programme Notes
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];
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
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
}
}
return 0;
}
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;
}
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.
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);
}
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;
}