List of Different Specifiers in C

  • %c – Print a character
  • %d – Print a Integer
  • %i – Print a Integer
  • %e – Print float value in exponential form.
  • %f – Print float value
  • %g – Print using %e or %f whichever is smaller
  • %o – Print actual value
  • %s – Print a string
  • %x – Print a hexadecimal integer (Unsigned) using lower case a – F
  • %X – Print a hexadecimal integer (Unsigned) using upper case A – F
  • %a – Print a unsigned integer.
  • %p – Print a pointer value
  • %hx – hex short
  • %lo – octal long
  • %ld – long

Program using fprintf & fscanf functions in file Handling



#include< stdio.h >
/*Program using fprintf & fscanf functions in file Handling */
main()
{
FILE *fp;
int num,qty,I;
float price,value;
char item[10],filename[10];
printf(“Enter filename”);
scanf(“%s”,filename);
fp=fopen(filename,”w”);
printf(“Input Some data\n\n”0;
printf(“Enter Values for Item name number price quantity\n”);
for (I=1;I< =3;I++)
{
fscanf(stdin,”%s%d%f%d”,item,&number,&price,&quality);
fprintf(fp,”%s%d%f%d”,itemnumber,price,quality);
}
fclose (fp);
fprintf(stdout,”\n\n”);
fp=fopen(filename,”r”);
/*Reading data from file*/
printf(“Item name number price quantity value”);
for(I=1;I< =3;I++)
{
fscanf(fp,”%s%d%f%d”,item,&number,&prince,&quality);
value=price*quantity”);
fprintf(“stdout,”%s%d%f%d%d\n”,item,number,price,quantity,value);
}
fclose(fp);
}

Program using getw and putw functions in file Handling



#include< stdio.h >
main()
{
FILE *f1,*f2,*f3;
int number I;
printf(“Text inside the data file\n\n”);
f1=fopen(“DATA”,”W”);
for(I=1;I< 30;I++)
{
scanf(“%d”,&number);
if(number==-1)
break;
putw(number,f1);
}
fclose(f1);
f1=fopen(“DATA”,”r”);
f2=fopen(“ODD”,”w”);
f3=fopen(“EVEN”,”w”);
while((number=getw(f1))!=EOF)/* Reading data from the file*/
{
if(number%2==0)
putw(number,f3);/*Write to even Numbers file*/
else
putw(number,f2);/*write to odd Numbers file*/
}
fclose(f1);
fclose(f2);
fclose(f3);
f2=fopen(“ODD”,”r”);
f3=fopen(“EVEN”,”r”);
printf(“\n\nData From the odd file\n\n”);
while(number=getw(f2))!=EOF)
printf(“%d%d”,number);
printf(“\n\nData from the even file”);
while(number=getw(f3))!=EOF)
printf(“%d”,number);
fclose(f2);
fclose(f3);
}

Program to opening a file



FILE *fp;
fp=fopen(“filename”,”mode”);
FILE *p1, *p2;
p1=fopen(“data”,”r”);
p2=fopen(“results”,”w”);

List of File Handling operation functions in C

Name of Function

Operation of Function

  • fopen()

Creates a new file for use
Opens a new existing file for use

  • fclose

Closes a file which has been opened for use

  • getc()

Reads a character from a file

  • putc()

Writes a character to a file

  • fprintf()

Writes a set of data values to a file

  • fscanf()

Reads a set of data values from a file

  • getw()

Reads a integer from a file

  • putw()

Writes an integer to the file

  • fseek()

Sets the position to a desired point in the file

  • ftell()

Gives the current position in the file

  • rewind()

Sets the position to the beginning of the file

program to create and display link list



#include
#include
struct node
{
int data; //data field//
node *next; //pointer to next field//
};
class link_list
{
private:
node *start;
public:
link_list() //constructor//
{
start=NULL;
}
void add_data(int i)
{
node *new_link=new node;
new_link->data=i;
new_link->next=start;
//make new node to print to the first node//

start=new_link;
//make new node as the first node in the list//
}
void display(int no);
void search(int item);
void insloc(int item,int loc);
int find(int item);
};

void link_list::display(int no)
{
int i=1;
int n=no;
cout<<"Element of link list\n";
cout<<"Node no:\t"<<"Node data:\t"<<"Node location:\t"<<"Node link:\t";
node *move=start;
node *temp;
while(move)
{
cout< temp=move;
move=move->next; //move to next node//
if(i==n )
{
cout<data<<"\t"<\t"<<"NULL";
// delete temp;
break;
}

cout<data<<"\t"<\t"< // delete temp;
i++;
}
}

void link_list :: search(int item)
{
node *ptr=start;
while(ptr)
{
if(item>ptr->data)
{
ptr=ptr->next;
}
else
{
if(item==ptr->data)
{
cout<<"element"< return;
}
else
{
cout<<"element not fount";
return;
}
}
}
cout<<"element not found";
}
void link_list::insloc(int item,int loc)
{
node *newlink=new node;
newlink->data=item;
node *ptr=start;
//node *temp;
int i=1;
if(loc==0)
{
newlink->next=start;
start=newlink;
return;
}


while(ptr)
{
if(i==loc)
{
newlink->next=ptr->next;
ptr->next=newlink;
return;
}
i++;
ptr=ptr->next;
}
}
int link_list::find(int item)
{
int loc;
if(start==NULL;)
{
loc=0;
return loc;
}
node *ptr=start;
int save=1;
if(itemdata)
{
loc=0;
return loc;
}
ptr=ptr->next;
while(ptr)
{
if(itemdata)
{
loc=save;
return loc;
}
save++;
ptr=ptr->next;
}
loc=save;
return loc;
}
void main()
{

int i,a[10],item,n,loc;
link_list link;

cout<<"Enter number of elements";
cin>>n;
cout<<"Enter elements\n";
for(i=0;i {
cin>>a[i];
link.add_data(a[i]);
}
link.display(n);
cout<<"\nEnter item to insert";
cin>>item;
/* cout<<"\n enter location to insert";
cin>>loc;*/
loc=link.find(item);
link.insloc(item,loc);
n=n+1;
link.display(n);
getch();
}


What will be output of following program of C?


#include
void main()
{
int arr[]={1,2,3,4,5,6};
void xxx(int[5]);
xxx(arr);
}
void xxx(int ch[5])
{
printf("%d",-1[ch]);
}



Output: -2
Explanation:
We are passing the array by xxx function. 1[ch] means *(ch+1) which is ch[1] =2.

What is use of #line directive?

by using #line directive compiler that next line of source code is at the line number which has been specified by constant in #line directive i.e it transfer the program control to the line number which has been specified by #line directive.

Ex :

#line 15
void main()
{
int a=10;
a++;
clrscr();
a++;
#line 5
printf(“%d”,a);
getch();
}

What is #error directives ?

The Error Message Given by c Compiler, so no see the details information of an error message..

Syntax :
#error
If compiler compiles this line then it shows a compiler fatal error i.e it only issue an error message and this error message includes . i.e it only issue an error message and this error message includes .
e.g :

#ifndef __MATH_H
#error First include
then compile
#else
void main()
{
float a,b=25;
a=sqrt(b);
printf(“%f”,a);
}
#endif


Output: compiler error --> Error directive :First include then compile

What is use of # and ## operator in c program ?

There are two operator in preprocessor.
1. # this operator is called stringizing operator which convert any argument in the macro function in the string. So we can say pound sign # is string maker.
e.g
#define string(s) #s
void main()
{
char str[15]=string(World is our ) ;
printf(“%s”,str);
}

Output: World is our
Explanation : Its intermediate file is :


Argument of string macro function ‘World is our’ is converted into string by the operator # .Now the string constant “World is our” is replaced the macro call function in line number 4.
2. ##

This operator is called token pasting operator. When we use a macro function with various argument then we can merge the argument with the help of ## operator.
e.g
#define merge(p,q,r) p##q##r
Void main()
{
int merge(a,b,c)=45;
printf(“%d”,abc);
}

Output : 45
Explanation :
Arguments a,b,c in merge macro call function is merged in abc by ## operator .So in the intermediate file declaration statement is converted as :
int abc=45;

What is file inclusion directive or #include directive ?

#include <>

or

#include “file name.h”
This directive treats has included in the current file i.e in current file also contain data of the file which has included by #include directive.

e.g
first create a file of file name
cube.h which contain :


int cube ( int a)
{
Int b;
B=(a)*(a)*(a);
Return b;
}


Now create any another c file let math.c which contain :

#include “cube.h”
void main()
{
int p=5,q;
q=cube(p);
printf(“%d”,q);
}

How can take a string from command line with main function has no parameter and convert the string in uppercase?

void main()

{

char str[15];

int i=0;

strcpy(str,_argv[1]);

for(i=0;i<=strlen(str);i++)

{

if(str[i]>=97&&str[i]<=122)

str[i]=str[i]-32;

}

printf("\nstring in uppercase : %s",str);

}

Explain macro substitution directive?

Syntax: #define [ ,, …]
Here [,,…] is optional.

When you use this in the program then in the program this is called macro and #define directive only replaces the macro by before starting of actual compilation.

e.g :

#define pie 3.14
Void main()
{
float r=3,area;
area=3*r*pie;
printf(“%f”,area);
getch();
}


Before the starting of actual compilation an intermediate is formed which is :

We can see only in place of pie ,3.14 has pasted.
If
is very long or we want to write in next line ,end first line by \.

e.g :
#define word c is powerful
language.
MACRO FUNCTION:

program to create an display link list


/*program to create an display link list*/
#include
#include
struct node
{
int data; //data field//
node *next; //pointer to next field//
};
class link_list
{
private:
node *start;
public:
link_list() //constructor//
{
start=NULL;
}
void add_data(int i)
{
node *new_link=new node;
new_link->data=i;
new_link->next=start;
//make new node to print to the first node//

start=new_link;
//make new node as the first node in the list//
}
void display(int no);
void search(int item);
void insloc(int item,int loc);
int find(int item);
};

void link_list::display(int no)
{
int i=1;
int n=no;
cout<<"Element of link list\n";
cout<<"Node no:\t"<<"Node data:\t"<<"Node location:\t"<<"Node link:\t";
node *move=start;
node *temp;
while(move)
{
cout<next; //move to next node//
if(i==n )
{
cout<<<"\t"<<"\t\t"<data<<"\t"<<<"->\t"<<"NULL"; //
delete temp;
break;
}
cout<<<"\t"<<"\t\t"<data<<"\t"<<<"->\t"<ptr->data)
{
ptr=ptr->next;
}
else
{
if(item==ptr->data)
{

cout<<"element"<<<"found at location"<<<"element not fount";
return;
}
}
}
cout<<"element not found";
}
void link_list::insloc(int item,int loc)
{
node *newlink=new node; newlink->data=item;
node *ptr=start;
//node *temp;
int i=1;
if(loc==0)
{
newlink->next=start;
start=newlink;
return;
}


while(ptr)
{
if(i==loc)
{
newlink->next=ptr->next;
ptr->next=newlink;
return;
}
i++;
ptr=ptr->next;
}
}
int link_list::find(int item)
{
int loc;
if(start==NULL;)
{
loc=0;
return loc;
}
node *ptr=start;
int save=1;
if(itemdata)
{
loc=0;
return loc;
}
ptr=ptr->next;
while(ptr)
{
if(itemdata)
{
loc=save;
return loc;
}
save++;
ptr=ptr->next;
}
loc=save;
return loc;
}
void main()
{

int i,a[10],item,n,loc;
link_list link;

cout<<"Enter number of elements"; cin>>n;
cout<<"Enter elements\n"; for(i=0;i>a[i];
link.add_data(a[i]);
}
link.display(n);
cout<<"\nEnter item to insert"; cin>>item;
/* cout<<"\n enter location to insert"; cin>>loc;*/
loc=link.find(item);
link.insloc(item,loc);
n=n+1;
link.display(n);
getch();
}



Program to delete Elements from given Array in C++



#include
#include

class Array
{
private:int loc1,i,a[20],n;
public:void input();
void delete1();
};

void Array::input()
{
cout<<"\nEnter the Number Of Elements in an array : ";
cin>>n;

cout<<"\nEnter The Elements in an Array :";
for(i=0;i>a[i];

}

void Array::delete1()
{
cout<<"\nEnter the Position from where you want to Delete: ";
cin>>loc1;

for(int j=loc1;j<<"\nUpdated Array is :";
for(i=0;i<=n-1;i++)
cout<<<" ";
}
void main()
{
Array s;
s.input();
s.delete1();
getch();
}

Program to delete a Node from given List.



#include
#include
struct node
{
int info;
node *link;
};
class linklist
{
private:
node *start;
public:
linklist()
{
start=NULL;
}
void addnode(int i)
{
node *newlink=new node;
newlink->info=i;
newlink->link=start;
start=newlink;
}
void display(int no);
void search(int item);
void insloc(int item,int loc);
int find(int item);
void del(int loc,int locp);

};
void linklist::display(int no)
{
int i=1;
int n=no;
node *move=start;
node *temp;
cout<<"node no\t"<<"node info\t\t"<<"location\t\t"<<"link\n";
while(move)
{
temp=move;
move=move->link;
if(i==n)
{
cout<<<"\t\t"<info<<"\t\t"<<<"\t\t"<<"null\n";
i++;
break; } cout<<<"\t\t"<info<<"\t\t"<<<"\t\t"<<ptr->info)
{
ptr=ptr->link;
}
else if(item==ptr->info)

{
cout<<"Element"<<<"found at\t"<<<"element not found";
return;
}
}
cout<<"element not found";
}
void linklist :: insloc(int item , int loc)
{
int i=1;
node *newlink=new node;
newlink->info=item;
if(loc==0)
{
newlink->link=start;
start=newlink;
return;
}
node *ptr=start;
while(ptr)
{
if(i==loc)
{
newlink->link=ptr->link;
ptr->link=newlink;
return;
}
ptr=ptr->link;
i++;
}
}


int linklist :: find (int item)
{
node *ptr=start;
int loc;
int save=1;
if(start==NULL)
{
loc=0;
return loc;
}
if(iteminfo)
{
loc=0;
return loc;
}
ptr=ptr->link;

while(ptr)
{
if(iteminfo)
{
loc=save;
return loc;
}
save++;
ptr=ptr->link;
}
loc=save;
return loc;

}

void linklist :: del(int loc,int locp)
{
node *ptrloc=start;
node *ptrlocp;
int i=1;
if(locp==0)
{
start=start->link;

}
else
{
while(ptrloc)
{
ptrlocp=ptrloc;
ptrloc=ptrloc->link;
if(i==locp)
{
ptrlocp->link=ptrloc->link;
return;
}
i++;

}

}
}
void main()
{
int n,i,a[10],item,loc,locp;
linklist link;
cout<<"enter no. of nodes\n"; cin>>n;
cout<<"enter value\n"; for(i=0;i>a[i];
link.addnode(a[i]);
}
link. display(n);
cout<<"enter of search"; cin>>item;

cout<<"enter location to delete"; cin>>loc;
locp=loc-1;
link.del(loc,locp);
n=n-1;
link.display(n);
getch();
}

PROGRAM FOR BINARY SEARCH in C++



#include
#include
class Bsearch
{
private:
int beg,end,mid,lb,ub,item,i,n,loc;
int data[10];
public:
void create();
void display();
void Binsearch();
};

void Bsearch::create()
{
cout<<"\n enter data in ascending order";
cout<<"\n enter no of elements of data\n";
cin>>n;
lb=0;
ub=n-1;
cout<<"\n enter elements";
for(i=1;i<=n;i++)
{
cin>>data[i];

}
}

void Bsearch::display()
{
cout<<"elements of data";
for (i=1;i<=n;i++)
{
cout<<<"\t";
}
}
void Bsearch::Binsearch()
{
cout<<"\n enter item to search\n";
cin>>item;
//refer Binary saerch algo.
//step1
beg=lb;
end=ub;
mid=int(beg+end)/2;
//step2
while((beg<<"element found at location\t"<<<"element not found";
}
}


void main()
{
Bsearch obj;
obj.create();
obj.display();
obj.Binsearch();
getch();

}



Output:

enter data in ascending order
enter no of elements of data
5

enter elements12
13
14
15
16
elements of data12 13 14 15 16
enter item to search
15
element found at location 4
Press any key to continue




What are merits and demerits of array in c?

(1) We can easily access each element of array.
(2) Not necessity to declare two many variables.
(3) Array elements are stored in continuous memory location.

Write a c program to find out factorial of given number using function recursion.



void main()
{
long num,f;
clrscr();
printf("Input a number: ");
scanf("%ld",&num);
f=fact(num);
printf("\nFactorial is %ld",f);
getch();
}
int fact(long n)
{
if(n==0)
return 1;
else
return(n*fact(n-1));
}

How will you modify a const variable in c?



We can modify the const variable with the help of pointer.

void main(){
const int a=10;
int *ptr=(int *)&a;
*ptr=20;
clrscr();
printf("%d",a);
getch();
}

Output: 20

How to Write a c program without using any semicolon which output is: Hello word.


void main(){
if(printf("Hello world")){
}
}

Solution: 2

void main(){
while(!printf("Hello world")){
}
}

Solution: 3

void main(){
switch(printf("Hello world")){
}
}