ECE 103 Engineering Programming Chapter 46 argc, argv, envp

Slides:



Advertisements
Similar presentations
Lecture 3 Some commonly used C programming tricks. The system command Project No. 1: A warm-up project.
Advertisements

ECE 103 Engineering Programming Chapter 54 Recursion Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed.
Introduction to C Programming CE Lecture 15 Files and Program Parameters.
Command-line arguments CS 201 Fundamental Structures of Computer Science.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
1 CS 161 Introduction to Programming and Problem Solving Chapter 10 g++ Compiler Usage Herbert G. Mayer, PSU Status 10/21/2014.
CPT: Arrays of Pointers/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to illustrate the use of arrays.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
ECE 103 Engineering Programming Chapter 9 gcc Compiler Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 50 Structures Unions, Part 2 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
ECE 103 Engineering Programming Chapter 7 Compiling C Programs Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 41 C Pointers, Part 3 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Introduction to C Topics Compilation Using the gcc Compiler
Test 2 Review Outline.
5.13 Recursion Recursive functions Functions that call themselves
C Primer.
A bit of C programming Lecture 3 Uli Raich.
Characters and Strings
Command Line Arguments
Command line arguments
Command Line Arguments
Understand argc and argv
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Command Line Arguments
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Topics Compilation Using the gcc Compiler
Command-Line Arguments
KALINGA INSTITUTE OF INDUSTRIAL TECHNOLOGY
Programmazione I a.a. 2017/2018.
Computer Science 210 Computer Organization
CS111 Computer Programming
typedef typedef int Index; typedef char Letter; Index i; i = 17;
Command Line Arguments
Computer Science 210 Computer Organization
Command Line Parameters
Command Line Arguments
C Stuff CS 2308.
Dale Roberts, Lecturer IUPUI
CS 106 Computing Fundamentals II Chapter 71 “Indexing”
Arrays Strings and Parameter Passing CSCI N305
Command Line Parameters
ECE 103 Engineering Programming Chapter 32 Array Parameters
ECE 103 Engineering Programming Chapter 56 Runtime Errors
ECE 103 Engineering Programming Chapter 19 Nested Loops
ECE 103 Engineering Programming Chapter 12 More C Statements
ECE 103 Engineering Programming Chapter 51 Random Numbers
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
ECE 103 Engineering Programming Chapter 37 C Macro Parameters
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
ECE 103 Engineering Programming Chapter 62 Stack Implementation
ECE 103 Engineering Programming Chapter 64 Tree Implementation
ECE 103 Engineering Programming Chapter 20 Change in Flow of Control
ECE 103 Engineering Programming Chapter 35 C Pointers, Part 1
C By Example The assumption is that you know Java and need to extend that knowledge so you can program in C. 1. Hello world 2. declarations 3. pass.
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
ECE 103 Engineering Programming Chapter 18 Iteration
(PART 2) prepared by Senem Kumova Metin modified by İlker Korkmaz
Characters and Strings
Introduction to C Topics Compilation Using the gcc Compiler
15213 C Primer 17 September 2002.
Files Chapter 8.
Presentation transcript:

ECE 103 Engineering Programming Chapter 46 argc, argv, envp Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed by Professor Phillip Wong @ PSU ECE

Syllabus Command Line Arguments Examples

Accessing Command Line Arguments Command line arguments are extra options that a user can pass to a program that is started from the command line. Format: exec_filename arg1 arg2 arg3 … Arguments are listed after the executable name. Arguments are separated with whitespace. 2

gcc –ansi –Wall –pedantic prog.c One implicit argument always exists, which is the name of the executable file itself. Example: gcc –ansi –Wall –pedantic prog.c The example has five arguments. The gcc is implicit, while the rest are explicit. 3

int main (int argc, char * argv[]) By modifying the main() function header, a C program can access command line arguments. Change this: int main (void) To this: int main (int argc, char * argv[]) 4

main() function parameters: argc holds the total number of command line arguments (implicit+explicit). argv is an array of pointers to strings. Each argument value is stored as a string. argv[0] contains the name of the executable. Subsequent argv elements contain the explicit arguments (if any), in the order they appeared on the command line. The final element in argv is always a NULL pointer. 5

Example: #include <stdio.h> /* Executable filename is "mygcc" */   /* Executable filename is "mygcc" */ int main (int argc, char * argv[]) { int k; /* Index */ /* Display the number of arguments */ printf("argc = %d\n", argc); /* Display each argument string */ for (k = 0; k < argc; k++) printf("argv[%d] = %s\n", k, argv[k]); return 0; } 6

/mygcc –ansi –Wall –pedantic prog.c The output from the program is: If the command line is: /mygcc –ansi –Wall –pedantic prog.c   The output from the program is: argc = 5 argv[0] = /mygcc argv[1] = -ansi argv[2] = -Wall argv[3] = -pedantic argv[4] = prog.c 7

The arguments are stored like this in argv: This is not a 2-D array of characters. It is a 1-D array of pointers to strings. 1 2 3 4 5 6 7 8 9 '/' 'm' 'y' 'g' 'c' '\0' argv[0] argv[1] argv[2] argv[3] argv[4] argv[5] '-' 'a' 'n' 's' 'i' '\0' '-' 'W' 'a' 'l' '\0' '-' 'p' 'e' 'd' 'a' 'n' 't' 'i' 'c' '\0' 'p' 'r' 'o' 'g' '.' 'c' '\0' NULL 8