c
c
***********
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
main
int main()
1. Arithmetic operators
2. relational operators
3. logical operators
4. bit-wise operators
6. conditional operator
7. assignment operators
8. Hierarchy of operators
1. Arithmetic operators
2. relational operators
>
>=
<
<=
==
3. logical operators
&& (logical and)
|| (logical or)
! (logical not)
4.bit-wise operator:
| (bitwise or)
^ (bitwise xor)
i=i+1
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)
Control Statements:
-> if condition
if condition:
-> simple if
-> if ...else
-> nested if
if (i==10)
-> if ..else:
if (i==10)
else{
-> nested if
if (i<10)
if (i>5)
else
else
}
10. Write a C program explaining the use of relational operators
-> 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:
break;
case 2:
break;
default:
break;
break;
case 2:
break;
default:
printf("Default");
return 0;
}
14.Write a C program to explain the use of switch case also
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:
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)
for(i=1;i<100;i=i+1)
{
printf("\n %d",i);
for(initial value;condition;increment/decrement)
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
5. goto
1. auto
2. static
3. extern
4. register
1. auto:
-> when it will die (once you come out of the block/function
We are telling the compiler that store this variable in CPU register memory.
Syntax:
register int i;
-> when it will die (once you come out of the block/function
3.Static:
-> when it will die (once you come out of the program)
int main()
if(i>5)
{
exit(0);
else
i=i+1;
main();
return 0;
void display()
printf("
i=i+1;
int main()
display();
display();
display();
return 0;
}
6. Write a C program explaining the use of Auto Storage Class
Extern:
Step 1:
Step 2:
right click on the project -> add files -> right click on main.c -> paste ->
int i=100;
Step 3:
-> extern i;
extern display();
display();
Functions:
Reusable code.
syntax:
returntype functioname(parameters)
ex:
int addition();
void display();
Declaration:
is the :
-> parameters/arguments
int addition();
void display();
ex:
int addition()
printf("xyz");
2.
Function Call:
arguments.
ex:
display();
addition(10,20);
#include <stdio.h>
#include <stdlib.h>
void myfunction()
}
int main()
myfunction();
return 0;
inside a function.
1. Addition
2. Subtraction
3. Multiplication
4. Divison
16. Write a C Program to Check Whether a Number is Palindrome or Not Using While Loop and function.
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:
1. Call by value
Call by value:
-> When you call a function using the address of the parameter,
in one line:
in functions
Pointer:
type *var-name;
Ex:
int i=10;
int *j=&i;
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
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
#include <stdio.h>
#include <stdlib.h>
int main()
int i=10;
return 0;
ex:
int i;
int *j=&i;
int i;
int *j=&i;
int i;
int *j=&i;
*****
*****
*****
*****
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
why?
Array:
or
ex:
int a[10];
initialize an array:
int a[10]={100,200,300,400,500,600,650,700,750,800};
of a 1D array.
9. Write a program in C to copy the elements of one array into another array.
Important questions:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[10]={100,200,300,400,500,600,650,700,750,800};
for(int i=0;i<10;i++)
printf("\n %d",a[i]);
return 0;
otherwise
bookmyshow
ex:
int a[5];
ex:
using pointers
->malloc()
->calloc()
->realloc()
->free()
-> stdlib.h
-> malloc.h
malloc:
syntax:
pointer=(void *)malloc(noofblocks*sizeof(datatype));
ex:
1. int *a;
a=(int *)malloc(10*sizeof(int));
float *a;
a=(float *)malloc(10*sizeof(float));
char *a;
a=(char *)malloc(10*sizeof(char));
double *a;
a=(double *)malloc(10*sizeof(double));
1. Write a C program to scanf integer array and print all the elements.
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).
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++)
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:
2. Binary Search
2 dimensional array (2d array):
how to declare:
int a[3][3]= {
{10,20,30},
{40,50,60},
{70,80,90}
};
for(int j=0;j<3;j++)
printf("%d ",a[i][j]);
printf("\n");
}
2D array using dynamic memory allocation
2 steps:
int **p;
p=(int **)malloc(9*sizeof(int));
for(i=0;i<9;i++)
p[i]=(int *)malloc(3*sizeof(int)
17. Write a C program to take the elements of 2D from keyboard , display the values
19. Write a C program to scan 2D array and find out the sum of all the elements
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.
*********************************************************************************
Macros:
Functions:
Preprocessor directives:
-> macros
Macro:
-> variables
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
#define DATE 31
int main()
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
int main()
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()
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>
int main()
int a = 18;
int b = 76;
return 0;}
compiler will paste the code where the macro is called.
syntax:
#define
Variable:
#define i 100
macro Block:
3. undef a variable
conditional compilation:
#define i 100
#ifdef i
printf("I am good");
#else
#endif
-> #ifdef
-> #else
-> #ifndef
-> #undef
File operations:
1. open a file
2. close a fille
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");
Syntax:
ch=fgetc(fptr)
4. fclose
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)
else
while((ch=fgetc(ptr))!=EOF)
printf("%c",ch);
fclose(ptr);
return 0;
}
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.
hint: open first file in read mode, second file in write mode
--- 0
--x 1
-w- 2
-wx 3
r-- 4
r-x 5
rw- 6
rwx 7
-> chmod u=rwx file3.txt
Bitwise Operators:
Bitwise Or (|)
shift right(>>)
int j=4;
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;
return 0;}
Array of structures:
s->val1=100;
s->var2=10.25;
->
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
enums: An enum is a special type that represents a group of constants (unchangeable values).
#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;
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;
return 0;
}
Printing the values of linked list in reverse order:
if(temp==NULL)
return;
else
displaylist(temp->next);
if(temp==NULL)
return;
else
displaylist(temp->next);
#include<stdio.h>
#include<stdlib.h>
struct node
int d;
};
int main()
head=NULL;
for(int i=0;i<5;++i)
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;
Stack
Queue
Arrays
Stack:
push (element)
pop()(removing element)
top - index
=====
top=-1
void push(int x)
if(top==size)
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:
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.
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()
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
The C programming language provides a keyword called typedef, which you can use to give a type a new
name.
You can use typedef to give a name to your user defined data types as well.
char title[50];
char author[50];
char subject[100];
int book_id;
} Book;