Difference between revisions of "Advance-C/C2/Command-line-arguments-in-C/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "'''Title of script''': '''Command Line Arguments''' '''Author: '''Ashwini Patil '''Keywords: '''Video tutorial,''' '''Command Line Arguments, main() {| style="border-spac...")
 
 
(One intermediate revision by one other user not shown)
Line 17: Line 17:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Display Slide  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Display Slide  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial, -we will learn about
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial, we will learn about
 
+
* '''main function with arguments''' with an example.
* '''main function with arguments with an exampleWhat does this line mean<br/> Correct the sentence.
+
 
+
 
+
  
 
|-
 
|-
Line 28: Line 25:
 
'''System Requirements'''
 
'''System Requirements'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| For this tutorial I am using  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| For this tutorial I am using  
 
+
* '''Ubuntu Operating system version 11.10''' and  
* '''Ubuntu Linux 12.04''' '''OS''' and  
+
 
* '''gcc Compiler version 4.6.1 on Ubuntu'''
 
* '''gcc Compiler version 4.6.1 on Ubuntu'''
 
 
  
 
|-
 
|-
Line 38: Line 32:
  
 
'''Prerequisites'''
 
'''Prerequisites'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To follow this tutorial, you should be familiar with '''C''' tutorials.
  
  
'''spoken-tutorial.org '''
+
If not, for relevant tutorials please visit our website which is as shown.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To follow this tutorial you should be familiar with C tutorials.
+
 
+
 
+
If not, for relevant tutorials please visit''' spoken hyphen tutorial dot org.'''
+
  
 
|-
 
|-
Line 59: Line 50:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let me explain the program.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let me explain the program.
 
 
 
  
 
|-
 
|-
Line 67: Line 55:
  
 
<nowiki>#include <stdlib.h> </nowiki>
 
<nowiki>#include <stdlib.h> </nowiki>
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| These are the header files.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| These are the '''header files.'''
 
+
'''stdio.h '''defines core input and output functions.
+
 
+
'''stdlib.h''' header file defines,
+
 
+
* Numeric conversion function.
+
* Pseudo-random numbers.
+
* Generation function.
+
* Memory allocation.
+
* Process control functions.
+
  
 +
'''stdio.h '''defines '''core input''' and '''output functions'''.
  
 +
'''stdlib.h header file''' defines-
 +
* '''Numeric conversion function'''.
 +
* '''Pseudo-random numbers'''.
 +
* '''Generation function'''.
 +
* '''Memory allocation'''.
 +
* '''Process control functions'''.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''int main(int argc, char **argv)'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''int main(int argc, char **argv)'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our main function.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our '''main function.'''
  
Inside this we have passed two arguments.
+
Inside this, we have passed '''two arguments'''-
  
 
'''int argc, char **argv'''
 
'''int argc, char **argv'''
Line 91: Line 76:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| int main(int argc, char **argv)  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| int main(int argc, char **argv)  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''“argc”''' refers to the number of command line arguments passed to the program.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''“argc”''' refers to the number of '''command line arguments''' passed to the program.
  
 
This includes the actual name of the program.
 
This includes the actual name of the program.
Line 97: Line 82:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Argv''' contains actual arguments.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''argv''' contains actual '''arguments''' starting from '''index 0'''.
  
Starting from index 0.
+
'''Index 0''' is the name of the program.
  
Index 0 is the name of the program.
+
'''Index 1''' will be the first '''argument''' passed to the program.
  
Index 1 will be the first argument passed to the program.
+
'''Index 2''' will be the second '''argument''' passed to the program.
 
+
Index 2 will be the second argument passed to the program.
+
  
 
And so on.
 
And so on.
Line 111: Line 94:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''printf("argc is %d\n",argc); '''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''printf("argc is %d\n",argc); '''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This statement will display the total number of arguments passed to the program.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This statement will display the total number of '''arguments''' passed to the program.
  
 
|-
 
|-
Line 117: Line 100:
  
 
'''<nowiki>printf("argv is %s\n",argv[1]);</nowiki>'''
 
'''<nowiki>printf("argv is %s\n",argv[1]);</nowiki>'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This will display the 1st argument passed to the program.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This will display the '''first argument''' passed to the program.
  
1 represents the argument at index 1.
+
'''1''' represents the '''argument''' at '''index 1'''.
  
 
|-
 
|-
Line 125: Line 108:
  
 
'''while(argc--)'''
 
'''while(argc--)'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| While condition will decrement the number of arguments.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''While condition''' will decrement the number of '''arguments'''.
  
 
|-
 
|-
Line 131: Line 114:
  
 
'''printf("arguments are %s\n", *argv++);'''
 
'''printf("arguments are %s\n", *argv++);'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This statement will print all the arguments passed to the program.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This statement will print all the '''arguments''' passed to the program.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''return 0;'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''return 0;'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| At the end we have''' return 0 '''statement'''.'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| At the end we have''' return 0 '''statement.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Press '''Ctrl+Alt+T'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Press '''Ctrl+Alt+T'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us open the terminal by pressing '''Ctrl+Alt+T''' keys.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us open the '''terminal''' by pressing '''Ctrl+Alt+T''' keys simultaneously on your keyboard.
 
+
 
+
 
+
  
 
|-
 
|-
Line 148: Line 128:
  
 
'''Type: gcc main-with-args.c -o args'''
 
'''Type: gcc main-with-args.c -o args'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type:''' gcc main-with-args.c -o args'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type:''' gcc space main-with-args.c -o args'''
  
 
Press''' Enter'''
 
Press''' Enter'''
Line 156: Line 136:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type:''' ./args'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type:''' ./args'''
  
Press''' Enter'''
+
Press''' Enter'''.
  
 
|-
 
|-
Line 172: Line 152:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Command line arguments '''are given during execution.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Command line arguments '''are given during execution.
  
'''Total number of arguments are''' '''1''' as the zeroth argument is the executable filename itself.
+
'''Total number of arguments are 1''' as the '''zeroth argument''' is the executable filename itself.
  
'''The first argument is (null) '''as we have not passed any argument to the program.  
+
'''The first argument is (null) '''as we have not passed any '''argument''' to the program.  
  
Arguments are only one ie. '''./args'''
+
'''Arguments''' are only one ie. '''./args'''
  
 
|-
 
|-
Line 189: Line 169:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us execute again
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us execute again
  
Type at the terminal: '''./args Sunday Monday''' '''Tuesday'''
+
Press the uparrow key.
 +
 
 +
'''space''' type '''Sunday space Monday space Tuesday'''
 +
 
 +
Press '''Enter'''
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''(change in your program also)'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''(change in your program also)'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We see the output as:
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we can see the output:
  
 
'''Total number of arguments are 4 '''
 
'''Total number of arguments are 4 '''
Line 199: Line 183:
 
'''The first argument is Sunday '''
 
'''The first argument is Sunday '''
  
'''Argument is ./args Week days start with capital letters you can type them as I corrected''Reply to Madhuri (17/07/2014, 12:05): "..."''
+
'''Argument are ./args '''
  
'''Ok'''
+
'''Sunday Monday ''' and '''Tuesday '''.
 
+
 
+
 
+
 
+
'''Argument is Sunday '''
+
 
+
'''Argument is Monday '''
+
 
+
'''Argument is Tuesday '''
+
  
 
|-
 
|-
Line 221: Line 196:
  
  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Total number of arguments are 4 '''as '''./args, Sunday, Monday,Tuesday.'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Total number of arguments are 4 '''as '''./args, Sunday, Monday and Tuesday.'''
  
'''The first argument is Sunday '''
+
The '''first argument''' is '''Sunday '''
  
The '''zero'''th argument always gives “executable filename”
+
The '''zeroeth argument''' always gives executable filename.
  
'''Sunday''' is assigned to '''first argument''' .
+
'''Sunday''' is assigned to '''first argument'''.
  
 
'''Monday '''is assigned to '''second argument.'''
 
'''Monday '''is assigned to '''second argument.'''
  
'''Tuesday '''is assigned to '''third argument'''  
+
'''Tuesday '''is assigned to '''third argument'''.
  
  
Line 247: Line 222:
 
* '''argc'''
 
* '''argc'''
 
* '''argv'''
 
* '''argv'''
 
Slide is not as the script''Reply to Madhuri (18/07/2014, 11:04): "..."''
 
 
'''It is same.'''
 
  
 
|-
 
|-
Line 256: Line 227:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| As an assignment,
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| As an assignment,
  
* Execute the program with different arguments.
+
* Execute the program with different '''arguments'''.
 
+
 
+
  
 
|-
 
|-

Latest revision as of 16:52, 22 September 2014

Title of script: Command Line Arguments

Author: Ashwini Patil

Keywords: Video tutorial, Command Line Arguments, main()


Visual Cue
Narration
Display Slide Hello and welcome to the spoken tutorial on Command Line Arguments.
Display Slide In this tutorial, we will learn about
  • main function with arguments with an example.
Display Slide

System Requirements

For this tutorial I am using
  • Ubuntu Operating system version 11.10 and
  • gcc Compiler version 4.6.1 on Ubuntu
Display Slide

Prerequisites

To follow this tutorial, you should be familiar with C tutorials.


If not, for relevant tutorials please visit our website which is as shown.

Show the pointer to Let us start with our program.

I have a code file.

I will open it.

Filename is main-with-args.c

Let me explain the program.
#include <stdio.h>

#include <stdlib.h>

These are the header files.

stdio.h defines core input and output functions.

stdlib.h header file defines-

  • Numeric conversion function.
  • Pseudo-random numbers.
  • Generation function.
  • Memory allocation.
  • Process control functions.
int main(int argc, char **argv) This is our main function.

Inside this, we have passed two arguments-

int argc, char **argv

int main(int argc, char **argv) “argc” refers to the number of command line arguments passed to the program.

This includes the actual name of the program.

argv contains actual arguments starting from index 0.

Index 0 is the name of the program.

Index 1 will be the first argument passed to the program.

Index 2 will be the second argument passed to the program.

And so on.

printf("argc is %d\n",argc); This statement will display the total number of arguments passed to the program.
[highlight]

printf("argv is %s\n",argv[1]);

This will display the first argument passed to the program.

1 represents the argument at index 1.

[highlight]

while(argc--)

While condition will decrement the number of arguments.
[highlight again!!]

printf("arguments are %s\n", *argv++);

This statement will print all the arguments passed to the program.
return 0; At the end we have return 0 statement.
Press Ctrl+Alt+T Let us open the terminal by pressing Ctrl+Alt+T keys simultaneously on your keyboard.
Compile

Type: gcc main-with-args.c -o args

Type: gcc space main-with-args.c -o args

Press Enter

Execute Type: ./args

Press Enter.

Point to the output on the Terminal You can see the output as:

Total number of arguments are 1

The first argument is (null)

arguments are ./args

Command line arguments are given during execution.

Total number of arguments are 1 as the zeroth argument is the executable filename itself.

The first argument is (null) as we have not passed any argument to the program.

Arguments are only one ie. ./args

Highlight:

Type:

./arg.sh Sunday Monday Tuesday


Now let us execute again

Press the uparrow key.

space type Sunday space Monday space Tuesday

Press Enter

(change in your program also) Now we can see the output:

Total number of arguments are 4

The first argument is Sunday

Argument are ./args

Sunday Monday and Tuesday .

Let me explain the output.
Output:


Total number of arguments are 4 as ./args, Sunday, Monday and Tuesday.

The first argument is Sunday

The zeroeth argument always gives executable filename.

Sunday is assigned to first argument.

Monday is assigned to second argument.

Tuesday is assigned to third argument.


This brings us to the end of this tutorial.

Let us summarize.

Display slide

Summary

In this tutorial we learnt,
  • Command line arguments
  • argc
  • argv
Assignment As an assignment,
  • Execute the program with different arguments.
Display Slide


Watch the video available at the link shown below

It summarises the Spoken Tutorial project

If you do not have good bandwidth, you can download and watch it

Display Slide

Spoken Tutorial Workshops

The Spoken Tutorial Project Team

Conducts workshops using spoken tutorials

Gives certificates to those who pass an online test

For more details, please write to

contact@spoken-tutorial.org

Display Slide

Acknowledgement

Spoken Tutorial Project is a part of the Talk to a Teacher project

It is supported by the National Mission on Education through ICT, MHRD, Government of India

More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro

This is Ashwini Patil from IIT Bombay signning off.

Thank you for joining.

Contributors and Content Editors

Ashwini, Nancyvarkey