Command Line Arguments

1y ago
16 Views
3 Downloads
4.28 MB
16 Pages
Last View : 12d ago
Last Download : 3m ago
Upload by : Jewel Payne
Transcription

Command LineArgumentsplusVariable-Length ArraysSystems Programming Concepts

Command Line Arguments A C program must always have a functionnamed main. This function is directlyinvoked by the Linux/Unix system.main has two arguments conventionallynamed argc and argv.The argc argument is of type int andcorresponds to the number of argumentsprovided on the command line (including theprogram name as the first argument).Systems ProgrammingCommand Line Arguments2

Command Line Arguments The second argument to main, argv, isan array of pointers to strings.Each string contains the ASCII stringrepresentation of what is typed on theprogram command line.Systems ProgrammingCommand Line Arguments3

Command Line ArgumentsFor example, if the command line typedis:./prog3 file1 200argc will have the value 3andargv[0] points to string “./prog3”argv[1] points to string “file1”argv[2] points to string “200”.Systems ProgrammingCommand Line Arguments4

Command Line Arguments It is standard and safe programmingpractice for main to immediately checkto see if it has received the correctnumber of arguments from the Linuxcommand line.If there is a mismatch, main printsout a proper usage statement andimmediately ends the program.Systems ProgrammingCommand Line Arguments5

Command Line ArgumentsCommand line arguments intended as integer programparameters requires converting the ASCII stringrepresentation to an integer using the standard libraryfunction atoi {ascii-to-integer}.Example atoi#include stdio.h The string “2593” convert to int 2593#include stdlib.h int main (void){int i; // variable to hold converted stringi atoi (“2593”);printf( “%s%d\n”, “The string \”2593\” converts to int “ , i);return 0;}Systems ProgrammingCommand Line Arguments6

A Command Line Argument Sample Program/* An Example of the Use of Command Line Arguments */#include stdio.h #include stdlib.h #define SIZE 100Note: main function argumentsint main (int argc, char *argv[]){int i, samples, table[SIZE];char *samstring, *timestring; /* strings are needed */char *progstring;Check for number of argumentsif(argc ! 3)printf("Proper Usage is: com arg samples time\n");Systems ProgrammingCommand Line Arguments7

A Command Line Argument Sample Program}else{progstring argv[0];samstring argv[1];timestring argv[2];printf("Program %s\n", progstring);/* convert to integer */samples atoi(samstring);printf("Please enter %d samples\n", samples);for (i 0; i samples; i )scanf("%d", &table[i]);for (i 0; i samples; i )printf("sample[%d] %d\n", i 1, table[i]);printf("Time %d\n", atoi(timestring));}return;Systems Programming./com arg 3 500Program ./com argPlease enter 3 samples74510234952sample[1] 745sample[2] 1023495sample[3] 2Time 500Command Line Arguments8

Command Line Arguments Using command line arguments tospecify variable size for inputparameters is a preferred technique.Remember C treats the arguments asASCII strings.Use atoi to convert the strings tointegers.Systems ProgrammingCommand Line Arguments9

6.10 Variable-Length Arrays A variable-length array is an array whose length, or size,is defined in terms of an expression evaluated atexecution time.The program of Fig. 6.23 declares and prints severalVLAs.[Note: This feature is not supported in Microsoft VisualC .]Copyright Pearson, Inc. 2013. All Rights Reserved.Systems ProgrammingCommand Line Arguments10

6.10 Variable-Length Arrays (Cont.)Copyright Pearson, Inc. 2013. All Rights Reserved.Systems ProgrammingCommand Line Arguments11

6.10 Variable-Length Arrays (Cont.)Note: declarationsdeliberately come later!Copyright Pearson, Inc. 2013. All Rights Reserved.Systems ProgrammingCommand Line Arguments12

6.10 Variable-Length Arrays (Cont.)Copyright Pearson, Inc. 2013. All Rights Reserved.Systems ProgrammingCommand Line Arguments13

6.10 Variable-Length Arrays (Cont.)Copyright Pearson, Inc. 2013. All Rights Reserved.Systems ProgrammingCommand Line Arguments14

6.10 Variable-Length Arrays (Cont.)Copyright Pearson, Inc. 2013. All Rights Reserved.Systems ProgrammingCommand Line Arguments15

Review of Command Line Arguments main arguments– argc and argvatoi functionVariable-length arrays– Array declarations comes after variablelength inputs– Length defined at execution timeSystems ProgrammingCommand Line Arguments16

Command Line Arguments It is standard and safe programming practice for main to immediately check to see if it has received the correct number of arguments from the Linux command line. If there is a mismatch, main prints out a proper usage statement and immediately ends the program. Systems Programming Command Line Arguments 5

Related Documents:

MATLAB Programming Tips 16 Function Arguments This section covers the following topics: “Getting the Input and Output Arguments” “Variable Numbers of Arguments” “String or Numeric Arguments” “Passing Arguments in a Structure” “Passing Arguments in a Cell Array” Getting the Input and Output Arguments Use na

Command Library - String Operation Command Command Library - XML Command Command Library - Terminal Emulator Command (Per Customer Interest) Command Library - PDF Integration Command Command Library - FTP Command (Per Customer Interest) Command Library - PGP Command Command Library - Object Cloning

VLAAMS SUPERCOMPUTER CENTRUM Command line arguments Interpreted by the command itself usage depends on the command Order of arguments often doesn't matter. Convention: options frst, non-option arguments last. Short options can be combined, i.e. date -R -u date -Ru For some commands, strict rules apply, e.g. find Meaningof arguments Non-option argument: often a fle name

Type the desired command or the command‟s alias at the command prompt. Command : LINE Command: L 2. Press ENTER on the keyboard. 3. Type an option at the command prompt. TIP: Many AutoCAD commands require you to press ENTER to complete the command. You know you are no longer in an AutoCAD command when you see a blank command line.

Other Shortcut Keys 28 Command Line Reference 30 Information for DaRT Notes Users 32 . Open command 39 Save command 39 Save As command 39 Close command 39 Recent Files command 39 Clear MRU List command 40 Remove Obsolete command 40 Auto Save command 40 Properties comman

Compute Command Line Profiler DU-05982-001_v03 4 COMPUTE COMMAND LINE PROFILER OVERVIEW This document is intended for users of Compute Command Line Profiler for NVIDIA CUDATM technology. Compute Command Line Profiler is a command line based profiling tool that can be used to measure performance and find potential opportunities for

Oct 25, 2011 · PKZIP Command Line and SecureZIP for Windows Command Line provide a command-line interface to PKZIP/SecureZIP for use in creating scripts and batch files. With Command Line, you execute PKZIP/SecureZIP commands and options by entering them at a character-based command prompt and running the resulting

Andreas Wimmer, Indra de Soysa, Christian Wagner Number 61 Political Science Tools for Assessing Feasibility and Sustainability of Reforms ZEF – Discussion Papers on Development Policy Bonn, February 2003. The CENTER FORDEVELOPMENT RESEARCH (ZEF) was established in 1997 as an international, interdisciplinary research institute at the University of Bonn. Research and teaching at ZEF aims to .