Introduction to C

Preview:

DESCRIPTION

Introduction to C. Doug Sondak SCV sondak@bu.edu. Outline. Goals Introduction Emacs C History Basic syntax makefiles Additional syntax . Goals. To be able to write simple C programs To be able to understand and modify existing C code - PowerPoint PPT Presentation

Citation preview

Introduction to C

Doug SondakSCV

sondak@bu.edu

Outline

• Goals• Introduction• Emacs• C History• Basic syntax• makefiles• Additional syntax

Goals

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

C code• To be able to manage program projects using

an editor and makefile

Introduction

• Matlab is great! Why do I need to learn a new language?!

• All codes must be translated to machine language • Interpreted language– Matlab, Python, Java– Translation is performed incrementally at run time

• Compiled language– Fortran, C, C++– Translation is performed once, then executable is run

Introduction (cont’d)

• Compiled languages run faster– I translated a program from Matlab to C for a user,

and it ran 7 times as fast– Large-scale computing is usually done with

compiled language• Some convenient features of interpreted

languages result in performance and/or memory penalties

Emacs

• On Unix systems, one typically uses an editor to write/modify code

• Emacs and vi are most popular– We will talk about emacs– If you want to use vi, that’s fine

Emacs (cont’d)

• Emacs is a wonder tool!• We will just scratch the surface• Commands can be executed via menus or

using the underlying character sequences– Although I like the latter, we’ll use the menus here

• Copy the file “junk” from /scratch/sondak– This will be our example file for editing

cp /scratch/sondak/junk .

Emacs Exercise• Type “emacs”– An emacs window will appear

• To read an existing file File -> Open File…– You will be prompted for file name at bottom of

window– Type “junk” and hit return

Emacs Exercise (cont’d)

• You should now see file contents in window• can navigate using arrow keys• You can also navigate by clicking on the

desired location– Click on the 0 in 0.282

Emacs Exercise (3)

• To delete a character use Delete button– Hit Delete 3 times to delete 0.2– Type 1.3– You’ve now changed 0.282 to 1.382

• Highlight 0.288 with the mouse– Don’t include spaces before or after– Edit -> Cut will delete the highlighted characters– Oh no, we made a mistake! Edit -> Undo undoes the

previous command. Try it; 0.288 should reappear.

Emacs Exercise (4)

• The point is the location on the left side of the cursor; i.e., between the character on which the cursor resides and the character to its left.

• The mark is similar to the point, but it’s location is set (i.e., doesn’t move with cursor).

• Suppose we want to delete all the characters between (inclusively) 0.288 and 0.407.

Emacs Exercise (5)

• Set the cursor on the 0 in 0.288.• To set the mark, CTL-spacebar– The note “Mark set” will appear at the bottom of

the screen.• Move the cursor to the right of 0.407– We place it to the right of the 7 rather than on it

because the point is always to the left of the cursor

Emacs Exercise (6)

• Edit -> Cut will delete all characters between the mark and the point

• For now, let’s put the characters back in by using Edit -> Undo

• Move the cursor to the start of the current line using CTL-a – (CTL-e moves to end of current line)

• Delete (“kill”) the line with CTL-k• Another CTL-k deletes the newline character

Emacs Exercise (7)

• “Meta” key is the Esc key• We will use M here to indicate the Meta key• The Meta key is hit prior to the subsequent

key(s), not simultaneously as with CTL• Place the cursor at the top of the file• M-> will move the cursor to the bottom of the

file• M-< will move it back to the top

Emacs Exercise (8)

• Whatever we deleted last is available in a buffer

• Move the cursor to the beginning of the “M1rel” line

• CTL-y “yanks” the current buffer

Emacs Exercise (9)• To save the modified file, File -> Save (current buffer)

– A note will appear at the bottom of the window saying the file has been saved

• To save it under a new name, File -> Save Buffer As…– You’ll be prompted for the name at the bottom of the screen– Note that when you get these kinds of prompts, you can edit

them using emacs commands• Type a file name and then move back and forth with CTL-b and CTL-f

• File -> Exit Emacs to quit• Previous version will appear with ~ suffix

Emacs Exercise (10)

• A built-in tutorial is available– Type CTL-h t– We won’t do anything with the tutorial here, but

it’s a good resource

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

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 are enclosed by /* */– Many compilers also accept // at beginning of

comment as in 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 is usually .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.)• 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}

C Syntax (4)

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

• Either way is fine– Be consistent!

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– Usually have a .h suffix#include <header_file_name.h>– Included before function definition– Note that the #include statement does not end

with a ;

C Syntax (6)

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

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

• Single character is enclosed in single quotes‘h’

C Syntax (7)

• Functions are executed by statements that consist of the function name, any required arguments within (), and the ending ;myfunc(arg1, arg2);

• The function printf can take a character string as an argument and print the string to the screen printf(“mystring\n”);– Definition of printf is in the include file stdio.h

Exercise 1

• Write a “hello world” program in an editor• Program should print a character string• General structure of code:– include stdio.h– define main function– call printf function

• Save it to a file name with a .c suffix (e.g., hello.c)• solution

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

• On katana we have Portland Group compilers (pgcc) and GNU compilers (gcc)

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

Compilation (cont’d)

• Compilers have huge numbers of options– See gcc compiler documentation athttp://gcc.gnu.org/onlinedocs/– Katana presently has version 4.1.1

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

• In a Unix window:gcc –o hello hello.c

Compilation (3)

• 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

• Once it compiles correctly, type the executable name at the Unix prompt, and it will print your string

Declarations• Lists of variables with their associated types• Placed in source code at beginning of function• Basic types:

– int• Usually 4 bytes

– float• Usually 4 bytes

– double• Usually 8 bytes

– char• Single character• One byte

Declarations (cont’d)

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

• A “char” variable represents a single character

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

Arithmetic (cont’d)

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

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

Printing Values• printf will print values as well as character strings• Must provide format for each value

int num;float x;printf(“num = %d x = %f \n”, num, x);

• %d is integer conversion specification (format)• %f is float conversion specification• Formats correspond to variable list• I sometimes line them up for clarity:

printf(“num = %d x = %f \n”, num, x );

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

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

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

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;

Pointers (4)

float x, *p;p = &x;

1040104810521056

address08

1632

address

p 16

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

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]

• Array name is actually a pointer to the memory location of the first element

• These are equivalent:x[0] = 4.53;*x = 4.53;

Arrays (cont’d)

• 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;

Arrays (3)

• Multiple-dimension arrays are declared as follows:int a[10][20];

• Values are stored in memory with last index varying most rapidly– Opposite of Matlab and Fortran

• The statements in each box are equivalent:a[0][17] = 1; a[1][0] = 5;*(a+17) = 1; *(a+20) = 5;

Arrays (4)

• 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

sizeof

• Some C 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)

Input from Keyboard• fgets reads a character string (character array)

char line[100];fgets(line, sizeof(line), stdin);– 2nd argument is no. bytes to read– lives in stdio.h

• sscanf “decodes” a character string according to specified formatint i, j;sscanf(line, “%d %d”, &i, &j);– 2nd argument is character string, so it’s in quotes– last arguments are addresses– lives in stdio.h

Exercise 3

• Modify Celcius program to read value from keyboard– Declare character array and floats– Read character array from keyboard– Decode character array into float– Calculate temperature– Print temperature

• solution

Dynamic Allocation

• Suppose you need to allocate 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

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);

For Loop

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

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

Exercise 4

• Write program to:– prompt for length of vector (n)– malloc 2 floating-point vectors of length n– prompt for vector values– calculate dot product– print the result

• solution

i

n

ii bac

1

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

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”);}

if/else if/else (3)

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

Exercise 5

• In dot product code, check if the magnitude of the dot product is less than using the absolute value function abs. If it is, print a message.– the abs function lives in math.h– when you compile, don’t forget –lm

• solution

610

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;}

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

Functions (3)

b = 2.0; c = 3.0;a = sumsqr(b, c);printf(“b = %f\n”, 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

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;}

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

Functions (6)

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

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

16202428

variable

ba

32

address

48525660

variable

ji

2420&a i

&b j

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

Functions (7)

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

k

address

16202428

variable

ba

32

address

48525660

variable

ji

2420

int k;

address

16202428

variable

ba

32

address

48525660

variable

ji

2420

k = *i;k2

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;

Functions (9)

return 0 ;

address

16202428

variable

ba

23

address

48525660

variable

2420

2

Exercise 6

• 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• solution

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

Function Prototypes (cont’d)

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

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

Function Prototypes (3)

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

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 latergcc –c mycode.cgcc –c myfunc.cgcc –o mycode mycode.o myfunc.o

Exercise 7

• 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

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

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

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

Makefiles (4)

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

$(NAME)${NAME}

• Example:– Define compiler

CC = gcc– To use elsewhere in makefile:

$(CC)

Makefiles (5)

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

Makefiles (6)

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

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

Makefiles (7)

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

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

.o file from a .c 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

Makefiles (8)

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

func2.o

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

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

$(CC) $(COMPFLAGS) $*.c

### compilerCC = gccCOMMONFLAGS = -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

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 .c files• It checks the time stamps on the associated .o and .c files to

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

– In our case, compiles the routine

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

Exercise 8

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

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

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;

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.”

C Preprocessor (4)

• #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!

Exercise 9• Modify dot-product code to use preprocessor directives to

declare double-precision floats– Add directives to header file to define REAL as shown previously– Format in sscanf will have to be “%f” if REAL is defined as float

and “%lf” (long float) if REAL is defined as double. Add directive to define REALFORMAT containing appropriate format (including required quotes).• modify calls to sscanf and printf to use REALFORMAT• note that printf can contain multiple strings printf(“string1” “string2” “string3\n”);

– Don’t forget to modify all float declarations• solution

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

Structures (cont’d)

• To declare a variable struct grid grid1;

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

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

Exercise 10

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

• Modify dot-product code to use structure• solution

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>

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”);

i/o (3)

• Write to file using fprintf– similar to printf with addition of file pointer

argumentfprintf(fp, “x = %f\n”, x);

• Read from file using fscanf– arguments same as fprintf

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

Exercise 11

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

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

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

• solution

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

pointer to 1st element

no. bytes in each element

max. no. of elements

file pointer

Exercise 12

• 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

References

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

Language”• There are many references on the web such as

http://www.cs.cf.ac.uk/Dave/C/CE.html• gcc

http://gcc.gnu.org/onlinedocs/gcc-4.5.1/gcc/

Survey

• Please fill out the course survey athttp://scv.bu.edu/survey/fall10tut_survey.html

Recommended