Instructor: Ioannis A. Vetsikas

Slides:



Advertisements
Similar presentations
Chapter 7: User-Defined Functions II
Advertisements

1 Review of Class on Oct Outline  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
Storage Classes.
1 Chapter 9 Scope, Lifetime, and More on Functions.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
chap13 Chapter 13 Programming in the Large.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
18. DECLARATIONS.
CS212: Object Oriented Analysis and Design Lecture 2: Introduction to C++
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.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
FUNCTIONS. Funtions  The heart of effective problem solving is problem decomposition.  breaking a problem into small, manageable pieces  In C, the.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
C Part 2 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens The Three Attributes of an Identifier Identifiers have three essential.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
C++ Lesson 1.
Eine By: Avinash Reddy 09/29/2016.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Test 2 Review Outline.
The Machine Model Memory
Chapter 7: User-Defined Functions II
A bit of C programming Lecture 3 Uli Raich.
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
The Three Attributes of an Identifier
Command-Line Arguments
Introduction to C Programming Language
សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management
Programmazione I a.a. 2017/2018.
typedef typedef int Index; typedef char Letter; Index i; i = 17;
Scope, Parameter Passing, Storage Specifiers
Chapter 9 Scope, Lifetime, and More on Functions
Advanced Programming Basics
C Stuff CS 2308.
User Defined Functions
Instructor: Ioannis A. Vetsikas
CS 240 – Lecture 5 Scope of Variables, The Stack, Automatic Variables, Global Variables, Constant Type.
Chapter 14 - Advanced C Topics
Pointers.
Govt. Polytechnic,Dhangar
Dr. Bhargavi Dept of CS CHRIST
Variables Title slide variables.
Classes and Objects.
Storage class.
Scope Rules and Storage Types
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Scope Rules Of Variables
Submitted By : Veenu Saini Lecturer (IT)
The vector Container Type
Standard Version of Starting Out with C++, 4th Edition
Programming Languages and Paradigms
Scope of Identifier The Scope of an identifier (or named constant) means the region of program where it is legal to use that.
Compiler vs linker The compiler translates one .c file into a .o file
Pointers.
C Programming Lecture-17 Storage Classes
Variables and Constants
Storage classes in C In C language, each variable has a storage class which decides the following things: scope i.e where the value of the variable would.
ENERGY 211 / CME 211 Lecture 29 December 3, 2008.
Storage Classes.
Presentation transcript:

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

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 = 3.14159; 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 */

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;

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?

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.

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…

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

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;}

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;

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

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

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