0% found this document useful (0 votes)
0 views

Command Line Argument,Typedef Keyword,Enum

Yuuuiiiijiiiiiiiiii

Uploaded by

rameshjaiswal365
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Command Line Argument,Typedef Keyword,Enum

Yuuuiiiijiiiiiiiiii

Uploaded by

rameshjaiswal365
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Command line

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};

Enumeration (or enum)


• The enum in C is also known as the enumerated type. It is a user-
defined data type that consists of integer values, and it provides
meaningful names to these values. The use of enum in C makes the
program easy to understand and maintain. The enum is defined by
using the enum keyword.
Note-:
If we assign a float value in a character value, then the compiler
generates an error. In the same way if we try to assign any other value
to the enumerated data types, the compiler generates an error.
Enumerator types of values are also known as enumerators. It is also
assigned by zero the same as the array. It can also be used with switch
statements.
• The following is the way to define the enum in C:
enum flag{integer_const1, integer_const2,.....integter_constN};
Conti.
• enum keyword is used to declare enumerated types
after that enumerated type name was written then
under curly brackets possible values are defined.
Note-:
By default, the starting code value of the first element of
enum is 0 (as in the case of array) . But it can be
changed explicitly.
For example: enum enumerated-type-
name{value1=1, value2, value3};
How to declare variable of enum
• Variables of type enum can also be defined. They can
be defined in two ways:

enum week{Mon, Tue, Wed};


enum week day; //
Or
enum week{Mon, Tue, Wed}day;
Example-1
// An example program to demonstrate working
// of enum in C
#include<stdio.h>

enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};

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

You might also like