Command-Line Arguments

Slides:



Advertisements
Similar presentations
Pointer Variables The normal variables hold values. For example, int j; j = 2; Then a reference to j in an expression will be identified with the value.
Advertisements

Chapter 7: User-Defined Functions II
1 Review of Class on Oct Outline  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
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.
Pointers and Arrays C and Data Structures Baojian Hua
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
Functions #include int sum(int i, int j); main() { int a, b, res; a = 1; b = 2; res = sum(a, b); printf("%d + %d = %d\n", a, b, res); } int sum(int i,
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
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.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
1 Functions  A function is a named, independent section of C++ code that performs a specific task and optionally returns a value to the calling program.
Arrays as Function Arguments Array can be used as a function argument. E.g., #include int sum(int b[], int n) { int i, res; res = 0; for(i = 0; i < n;
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Pointers and Arrays An array's name is a constant whose value is the address of the array's first element. For this reason, the value of an array's name.
Multi-dimensional Arrays and other Array Oddities Rudra Dutta CSC Spring 2007, Section 001.
The Cast Operator The cast operator converts explicitly from one data type of an expression to another. For example, if x is of type int, the value of.
Revisiting building. Preprocessing + Compiling 2 Creates an object file for each code file (.c ->.o) Each.o file contains code of the functions and structs.
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:
The Machine Model Memory
Function: Declaration
Chapter 7: User-Defined Functions II
UNIT 5 C Pointers.
C Functions -Continue…-.
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
Functions and Structured Programming
Understand argc and argv
CSE 303 Concepts and Tools for Software Development
Functions.
Cs288 Intensive Programming in Linux
Day 04 Introduction to C.
C Basics.
Programmazione I a.a. 2017/2018.
Instructor: Ioannis A. Vetsikas
Dynamic memory allocation and Intraprogram Communication
Dynamic memory allocation and Intraprogram Communication
Scope, Parameter Passing, Storage Specifiers
Yung-Hsiang Lu Purdue University
C Stuff CS 2308.
C Programming Language
Chapter 14 - Advanced C Topics
CSE1320 Strings Dr. Sajib Datta
Command Line Parameters
Outline Defining and using Pointers Operations on pointers
Beginning C for Engineers
In C Programming Language
Submitted By : Veenu Saini Lecturer (IT)
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.
(PART 2) prepared by Senem Kumova Metin modified by İlker Korkmaz
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Programming Languages and Paradigms
Arrays and Pointers (part 2)
ETE 132 Lecture 6 By Munirul Haque.
Arrays, Pointers, and Strings
Arrays and Pointers (part 2)
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:
Functions.
Files Chapter 8.
Presentation transcript:

Command-Line Arguments The function main() can have two parameters, argc and argv[]. argc is an int whose value is equal to the number of strings on the command line, and argv[] is an array of pointers to these strings. main(int argc, char* argv[]) { . . . }

Command-Line If a program with argc and argv declared is compiled with executable called a.out, then after a.out file1 5 the two parameters, argc and argv[], will contain following values when the program starts. argv argc argv[0] "a.out" 3 argv[1] "file1" argv[2] "5"

Command-Line Example #include <stdio.h> #include <stdlib.h> main(int argc, char *argv[]) { char record[80]; int i; FILE *fp; printf("There are %d files\n", argc-1); for(i = 1; i < argc; ++i) { printf("File: %s\n", argv[i]); fp = fopen(argv[i], "r"); while(fgets(record, 80, fp) != NULL) printf("%s", record); fclose(fp); } return EXIT_SUCCESS;

Use the Program When you evoke the program as a.out the printout is There are 0 files When you evoke with a.out pg360.c pg356.c There are 2 files File: pg360.c . . . (actual file) File: pg356.c

Storage Classes A variable's storage class determines when the cell is allocated and how long the cell remains in existence. The storage class of a variable or a function influences its visibility - that is, where in a program the variable or function can be referenced. The commonly used storage classes are: auto, extern, static.

Storage Classes (in a single file) #include <stdio.h> int g = 0; extern int k; void fun(char c); main() { char c ='A'; auto int i; ++g; fun(c); fun(c+1); fun(c+2); } void fun(char c) int y = 3; static int x=0; putchar(c); printf(" x=%d, y=%d, g=%d\n", x++, y--, ++g); A x=0, y=3, g=2 B x=1, y=3, g=4 C x=2, y=3, g=5 g and k have storage class "extern". g and k are global variables, visible to all functions, exist during whole program execution. c and i are storage class "auto". c and i are local to main() only, exist only when main() exists. x has storage class "static". x is local to fun(), known only in fun(). x is initialized once, exists during the whole program. y is local, storage class "auto", initialized every time when fun() is evoked, and exists only when fun() is active. Print out.

Storage Class Extern Declare cnt as extern. #include <stdio.h> int p(void); main() { extern int cnt; int k; cnt = -10; k = p(); printf("%d\n", k); ++cnt; } int cnt = 0; int p(void) return ++cnt; Declare cnt as extern. Define and initialize cnt as type int, storage class extern.

Storage Class (in multiple files) File 1: main.c #include <stdio.h> int p(void); int cnt = 0; main() { int k; cnt = -10; k = p(); printf("%d\n", k); ++cnt; } extern int cnt; int p(void) return ++cnt; Define and initialize cnt as type int, storage class extern. File 2: p.c Declare cnt as extern. Thus cnt is the same variable defined in File 1.

Compile Programs in Multiple Files If the whole program is stored in two separate files called main.c and p.c, we can compile them with cc -c main.c cc -c p.c cc main.o p.o Or in a single command cc main.c p.c With the UNIX command man cc look at the options available to the cc command.

Nested Blocks #include <stdio.h> main() { int x = 1; printf("%d\n", x); int x=2, y=3; printf("%d %d\n", ++x, y); } prints: 1 3 3 y is visible only in this bock, inner x is not the same as outer x.

Type Qualifiers Declaration of a variable is in the form Examples: [Type qualifiers] [storage class] data type name; Examples: int x; (data type only) auto int y; (storage class and data type) const static int z = 101; (type qualifier, storage class, data type)

Const Qualifier When a variable or parameter is qualified as const, then the variable or the parameter cannot occur as a lvalue, that is, as The left-hand side of an assignment expression. The target of an increment or decrement operation. A const variable must be initialized in its definition and the value cannot be changed.

const Examples If we define const int x = 111; then the following uses are wrong x=999; /* ERROR, illegal lvalue */ ++x; /* ERROR illegal lvalue */ The prototype declaration for strcat is (in <string.h>) char* strcat(char *s1, const char *s2); strcat() takes a (const) s2 and concatenate it to s1. const char *s2 guarantees that strcat() will not change s2.

Reading/Home Working Read Chapter 8, page 390 to 433. Work on Problems Section 8.1, page 395, exercise 1, 3, 5. Section 8.3, page 405, exercise 1, 3, 5. Check your answers in the back of the textbook. Do not hand in.