#include. Practical C Issues: #define. #define Macros. Example. #if

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

Practical C Programming

C Programming. The C Preprocessor and Some Advanced Topics. Learn More about #define. Define a macro name Create function-like macros.

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

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

Contents Lecture 3. C Preprocessor, Chapter 11 Declarations, Chapter 8. Jonas Skeppstedt Lecture / 44

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Macros and Preprocessor. CGS 3460, Lecture 39 Apr 17, 2006 Hen-I Yang

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

Chapter 7: Preprocessing Directives

Unit 4 Preprocessor Directives

Errors During Compilation and Execution Background Information

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

Appendix A. The Preprocessor

Intermediate Programming, Spring 2017*

G52CPP C++ Programming Lecture 8. Dr Jason Atkin

Topic 6: A Quick Intro To C

COMP322 - Introduction to C++

OBJECT ORIENTED PROGRAMMING USING C++

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

COMsW Introduction to Computer Programming in C

C and C++ 2. Functions Preprocessor. Alan Mycroft

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 C: Linked list, casts, the rest of the preprocessor (Thanks to Hal Perkins)

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

A Fast Review of C Essentials Part II

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

Exercise Session 2 Simon Gerber

COSC 2P91. Bringing it all together... Week 4b. Brock University. Brock University (Week 4b) Bringing it all together... 1 / 22

CS240: Programming in C

EL6483: Brief Overview of C Programming Language

Lectures 5-6: Introduction to C

211: Computer Architecture Summer 2016

Conditional Compilation

Programming in C - Part 2

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

C: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13

Advanced Pointer & Data Storage

#include <stdio.h> int main() { printf ("hello class\n"); return 0; }

Control flow and string example. C and C++ Functions. Function type-system nasties. 2. Functions Preprocessor. Alastair R. Beresford.

Computational Methods of Scientific Programming Fall 2007

High Performance Programming Programming in C part 1

A Fast Review of C Essentials Part I

CS3157: Advanced Programming. Outline

C for Engineers and Scientists: An Interpretive Approach. Chapter 7: Preprocessing Directives

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Class Information ANNOUCEMENTS

More on C programming

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

M1-R4: Programing and Problem Solving using C (JAN 2019)

BLM2031 Structured Programming. Zeyneb KURT

Lesson 5: Functions and Libraries. EE3490E: Programming S1 2018/2019 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

CMPSC 311- Introduction to Systems Programming Module: Build Processing

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

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

Principles of C and Memory Management

Programming for Engineers C Preprocessor

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Course organization. Course introduction ( Week 1)

Have examined process Creating program Have developed program Written in C Source code

CSCI 171 Chapter Outlines

CSE 374 Programming Concepts & Tools

Chapter 11 Introduction to Programming in C

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

Chapter 11 Introduction to Programming in C

Kurt Schmidt. October 30, 2018

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

MODULE 5: Pointers, Preprocessor Directives and Data Structures

CMPSC 311- Introduction to Systems Programming Module: Build Processing


Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Lectures 5-6: Introduction to C

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 2. Spring 2016

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Recitation #11 Malloc Lab. November 7th, 2017

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE

Princeton University COS 333: Advanced Programming Techniques A Subset of C90

Macros in C/C++ Computer Science and Engineering College of Engineering The Ohio State University. Lecture 33

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Advanced Pointer Topics

C PROGRAMMING LANGUAGE. POINTERS, ARRAYS, OPERATORS AND LOOP. CAAM 519, CHAPTER5

Chapter 11 Introduction to Programming in C

CSE 333 Midterm Exam 7/27/15 Sample Solution

Lecture 19 Notes Data Structures in C

ECEN 449 Microprocessor System Design. Review of C Programming

UNIT III (PART-II) & UNIT IV(PART-I)

CSE 374 Programming Concepts & Tools

G52CPP C++ Programming Lecture 9

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

CSE 333 Lecture 2 Memory

MODULE 10 PREPROCESSOR DIRECTIVES

Page 1. Agenda. Programming Languages. C Compilation Process

2. Which of the following will print the value 2 for the above code?

CSE 374 Final Exam Sample Solution 6/10/10

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

Transcription:

#include Practical C Issues: Preprocessor Directives, Typedefs, Multi-file Development, and Makefiles Jonathan Misurda jmisurda@cs.pitt.edu Copies the contents of the specified file into the current file < > indicate to look in a known location for includes indicate to look in the current directory or specified path #include myheader.h #define Textual Symbol Replacements #define Macros Textual replacements with parameters: #define PI 3.1415926535 #define MAX 10 float f = PI; for(i=0;i<max;i++) Good: #define MAX(a, b) (a > b)? a : b Not so good: #define SWAP(a,b) int t=a; a=b; b=t; #if #if <condition that can be evaluated by the preprocessor> What does preprocessor know? Values of #defined variables Constants Example #if 0 printf( this is not printed\n ); printf( This is printed\n );

Example 2 #else #define VERSION 5 #if VERSION < 5 printf( this is not printed\n ); printf( This is printed\n ); #if #elif #else #ifdef #if defined Checks to see if a macro has been defined, but doesn t care about the value A defined macro might expand to nothing, but is still considered defined #define MACRO Example #if defined MACRO printf( this is printed\n ); printf( This is also printed\n ); Undefines a macro: #define MACRO #undef MACRO #undef Shortcuts #if defined #ifdef #if!defined #ifndef #if defined MACRO printf( this is not printed\n ); printf( This is printed\n );

Uses Handle Operating System/Architecture specific code Handle differences in compilers Build program with different features Debugging: #ifdef DEBUG printf() Notes Can define variables from the commandline with D gcc o test DVERSION=5 test.c gcc o test DMACRO test.c Other Preprocessor Details Defined Constants # quotes a string ## concatenates two things #pragma #warn #error Macro FILE LINE DATE TIME STDC Meaning The currently compiled file The current line number The current date The current time Defined if compiler supports ANSI C Many other compiler specific flags typedef Type Clarity typedef type declaration synonym; Examples: typedef int * int_pointer; typedef int * int_array; void takes_int(int_pointer x) *x = 3; void takes_array(int_array x, int n) int i; for(i=0; i<n; i++) printf( %d\n, x[i]);

Typedef Structures Struct with Instance #include <stdlib.h> typedef void (*FP)(int, int); Function Pointers typedef struct node int i; struct node *next; Node; Node *head; struct node int i; struct node *next; Node; void f(int a, int b) printf("%d\n", a+b) void g(int a, int b) printf("%d\n", a*b) FP ar1 = f; FP ar2 = g; ar1(2,3); ar2(2,3); Function Pointers As Parameters void qsort ( void *base, size_t num, size_t size, int (*comparator)(const void *, const void *) ); Comparator int compare_ints(const void *a,const void *b) int *x = (int *)a; int *y = (int *)b; return *x *y; Multi-file Development Want to break up a program into multiple files Easier to maintain Multiple authors Quicker compilation Modularity Static Local Scope Scope: Local Lifetime: Global (life of program) void f() static int x;

File Scope Global Variables are actually limited to the file extern maybe be used to import variables from other files File A int x; File B extern int x; int x = 0; a.c int f(int y) return x+y; Example b.c extern int x; int f(int); x = 5; printf("%d", f(0)); Will refer to the same memory location Compiling Static gcc a.c b.c./a.out 5 a.c static int x = 0; static int f(int y) return x+y; b.c extern int x; int f(int); x = 5; printf("%d", f(0)); gcc a.c b.c Compiling /tmp/cccyucua.o(.text+0x6): In function `main': : undefined reference to `x' /tmp/cccyucua.o(.text+0x19): In function `main': : undefined reference to `f' collect2: ld returned 1 exit status Header Files Usually only contain declarations Variables Functions #defined macros Paired with an implementation file

Including a Header File Once Headers and Implementation #ifndef _MYHEADER_H_ #define _MYHEADER_H_ Definitions of header to only be included once mymalloc.h void *my_nextfit_malloc(int size); void my_free(void *ptr); mymalloc.c static MallocInfo *head; void *my_nextfit_malloc(int size) static MallocInfo *current;... void my_free(void *ptr)... Driver Driver program: #include mymalloc.h Makefiles Express what files depend upon others If any are modified, build smallest set required Can now use those functions Compile: gcc o malloctest mymalloc.c mallocdriver.c Makefile Dependency Graph malloctest: mymalloc.o mallocdriver.o gcc o malloctest mymalloc.o mallocdriver.o malloctest mymalloc.o: mymalloc.c mymalloc.h gcc c mymalloc.c mallocdriver.o: mymalloc.h mallocdriver.c gcc c mallocdriver.c mymalloc.o mallocdriver.o mymalloc.c mymalloc.h mallocdriver. c