Command Line Arguments

Slides:



Advertisements
Similar presentations
Introduction to C Programming CE Lecture 15 Files and Program Parameters.
Advertisements

Command-line arguments CS 201 Fundamental Structures of Computer Science.
Rings This chapter demonstrates how processes can be formed into a ring using pipes for communication purposes.
Unix Continuum of Tools Do something once: use the command line Do something many times: –Use an alias –Use a shell script Do something that is complex.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
CS1061 C Programming Lecture 15: More on Characters and Strings A. O’Riordan, 2004.
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
Some Example C Programs. These programs show how to use the exec function.
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Command Line Arguments plus Variable-Length Arrays Systems Programming.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Command Line Arguments.
CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
Java and C++ Transitioning. A simple example public class HelloWorldApp { public static void main(String[] args) { //Display the string. System.out.println("Hello.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursion.
CMSC 104, Version 8/061L25Strings.ppt Strings Topics String Libraries String Operations Sample Program Reading Sections
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;
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
6/9/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 10 (C-4)
Using System Calls (Unix) Have to tell compiler (if C/C++) where to find the headers, etc. – i.e., the “include” files May have to tell compiler where.
LINKED LISTS.
Stack and Heap Memory Stack resident variables include:
Lesson #8 Structures Linked Lists Command Line Arguments.
Cs288 Intensive Programming in Linux
A bit of C programming Lecture 3 Uli Raich.
Characters and Strings
Command Line Arguments
Command line arguments
Command Line Arguments
Functions and Structured Programming
Understand argc and argv
C Language By Sra Sontisirikit
Day 02 Introduction to C.
Structure of C Programs
Command Line Arguments
Chapter 18 I/O in C.
Command-Line Arguments
KALINGA INSTITUTE OF INDUSTRIAL TECHNOLOGY
Programmazione I a.a. 2017/2018.
Computer Science 210 Computer Organization
Buy book Online -
More Examples of argc and argv
Command-line Arguments
Command Line Arguments
Computer Science 210 Computer Organization
Yung-Hsiang Lu Purdue University
C Stuff CS 2308.
Command-line arguments
sscanf()- string scan function
Programming and Data Structure
Command Line Parameters
File Handling.
Introduction to Computer Organization & Systems
Command-line arguments
Strings and Pointer Arrays
ECE 103 Engineering Programming Chapter 46 argc, argv, envp
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.
Introduction to Computer Organization & Systems
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Programming in C Pointers and Arrays.
Strings #include <stdio.h>
Characters and Strings Functions
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2016.
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.
I/O CS580U - Fall 2018.
ENERGY 211 / CME 211 Lecture 29 December 3, 2008.
Files Chapter 8.
Chapter 18 I/O in C.
Presentation transcript:

Command Line Arguments Systems Programming

Command Line Arguments A C program must always have a function named main. This function is directly invoked by the Linux/Unix system. main has two arguments conventionally named argc and argv. The argc argument is of type int and corresponds to the number of arguments provided on the command line (including the program name as the first argument). Systems Programming: Systems Programming: Command Line Arguments 2

Command Line Arguments The second argument to main, argv, is an array of pointers to strings. Each string contains the ASCII string representation of what is typed on the program command line. Systems Programming: Systems Programming: Command Line Arguments 3

Command Line Arguments For example, if the command line typed is: ./prog3 file1 200 argc will have the value 3 and argv[0] points to string “./prog3” argv[1] points to string “file1” argv[2] points to string “200”. Systems Programming: Systems Programming: Command Line Arguments 4

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

Command Line Arguments For command line arguments that are intended as integer parameters to the program, the ASCII string representing of that integer has to be converted to an integer using the standard library function atoi. See pages 333-334 in D&D for complete syntax and an example of atoi usage. Systems Programming: Command Line Arguments

A Command Line Argument Sample Program /* An Example of the Use of Command Line Arguments */ #include <stdio.h> #include <stdlib.h> #define SIZE 100 int main (int argc, char *argv[]) { int i, samples, table[SIZE]; char *samstring, *timestring; char *progstring; if(argc != 3) printf("Proper Usage is: com-arg samples time\n"); note main function arguments Systems Programming: Command Line Arguments

A Command Line Argument Sample Program else { progstring = argv[0]; samstring = argv[1]; timestring = argv[2]; printf("Program = %s\n", progstring); samples = atoi(samstring); printf("Please enter %d samples\n", samples); for (i=0; i < samples; i++) scanf("%d", &table[i]); printf("sample[%d] = %d\n", i+1, table[i]); printf("Time = %d\n", atoi(timestring)); } return; ./com-arg 3 500 Program = ./com-arg Please enter 3 samples 745 1023495 2 sample[1] = 745 sample[2] = 1023495 sample[3] = 2 Time = 500 Systems Programming: Command Line Arguments