Best Industrial Training in C,C++,PHP,Dot Net,Java in Jalandhar

Wednesday, 16 April 2014

Command line arguments in C:

Command line arguments in C:

      It is possible to pass some values from the command line to your C programs when they are executed. These values are called command line arguments and many times they are important for your program specially when you want to control your program from outside instead of hard coding those values inside the code.
The command line arguments are handled using main() function arguments where argc refers to the number of arguments passed, and argv[] is a pointer array which points to each argument passed to the program. 
argc      - Number of arguments in the command line including program name
argv[]   – This is carrying all the arguments
  • In real time application, it will happen to pass arguments to the main program itself.  These arguments are passed to the main () function while executing binary file from command line.
  • For example, when we compile a program (test.c), we get executable file in the name “test”.
  • Now, we run the executable “test” along with 4 arguments in command line like below.
this is a program
Where,
argc             =       5
argv[0]         =       “test”
argv[1]         =       “this”
argv[2]         =       “is”
argv[3]         =       “a”
argv[4]         =       “program”
argv[5]         =       NULL

Example program for argc() and argv() functions in C:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])   //  command line arguments
{
if(argc!=5) 
{
   printf("Arguments passed through command line " \
          "not equal to 5");
   return 1;
}

   printf("\n Program name  : %s \n", argv[0]);
   printf("1st arg  : %s \n", argv[1]);
   printf("2nd arg  : %s \n", argv[2]);
   printf("3rd arg  : %s \n", argv[3]);
   printf("4th arg  : %s \n", argv[4]);
   printf("5th arg  : %s \n", argv[5]);

return 0;
}

 Output:

Program name : test
1st arg : this
2nd arg : is
3rd arg : a
4th arg : program
5th arg : (null)
IN OTHER WORDS:
C provides a fairly simple mechanism for retrieving command line parameters entered by the user. It passes an argv parameter to the main function in the program. argv structures appear in a fair number of the more advanced library calls, so understanding them is useful to any C programmer.
Enter the following code and compile it:
#include <stdio.h>

int main(int argc, char *argv[])
{
    int x;

    printf("%d\n",argc);
    for (x=0; x<argc; x++)
        printf("%s\n",argv[x]);
    return 0;
}
In this code, the main program accepts two parameters, argv and argc. The argv parameter is an array of pointers to string that contains the parameters entered when the program was invoked at the UNIX command line. The argc integer contains a count of the number of parameters. This particular piece of code types out the command line parameters. To try this, compile the code to an executable file named aaa and type aaa xxx yyy zzz. The code will print the command line parameters xxx, yyy and zzz, one per line.
The char *argv[] line is an array of pointers to string. In other words, each element of the array is a pointer, and each pointer points to a string (technically, to the first character of the string). Thus, argv[0] points to a string that contains the first parameter on the command line (the program's name), argv[1] points to the next parameter, and so on. The argc variable tells you how many of the pointers in the array are valid. You will find that the preceding code does nothing more than print each of the valid strings pointed to by argv.
Because argv exists, you can let your program react to command line parameters entered by the user fairly easily. For example, you might have your program detect the word help as the first parameter following the program name, and dump a help file to stdout. File names can also be passed in and used in your fopen statements.

Thursday, 3 April 2014

Last Year C Questions??

1. // program to count characters,spaces,tabs,new lines in a file //
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
int c;
int b=0,t=0,nl=0;
clrscr();
fp=fopen("good.txt","w");
printf("enter the string:");
while((c=getchar())!=EOF)
{
if(c==' ')
++b;
if(c=='\t')
++t;
if(c=='\n')
++nl;
}
fprintf(fp,"input has %d blanks,%d tabs and %d newlines\n",b,t,nl);
fclose(fp);
getch();

}





2. what do you mean by structure? How they are declared and initialized? How they are different from arrays?

STRUCTURES—these are the collection of heterogeneous elements in simple words we can say that is a collection of different data types. Each element in structure is called member. If you want to access structure members in C language ,structure variable should be declared. Many structure variable can be declared for same structure and memory will be allocated for each separately.

STRUCTURE DECLARATION—
A structure declaration specifies the grouping of variables of different types in a single unit. It simply acts as a temple or blueprint that may be used to create structure variables.
The syntax for declaring a structure in C is:

Struct struct_name
{
data_type member1;
data_type member2;
……………………
data_type membern;
};

STRUCTURE INITIALIZATION—
Lets us consider an example of employee structure which is declared as follows’
Struct employee
{
int emp_no;
char emp_name[20];
int dapt_code;
double salary;
}
we can define and initialize structure variable emp1 of type employee as follows,
employee emp1={101,”raghav”,05,23453.50};

PROGRAM TO ILLUSTRATE—
#
#
struct employee
{
int empid;
char name[50];
char add[50];
float salary;
char email[50];
}s1;
void main()
{
clrscr();
scanf(“%d”,&s1.empid);
scanf(“%s”,&s1.name);
scanf(“%s”,&s1.add);
scanf(“%f”,&s1.salary);
scanf(“%s”,&s1.email);
printf(“%d”,s1.empid);
printf(“%s”,s1.name);
printf(“%s”,s1.add);
printf(“%f”,s1.salary);
printf(“%s”,s1.email);
getch();
}

DIFFERENCE BETWEEN STRUCTURES AND ARRAYS

STRUCTURES                                                      ARRAYS
1.they are a group of heterogeneous               1.they are defined as a finite set
elements.                                                             of homogeneous elements.
 2.syntax                                                                                                         
 struct struct_name                                                2.syntax:
{                                                                             storage_class datatype arrayname[size];
datatype  variable name;
----------------------------
----------------------------
};
3.initialization:                                                      3.initialization:
employee emp1={101,”raghav”,05,23453.50};    float digit[3]={0.25,0,0.50};
  

Diffrence between Pointer to Array and Array of Pointers?????


Wednesday, 2 April 2014

Self Referential Structures

Self Referential Structures
A structure may have a member whose is same as that of a structure itself.
Such structures are called self-referential.
Self-Referential Structure are one of the most useful features.
They allow you to create data structures that contains references to data of the same type as themselves.
Self-referential Structure is used in data structure such as binary tree, linked list, stack, Queue etc.
Example : Single linked list is implemented using following data structures
struct node {
int value;
struct node *next;

};














































In Other Words.Link List is: