BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

Similar documents
Overview (1A) Young Won Lim 9/14/17

Overview (1A) Young Won Lim 9/9/17

Overview (1A) Young Won Lim 9/25/17

The New C Standard (Excerpted material)

Arrays and Pointers (part 2) Be extra careful with pointers!

PROGRAMMAZIONE I A.A. 2017/2018

Friday, September 16, Lab Notes. Command line arguments More pre-processor options Programs: Finish Program 1, begin Program 2 due next week

COMP s1 Lecture 1

CpSc 1010, Fall 2014 Lab 10: Command-Line Parameters (Week of 10/27/2014)

SU2017. LAB 1 (May 4/9) Introduction to C, Function Declaration vs. Definition, Basic I/O (scanf/printf, getchar/putchar)

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Array Initialization

Arrays and Pointers (part 2) Be extra careful with pointers!

Organization of a file

Friday, February 10, Lab Notes

Binghamton University. CS-211 Fall Variable Scope

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

Problem Set 1: Unix Commands 1

COMP 2001/2401 Test #1 [out of 80 marks]

Introduction to C An overview of the programming language C, syntax, data types and input/output

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

Dynamic memory allocation (malloc)

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

CSE 333 Lecture 7 - final C details

Course organization. Course introduction ( Week 1)

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

Personal SE. Functions, Arrays, Strings & Command Line Arguments

BSM540 Basics of C Language

Programming in C. Pointers and Arrays

CS 220: Introduction to Parallel Computing. Input/Output. Lecture 7

CSE 303 Lecture 8. Intro to C programming

BSM540 Basics of C Language

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

Dynamic memory allocation

Dynamic Memory Allocation and Command-line Arguments

CS 0449 Sample Midterm

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

PRINCIPLES OF OPERATING SYSTEMS

Saint Louis University. Intro to Linux and C. CSCI 2400/ ECE 3217: Computer Architecture. Instructors: David Ferry

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

CMPT 300. Operating Systems. Brief Intro to UNIX and C

Slide Set 9. for ENCM 335 in Fall Steve Norman, PhD, PEng

Basic and Practice in Programming Lab 10

APT Session 4: C. Software Development Team Laurence Tratt. 1 / 14

Common Misunderstandings from Exam 1 Material

Binghamton University. CS-211 Fall Variable Scope

UNIT III (PART-II) & UNIT IV(PART-I)

Binghamton University. CS-211 Fall Variable Scope

CSE 303: Concepts and Tools for Software Development

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

ECE15: Homework 10. void wordstats(file *fp, char string[]) { void printfrequencies(file *fp) {

GCC C++: Comment Text

COMP 2355 Introduction to Systems Programming

Tutorial 1 C Tutorial: Pointers, Strings, Exec

Pointers. Pointers. Pointers (cont) CS 217

Ch 4. Parameters and Function Overloading

An overview of Java, Data types and variables

Chapter 11 Introduction to Programming in C

Direct Memory Access. Lecture 2 Pointer Revision Command Line Arguments. What happens when we use pointers. Same again with pictures

CSCI565 Compiler Design

A Fast Review of C Essentials Part II

just a ((somewhat) safer) dialect.

Quick review pointer basics (KR ch )

Understanding the Program Run

Final CSE 131B Spring 2004

Lesson #8. Structures Linked Lists Command Line Arguments

2 Compiling a C program

Software Development With Emacs: The Edit-Compile-Debug Cycle

Arrays and Pointers in C. Alan L. Cox

COMP2521. Generic ADTs in C

CSC209F Midterm (L5101) Fall 1998 University of Toronto Department of Computer Science

Computer Systems Lecture 9

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

25.2 Opening and Closing a File

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 14, SPRING 2013

8. Characters, Strings and Files

Creating, Compiling and Executing

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

CSC111 Computer Science II

Pointers and scanf() Steven R. Bagley

CSE 303 Midterm Exam

Chapter 11 Introduction to Programming in C

Understanding Pointers

Communication With the Outside World

CSE 333 Autumn 2014 Midterm Key

Computer Systems Assignment 2: Fork and Threads Package

The output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 {

EL2310 Scientific Programming LAB2: C lab session. Patric Jensfelt, Andrzej Pronobis

Today s Learning Objectives

Summary of Last Class. Processes. C vs. Java. C vs. Java (cont.) C vs. Java (cont.) Tevfik Ko!ar. CSC Systems Programming Fall 2008

C programming for beginners

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

Luar Topics. C A Low level Programming Language Make A dependency based build system YUYV Representation of Color ZMP Zero Moment Point control

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

Transcription:

BIL 104E Introduction to Scientific and Engineering Computing Lecture 14

Because each C program starts at its main() function, information is usually passed to the main() function via command-line arguments. A command-line argument is a parameter that follows a program's name when the program is invoked from the operating system's command line. For instance, given a C program, test.c, whose executable file is called test.exe, if you run the program from a DOS prompt like this, C:\app>test.exe argument1 argument2 argument3 argument1, argument2, and argument3 are called command-line arguments to the main() function in the test.c program. Of course, you can simply omit an executable file's extension,.exe, when you run it from a DOS prompt. 10/4/11 Lecture 14 2

There are two built-in arguments in the main() function that can be used to receive command-line arguments. Usually, the name of the first argument is argc, and it is used to store the number of arguments on the command line. The second argument is called argv and is a pointer to an array of char pointers. Each element in the array of pointers points to a command-line argument that is treated as a string. In order to use argc and argv, you have to declare them in the main() function in your program like this: data_type_specifier main(int argc, char *argv[]) {... } Here data_type_specifier specifies the data type returned by the main() function. By default, the data type returned by the main() function is int. If the main() does not return any value, you should put the void keyword in front of the main() function definition. 10/4/11 Lecture 14 3

Suppose that the main() function defined in the test.c program looks like this: void main(int argc, char *argv[]) {... } If you run the executable file of the program from a DOS prompt, C:\app>test.exe argument1 argument2 argument3 the value received by argc is 4, because the name of the program itself is counted as the first command-line argument. Accordingly, argv[0] holds the string of the path and program name C: \app\test.exe, and argv[1], argv[2], and argv[3] contain the strings of argument1, argument2, and argument3, respectively. 10/4/11 Lecture 14 4

/* argc/argv example */ int main (int argc, char* argv[]) { printf("%s%d %s \n", "you entered", argc, "arguments"); printf("%s: %s\n", "the zeroth arg is the program name", argv[0]); printf("%s: %s\n", "the first argument is", argv[1]); printf("%s: %s\n", "the second argument is", argv[2]); } Ø gcc argv_example.c o argv_example Ø argv_example hello world Ø you entered 3 arguments Ø the zeroth argument is the program name: argv_example Ø the first argument is hello Ø the second argument is world 10/4/11 Lecture 14 5

/* Command-line arguments */ #include <stdio.h> main (int argc, char *argv[]) { int i; printf("the value received by argc is %d.\n", argc); printf("there are %d command-line arguments passed to main().\n", argc); } printf("the first command-line argument is: %s\n", argv[0]); printf("the rest of the command-line arguments are:\n"); for (i=1; i<argc; i++) printf("%s\n", argv[i]); return 0; The value received by argc is 3. There are 3 command-line arguments passed to main(). The first command-line argument is: C:\app\18L05.EXE The rest of the command-line arguments are: Hello, world! 10/4/11 Lecture 14 6

argc and argv are normally used as the two built-in arguments in the main() function, but you can use other names to replace them in their declarations. In addition to these two arguments, some compilers may support another argument to the main() function. The third argument is a pointer to an array of pointers that are used to point to memory locations containing the environmental parameters, such as the paths, the Windows boot directory name, the temporary directory name, and so on. Note that argv reads all arguments as Strings (i.e. sequences of characters). So, we cannot simply pass two number and add them, for example. First, we would have to convert to a numeric type. unix$ a.out 1 23 "third arg" 10/4/11 Lecture 14 7