Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
23_1-Command line Arguments.pptx
1. CREATED BY K. VICTOR BABU
COMPUTATIONAL THINKING FOR
STRUCTURED DESIGN
Topic:
COMMAND LINE ARGUMENTS
Mrs.U.Harita
Asst Professor
Department of BES-1
2. CREATED BY K. VICTOR BABU
AIM OF THE SESSION
To Understand the concept of Command Line Arguments
INSTRUCTIONAL OBJECTIVES
This Session is designed to:
1. To customize the behavior of a C program by passing input parameters at runtime.
2. Facilitate the testing and debugging of C programs by allowing developers to specify input
parameters easily and repeatedly during the development process.
LEARNING OUTCOMES
At the end of this session, you should be able to:
1.Understanding how to pass input parameters to a C program at runtime.
2.Familiarize with the main function's argc and argv arguments, and its usage.
3. CREATED BY K. VICTOR BABU
Command Line Arguments
Consider the declaration
int add(int a, int b);
add is a user defined function.
Two integers are passed as parameters to the function add()
Have you ever passed arguments to main() ?
Will main() accept any arguments?
KELF CTSD BES-1
4. CREATED BY K. VICTOR BABU
INTRODUCTION
• These are parameters passed to a program at runtime through the command
line.
•They enable programs to handle a wide range of input data and configuration
parameters without requiring prompts or input dialogs.
•This makes C programs more flexible and versatile, allowing them to perform
different tasks and operations based on user input.
KELF CTSD BES-1
5. CREATED BY K. VICTOR BABU
Session Description
• It reduces the need for manual input and enabling users to perform
tasks quickly and easily.
• They also make it easier to test and debug programs by providing a
way to automate the input data and configuration parameters used
during testing.
• They are useful when we want to control our program from outside
rather than hard coding the values inside the program.
KELF CTSD BES-1
6. CREATED BY K. VICTOR BABU
Command Line arguments
Syntax:
int main(int argc,char*argv[]);
The first argument i.e argc counts the number of arguments in the
command line
The argument argv[] is a pointer array that holds the pointer of type
char that points to arguments passed to the program.
KELF CTSD BES-1
7. CREATED BY K. VICTOR BABU
Sample Code : To Add two integers using
Command Line Arguments
#include <stdio.h>
#include<stdlib.h>
void main (int argc, char *argv[] )
{ int a,b,sum;
if ( arg!=3)
{ return -1;}
a=atoi(argv[1]);
b=atoi(argv[2]);
sum=a+b;
printf(“%d”,sum);
return 0}
KELF CTSD BES-1
8. CREATED BY K. VICTOR BABU
Program Execution
Steps to execute the program
1 Go to the command prompt and type cmd
2 c:usersharita>cd Documents
c:usersharitacd Documents> cla 5 5
10
KELF CTSD BES-1
9. CREATED BY K. VICTOR BABU
Command Line arguments – Sample Code 2
#include <stdio.h>
void main (int argc, char *argv[] )
{
print f("Program name is: %sn", argv[0]);
if (argc < 2)
{
printf ("No argument passed through command line.n");
}
else
{
printf ("First argument is: %sn", argv [1]); } }
KELF CTSD BES-1
10. CREATED BY K. VICTOR BABU
REFERENCES FOR FURTHER LEARNING OF THE SESSION
Reference Books:
1. Mark Allen weiss, Data Structures and Algorithm Analysis in C, 2008, Third Edition, Pearson Education.
2. Horowitz, Sahni, Anderson Freed, “Fundamentals of Data structures in C”, 2nd Edition2007.
3. Robert Kruse, C. L. Tondo, Bruce Leung, Shashi Mogalla, “Data structures and Program Design in C”, 4th Edition-2007
Sites and Web links:
1. www.hackerrank.com
2. www.codechef.com
Reference Books:
1. 1.) Brian W. Kernighan, Dennis M. Ritchie, “The C Programming Language: ANSI C Version”, 2/e, Prentice-Hall/Pearson Education-2005.
2.) E. Balagurusamy, “Programming in ANSI C” 4thed. Tata McGraw-Hill Education, 2008
Sites and Web links:
1) https://www.w3schools.com/c/c_arrays.php
2) https://www.includehelp.com/c-programs/c-programs-one-dimensional-arrays-problems-and-solutions.aspx
https://technictiming.com/c-programming/array/