Download presentation
Presentation is loading. Please wait.
1
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[]) { . . . }
2
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"
3
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;
4
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
5
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.
6
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.
7
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.
8
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.
9
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.
10
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.
11
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)
12
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.
13
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.
14
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.