Command Line Arguments plus Variable-Length Arrays Systems Programming.

Slides:



Advertisements
Similar presentations
Basic Java Constructs and Data Types – Nuts and Bolts
Advertisements

1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 14 - Advanced C Topics Outline 14.1Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Command-line arguments CS 201 Fundamental Structures of Computer Science.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
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",
1 Functions and Structured Programming. 2 Structured Programming Structured programming is a problem-solving strategy and a programming methodology. –The.
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.
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.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 14 - Advanced C Topics Outline 14.1Introduction 14.2Redirecting Input/Output on UNIX and DOS Systems.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Command Line Arguments.
File IO and command line input CSE 2451 Rong Shi.
C By Example 1 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 by.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
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”
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursion.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Functions.
CMSC 104, Version 8/061L25Strings.ppt Strings Topics String Libraries String Operations Sample Program Reading Sections
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.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
 2005 Pearson Education, Inc. All rights reserved Arrays.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
Week 4 – Functions Coding Functions. Purpose of Coding Functions A function is written to perform a well-defined task; rather than having all logic in.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
LINKED LISTS.
The Machine Model Memory
Characters and Strings
Command Line Arguments
Command line arguments
Command Line Arguments
Command Line Arguments
KALINGA INSTITUTE OF INDUSTRIAL TECHNOLOGY
Programmazione I a.a. 2017/2018.
Command-line Arguments
Command Line Arguments
C Stuff CS 2308.
INPUT & OUTPUT scanf & printf.
Introduction to C Topics Compilation Using the gcc Compiler
Command Line Parameters
Introduction to C Topics Compilation Using the gcc Compiler
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.
C Pointers Another ref:
Presentation transcript:

Command Line Arguments plus Variable-Length Arrays Systems Programming

Happy Chinese New Year Systems Programming Command Line Arguments 2

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 Command Line Arguments 3

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 Command Line Arguments 4

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

Command Line Arguments  safe programming  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.  proper usage statement  If there is a mismatch, main prints out a proper usage statement and immediately ends the program. Systems Programming Command Line Arguments 6

Command Line Arguments Command line arguments intended as integer program parameters requires converting the ASCII string representation to an integer using the standard library function atoi {ascii-to-integer}. Example atoi #include int main (void) { int i; // variable to hold converted string i = atoi (“2593”); printf( “%s%d\n”, “The string \”2593\” converts to int “, i); return 0; } Systems Programming Command Line Arguments 7 The string “2593” convert to int 2593

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

A Command Line Argument Sample Program else { progstring = argv[0]; samstring = argv[1]; timestring = argv[2]; printf("Program = %s\n", progstring); /* convert to integer */ /* convert to integer */ atoi 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]); atoi printf("Time = %d\n", atoi(timestring)); } return; }./com-arg Program =./com-arg Please enter 3 samples sample[1] = 745 sample[2] = sample[3] = 2 Time = 500 Systems Programming Command Line Arguments 9

Command Line Arguments  Using command line arguments to specify variable size for input parameters is a preferred technique.  Remember C treats the arguments as ASCII strings.  Use atoi to convert the strings to integers. Systems Programming Command Line Arguments 10

6.10 Variable-Length Arrays (Cont.)  A variable-length array is an array whose length, or size, is defined in terms of an expression evaluated at execution time.  The program of Fig declares and prints several VLAs.  [Note: This feature is not supported in Microsoft Visual C++.] Copyright © Pearson, Inc All Rights Reserved. 11 Systems Programming Command Line Arguments

Copyright © Pearson, Inc All Rights Reserved. Systems Programming Command Line Arguments Variable-Length Arrays (Cont.)

Copyright © Pearson, Inc All Rights Reserved. Systems Programming Command Line Arguments Variable-Length Arrays (Cont.) Note: declarations deliberately come later!

Copyright © Pearson, Inc All Rights Reserved. Systems Programming Command Line Arguments Variable-Length Arrays (Cont.)

Copyright © Pearson, Inc All Rights Reserved. Systems Programming Command Line Arguments Variable-Length Arrays (Cont.)

Copyright © Pearson, Inc All Rights Reserved. Systems Programming Command Line Arguments Variable-Length Arrays (Cont.)