Thursday, July 18, 2013

Variable number of arguments in the function of c language

                  Generally if we want to add the variable number of integers or multiply the variable number of floats/integers we need separate functions for each number of arguments. Fortunately C language provides flexibility for the programmer. It supports the variable number of arguments in the function. The function signature fallows the fallowing syntax:
Returntype functionname(int n,...)

                  This is eclipse notation. The three dots serve for this purpose. To use this facility we have to add the stdarg header file. We need to use va_list this points to the list of the arguments. 
va_start 
   This must be called before using the argument from the list. The syntax of this is va_start(ap,count). First argument is the list and second argument is the number of arguments passed. 

va_arg(ap,datatype):
    This must be called every time to get the value of the argument in a loop. The first parameter is the list and second one is the datatype of the argument.

va_end(ap):
     At the end of program we have to use this to clear the resources used by the list other wise the resources will be there until the program terminates resulting in memory leaks (eating up the RAM).

Consider the fallowing example

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

using namespace std;

int add(int count,...)
{
int result=0;
va_list ap;
va_start(ap,count);
for(int i=0;i<count;i++)
{
result=result+va_arg(ap,int);
}
va_end(ap);
return result;
}

int main()
{
int r;
r=add(9,1,2,3,4,5,6,7,8,9);
cout<<"\n"<<"the result of summation is :"<<r;
r=add(3,1,2,3);
cout<<"\n"<<"the result of summation is :"<<r;
_getch();
return 0;
}

  The main function calls the two times the add function with the variable number of arguments. Observe that the first parameter is the number of arguments except the first one. The first call to the function has ten arguments the first one is the number of arguments and the renaming arguments are the values to calculate the addition.

Thursday, July 11, 2013

Directory Lister using c language

The command line argument is the folder to be listed. for example if we want ot list hte file names
in the folder images in the c drive we have to specify it as C:\images\.
we need dirent.h file for the code to work. it is open source header file.

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

int main(int argc,char ** argv)
{
DIR *dp;
struct dirent *ep;
if(argc<2)
{
printf("Insufficinet number of arguments");
return -1;
}
dp = opendir (argv[1]);
while(ep=readdir(dp))
{
puts(ep->d_name);
}
closedir(dp);
puts("Press any key to return");
_getch();
return 0;
}

           The program takes the one argument. that is the folder absolute path to get the file names with the extension. dp is the pointer to the folder or directory to be listed. ep points to the file in the folder. the while loop continuously iterates through the folder or directory. when there is no more files to list then this returns the null then the condition in the while loop becomes void and the loop breaks. ep is the pointer to the currently selected file in the directory. ep->d_name gives the file name currently pointed along with the extension of the file. after completing our task we have to close the directory which is opened in the program.
if we forgot to close then the directory will be closed when we exit the program. based on the need we can modify the code to get the desired files with the specific extension.



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.

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...