1 CSSE 332 Standard Library, Storage classes, and Make.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
Functions and scope Confidence In scope Reference: K&K textbook, Chapter 4.
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
The make Utility Programming Tools and Environments Winter 2006.
Chapter 7: User-Defined Functions II
1 CS 161 Introduction to Programming and Problem Solving Chapter 9 C++ Program Components Herbert G. Mayer, PSU Status 10/20/2014.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Informationsteknologi Tuesday, September 18, 2007 Computer Systems/Operating Systems - Class 61 Today’s class Finish review of C Process description and.
CS414 C Programming Tutorial Ben Atkin
Lifetime “The lifetime of a variable is the time during which the variable is bound to a specific memory location.” [p. 219] “…the lifetime of a variable.
Guide To UNIX Using Linux Third Edition
Storage Classes.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Scope.
Computer Science 210 Computer Organization Introduction to C.
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.
Copyright 2001 Oxford Consulting, Ltd1 January Storage Classes, Scope and Linkage Overview Focus is on the structure of a C++ program with –Multiple.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
Adv. UNIX: large/131 Advanced UNIX v Objectives of these slides: –learn how to write/manage large programs consisting of multiple files, which.
Chapter 6: User-Defined Functions
FLEX Fast Lexical Analyzer EECS Introduction Flex is a lexical analysis (scanner) generator. Flex is provided with a user input file or Standard.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Week 2-3 Control flow (review) Conditional statements If, else, else if, switch-case, break Loop constructs for, while, do-while, break, continue, label--go;
0 4.1 Basics of Functions + Execution Diagram + make 4.2 Functions Returning Non-integers + Prototype Function 4.3 External Variables 4.4 Scope Rules 4.5.
Makefiles. Multiple Source Files (1) u Obviously, large programs are not going to be contained within single files. u C provides several techniques to.
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.
Exam / Homework Exam 1 Starting K&R chapter 4 tonight
CSCI 171 Presentation 6 Functions and Variable Scope.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Functions, Scope, and The Free Store Functions Functions must be declared by a function prototype before they are invoked, return_type Function_name(type,
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
C code organization CSE 2451 Rong Shi. Topics C code organization Linking Header files Makefiles.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
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.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
Makefiles CSSE 332 Operating Systems
Lecture 3 Translation.
Computer Science 210 Computer Organization
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
C Functions -Continue…-.
Chapter 7 Text Input/Output Objectives
A Lecture for the c++ Course
Storage class in C Topics Automatic variables External variables
Functions.
Day 04 Introduction to C.
C programming language
C Basics.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Instructor: Ioannis A. Vetsikas
Computer Science 210 Computer Organization
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Dr. Bhargavi Dept of CS CHRIST
Functions, Part 1 of 3 Topics Using Predefined Functions
Programming Languages and Paradigms
Compiler vs linker The compiler translates one .c file into a .o file
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions.
Presentation transcript:

1 CSSE 332 Standard Library, Storage classes, and Make

<stdio.h> “Provides a simple and efficient buffered I/O interface” – from man stdio “Provides a simple and efficient buffered I/O interface” – from man stdio –Prototypes “standard” I/O functions  (Mostly) system-independent  (e.g. fprintf, fgets, feof) –Defines required types (e.g. FILE) –Defines required macros Functions are defined in “standard library” Functions are defined in “standard library” –System-dependent implementations –Linked automatically 2

3 Input and Output functions fgets(stdin,…); fgets(stdin,…); –Similar to scanf(…) –Safe from buffer overflow fprintf(stdout,”format”,…); fprintf(stdout,”format”,…); –Buffered output –Equivalent to printf(“format”,…) fprintf(stderr,”…”,…); fprintf(stderr,”…”,…); –Un-buffered output –Use for error messages.

4 Other useful libraries and header files –String processing functions -- str*() & mem*() –Functions for testing characters –Mathematical functions and macros –Functions for number conversion, storage allocation, and similar tasks

Finding the right library In bash: In bash: –for x in /usr/lib/*.a; do if ar t $x | grep pow &> /dev/null; then echo $x; fi; done 2> /dev/null

Storage classes Visibility: parts of source code within which variable can be referenced Visibility: parts of source code within which variable can be referenced Lifetime: phases of execution during which variable is allocated Lifetime: phases of execution during which variable is allocated Automatic (default for local variables) Automatic (default for local variables) –e.g. auto int i; –Visibility: within statement block –Lifetime: execution of statement block Register Register –e.g. register counter = 1; –Suggestion to compiler to place variable in a register –Visibility and lifetime same as auto 6

More storage classes – static e.g. static int i; e.g. static int i; Visibility: statement block Visibility: statement block Lifetime: program execution Lifetime: program execution Value is retained and space is de-allocated only when program (not function) terminates. Value is retained and space is de-allocated only when program (not function) terminates. Historically related to Java’s static fields Historically related to Java’s static fields 7

More storage classes – extern e.g. e.g. –int count; //declared in file 1  Visibility must be entire file (i.e. global) –extern int count; //used in files 2,3 … Promises compiler that variable will be visible from another compilation unit Promises compiler that variable will be visible from another compilation unit Provides type Provides type Common practice is to collect externs in a header file Common practice is to collect externs in a header file 8

9 enum - enumerated data types #include enum month{ JANUARY, /* like #define JANUARY 0 */ FEBRUARY, /* like #define FEBRUARY 1 */ MARCH /* … */ }; //In main: enum month birthMonth; if(birthMonth == JANUARY){…} /* alternatively, …. */ enum month{ JANUARY=1, /* like #define JANUARY 1 */ MARCH=3, /* like #define MARCH 3 */ FEBRUARY=2, /* … */ }; Note: if you use the #define, the value of JANUARY will not be visible in the debugger. An enumerated data type’s value will be.

10 Program with multiple files Library headers Library headers –Standard –User-defined void myproc(); extern int gData; #include #include “mypgm.h” void myproc(){ int mydata = gData*2;... /* some code */ } #include #include “mypgm.h” int gData=5; int main(){ myproc(); return 0; } main.c mypgm.c mypgm.h

11 To compile (by hand) gcc main.c mypgm.c –o run or or gcc -c main.c -o main.o gcc -c main.c -o main.o gcc -c mypgm.c -o mypgm.o gcc -c mypgm.c -o mypgm.o gcc mypgm.o main.o -o run gcc mypgm.o main.o -o run

In-class exercise Copy the makefile tutorial directory to your working environment, e.g. Copy the makefile tutorial directory to your working environment, e.g. –scp -r hulman.edu:/class/csse/csse332/0708c/Code/ maketutorial temp –svn export temp maketutorial –rm –rf temp 12

In-class exercise (cont.) Compile main.c and author.c Compile main.c and author.c Link them Link them Make a change to author.c and build the executable again Make a change to author.c and build the executable again Make a change to author.h and build the executable again Make a change to author.h and build the executable again 13

To compile (using a makefile) Run make (woohoo!) Run make (woohoo!) 14

In-class exercise (cont.) Run make Run make Make a change to author.c and run make again Make a change to author.c and run make again Make a change to author.h and run make again Make a change to author.h and run make again Examine the makefile Examine the makefile –Read the comments –Note the variables –Identify and understand the dependency rules (targets, dependencies, actions) –Note on whitespace:  Targets must be followed by tabs, not spaces  Actions must be preceded by tabs, not spaces –This makefile is more complicated than it needs to be, but scales well 15

16 Tips to programming well in C Always initialize your variables before using their values (especially pointers) Always initialize your variables before using their values (especially pointers) Don’t use pointers after freeing them Don’t use pointers after freeing them Don’t return pointers to local variables of functions Don’t return pointers to local variables of functions There are no “exceptions,” so check for errors everywhere There are no “exceptions,” so check for errors everywhere An array is also a pointer, but its value is immutable. An array is also a pointer, but its value is immutable. Compile your programs with a Makefile Compile your programs with a Makefile