introduction to c (with a bit of c++ syntax) doug sondak scv [email protected]

97
Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV [email protected]

Post on 22-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Introduction to C (with a Bit of C++ Syntax)

Doug SondakSCV

[email protected]

Page 2: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Outline

• Goals• C/C++ History• Basic syntax• makefiles• Additional syntax

Page 3: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Goals

• To be able to write simple C (C++) programs• To be able to understand and modify existing

C code• To be able to write and use makefiles

Page 4: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

C History

• Developed by Dennis Ritchie at Bell Labs in 1972– Originally designed for system software– Impetus was porting of Unix to a DEC PDP-11• PDP-11 had 24kB main memory!

• 1978 book “The C Programming Language” by Kernighan & Ritchie served as standard

• Official ANSI standard published in 1989– Updated in 1999

Page 5: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

C++ History

• C++ was developed by Bjarne Stroustrup at Bell Labs in 1979

• implemented object-oriented features of another language, “Simula,” in C

• originally called “C with Classes”• name changed to C++ in 1983• first commercial compiler in 1985• official standard published in 1988

Page 6: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

C vs. C++

• C is essentially a subset of C++• There are some minor “convenience features”

in C++ that we will utilize here• Here we will use the GNU C++ compiler g++• Since we will be writing rudimentary codes,

we will not get into object-oriented constructs

Page 7: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

C/C++ Syntax

• Source lines end with semicolons (as in Matlab)• Case-sensitive• Spaces don’t matter except within literal

character strings– I use them liberally to make code easy to read

• Comments– C: enclosed by /* */– C++: // at beginning of comment

• many C compilers also accept this syntax

Page 8: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

C/C++ Syntax (cont’d)

• Declarations – must declare each variable as being integer, floating-point, character, etc.– Required because different types are represented differently

internally• Since integers have no decimal places, integer arithmetic

will truncate result– If i=3 and k=2, i/k=1, k/i=0

• Program blocks are enclosed within { }• Source file suffix– C: .c– C++: usually .cc or .C

Page 9: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

C/C++ Syntax (3)

• Simple programs consist of functions and header files• Main program is a function called main (surprise!)• Functions have return types (int, float, etc.)

– even if they don’t return anything• Main function is defined by the return type, the word main

followed by any arguments within parenthesis, followed by the function statements within {}

int main(){ function statements}

type declaration function name function arguments (we have no arguments here but still need parentheses)

Page 10: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

C/C++ Syntax (4)

• Style note: some people like to arrange the brackets likeint main(){ function statements}

• Either way is fine– Be consistent!

Page 11: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

C/C++ Syntax (5)

• Header files contain re-usable code elements– Function definitions, variable declarations, etc.– Sometimes use system header files– Sometimes you write them yourself– In C usually have a .h suffix, may not in C++ #include <header_file_name.h>– Included before function definition– Note that the #include statement does not end

with a ;

Page 12: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

C/C++ Syntax (6)

• A character string is enclosed by double quotes– Characters within the quotes will be taken literally“This is my character string.”

• Single character is enclosed in single quotes‘h’

Page 13: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

C/C++ Syntax (7)

• cout is a stream that is used to direct output to the screen, e.g.cout << “my string”;– sends the string to cout, i.e., to the screen

• The above syntax does not include a carriage return at the end of the line. We can add the carriage return with:cout << “my string” << endl;

Page 14: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

C/C++ Syntax (8)

• In C and C++ many standard functions, variables, streams, etc. are defined in header files.

• If you want to use these variables, etc., you must include the appropriate header file in your code

• C++ (but not C) has a feature, namespace, that defines packages of functions, etc.– This is fairly new, and you might not see it in older C+

+ programs

Page 15: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

C/C++ Syntax (9)

• The cout stream is defined in the iostream header file, which is part of the “std” namespace

• The syntax to use the std namespace isusing namespace std;

Page 16: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Exercise 1

• Write a “hello world” program in an editor• Program should print a character string• General structure of code, in order:– include iostream (see slide 11)

• not iostream.h, just “iostream”• use the “std” namespace (see slide 15)

– define main function (see slide 9)– use cout and endl to print string to screen (see slide 13)

• Save it to the file name hello.cc• solution

Page 17: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Compilation

• A compiler is a program that reads source code and converts it to a form usable by the computer

• Code compiled for a given type of processor will not generally run on other types– AMD and Intel are compatible

• We’ll use g++, since it’s free and ubiquitous

Page 18: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Compilation (cont’d)

• Compilers have huge numbers of options– See gcc compiler documentation athttp://gcc.gnu.org/onlinedocs/– gcc refers to the “GNU compiler collection,” which

includes the C compiler (gcc) and the C++ compiler (g++)

• For now, we will simply use the –o option, which allows you to specify the name of the resulting executable

Page 19: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Compilation (3)

• In a Unix window:g++ –o hello hello.cc

• Compile your code• If it simply returns a Unix prompt it worked• If you get error messages, read them carefully

and see if you can fix the source code and re-compile

Page 20: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Compilation (4)

• Unlike all other compilers I’ve used (gcc, gfortran, pgcc, etc.), g++ automatically adds a .exe suffix to your specified executable name– hello.exe

• Once it compiles correctly, type ./hello.exeat the Unix prompt, and it will print your string

Page 21: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Declarations• Lists of variables with their associated types

– C compilers usually require that they come before any executable statements

– C++ compilers allow them anywhere, often at the first use of the variable• Basic types:

– int• Usually 4 bytes

– float• Usually 4 bytes

– double• Usually 8 bytes

– char• Single character• One byte

Page 22: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Declarations (cont’d)

• Examples:int i, j, k;float xval, time;char name, date;

• A “char” variable represents a single character

Page 23: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Arithmetic

• +, -, *, /– No power operator (see next bullet)

• Math functions in math.h– pow(x,y) raises x to the y power– sin, acos, tanh, exp, sqrt, etc.– add –lm flag to compile command to access math library

• Exponential notation indicated by letter “e” 4.2e3

• Good practice to use decimal points with floats, e.g., x = 1.0 rather than x = 1

3102.4

Page 24: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Arithmetic (cont’d)

• ++ and -- operators– these are equivalent:i = i+1;i++;– always increment/decrement by 1

• +=– these are equivalent:x = x + 46.3*y;x += 46.3*y;

Page 25: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Arithmetic (3)

• Can convert types with cast operatorfloat xval;int i, j;xval = (float) i / (float) j;

Page 26: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Exercise 2

• Write program to convert a Celcius temperature to Fahrenheit and print the result.– Hard-wire the Celcius value to 100.0• We’ll make it an input value in a subsequent exercise

– Don’t forget to declare all variables F = (9/5)C + 32

• solution

Page 27: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

cin

• cin reads input from the screencin >> var;– Note: >> rather than << as with cout– Writes input value to variable var

• Often use cout and cin to prompt for a value:cout << “Enter value: ”;cin >> x;

Page 28: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Exercise 3

• Modify Celcius program to read value from keyboard– Prompt for value using cout– Read value using cin– Rest can remain the same as last exercise

• solution

Page 29: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Arrays

• Can declare arrays using [ ]float x[100];char a[25];

• Array indices start at zero– Declaration of x above creates locations for x[0]

through x[99]• Multiple-dimension arrays are declared as

follows:int a[10][20];

Page 30: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Arrays (cont’d)

• Character strings (char arrays) always end with the character \0– You usually don’t have to worry about it as long as

you dimension the string 1 larger than the length of the required string

char name[5];name = “Fred”;

char name[4];name = “Fred”;

works

doesn’t work

Page 31: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

For Loop

• for loop repeats calculation over range of indicesfor(i=0; i<n; i++){ a[i] = sqrt( pow(b[i],2) + pow(c[i],2) )}

• for statement has 3 parts:– initialization– completion condition– what to do after each iteration

Page 32: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Exercise 4

• Write program to:– declare two vectors of length 3– prompt for vector values– calculate dot product– print the result

• solution

i

n

ii bac

1

Page 33: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Pointers

• Memory is organized in units of words– Word size is architecture-dependent– Pentium 4 bytes– Xeon, Itanium 8 bytes– Each word has a numerical address

08

1632

Page 34: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Pointers (cont’d)

• When you declare a variable, a location of appropriate size is reserved in memory

• When you set its value, the value is placed in that memory location

float x; x = 3.2;

08

1632

3.2

address

Page 35: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Pointers (3)

• A pointer is a variable containing a memory address

• Declared using * prefixfloat *p;

• Address operator &– Address of specified variablefloat x, *p;p = &x;

Page 36: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Pointers (4)

float x, *p;p = &x;

1040104810521056

address

08

1632

address

p 16

Page 37: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Pointers (5)

• Depending on context, * can also be the dereferencing operator– Value stored in memory location pointed to by specified pointer*p = 3.2;

• Common newbie errorfloat *p;*p = 3.2;

float x, *p;p = &x;*p = 3.2;

Wrong! – p doesn’t have value yet

correct

Page 38: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Pointers (6)

• The name of an array is actually a pointer to the memory location of the first element– a[100]– “a” is a pointer to the first element of the array

(a[0])• These are equivalent:

x[0] = 4.53;*x = 4.53;

Page 39: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Pointers (7)

• If p is a pointer and n is an integer, the syntax p+n means to advance the pointer by n memory locations

• These are therefore equivalent:x[4] = 4.53;*(x+4) = 4.53;

Page 40: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Pointers (8)

• In multi-dimensional arrays, values are stored in memory with last index varying most rapidly (a[0][0], a[0][1], … )– Opposite of Matlab and Fortran

• The two statements in each box are equivalent:

a[0][17] = 1; a[1][0] = 5;*(a+17) = 1; *(a+20) = 5;

Page 41: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

sizeof

• Some functions require size of something in bytes• A useful function – sizeof(arg)– The argument arg can be a variable, an array name, a

type– Returns no. bytes in argfloat x, y[5];sizeof(x) ( 4)sizeof(y) (20)sizeof(float) ( 4)

Page 42: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Dynamic Allocation

• Suppose you need an array, but you don’t know how big it needs to be until run time.

• Use malloc functionmalloc(n)– n is no. bytes to be allocated– returns pointer to allocated space– lives in stdlib.h

Page 43: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Dynamic Allocation (cont’d)

• Declare pointer of required typefloat *myarray;

• Suppose we need 101 elements in array• malloc requires no. bytes, cast as appropriate

pointermyarray = (float *) malloc(101*sizeof(float));

• free releases space when it’s no longer needed:free(myarray);

Page 44: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Exercise 5

• Modify dot-product program to handle vectors of any length– Prompt for length of vectors– Read length of vectors from screen– Dynamically allocate vectors– Don’t forget to include stdlib.h so you have access

to the malloc function• solution

Page 45: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

if/else if/else

• Conditional execution of block of source code• Based on relational operators

< less than> greater than== equal<= less than or equal>= greater than or equal!= not equal&& and|| or

Page 46: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

if/else if/else (cont’d)

if( x > 0.0 && y > 0.0 ){ z = 1.0/(x+y);}else if( x < 0.0 && y < 0.0){ z = -1.0/(x+y);}else{ printf(“Error condition\n”);}

Page 47: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

if/else if/else (3)

• Can use “if” without “else”

if( x > 0.0 && y > 0.0 ){ printf(“x and y are both positive\n”);}

Page 48: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Exercise 6

• In dot product code, check if the magnitude of the dot product is less than using the absolute value function fabsf. If it is, print a warning message.– With some compilers you would need to include

math.h for the abs function– With some compilers you would need to link to the

math library by adding the flag –lm to the end of your compile/link command

• solution

610

Page 49: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Functions

• Function returns a single object (number, array, etc.)• Return type must be declared• Argument types must be declared• Sample function definition:

float sumsqr(float x, float y){ float z; z = x*x + y*y; return z;}

Page 50: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Functions (cont’d)

• Use of sumsqr function:a = sumsqr(b,c);

• Call by value– when function is called, copies are made of the

arguments– scope of copies is scope of function• after return from function, copies no longer exist

Page 51: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Functions (3)

b = 2.0; c = 3.0;a = sumsqr(b, c);cout << b;

float sumsqr(float x, float y){ float z; z = x*x + y*y; x = 1938.6; return z;}

this line does nothing!

will print 2.0

Page 52: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Functions (4)

• If you want to change argument values, pass pointersint swap(int *i, int *j){ int k; k = *i; *i = *j; *j = k; return 0;}

Page 53: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Functions (5)

• Let’s examine the following code fragment:int a, b;a = 2; b = 3;swap(&a, &b);

• Memory after setting values of a and b

address

16202428

variable

ba

32

Page 54: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Functions (6)

• When function is called, copies of arguments are created in memory

• i, j are pointers to ints with values &a and &b

address

16202428

variable

ba

32

address

48525660

variable

ji

2420&a i

&b j

swap(&a, &b); int swap(int *i, int *j){ ... }

Page 55: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Functions (7)

• What happens to memory for each line in the function?

k

address

16202428

variable

ba

32

address

48525660

variable

j

i

2420

int k;

address

16202428

variable

ba

32

address

48525660

variable

ji

2420

k = *i;k2

Page 56: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Functions (8)

*i = *j;

address

16202428

variable

ba

33

address

48525660

variable

ji

2420

k2

address

16202428

variable

ba

23

address

48525660

variable

ji

2420

k2

*j = k;

Page 57: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Functions (9)

return 0 ;

address

16202428

variable

ba

23

address

48525660

variable

2420

2

Page 58: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Exercise 7

• Modify dot-product program to use a function to compute the dot product– The function definition should go before the main

program in the source file– Arguments can be an integer containing the length

of the vectors and a pointer to each vector– Do not give function same name as executable

• I called my executable “dotvec” and the function “dotprod”

• solution

Page 59: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Function Prototypes

• C compiler checks arguments in function definition and calls– number– type

• If definition and call are in different files, compiler needs more information to perform checks– this is done through function prototypes

Page 60: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Function Prototypes (cont’d)

• Prototype looks like 1st line of function definition– type– name– argument typesfloat dot(int n, float *x, float *y);

• Argument names are optional:float dot(int, float*, float*);

Page 61: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Function Prototypes (3)

• Prototypes are often contained in include files#include “mycode.h”int main(){…myfunc(x);…}

Page 62: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Basics of Code Management

• Large codes usually consist of multiple files• I create a separate file for each function– Easier to edit– Can recompile one function at a time

• Files can be compiled, but not linked, using –c option; then object files can be linked laterg++ –c mycode.cg++ –c myfunc.cg++ –o mycode mycode.o myfunc.o

Page 63: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Exercise 8

• Put dot-product function and main program in separate files

• Create header file– function prototype– .h suffix– include at top of file containing main

• Compile, link, and run• solution

Page 64: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Makefiles

• Make is a Unix utility to help manage codes• When you make changes to files, it will– automatically deduce which files have been

modified and compile them– link latest object files

• Makefile is a file that tells the make utility what to do

• Default name of file is “makefile” or “Makefile”– Can use other names if you’d like

Page 65: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Makefiles (cont’d)

• Makefile contains different sections with different functions– The sections are not executed in order!

• Comment character is #• There are defaults for some values, but I like

to define everything explicitly

Page 66: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Makefiles (3)

• variables– Some character strings appear repeatedly in makefiles– It’s convenient to give them names so if they are changed,

you only have to do it in one place– To define variable: NAME = string– No quotes are required for the string– String may contain spaces– “name” is any name you want– Variable names are usually all capitals– To continue line, use \ character

Page 67: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Makefiles (4)

• Variables (cont’d)– To use variable, either of these work:

$(NAME)${NAME}

• Example:– Define compiler

CC = g++

– To use elsewhere in makefile:$(CC)

Page 68: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Makefiles (5)

• Good practice to define compiler info in variablesCC = g++COMMONFLAGS = -O3COMPFLAGS = -c $(COMMONFLAGS)LINKFLAGS = $(COMMONFLAGS)

Page 69: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Makefiles (6)

• Have to define all file suffixes that may be encountered.SUFFIXES: .o .cc

• Just to be safe, delete any default suffixes first with a null .SUFFIXES: command.SUFFIXES:.SUFFIXES: .o .cc

Page 70: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Makefiles (7)

• Have to tell how to create one file suffix from another with a suffix rule.cc.o:

$(CC) $(COMPFLAGS) $*.cc• The first line indicates that the rule tells how to create

a .o file from a .cc file• The second line tells how to create the .o file• The big space before $(CC) is a tab, and you must use

it!• *$ is automatically the root of the first file

Page 71: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Makefiles (8)

• Usually define variable with all object file namesOBJ = mymain.o func1.o anotherfunc.o \

func2.o

Page 72: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Makefiles (9)

• Finally, everything falls together with the definition of a ruletarget: prerequisites

recipe• The target is any name you choose– Often use name of executable

• Prerequisites are files that are required by other files– e.g., executable requires object files

• Recipe tells what you want the makefile to do

Page 73: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Makefiles (10)### suffix rule.SUFFIXES:.SUFFIXES: .cc .o

$(CC) $(COMPFLAGS) $*.cc

### compilerCC = g++COMMONFLAGS = -O3COMPFLAGS = -c $(COMMONFLAGS)LINKFLAGS = $(COMMONFLAGS)

### objectsOBJ = mymain.o fun1.o fun2.o fun3.o

### compile and linkmyexe: $(OBJ)

$(CC) –o $@ $(LINKFLAGS) $(OBJ)Automatic variable for target

Page 74: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Makefiles (11)

• When you type “make,” it will look for a file called “makefile” or “Makefile”

• It then searches for the first target in the file• In our example (and the usual case) the object files are

prerequisites• It checks the suffix rule to see how to create an object file• In our case, it sees that .o files depend on .cc files• It checks the time stamps on the associated .o and .cc files

to see if the .cc is newer• If the .cc file is newer it performs the suffix rule

– In our case, compiles the routine

Page 75: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Makefiles (12)

• Once all the prerequisites are updated as required, it performs the recipe

• In our case it links the object files and creates our executable

• Many makefiles have an additional target, “clean,” that removes .o and other filesclean:

rm –f *.o• When there are multiple targets, specify desired target as

argument to make commandmake clean

Page 76: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Makefiles (13)

• Also may want to set up dependencies for header files– When header file is changed, files that include it

will automatically recompile• example:

myfunction.o: myincludefile.h– if time stamp on .h file is newer than .o file and .o

file is required in another dependency, will recompile myfunction.cc

Page 77: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Exercise 9

• Create a makefile for your dot product code• Delete your old object files• Build your code using the makefile• solution

Page 78: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

C Preprocessor

• Initial processing phase before compilation• Directives start with #• We’ve seen one directive already, #include– simply includes specified file in place of directive

• Another common directive is #define#define NAME text– NAME is any name you want to use– text is the text that replaces NAME wherever it

appears in source code

Page 79: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

C Preprocessor (cont’d)

• #define often used to define global constants#define NX 51#define NY 201…float x[NX][NY];

• Also handy to specify precision#define REAL double…REAL x, y;

Page 80: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

C Preprocessor (3)

• Since #define is often placed in header file, and header will be included in multiple files, this construct is commonly used:#ifndef REAL#define REAL double#endif

• This basically says “If REAL is not defined, go ahead and define it.”

Page 81: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

C Preprocessor (4)

• Can also check values using the #if directive• In the current exercise code, the function fabsf is

used, but that is for floats. For doubles, the function is fabs. We can add this to out .h file:

#if REAL == double#define ABS fabs#else#define ABS fabsf#endif

Page 82: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

C Preprocessor (5)

• #define can also be used to define a macro with substitutable arguments#define ind(m,n) (n + NY*m)k = 5*ind(i,j); k = 5*(i + NY*j);

• Be careful to use () when required!– without () above example would come out wrong k = 5*i + NY*j wrong!

Page 83: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Exercise 10

• Modify dot-product code to use preprocessor directives to declare double-precision floats– Add directives to header file to define REAL as shown in “C

Preprocessor (3)”– Add directives to header file to choose ABS as shown in “C

Preprocessor (4)”– Change “fabsf” to “ABS” in main routine– include math.h in main program– Include header file in dotprod.cc– Don’t forget to modify all occurrences of “float” to “REAL” in

dot.cc and dotprod.cc• solution

Page 84: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Structures

• Can package a number of variables under one namestruct grid{ int nvals; float x[100][100], y[100][100], jacobian[100][100];};

• Note semicolon at end of definition

Page 85: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Structures (cont’d)

• To declare a variable struct grid mygrid1;

• Components are accessed using .mygrid1.nvals = 20;mygrid1.x[0][0] = 0.0;

• Handy way to transfer lots of data to a functionint calc_jacobian(struct grid mygrid1){…

Page 86: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Exercise 11

• Define struct rvec with 2 components in your header file (.h)– vector length (int)– pointer to REAL vector

• Modify dot-product code to use rvec structure• solution

Page 87: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

i/o

• Often need to read/write data from/to files rather than screen

• File is associated with a file pointer through a call to the fopen function

• File pointer is of type FILE, which is defined in <stdio.h>

Page 88: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

i/o (cont’d)

• fopen takes 2 character-string arguments– file name– mode• “r” read• “w” write• “a” append

FILE *fp;fp = fopen(“myfile.d”, “w”);

Page 89: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

i/o (3)

• Write to file using fprintf– Need stdio.h

• fprintf has 3 arguments1. File pointer2. Character string containing what to print and any

formats• %f for float or double• %d for int• %s for character string

3. Variable list corresponding to formats

Page 90: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

i/o (4)

• Special character \n produces new line (carriage return & line feed)– Often used in character strings“This is my character string.\n”

• Example:fprintf(fp, “x = %f\n”, x);

• Read from file using fscanf– arguments same as fprintf

• When finished accessing file, close itfclose(fp);

Page 91: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Exercise 12

• Modify dot-product code to write the dot-product result to a file

• If magnitude is small, still write message to screen rather than file

• After result is written to file, write message “Output written to file” to screen.

• solution

Page 92: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Binary i/o

• Binary data require much less disk space than ascii (formatted) data

• Use “b” suffix on modefp = fopen(“myfile.d”, “wb”);

• Use fwrite, fread functionsfloat x[100];fwrite( x, sizeof(float), 100, fp )

– Note that there is no format specification• We’re strictly writing data

pointer to 1st element

no. bytes in each element

max. no. of elements

file pointer

Page 93: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Exercise 13

• Modify dot-product program to:– Write result to binary file• just write value, not character string

– After file is closed, open it back up and read result to make sure that it wrote/read correctly

• solution

Page 94: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Command-Line Arguments

• It’s often convenient to type some inputs on the command line along with the executable name, e.g.,mycode 41.3 “myfile.d”

• Define main with two arguments:1. argc is the number of items on the command line2. argv is an array of character strings containing

the arguments int main(int argc, char *argv[])

Page 95: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Command-Line Arguments (cont’d)

• Since arguments are character strings, often would like to convert them to numbers

• Some handy functions:– atoi converts string to integer– atof converts string to double• To convert to float, recast result of atof

– They live in stdlib.h

Page 96: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

Exercise 14

• Convert dot-product code to take arguments on command line rather than prompt for them

• Arguments will be:– Size of vectors– Values of first vector– Values of second vector

• Use atoi and atof– Recast doubles to “REAL”

• solution

Page 97: Introduction to C (with a Bit of C++ Syntax) Doug Sondak SCV sondak@bu.edu

References

• Lots of books available– I have Kernighan & Ritchie, “The C Programming

Language”• Good C++ book for scientists:– Barton and Nackman, “Scientific and Engineering C++”

• Quick and dirty C++ book:– Liberty, “Teach Yourself C++ in 21 Days”

• gcc/g++http://gcc.gnu.org/onlinedocs/gcc-4.5.1/gcc/