0% found this document useful (0 votes)
18 views1 page

Unit 4 Command Line Arguments

The document explains how to use command line arguments in C programs through the main() function with parameters argc and argv. It provides examples of how to check for arguments and demonstrates the behavior of passing multiple arguments, including the effect of using double quotes. The document emphasizes the importance of command line arguments for controlling program behavior externally rather than hard coding values.

Uploaded by

div29042
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views1 page

Unit 4 Command Line Arguments

The document explains how to use command line arguments in C programs through the main() function with parameters argc and argv. It provides examples of how to check for arguments and demonstrates the behavior of passing multiple arguments, including the effect of using double quotes. The document emphasizes the importance of command line arguments for controlling program behavior externally rather than hard coding values.

Uploaded by

div29042
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Command Line Arguments

It is possible to pass some values from the command line to your C programs when they are executed.
These values are called command line arguments and many times they are important for your program
especially when you want to control your program from outside instead of hard coding those values
inside the code.
The command line arguments are handled using main() function arguments. To pass command line
arguments, we typically define main() with two arguments.
int main(int argc, char *argv[]) { /* ... */ }

where argc refers to the number of arguments passed, and argv[] is a pointer array which points to each
argument passed to the program. Following is a simple example which checks if there is any argument
supplied from the command line and take action accordingly
#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]);
}
}

Run this program as follows in Linux:


./program hello
Run this program as follows in Windows from command line:
program.exe hello

Output:
Program name is: program
First argument is: hello
If you pass many arguments, it will print only one.
./program hello c how r u
Output:
Program name is: program
First argument is: hello

But if you pass many arguments within double quote, all arguments will be treated as a single argument
only.
./program "hello c how r u"
Output:
Program name is: program
First argument is: hello c how r u
You can write your program to print all the arguments. In this program, we are printing only argv[1],
that is why it is printing only one argument.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy