Command Line Argument,Typedef Keyword,Enum
Command Line Argument,Typedef Keyword,Enum
argument,typedef
keyword,enum datatype
By
Dinesh Kumar Kushwaha
Command Line Arguments in C
• The arguments passed from command line are called
command line arguments. These arguments are
handled by main() function.
• To support command line argument, you need to
change the structure of main() function as given below.
int main(int argc, char *argv[] )
• Here, argc counts the number of arguments. It counts
the file name as the first argument.
• The argv[] contains the total number of arguments.
The first argument is the file name always.
Example-1
#include <stdio.h>
void main(int argc, char *argv[] ) {
printf("Program name is: %s\n", argv[0]);
if(argc < 2)
{
printf("No argument passed through command line.\n");
}
else
{
printf("First argument is: %s\n", argv[1]);
}
}
Example-2
#include <stdio.h>
void main(int argc, char *argv[] )
{
printf("Program name is: %s\n", argv[0]);
if(argc < 2)
{
printf("No argument passed through command line.\n");
}
else
{
printf("First argument is: %s\n", argv[1]); }
}
typedef in C
• The typedef is a keyword used in C programming to
provide some meaningful names to the already existing
variable in the C program. It behaves similarly as we
define the alias for the commands. In short, we can say
that this keyword is used to redefine the name of an
already existing variable.
• Syntax of typedef
typedef <existing_name> <alias_name>
• In the above syntax, 'existing_name' is the name of an
already existing variable while 'alias name' is another
name given to the existing variable.
Conti.
• For example, suppose we want to create a variable of
type unsigned int, then it becomes a tedious task if we
want to declare multiple variables of this type. To overcome
the problem, we use a typedef keyword.
typedef unsigned int unit;
• In the above statements, we have declared the unit variable
of type unsigned int by using a typedef keyword.
• Now, we can create the variables of type unsigned int by
writing the following statement:
unit a, b;
• instead of writing:
unsigned int a, b;
Example
#include <stdio.h>
int main()
{
typedef unsigned int unit;
unit i,j;
i=10;
j=20;
printf("Value of i is :%d",i);
printf("\nValue of j is :%d",j);
return 0;
}
Syntax:
enum enumerated-type-name{value1, value2, value3…..valueN};
int main()
{
enum week day;
day = Wed;
printf("%d",day);
return 0;
}
Example-2
// Another example program to demonstrate working
// of enum in C
#include<stdio.h>
enum year{Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};
int main()
{
int i;
for (i=Jan; i<=Dec; i++)
printf("%d ", i);
return 0;
}
Interesting facts about
initialization of enum.
•
1. Two enum names can have same value. For example, in the following C
program both ‘Failed’ and ‘Freezed’ have same value 0.
• Ex:
#include <stdio.h>
enum State {Working = 1, Failed = 0, Freezed = 0};
int main()
{
printf("%d, %d, %d", Working, Failed, Freezed);
return 0;
}
Output-: 1 0 0
Conti.
2. If we do not explicitly assign values to enum names, the compiler by
default assigns values starting from 0. For example, in the following C
program, sunday gets value 0, monday gets 1, and so on.
#include <stdio.h>
enum day {sunday, monday, tuesday, wednesday, thursday, friday, saturday};
int main()
{
enum day d = thursday;
printf("The day number stored in d is %d", d);
return 0;
}
Output-: The day number stored in d is 4
1 2 5 6 10 11 12
Conti.
• 3. We can assign values to some name in any order. All unassigned names
get value as value of previous name plus one.
• Ex:
#include <stdio.h>
enum day {sunday = 1, monday, tuesday = 5, wednesday, thursday = 10, friday, saturday};
int main()
{
printf("%d %d %d %d %d %d %d", sunday, monday, tuesday,
wednesday, thursday, friday, saturday);
return 0;
}
Output-:1 2 5 6 10 11 12