Advance-C/C2/Command-line-arguments-in-C/English

From Script | Spoken-Tutorial
Revision as of 13:25, 22 September 2014 by Ashwini (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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 exampleWhat does this line mean
    Correct the sentence.


Display Slide

System Requirements

For this tutorial I am using
  • Ubuntu Linux 12.04 OS and
  • gcc Compiler version 4.6.1 on Ubuntu


Display Slide

Prerequisites


spoken-tutorial.org

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


If not, for relevant tutorials please visit spoken hyphen tutorial dot org.

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

Type at the terminal: ./args Sunday Monday Tuesday

(change in your program also) We see the output as:

Total number of arguments are 4

The first argument is Sunday

Argument is ./args Week days start with capital letters you can type them as I correctedReply to Madhuri (17/07/2014, 12:05): "..."

Ok



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,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


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

Slide is not as the scriptReply to Madhuri (18/07/2014, 11:04): "..."

It is same.

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