CSE 333 Lecture 7 - final C details

Similar documents
Final C Details. CSE 333 Autumn 2018

Final C Details. CSE 333 Winter Teaching Assistants: Alexey Beall Renshu Gu Harshita Neti David Porter Forrest Timour Soumya Vasisht

Final C Details, Build Tools

Programming. Projects with Multiple Files

Final C Details, Build Tools (make)

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Exercise Session 2 Simon Gerber

CSE 333 Lecture 6 - data structures

Modifiers. int foo(int x) { static int y=0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; }

CSE 333 Lecture 6 - data structures

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Chapter 11 Introduction to Programming in C

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Kurt Schmidt. October 30, 2018

Chapter 11 Introduction to Programming in C

Model Viva Questions for Programming in C lab

CSE 333 Midterm Exam July 24, Name UW ID#

Chapter 11 Introduction to Programming in C

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

Chapter 11 Introduction to Programming in C

CS 261 Fall Mike Lam, Professor. Structs and I/O

CSE 333 Lecture 5 - data structures & modules

Chapter 11 Introduction to Programming in C

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

CSE 333 Lecture 2 Memory

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

C-String Library Functions

Lecture 03 Bits, Bytes and Data Types

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

CSE 333 Midterm Exam July 24, 2017 Sample Solution

Chapter 11 Introduction to Programming in C

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

CS 0449 Sample Midterm

Contents. A Review of C language. Visual C Visual C++ 6.0

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

JTSK Programming in C II C-Lab II. Lecture 3 & 4

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Signals, Instruments, and Systems W2. C Programming (continued)

211: Computer Architecture Summer 2016

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction.

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Programming. The greatest gift you can give another is the purity of your attention. Richard Moss

C for C++ Programmers

CS 220: Introduction to Parallel Computing. Input/Output. Lecture 7

Lecture 10: building large projects, beginning C++, C++ and structs

Deep C by Olve Maudal

High Performance Programming Programming in C part 1

CSE 374 Programming Concepts & Tools

C programming basics T3-1 -

CSE 333 Midterm Exam Nov. 3, 2017 Sample Solution

8. Characters, Strings and Files

Program Translation. text. text. binary. binary. C program (p1.c) Compiler (gcc -S) Asm code (p1.s) Assembler (gcc or as) Object code (p1.

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

Lectures 5-6: Introduction to C

block structure, scope and macros

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

CSCI565 Compiler Design

CE221 Programming in C++ Part 1 Introduction

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

CSSE 332 Standard Library, Storage classes, and Make

Deep C (and C++) by Olve Maudal

CSE 333 Midterm Exam 7/27/15 Sample Solution

CpSc 1010, Fall 2014 Lab 10: Command-Line Parameters (Week of 10/27/2014)

Lesson #8. Structures Linked Lists Command Line Arguments

THE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY. ISA 563: Fundamentals of Systems Programming

Intermediate Programming, Spring 2017*

Slide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 12, FALL 2012

CSE 333 Lecture 3 - arrays, memory, pointers

CSE2301. Functions. Functions and Compiler Directives

CSE 333 Autumn 2013 Midterm

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

MIDTERM EXAM. CS 217 October 28, Name: Precept: Honor Code: Score: Problem Score Max

Model Viva Questions for Programming in C lab

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

Chapter 8 C Characters and Strings

CSE 333 Lecture 9 - intro to C++

CSE 333 Midterm Exam 7/22/12

Work relative to other classes

PRINCIPLES OF OPERATING SYSTEMS

CMPE-013/L. Introduction to C Programming. Unit testing

Personal SE. Functions, Arrays, Strings & Command Line Arguments

Lectures 5-6: Introduction to C

Chapter IV Introduction to C for Java programmers

ELEC 377 C Programming Tutorial. ELEC Operating Systems

Review: Constants. Modules and Interfaces. Modules. Clients, Interfaces, Implementations. Client. Interface. Implementation

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Practical C Issues:! Preprocessor Directives, Multi-file Development, Makefiles. CS449 Fall 2017

Exercise Session 2 Systems Programming and Computer Architecture

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

SWEN-250 Personal SE. Introduction to C

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Luar Topics. C A Low level Programming Language Make A dependency based build system YUYV Representation of Color ZMP Zero Moment Point control

Transcription:

CSE 333 Lecture 7 - final C details Steve Gribble Department of Computer Science & Engineering University of Washington

Today s topics: - a few final C details header guards and other preprocessor tricks extern, static and visibility of symbols some topics for you to research on your own - do a programming exercise together

an #include problem What happens when we compile foo.c? typedef void *LinkedList; // more definitions below #include "ll.h" typedef void *HashTable; ll.h ht.h #include "ll.h" #include "ht.h" int main(int argc, char **argv) { //... do stuff here... return 0; foo.c // A hypothetical function LinkedList HTKeyList(HashTable t);

an #include problem What happens when we compile foo.c? bash$ gcc -Wall -g -o foo foo.c In file included from ht.h:1, from foo.c:2: ll.h:1: error: redefinition of typedef LinkedList ll.h:1: note: previous declaration of LinkedList was here foo.c includes ll.h twice! - 2nd time is indirectly via ht.h - so, typedef shows up twice! - try using cpp to see this foo.c ll.h ht.h

header guards A commonly used C preprocessor trick to deal with this - uses macro definition (#define) - uses conditional compilation (#ifndef and #endif) #ifndef _LL_H_ #define _LL_H_ 1 typedef void *LinkedList; // more definitions below #endif // _LL_H_ ll.h #ifndef _HT_H_ #define _HT_H_ #include "ll.h" typedef void *HashTable; // A hypothetical function LinkedList HTKeyList(HashTable t); #endif // _HT_H_ ht.h

Other preprocessor tricks A way to deal with magic constants int globalbuffer[1000]; void circalc(float rad, float *circumf, float *area) { *circum = rad * 2.0 * 3.1415; *area = rad * 3.1415 * 3.1415; bad code (littered with magic constants) #define BUFSIZE 1000 #define PI 3.14159265359 int globalbuffer[bufsize]; void circalc(float rad, float *circumf, float *area) { *circum = rad * 2.0 * PI; *area = rad * 3.1415 * PI; better code

Macros You can pass arguments to macros #define ODD(x) ((x) % 2!= 0) void foo(void) { if ( ODD(5) ) printf("5 is odd!\n"); cpp void foo(void) { if ( ((5) % 2!= 0) ) printf("5 is odd!\n"); Be careful of precedence issues; use parenthesis: #define ODD(x) ((x) % 2!= 0) #define BAD(x) x % 2!= 0 ODD(5 + 1); BAD(5 + 1); cpp ((5 + 1) % 2!= 0); 5 + 1 % 2!= 0;

Namespace problem If I define a global variable named counter in foo.c, is it visible in bar.c? - if you use external linkage: yes the name counter refers to the same variable in both files the variable is defined in one file, declared in the other(s) when the program is linked, the symbol resolves to one location - if you use internal linkage: no the name counter refers to different variables in each file the variable must be defined in each file when the program is linked, the symbols resolve to two locations

External linkage #include <stdio.h> // A global variable, defined and // initialized here in foo.c. // It has external linkage by // default. int counter = 1; int main(int argc, char **argv) { printf("%d\n", counter); bar(); printf("%d\n", counter); return 0; foo.c #include <stdio.h> // "counter" is defined and // initialized in foo.c. // Here, we declare it, and // specify external linkage // by using the extern // specifier. extern int counter; void bar(void) { counter++; printf("(b): counter %d\n", counter); bar.c

Internal linkage #include <stdio.h> // A global variable, defined and // initialized here in foo.c. // We force internal linkage by // using the static specifier. static int counter = 1; int main(int argc, char **argv) { printf("%d\n", counter); bar(); printf("%d\n", counter); return 0; foo.c #include <stdio.h> // A global variable, defined and // initialized here in bar.c. // We force internal linkage by // using the static specifier. static int counter = 100; void bar(void) { counter++; printf("(b): counter %d\n", counter); bar.c

Some gotchas Every global (variables and functions) is extern by default. - unless you specify the static specifier, if some other module uses the same name, you ll end up with a collision! best case: compiler error worst case: stomp all over each other - it s good practice to: use static to defend your globals (hide your private stuff!) place external global variable declarations in a module s header file

Extern, static functions // By using the static specifier, we are indicating // that foo() should have internal linkage. Other //.c files cannot see or invoke foo(). static int foo(int x) { return x*3 + 1; // Bar is "extern" by default. Thus, other.c files // could declare our bar() and invoke it. int bar(int x) { return 2*foo(x); bar.c #include <stdio.h> extern int bar(int); int main(int argc, char **argv) { printf("%d\n", bar(5)); return 0; main.c

Somebody should get fired C has a second, different use for the word static - to declare the extent of a local variable - if you declare a static local variable, then: the storage for that variable is allocated when the program loads, in either the program s.data or.bss segment the variable retains its value across multiple function invocations (see static_extent.c for an example)

Additional C topics Homework: teach yourself the following topics - bit-level manipulation in C: ~ & << >> - string library functions provided by the C standard library #include <string.h> strlen( ), strcp( ), strdup( ), strcat( ), strcmp( ), strchr( ), strstr( ),... learn why strncat is safer (in the security sense) than strcat, etc. #include <stdlib.h> or #include <stdio.h> atoi( ), atof( ), sprintf( ), sscanf( ) - man pages are your friend!

Additional C topics Teach yourself: - the syntax for function pointers, including passing as args - how to declare, define, and use a function that accepts a variable-lengthed number of arguments (varargs) - unions and what they are good for - what argc and argv are for in main #include <stdio.h> int main(int argc, char **argv) { int i; for (i = 0; i < argc, i++) { printf("%d: %s\n", i, argv[i]); return 0; argv.c bash$ gcc -o argv argv.c bash$./argv 0:./argv bash$./argv foo bar 0:./argv 1: foo 2: bar bash$

Additional C topics Teach yourself: - the difference between pre-increment (++v) and postincrement (v++) - the meaning of the register storage class - harder: the meaning of the volatile storage class pages 91, 92 of CARM

At your leisure To understand some of the beauty, lore, and craft of C: - http://www-cs-students.stanford.edu/~blynn/c/ To understand the potential horrors of C: - http://www0.us.ioccc.org/main.html - e.g., http://www0.us.ioccc.org/2004/gavare.c

LIVE CODING EXAMPLE

Exercise 1 Write a program that: - prompts the user to input a string (use fgets( )) assume the string is a sequence of whitespace-separated integers e.g., 5555 1234 4 5543 - converts the string into an array of integers - converts an array of integers into an array of strings where each element of the string array is the binary representation of the associated integer - prints out the array of strings

Exercise 2 Modify the linked list code from last lecture / exercise 1 - add static declarations to any internal functions you impemented in linkedlist.h - add a header guard to the header file - write a Makefile use Google to figure out how to add rules to the Makefile to produce a library (liblinkedlist.a) that contains the linked list code

See you on Wednesday!