Presentation is loading. Please wait.

Presentation is loading. Please wait.

Instructor: Ioannis A. Vetsikas

Similar presentations


Presentation on theme: "Instructor: Ioannis A. Vetsikas"— Presentation transcript:

1 Instructor: Ioannis A. Vetsikas E-mail: vetsikas@cs.cornell.edu
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 9 : September 13

2 const Type Qualifier Can be applied to the declaration of any variable – even function parameters Specifies that the variable’s value won’t be changed; for arrays it means that the elements will not be altered… const double pi = ; const char msg[] = “warning: ”; int strlen(const char[]); const int a; /* a is const int */ const int *b; /* b is pointer to const int */ int * const c; /* c is a const pointer to int */ const int * const d; /* d is a const pointer to const int */

3 enum In C, enum types are just int (starting usually from 0)
enum day { SUN, MON, TUE, WED, THU, FRI, SAT }; enum boolean {FALSE, TRUE}; enum {FALSE, TRUE}; same as #define FALSE 0; #define TRUE 1; enum {AA=1, BB, CC}; The constant are numbered sequentially Can now define: enum day d; etc. Note: Enumerated constants cannot appear in two enums Enumeration tags form their own namespace enum boolean {FALSE, TRUE} boolean;

4 Scope of Type Definition
Depending on the placement there is file or block scope usually… This applies to all kinds of definitions (e.g. also to enum, struct, typedef) int a=3; { int a=4; } printf(“a=%d\n”); What is the output?

5 Storage classes Storage class auto
Every variable and function has a storage class Four storage classes: auto, extern, register and static Storage class auto Variables within functions or blocks default to automatic (unless defined otherwise), but one can also declare this explicitly: auto int i=1; Memory allocated upon entering a block is released at exit from block. So values are not kept between invocations.

6 Storage class static When variables declared within a block are defined as static then they retain their value between consecutive invocations. Thus static variables might be useful for debugging purposes When a global variable is declared static then it is not accessible from another file! See extern for more details on when a variable is accessible from other files…

7 Storage class register
This is a relic from the “old times” I suggest that you not use it at all It tells the compiler to use a register in order to store the variable, instead of a space in memory Defaults to auto if the compiler decides otherwise Most modern compilers will ignore this statement and optimize the code as they see fit anyway! Hence you cannot point to a register class variable

8 Storage class extern Variables defined outside functions (global) and all functions themselves have default class extern extern tells the compiler to look for a variable elsewhere (out of the current scope) either in the same file or in another file (no new memory used) file1.c: int a=1, b=2, c=3; int f(); int main() { printf(“%3d%3d%3d%3d\n”, f(), a, b, c); } file2.c: inf f() { extern int a; int b, c; a=b=c=4; return a+b+c;}

9 Initialization Extern and static variables can only be initialized with a constant value (think that the initialization is decided at compile time) and the initialization happens only once. If no value is given at declaration then they are initialized to zero (in whatever form applicable) Register and auto variable are initialized every time the block where they are defined is entered and they can have expression to be evaluated too in the initialization. If no value is given then they may not be initialized and thus have junk values int i=x*y; static int j=4;

10 Header files When a program includes many files we might want the definitions and declarations to be in just one place Also useful for libraries, since you just include the header and have all the functions and definitions available (e.g. stdio.h) Use #include to include this files into the code If you make your own library remember to include it when you compile your code E.g. use #include “library.h” in main.c and compile using gcc main.c library.c

11 Command line arguments
int main (int argc, char *argv[]) argc: number of command line arguments argv: array of string (each string an argument) First argument (argv[0]) is always the name of the program After last argument pointer there is always a null pointer (argv[argc]=NULL) E.g. if we type c:main.exe hello, world 3 then argc=4 and argv[0]=“c:main.exe” and argv[1]=“hello,” and argv[2]=“world” and argv[3]=“3” and argv[4]=NULL

12 Chapters covered from K&R
Chapter 4 (all except 4.11) Chapter 5 (all except ) Chapter 6 (all except 6.9)


Download ppt "Instructor: Ioannis A. Vetsikas"

Similar presentations


Ads by Google