Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to C Doug Sondak SCV Outline Goals Introduction Emacs C History Basic syntax makefiles Additional syntax.

Similar presentations


Presentation on theme: "Introduction to C Doug Sondak SCV Outline Goals Introduction Emacs C History Basic syntax makefiles Additional syntax."— Presentation transcript:

1 Introduction to C Doug Sondak SCV sondak@bu.edu

2 Outline Goals Introduction Emacs C History Basic syntax makefiles Additional syntax

3 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

4 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

5 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

6 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

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

8 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

9 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

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

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

12 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

13 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

14 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

15 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

16 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

17 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

18 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

19 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++

20 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

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

22 C Syntax (4) Style note: some people like to arrange the brackets like int main() { function statements } Either way is fine – Be consistent!

23 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 – Included before function definition – Note that the #include statement does not end with a ;

24 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’

25 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

26 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

27 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

28 Compilation (cont’d) Compilers have huge numbers of options – See gcc compiler documentation at http://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

29 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

30 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

31 Declarations (cont’d) Examples: int i, j, k; float xval, time; char name, date; A “char” variable represents a single character

32 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

33 Arithmetic (cont’d) ++ and -- operators – these are equivalent: i = i+1; i++; – always increment/decrement by 1 Can convert types with cast operator float xval; int i; xval = (float) i;

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

35 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

36 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 0 8 16 32

37 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; 0 8 16 32 3.2 address

38 Pointers (3) A pointer is a variable containing a memory address Declared using * prefix float *p; Address operator & – Address of specified variable float x, *p; p = &x;

39 Pointers (4) float x, *p; p = &x; 1040 1048 1052 1056 address 0 8 16 32 address p 16

40 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 error float *p; *p = 3.2; float x, *p; p = &x; *p = 3.2; Wrong! – p doesn’t have value yet correct

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

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

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

44 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

45 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 arg float x, y[5]; sizeof(x)( 4) sizeof(y)(20) sizeof(float)( 4)

46 Input from Keyboard fgets reads a character string (character array) char line[100]; fgets(line, sizeof(line), stdin); – 2 nd argument is no. bytes to read – lives in stdio.h sscanf “decodes” a character string according to specified format int i, j; sscanf(line, “%d %d”, &i, &j); – 2 nd argument is character string, so it’s in quotes – last arguments are addresses – lives in stdio.h

47 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

48 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 function malloc(n) – n is no. bytes to be allocated – Returns pointer to allocated space – lives in stdlib.h

49 Dynamic Allocation (cont’d) Declare pointer of required type float *myarray; Suppose we need 101 elements in array malloc requires no. bytes, cast as appropriate pointer myarray = (float *) malloc(101*sizeof(float)); free releases space when it’s no longer needed: free(myarray);

50 For Loop for loop repeats calculation over range of indices for(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

51 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

52 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

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

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

55 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

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

57 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

58 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

59 Functions (4) If you want to change argument values, pass pointers int swap(int *i, int *j){ int k; k = *i; *i = *j; *j = k; return 0; }

60 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 16 20 24 28 variable b a 3 2

61 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 16 20 24 28 variable b a 3 2 address 48 52 56 60 variable j i 24 20 &a i &b j swap(&a, &b); int swap(int *i, int *j){... }

62 Functions (7) What happens to memory for each line in the function? k address 16 20 24 28 variable b a 3 2 address 48 52 56 60 variable j i 24 20 int k; address 16 20 24 28 variable b a 3 2 address 48 52 56 60 variable j i 24 20 k = *i; k 2

63 Functions (8) *i = *j; address 16 20 24 28 variable b a 3 3 address 48 52 56 60 variable j i 24 20 k 2 address 16 20 24 28 variable b a 2 3 address 48 52 56 60 variable j i 24 20 k 2 *j = k;

64 Functions (9) return 0 ; address 16 20 24 28 variable b a 2 3 address 48 52 56 60 variable 24 20 2

65 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

66 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

67 Function Prototypes (cont’d) Prototype looks like 1 st line of function definition – type – name – argument types float dotprod(int n, float *x, float *y); Argument names are optional: float dotprod(int, float*, float*);

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

69 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 later gcc –c mycode.c gcc –c myfunc.c gcc –o mycode mycode.o myfunc.o

70 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

71 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

72 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

73 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

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

75 Makefiles (5) Good practice to define compiler info in variables CC = gcc COMMONFLAGS = -O3 COMPFLAGS = -c $(COMMONFLAGS) LINKFLAGS = $(COMMONFLAGS)

76 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

77 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

78 Makefiles (8) Usually define variable with all object file names OBJ = mymain.o func1.o anotherfunc.o \ func2.o

79 Makefiles (9) Finally, everything falls together with the definition of a rule target: 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

80 Makefiles (10) ### suffix rule.SUFFIXES:.SUFFIXES:.c.o.c.o: $(CC) $(COMPFLAGS) $*.c ### compiler CC = gcc COMMONFLAGS = -O3 COMPFLAGS = -c $(COMMONFLAGS) LINKFLAGS = $(COMMONFLAGS) ### objects OBJ = mymain.o fun1.o fun2.o fun3.o ### compile and link myexe: $(OBJ) $(CC) –o $@ $(LINKFLAGS) $(OBJ) Automatic variable for target

81 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

82 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 files clean: rm –f *.o When there are multiple targets, specify desired target as argument to make command make clean

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

84 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

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

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

87 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!

88 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

89 Structures Can package a number of variables under one name struct grid{ int nvals; float x[100][100], y[100][100], jacobian[100][100]; }; Note semicolon at end of definition

90 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 function int calc_jacobian(struct grid grid1){…

91 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

92 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

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

94 i/o (3) Write to file using fprintf – similar to printf with addition of file pointer argument fprintf(fp, “x = %f\n”, x); Read from file using fscanf – arguments same as fprintf When finished accessing file, close it fclose(fp);

95 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

96 Binary i/o Binary data require much less disk space than ascii (formatted) data Use “b” suffix on mode fp = fopen(“myfile.d”, “wb”); Use fwrite, fread functions float x[100]; fwrite( x, sizeof(float), 100, fp ) – Note that there is no format specification pointer to 1 st element no. bytes in each element max. no. of elements file pointer

97 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

98 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/

99 Survey Please fill out the course survey at http://scv.bu.edu/survey/fall10tut_survey.html


Download ppt "Introduction to C Doug Sondak SCV Outline Goals Introduction Emacs C History Basic syntax makefiles Additional syntax."

Similar presentations


Ads by Google