A Fast Review of C Essentials Part II

Similar documents
independent compilation and Make

A Fast Review of C Essentials Part I

Appendix A. The Preprocessor

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

Chapter 7: Preprocessing Directives

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

Conditional Compilation

OBJECT ORIENTED PROGRAMMING USING C++

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

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

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

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

Unit 4 Preprocessor Directives

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

PROGRAMMAZIONE I A.A. 2017/2018

Intermediate Programming, Spring 2017*

CS2141 Software Development using C/C++ Compiling a C++ Program

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

Chapter 2, Part I Introduction to C Programming

fpp: Fortran preprocessor March 9, 2009

CS240: Programming in C

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

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #54. Organizing Code in multiple files

The Make Utility. Independent compilation. Large programs are difficult to maintain. Problem solved by breaking the program into separate files

The Make Utility. Independent compilation. Large programs are difficult to maintain. Problem solved by breaking the program into separate files

Topic 6: A Quick Intro To C

Errors During Compilation and Execution Background Information

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

2 Compiling a C program

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

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

CSE 374 Programming Concepts & Tools

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

EL6483: Brief Overview of C Programming Language

Programming for Engineers C Preprocessor

Lecture 2: C Programming Basic

0x0d2C May your signals all trap May your references be bounded All memory aligned Floats to ints round. remember...

CSCI 171 Chapter Outlines

Fundamentals of Programming Session 4

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Full file at C How to Program, 6/e Multiple Choice Test Bank

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

Chapter 11 Introduction to Programming in C

The C Pre Processor ECE2893. Lecture 18. ECE2893 The C Pre Processor Spring / 10

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

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

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

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

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


C: How to Program. Week /Mar/05

C Preprocessor. Prabhat Kumar Padhy

Project Build Process. Abhijit Bhosale M.Tech (IT) School of Information Technology, IIT Kharagpur

Intermediate Programming, Spring 2017*

System calls. Reading from a file. Closing a file

COMsW Introduction to Computer Programming in C

Chapter 11 Introduction to Programming in C

CS3157: Advanced Programming. Outline

RVDS 4.0 Introductory Tutorial

Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang

Chapter 2 - Introduction to C Programming

Chapter 11 Introduction to Programming in C

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

Chapter 11 Introduction to Programming in C

The C Preprocessor (and more)!

Chapter 11 Introduction to Programming in C

Makefiles SE 2XA3. Term I, 2018/19

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

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

A Fast Review of C Essentials Part II

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

Programming for Engineers Introduction to C

3 PREPROCESSOR. Overview. Listing 3-0. Table 3-0.

NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313.

make and makefile CS 211

COMP322 - Introduction to C++

C introduction: part 1

SU2017. LAB 1 (May 4/9) Introduction to C, Function Declaration vs. Definition, Basic I/O (scanf/printf, getchar/putchar)

The make Utility in Unix SEEM

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

Chapter 1 & 2 Introduction to C Language

Chapter 11 Introduction to Programming in C

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

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

C Programming Review CSC 4320/6320

1 You seek performance 3

The make Utility in Unix SEEM

CMPSC 311- Introduction to Systems Programming Module: Build Processing

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

CSE 374 Programming Concepts & Tools

CSE 333 Autumn 2014 Midterm Key

Unit 1: Introduction to C Language. Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune

BLM2031 Structured Programming. Zeyneb KURT

RVDS 3.0 Introductory Tutorial

CSE 333 Midterm Exam July 24, Name UW ID#

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

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Transcription:

A Fast Review of C Essentials Part II Structural Programming by Z. Cihan TAYSI Outline Macro processing Macro substitution Removing a macro definition Macros vs. functions Built-in macros Conditional compilation Testing macro existence Include facility Line control 1

Macros All preprocessor directives begin with a pound sign (#), which must be the first nonspace character on the line Unlike C statements, a macro command ends with a newline, not a semicolon. to span a macro over more than one line, enter a backslash immediately before the newline #define LONG_MACRO This is a very long macro that \ spans two lines Macro Substitution The simplest and most common use of macros is to represent numeric constant values. It is also possible to create function like macros #define BUFF_LEN (512) char buf[buff_len]; char buf[(512)]; 2

Function Like Macros Be careful not to use ; at the end of macro or = in macro definition No type checking for macro arguments Try to expand min macro example for three numbers Example 1 : #define MUL_BY_TWO(a) ((a) + (a)) j = MUL_BY_TWO(5); f = MUL_BY_TWO(2.5); Example 2 : #define min(a, b) ( (a) < (b)? (a) : (b)) Side Effect #define min(a,b) ((a) < (b)? (a) : (b)) Remember min macro a = min(b++, c); a = ((b++) < (c)? (b++) : (c)); Suppose, for instance, that we invoked the min macro like this! The preprocessor translates this into! 3

Macros vs. Functions Advantages Disadvantages Macro arguments are reevaluated at each Macros are usually faster than mention in the macro body, which can lead to functions, since they avoid the unexpected behavior if an argument contains side effects! function call overhead. Function bodies are compiled once so that multiple calls to the same function can share No type restriction is placed on the same code. Macros, on the other hand, are expanded each time they appear in a arguments so that one macro program. may serve for several data Though macros check the number of arguments, they don t check the argument types. types. It is more difficult to debug programs that contain macros,because the source code goes through an additional layer of translation. Yıldız Teknik Üniversitesi - Bilgisayar Mühendisliği Bölümü Removing a Macro Definition Once defined a macro name retains its meaning until the end of the source file. or until it is expilicitly removed with an #undef directive. The most typical use of #undef is to remove a definition so you can redefine it. ifndef FALSE define FALSE 0 elif FALSE undef FALSE define FALSE 0 endif 4

Built-in Macros I LINE expands to the source file line number on which it is invoked. FILE expands to the name of the file in which it is invoked. TIME expands to the time of program compilation. DATE expands to the date of program compilation. STDC Expands to the constant 1, if the compiler conforms to the ANSI Standard. Built-in Macros II void print_version( ) { printf( This utility compiled on %s at %s\n, DATE, TIME ); } void print_version( ) { printf( This meesage is at %d line in %s\n, LINE, FILE ); } 5

Conditional Compilation I The preprocessor enables you to screen out portions of source code that you do not want compiled. This is done through a set of preprocessor directives that are similar to if and else statements. The preprocessor versions are #if, #else, #elif, #endif Conditional compilation particularly useful during the debugging stage of program development, since you can turn sections of your code on or off by changing the value of a macro Most compilers have a command line option that lets you define macros before compilation begins. gcc DDEBUG=1 test.c Conditional Compilation II The conditional expression in an #if or #elif statement need not be enclosed in parenthesis. Blocks of statements under the control of a conditional preprocessor directive are not enclosed in braces. Every #if block may contain any number of #elif blocks, but no more than one #else block, which should be the last one! Every #if block must end with an #endif directive! #if x==1 #undef x define x 0 #elif x == 2 #undef x #define x 3 #else #define y 4 #endif 6

Conditional Compilation III #if defined TEST #if defined (TEST) #if defined macro_name #ifdef macro_name #if!defined macro_name #ifndef macro_name Include Facility The #include command has two forms #include <filename> : the preprocessor looks in a list of implementation-defined places for the file. In UNIX systems, standard include files are often located in the directory /usr/include #include filename : the preprocessor looks for the file according to the file specification rules of operating system. If it can not find the file there, it searches for the file as if it had been enclosed in angle brackets. The #include command enables you to create common definition files, called header files, to be shared by several source files. Traditionally have a.h extention contain data structure definitions, macro definitions, function prototypes and global data 7

Line Control Allows you to change compiler s knowledge of the current line number of the source file and the name of the source file. The #line feature is particularly useful for programs that produce C source text. For example yacc (Yet Another Compiler Compiler) is a UNIX utility that facilitates building compilers. main() { #line 100 printf( Current line :%d\nfilename : %s\n\n, LINE, FILE ); #line 200 new name printf( Current line :%d\nfilename : %s\n\n, LINE, FILE ); } Splitting Your C Program I At least one of the files must have a main() function. To use functions from another file, make a.h file with the function prototypes, and use #include to include those.h files within your.c files. Be sure no two files have functions with the same name in it. The compiler will get confused. Similarly, if you use global variables in your program, be sure no two files define the same global variables. 8

Splitting Your C Program II If you use global variables, be sure only one of the files defines them, and declare them in your.h as follows: extern int globalvar; When you define a variable, it looks like this: int globalvar; When you declare a variable, it looks like this: extern int globalvar; The main difference is that a variable definition creates the variable, while a declaration indicates that the variable is defined elsewhere. A definition implies a declaration. An EXAMPLE #ifndef salute_h #define salute_h void salute( void ); #endif #include <stdio.h> #include salute.h int main ( void ) { #include <stdio.h> void salute( void ) { printf( \n\n HELLO!!! \n\n ); } } salute(); return 0; 9

Compiling with Several Files I The command to perform this task is simply gcc file.c There are 3 steps to obtain the final executable program Compiler stage Assembler stage Linker stage Compiling with Several Files II You can use the -c option with gcc to create the corresponding object (.o) file from a.c file. gcc -c green.c will not produce an a.out file, but the compiler will stop after the assembler stage, leaving you with a green.o file. 10

Compiling with Several Files III The three different tasks required to produce the executable program are as follows: Compile green.o: gcc -c green.c Compile blue.o: gcc -c blue.c Link the parts together: gcc green.o blue.o The Make Command helps you to manage large programs or groups of programs keeps track of which portions of the entire program have been changed compiles only those parts of the program which have changed since the last compile. 11

How Does Make do It? The make program gets its dependency "graph" from a text file called makefile or Makefile, which resides in the same directory as the source files make checks the modification times of the files, and whenever a file becomes "newer" than something that depends on it, it runs the compiler accordingly. How Dependency Works Case : while you are testing the program, you realize that one function in io.c has a bug in it. You edit io.c to fix the bug. notice that io.o needs to be updated because io.c has changed. Similarly, because io.o has changed, project1 needs to be updated as well. 12

Translating The Dependency Graph Each dependency shown in the graph is circled with a corresponding color in the Makefile, and each uses the following format: target : source file(s) command (must be preceded by a tab) Listing Dependencies Note that in the Makefile shown on the right, the.h files are listed, but there are no references in their corresponding commands. This is because the.h files are referred within the corresponding.c files through the #include "file.h". If you do not explicitly include these in your Makefile, your program will not be updated if you make a change to your header (.h) files. 13

Using the Makefile with make Once you have created your Makefile and your corresponding source files, you are ready to use make. If you have named your Makefile either Makefile or makefile, make will recognize it. If you do not wish to call your Makefile one of these names, you can use make -f mymakefile. Macros in make The make program allows you to use macros, which are similar to variables, to store names of files. The format is as follows: OBJECTS = data.o io.o main.o Whenever you want to have make expand these macros out when it runs, type the following corresponding string $(OBJECTS) 14

Special Macros In addition to those macros which you can create yourself, there are a few macros which are used internally by the make program. Here are some of those, listed below: References & More Reading References http://www.eng.hawaii.edu/tutor/make/index.html More reading on make command http://www.cs.duke.edu/~ola/courses/programming/makefiles/makefiles.ht ml http://www.hsrl.rutgers.edu/ug/make_help.html http://publib.boulder.ibm.com/infocenter/systems/index.jsp?topic=/com.ibm.aix.genprogc/doc/genprogc/make.htm 15