Monday, July 8, 2013

Command line arguments in C Language

Command line arguments:
                Generally command line arguments are the parameters taken by the main function of any c language program. The main takes two arguments. First one is the number of arguments available on the command line including the program name and the user specified arguments the second parameter is the character array which originally holds the typed arguments. Characters between the two spaces are interpreted as the argument on the command line. For example on the command line we typed
code.exe visual studio 2010
then the code.exe is the program we wish to execute and the visual is the second command line argument studio is the third and 2010 is the fourth. The main function which takes the command line arguments takes two forms like
                main(int argc, char* argv[])
                main(int argc, char** argv)
In the first form the argument array is specified with the single pointer and the array. In the second one it is specified as the double pointer. Generally when the array is declared the array name acts as the pointer to the first element in the array. In the second case we used double pointer so there is no need of the array.

To access the command line arguments we can fallow the normal array elements accessing rules. But one must remember that the first command line argument is the currently executing program name so if we want to access the user intentionally typed arguments we have to start from the second array element. That is index is 1. The fallowing code snippets help to visualise the command line arguments easily.

#include "stdafx.h"
#include <conio.h>

void main(int argc, char* argv[])
{
int num_arg=argc,i;
printf("Number of arguments on the commandline are %d",num_arg);
for(i=0;i<num_arg;i++)
{
printf("The %d command line argument is %s",i+1,argv[i]);
puts("\n");
}
_getch();
}

the for loop in the code prints the arguments on the command line. one thing we have to remember is the fourth argument 2010 is also considered as the character array so if we want the numerical value we have to convert back to the integer value using some conversion available in the c standard library like "atoi" function. in the output windows we can see the first command line argument is the absolute path of the currently executing program.

#include "stdafx.h"
#include <conio.h>

void main(int argc, char** argv)
{
int num_arg=argc,i;
printf("Number of arguments on the commandline are %d",num_arg);
for(i=0;i<num_arg;i++)
{
printf("The %d command line argument is %s",i+1,argv[i]);
puts("\n");
}
_getch();
}

the second example is another code demonstrating the use of command line arguments. The path for the file to open is passed as the second command line argument of index one and it is used to save the command line arguments in it.

Monday, April 8, 2013

Structure pointer as the function argument

Generally structures are used to store different data types. sometimes we need to pass the structure as the parameter to the function. Arguments are stored in the stack. If the size of the structure is very large then storing that on stack becomes hard and data accessing becomes slow. so we can pass the pointer to the structure as the parameter instead of the structure. This enables the faster data access.

consider the structure

struct data
{
// All the data
};

For this declare a pointer. Structure is also a data type so we can create a pointer to this as the same way we create for the int ,char etc.
for integer we create pointer as int *p=NULL;
p is the pointer of type integer. which always points the integer type variables.
Similarly we can create pointers to the structure.
struct data *dt=NULL;
Here dt is the pointer to the user defined data type data. To access the structure variables we have to use arrow operator ->.

struct var
{
int a;
char c;
};

let p is the pointer to the structure var. To access the integer variable we need statement like var->a.
Generally allocating data to the pointer is good before using it. If we are using the pointer in the same function no need of allotting memory. If we need to use the same pointer f to pass data between functions we need to allot memory using dynamic memory allocation techniques like malloc , calloc in C language and by using new in C++.
We need memory allocation because after returning from functions all the variables initialised and used are deleted from heap except if the variable is used with memory allocation.

var* function1(void)
{
struct var *p=NULL;
p=(struct var*)malloc(sizeof(struct var));;
p->a=10;
p->c="A";
return p;
}

void function2(var* p)
{
int r=p->a;
printf("\n Value of r is %d",r);
}

Here we are using the structure explained above. we are passing structure as pointer from one function to another function. and in the second function we just printing the value of the integer a. We are allotting the memory to pointer dynamically. This is C type memory allocation.
The C++ type allocation is as fallows we need to modify the second statement of the function1.


var* function1(void)
{
struct var *p=NULL;
p=new var;
p->a=10;
p->c="A";
return p;
}

some times we need to hide the type of pointer like we need to pass the structure but the functions should not identify the pointer as pointer to structure. To achieve this we can use void pointers. Just it is casting of pointers.

consider the void pointer void* ptr1=NULL;
strcut var *ptr2=NULL;
ptr2=new var;
Here ptr1 and ptr2 are the two pointers pointing void and structure respectively.

ptr1=(void*)ptr2;


Here just casting operation is done. Now consider the previous example where we are passing pointer to structure as argument. Modify that to pass structure pointer as void pointer.


void* function1(void)
{
struct var *p=NULL;
p=new var;
p->a=10;
p->c="A";
void *ptr=NULL;
ptr=(void*)p;
return ptr;
}
 In the second function we need to convert the void pointer back to pointer to structure. Then second function looks like

void function2(void *vptr)
{
struct var *sptr=NULL;
sptr=(struct var *)vptr;
int r=sptr->a;
printf("\n Value of r is %d",r);
}

Saturday, March 23, 2013

Creating DLL with Visual Studio 2010 Express Edition

We can create DLL with one source file and one header file. Header file contains only the declaration of the functions exported from the DLL.

Header file: header.h is the name of the header file.
extern "C" __declspec(dllexport) returntype function1(argument);
extern "C" __declspec(dllexport) returntype function2(argument);

Source file source.cpp is the name of the source file this is the actual file containing the actual functionality of the exported functions.




#include<header.h> //include the header file containing the declaration of the exported functions in the dll
returntype function1(arguments)
{
write your code here
return statement
}

returntype function2(arguments)
{
write your code here
return
}



Wednesday, December 26, 2012

What is the output?






main(){int x=4,y,z;y=--x;z=x--;printf("\n %d %d %d",x,y,z);}main(){int x=4,y=3,z;z=x-- -y;printf("\n %d %d %d",x,y,z);}main(){int i;for(i=1;i<=5;printf("\n %d",i);i++;}

Decimal number to Octal number conversion using C language


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,n1,o=0,r,p=0;
printf("\n Enter the number to convert octal number");
scanf("%d",&n);
n1=n;
while(n>0)
{
r=n%8;
n=n/8;
o=o+r*pow(10,p);
p++;
}
printf("\n The octal equivalanet number of %d is %d",n1,o);
printf("\n Press any key to exit");
getch();
}

Tuesday, December 25, 2012

Checking for Valid Triangle using C Language


/* Checking for validity of Triangle when three sides are entered*/

#include<stdio.h>
#include<conio.h>
void main()
{
int s1,s2,s3,l,s;/*s1=side one s2=side two s3=side three l=large side s=sum*/
clrscr();
printf("\n Enter the three sides of triangle");
scanf("%d %d %d",&s1,&s2,&s3);
if(s1>s2)
{
if(s1>s3)
{
s=s2+s3;
l=s1;
}
else
{
s=s1+s2;
l=s3;
}
}
else
{
if(s2>s3)
{
s=s1+s3;
l=s2;
}
else
{
s=s1+s2;
l=s3;
}
}
if(s>l)
printf("\n The triangle is valid");
else
printf("\n The triangle is invalid");
printf("\n Press any key to exit");
getch();
}

Tuesday, December 11, 2012

Program to check the Position of point from the center of circle using C language


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int x1,y1,x2,y2,r;
float dis;
clrscr();
printf("\n Enter the radius of the circle and the center coordinates");
scanf("%d%d%d",&r,&x1,&y1);
printf("\n Enter the dsired coordinate points");
scanf("%d%d%",&x2,&y2);
dis=(x2-x1)^2+(y2-y1)^2;
if(dis==(r*r))
printf("\n Enterd coordinate point lies on the circle");
else
{
if(dis>(r*r))
printf("\n Point is outside the circle");
else
printf("\n point is inside the circle");
}
printf("\n press any key to exit");
getch();
}

Monday, December 10, 2012

Checking for Collinear coordinate points in C language

#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int x1,x2,x3,y1,y2,y3;
float s1,s2,s3;
printf("\n Enter the first point coordinates");
scanf("%d%d",&x1,&y1);
printf("\n Enter the second point coordinates");
scanf("%d%d",&x2,&y2);
printf("\n Enter the third point coordinates");
scanf("%d%d",&x3,&y3);
/* calculating slope between the points*/
s1=abs(x2-x1)/abs(y2-y1);
s1=abs(x3-x1)/abs(y3-y1);
s3=abs(x3-x2)/abs(y3-y2);
if((s1==s2)&&(s1==s3))
printf("\n Points entered are collinear");
else
printf("\n Points entered are not collinear");
printf("\n Press any key to exit");
getch();
}

Sunday, December 2, 2012

Stair case of numbers using FOR loop in C++


#include<iostream.h>
#include<conio.h>
int main()
{
int i,j,c;
clrscr();
cout<<"Enter the lines"<<"\n";
cin>>c;
for(i=0;i<c;i++)
{
for(j=0;j<=i;j++)
{
cout<<i;
}
cout<<"\n";
}
return 0;
}

Swapping using reference variables as arguments in C++


#include<iostream.h>
#include<conio.h>
void swap(int &,int &);
int main()
{
int a,b;
clrscr();
cout<<"enter the values"<<"\n";
cin>>a>>b;
cout<<"Values of a and b before swapping"<<"\n";
cout<<"a="<<a<<"\n"<<"b="<<b;
swap(a,b);
cout<<"\n"<<"The values after exchange are"<<"\n";
cout<<"a="<<a<<"\n"<<"b="<<b;
return 0;
}
void swap(int &c,int &d)
{
int t;
t=c;
c=d;
d=t;
}

Larger value in two numbers using conditional operator in C++


#include<iostream.h>
int main()
{
int a,b,r;
cout<<"Enter two numbers to calculate bigger number \n";
cin>>a>>b;
r=a>b?a:b;
cout<<"The largest number is"<<r;
return 0;
}

DC motor control with Pulse Width Modulation Part 1

DC Motor intro DC motor is a device which converts electrical energy into kinetic energy. It converts the DC power into movement. The typica...