Command-line Arguments

Slides:



Advertisements
Similar presentations
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Advertisements

Lecture 20 Arrays and Strings
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
Programming in C Pointers and Arrays. 1/14/102 Pointers and Arrays In C, there is a strong relationship between pointers and arrays.In C, there is a strong.
Introduction to C Programming CE Lecture 15 Files and Program Parameters.
Command-line arguments CS 201 Fundamental Structures of Computer Science.
What does this program do ? #include int main(int argc, char* argv[]) { int i; printf("%d arguments\n", argc); for(i = 0; i < argc; i++) printf(" %d: %s\n",
CS1061 C Programming Lecture 15: More on Characters and Strings A. O’Riordan, 2004.
Programming in C Pointers and Arrays. 1/14/102 Pointers and Arrays In C, there is a strong relationship between pointers and arrays.In C, there is a strong.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Literals and Variables Dale Roberts,
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Strlen() implementation /* strlen : return length of string s */ int strlen(char *s) { int n; for (n = 0 ; s[n] != ‘\0’ ; n++) ; return n; } /* strlen.
File Handling Spring 2013Programming and Data Structure1.
Command Line Arguments plus Variable-Length Arrays Systems Programming.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
CS50 Section 2 21 September 2015 Sam Green ’17 (646)
File IO and command line input CSE 2451 Rong Shi.
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Cryptography.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
Programming in C Pointers and Arrays. 1/14/102 Pointers and Arrays In C, there is a strong relationship between pointers and arrays.In C, there is a strong.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
Week 6 - Friday.  What did we talk about last time?  Pointers  Passing values by reference.
Let’s summarize what we have learnt so far
LINKED LISTS.
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
Stack and Heap Memory Stack resident variables include:
Lesson #8 Structures Linked Lists Command Line Arguments.
Java Programming Lecture 2
A bit of C programming Lecture 3 Uli Raich.
Characters and Strings
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Command Line Arguments
Command line arguments
Command Line Arguments
Understand argc and argv
Command Line Arguments
Programmazione I a.a. 2017/2018.
Computer Science 210 Computer Organization
Loops The loop blocks you've used in Scratch can all be recreated in C! Loops allow for the repetition of code until a condition is met. There are three.
More Examples of argc and argv
Command Line Arguments
Computer Science 210 Computer Organization
Loops The loop blocks you've used in Scratch can all be recreated in C! Loops allow for the repetition of code until a condition is met. There are three.
Command Line Parameters
Yung-Hsiang Lu Purdue University
File I/O We are used to reading from and writing to the terminal:
Command Line Arguments
C Stuff CS 2308.
Command-line arguments
sscanf()- string scan function
Programming and Data Structure
Command Line Parameters
File Handling.
Command-line arguments
Notes about Homework #4 Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming:
Strings and Pointer Arrays
File Handling in C.
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Programming in C Pointers and Arrays.
Characters and Strings
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
C Pointers Another ref:
File I/O We are used to reading from and writing to the terminal:
Presentation transcript:

Command-line Arguments int main(void) int main(int argc, string argv[]) Programs, like functions, can take in arguments. Thus far, we haven't passed any arguments to main, and have declared it like this: int main(void) However, we can also declare main like this: int main(int argc, string argv[]) The parameters argc and argv provide a representation of the program's command line. argc is the number of strings that make up the command line (including the program name), and argv is an array that contains those strings.

Test Yourself jharvard@appliance (~): ./copy infile outfile 1. What is argc? 2. What is argv[0]? 3. What is argv[1]? 4. What is argv[2]? 5. What is argv[3]? 6. What is argv[4]? jharvard@appliance (~): ./copy infile outfile 1. 3 2. ./copy 3. infile 4. outfile 5. NULL 6. undefined

Mario Revisited jharvard@appliance (~): ./mario 10 Remember mario.c from pset 1? What if we modified the problem specification so that the program must read the pyramid height from the command line? Now, instead of prompting the user to enter a height using printf() and GetInt(), the user simply enters height on the command line. ./mario 10 should result in the printing of a pyramid of height 10.

int main(int argc, string argv[]) { if (argc != 2) printf("Usage: mario height"); return 1; } int height = atoi(argv[1]); // etc . . . First, check for correct usage by ensuring that only two command line arguments were entered (the program name and the pyramid's height). If argc != 2, instruct the user regarding correct usage and quit the program. Note the use of the function atoi(). atoi() converts ASCII strings to integers. That is, the command line string "10" becomes the integer value 10. So instead of relying on GetInt() to provide user input, we can now take the value supplied by the user on the command line to use as height.