Difference between revisions of "Advance-C/C2/Command-line-arguments-in-C/English"
(Created page with "'''Title of script''': '''Command Line Arguments''' '''Author: '''Ashwini Patil '''Keywords: '''Video tutorial,''' '''Command Line Arguments, main() {| style="border-spac...") |
|||
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, | + | | 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 | + | * '''main function with arguments with an example''' |
Line 29: | Line 29: | ||
| 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 | + | * '''Ubuntu Operating system version 11.10''' and |
* '''gcc Compiler version 4.6.1 on Ubuntu''' | * '''gcc Compiler version 4.6.1 on Ubuntu''' | ||
Line 189: | Line 189: | ||
| 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 | ||
− | + | Press the uparrow key 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;"| | + | | 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 as: |
'''Total number of arguments are 4 ''' | '''Total number of arguments are 4 ''' | ||
Line 199: | Line 201: | ||
'''The first argument is Sunday ''' | '''The first argument is Sunday ''' | ||
− | '''Argument is ./args | + | '''Argument is ./args ''' |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
'''Argument is Sunday ''' | '''Argument is Sunday ''' | ||
Line 221: | Line 218: | ||
− | | 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 | + | | 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 ''' | ||
Line 248: | Line 245: | ||
* '''argv''' | * '''argv''' | ||
− | |||
− | |||
|- | |- |
Revision as of 15:07, 22 September 2014
Title of script: Command Line Arguments
Author: Ashwini Patil
Keywords: Video tutorial, Command Line Arguments, main()
|
|
---|---|
Display Slide | Hello and welcome to the spoken tutorial on Command Line Arguments. |
Display Slide | In this tutorial, we will learn about
|
Display Slide
System Requirements |
For this tutorial I am using
|
Display Slide
Prerequisites
|
To follow this tutorial you should be familiar with C tutorials.
|
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,
|
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 1st 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.
|
Compile
Type: gcc main-with-args.c -o args |
Type: gcc 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 Monday space Tuesday Press Enter |
(change in your program also) | Now we can see the output as:
Total number of arguments are 4 The first argument is Sunday Argument is ./args Argument is Sunday Argument is Monday Argument is 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 zeroth argument always gives “executable filename” Sunday is assigned to first argument . Monday is assigned to second argument. Tuesday is assigned to third argument
Let us summarize. |
Display slide
Summary |
In this tutorial we learnt,
|
Assignment | As an assignment,
|
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. |