Scientific Programming in C X. More features & Fortran interface
|
|
- Meryl Simpson
- 5 years ago
- Views:
Transcription
1 Scientific Programming in C X. More features & Fortran interface Susi Lehtola 20 November 2012
2 typedef typedefs are a way to make shorthand for data types, and possibly also make the code more general (e.g., make a single-precision and double-precision version from the same code). / D e c l a r e r e a l to be the same as double / typedef double r e a l ; r e a l x, y, z ; You can t use modifiers with typedef d data types, e.g. long real would give a compiler error. Scientific Programming in C, fall 2012 Susi Lehtola More features 2/58
3 typedef typedefs are most often used for structs. Instead of s t r u c t v e c t o r { double x, y, z ; } ; s t r u c t v e c t o r r ; one can write typedef s t r u c t { double x, y, z ; } v e c t o r ; v e c t o r r ; Scientific Programming in C, fall 2012 Susi Lehtola More features 3/58
4 switch Now that we have a working knowledge of C, we can discuss some more features of C. The switch statement is an alternative to using many if statements, when an expression is only evaluated once. The wanted possibilities are identified in the switch statement, and the execution proceeds to the relevant branch if one exists. The default branch (if it exists) is executed if no other branch is matched. The execution continues until the end of the switch statement or until a break statement is encountered. Scientific Programming in C, fall 2012 Susi Lehtola More features 4/58
5 switch example #i n c l u d e <s t d i o. h> i n t main ( v o i d ) { / Amount o f c h a r a c t e r s, t a b s and l i n e s / s i z e t nc =0, n l =0; i n t c ; } w h i l e ( ( c=g e t c h a r ( ) )! =EOF) s w i t c h ( c ) { / I f c i s a n e w l i n e c h a r a c t e r / case ( \n ) : / I n c r e m e n t number o f l i n e s / n l ++; / Any o t h e r c h a r a c t e r than t h e one ( s ) s p e c i f i e d above / d e f a u l t : nc++; break ; } p r i n t f ( %u c h a r a c t e r s on %u l i n e s. \ n, nc, n l ) ; r e t u r n 0 ; Scientific Programming in C, fall 2012 Susi Lehtola More features 5/58
6 switch example, cont d In the preceding example, if c is a newline character, the number of lines is incremented. However, because there is no break statement, the program continues to the default case, and the number of characters is also incremented for the newline character. Scientific Programming in C, fall 2012 Susi Lehtola More features 6/58
7 2nd switch example i n t i s w h i t e ( i n t c ) { / Check f o r w h i t e s p a c e c h a r a c t e r / s w i t c h ( c ) { } / c i s w h i t e s p a c e / case ( ) : case ( \ f ) : case ( \n ) : case ( \ t ) : case ( \ v ) : r e t u r n 1 ; / Normally, you s h o u l d have t h i s h e r e. However, i n t h i s c a s e i t i s v o l u n t a r y s i n c e t h e r e t u r n s t a t e m e n t ends t h e f u n c t i o n. / break ; / c i s not w h i t e s p a c e / d e f a u l t : r e t u r n 0 ; } Scientific Programming in C, fall 2012 Susi Lehtola More features 7/58
8 2nd switch example, cont d The iswhite function returns 1 for white space characters and 0 for non-white space characters, analogously to the isspace() function. Scientific Programming in C, fall 2012 Susi Lehtola More features 8/58
9 Duff s device What does the following code fragment do? (Taken from learn C the hard way) v o i d d u f f s d e v i c e ( char from, char to, s i z e t count ) { s i z e t n = ( count + 7) / 8 ; } s w i t c h ( count % 8) { case 0 : do { to++ = from++; case 7 : to++ = from++; case 6 : to++ = from++; case 5 : to++ = from++; case 4 : to++ = from++; case 3 : to++ = from++; case 2 : to++ = from++; case 1 : to++ = from++; } w h i l e( n > 0 ) ; } Scientific Programming in C, fall 2012 Susi Lehtola More features 9/58
10 Duff s device What does the following code fragment do? (Taken from learn C the hard way) v o i d d u f f s d e v i c e ( char from, char to, s i z e t count ) { s i z e t n = ( count + 7) / 8 ; } s w i t c h ( count % 8) { case 0 : do { to++ = from++; case 7 : to++ = from++; case 6 : to++ = from++; case 5 : to++ = from++; case 4 : to++ = from++; case 3 : to++ = from++; case 2 : to++ = from++; case 1 : to++ = from++; } w h i l e( n > 0 ) ; } Don t write this at home. Scientific Programming in C, fall 2012 Susi Lehtola More features 10/58
11 Duff s device, cont d copies data from char *from to char *to copying is done byte by byte, up to 8 bytes at a time n is the number of iterations necessary in the nasty construct the execution hops within the while loop if count is not divisible by 8, only the remainder is copied in the first iteration otherwise the copy does 8 bytes Scientific Programming in C, fall 2012 Susi Lehtola More features 11/58
12 Duff s device, cont d copies data from char *from to char *to copying is done byte by byte, up to 8 bytes at a time n is the number of iterations necessary in the nasty construct the execution hops within the while loop if count is not divisible by 8, only the remainder is copied in the first iteration otherwise the copy does 8 bytes Don t write code like this. Scientific Programming in C, fall 2012 Susi Lehtola More features 12/58
13 enum enums are a way to declare constant integer values. By default the numbering starts from 0. / NO i s 0, YES i s 1 / enum t r u t h {NO, YES } ; You can also change the starting point enum weekday {MON=1, TUE, WED, THU, FRI, SAT, SUN} ; enum month {JAN=1, FEB, MAR, APR, MAY, JUN, \ JUL, AUG, SEP, OCT, NOV, DEC} ; or specify all of the values manually enum e s c a p e s { BELL= \a, BACKSPACE= \b, TAB= \ t, NEWLINE= \n, VTAB= \ v, RETURN= \ r } ; Scientific Programming in C, fall 2012 Susi Lehtola More features 13/58
14 Using enums The typical case where you want to use enums is when your program has many different runmodes. Using enums instead of a manually constructed list using #defines makes programming less error-prone. Scientific Programming in C, fall 2012 Susi Lehtola More features 14/58
15 enum example The following example is from the Casida TDDFT routines of ERKALE: / What k i n d o f c o u p l i n g i s used / enum c o u p l i n g m o d e { / I n d e p e n d e n t P a r t i c l e A p p r o x i m a t i o n / IPA, / Random Phase A p p r o x i m a t i o n / RPA, / Time Dependent L o c a l D e n s i t y A p p r o x i m a t i o n / TDLDA } ; Calculations can be made within different levels of theory, IPA being the simplest, RPA being much more sophisticated and TDLDA furthermore adding some minor corrections. Scientific Programming in C, fall 2012 Susi Lehtola More features 15/58
16 enum example, cont d In the code enum c o u p l i n g m o d e c o u p l i n g ; When the input is parsed, coupling is then set to IPA, RPA or TDLDA, depending on the input arguments. In the code comparisons can be then made using the symbolic values, e.g. s w i t c h ( c o u p l i n g ) { case (TDLDA ) : / Code c a l c u l a t i n g l o c a l d e n s i t y c o r r e c t i o n s / case (RPA ) : / Code c a l c u l a t i n g e f f e c t o f Coulomb i n t e r a c t i o n / case ( IPA ) : / No c o r r e c t i o n s / } Here TDLDA RPA IPA. Scientific Programming in C, fall 2012 Susi Lehtola More features 16/58
17 Another alternative to multidimensional arrays As it was mentioned in the pointer lecture, there are many ways to handle multidimensional arrays in C. The recommended way is to use dynamic memory allocation with manual indexing, as then one can be sure that the memory is allocated continguously and no memory trashing occurs. Scientific Programming in C, fall 2012 Susi Lehtola More features 17/58
18 Another alternative to multidimensional arrays As it was mentioned in the pointer lecture, there are many ways to handle multidimensional arrays in C. The recommended way is to use dynamic memory allocation with manual indexing, as then one can be sure that the memory is allocated continguously and no memory trashing occurs. However, there is also another alternative. Remember that arrays are pointers to the start of the array. Scientific Programming in C, fall 2012 Susi Lehtola More features 18/58
19 Another alternative to multidimensional arrays, cont d #i n c l u d e < s t d l i b. h> i n t main ( v o i d ) { / S i z e o f t h e a r r a y / s i z e t M=20, N=10; s i z e t i ; / P o i n t e r to t h e a r r a y o f p o i n t e r s / double p=null ; / A l l o c a t e memory f o r t h e p o i n t e r a r r a y / p=m a l l o c (M s i z e o f ( double ) ) ; / A l l o c a t e memory f o r t h e e l e m e n t s o f t h e a r r a y / p [0]= m a l l o c (M N s i z e o f ( double ) ) ; / I n i t i a l i z e t h e r e s t o f t h e a r r a y / f o r ( i =1; i <M; i ++) p [ i ]=p [0]+ i M s i z e o f ( double ) ; / Now e l e m e n t s o f t h e a r r a y can be a c c e s s e d w i t h p [ i ] [ j ] as w i t h t h e normal p o i n t e r a r r a y / } / F r e e t h e memory / f r e e ( p [ 0 ] ) ; f r e e ( p ) ; r e t u r n 0 ; Scientific Programming in C, fall 2012 Susi Lehtola More features 19/58
20 Another alternative to multidimensional arrays, cont d This is completely analogous to using static multidimensional arrays double t[2][3]; t t[0][0] t[0][1] t[0][2] t[1][0] t[1][1] t[1][2] t[0] t[1] the difference being that now the arrays are allocated dynamically. In contrast to the manual indexing scheme where you can decide what kind of ordering to use, here you always end up using the normal C ordering (last index runs fastest). Scientific Programming in C, fall 2012 Susi Lehtola More features 20/58
21 Pointers to functions Also functions can be handled as pointers in C. Note the difference in syntax / A f u n c t i o n t h a t r e t u r n s a p o i n t e r to i n t / i n t f ( ) ; / A p o i n t e r to a f u n c t i o n t h a t r e t u r n s i n t / i n t ( p f ) ( ) ; Scientific Programming in C, fall 2012 Susi Lehtola More features 21/58
22 Pointers to functions, cont d You can use function pointers to write generalized code, e.g. double e v a l ( double ( f ) ( double ), double x ) { / E v a l u a t e f u n c t i o n at x / return f ( x ) ; } which you can call with e v a l ( s i n, 1. 0 ) ; Scientific Programming in C, fall 2012 Susi Lehtola More features 22/58
23 Pointers to functions, cont d You can also use arrays of functions: #i n c l u d e <math. h> #i n c l u d e <s t d i o. h> #i n c l u d e < s t d l i b. h> i n t main ( v o i d ) { / P o i n t e r to p o i n t e r to f u n c t i o n f ( x ) / double ( f ) ( double x ) ; / H e l p e r / i n t i ; / A l l o c a t e memory f o r a r r a y / f=m a l l o c (3 s i z e o f ( double ( ) ( double ) ) ) ; / and s e t e l e m e n t s / f [0]= s i n ; / f ( x ) = s i n ( x ) / f [1]= l o g ; / f ( x ) = l o g ( x ) / f [2]= s q r t ; / f ( x ) = s q r t ( x ) / / P r i n t v a l u e s a t x =1.0 / f o r ( i =0; i <3; i ++) p r i n t f ( ( f )[% i ](1.0)=% e \n, i, f [ i ] ( 1. 0 ) ) ; f r e e ( f ) ; r e t u r n 0 ; } Scientific Programming in C, fall 2012 Susi Lehtola More features 23/58
24 Pointers to functions, cont d Running the code results in $. / a. out ( f ) [ 0 ] ( 1. 0 ) = e 01 ( f ) [ 1 ] ( 1. 0 ) = e+00 ( f ) [ 2 ] ( 1. 0 ) = e+00 which you can compare to the accurate values sin 1 = log 1 = 0 1 = 1 Scientific Programming in C, fall 2012 Susi Lehtola More features 24/58
25 Pointers to functions, cont d Running the code results in $. / a. out ( f ) [ 0 ] ( 1. 0 ) = e 01 ( f ) [ 1 ] ( 1. 0 ) = e+00 ( f ) [ 2 ] ( 1. 0 ) = e+00 which you can compare to the accurate values sin 1 = log 1 = 0 1 = 1 Remember that you could use qsort() to sort even this kind of an array, if you just come up with a comparison operator (e.g., wrt the value at x = 1) Scientific Programming in C, fall 2012 Susi Lehtola More features 25/58
26 Interfacing C with Fortran As you recall, there are some fundamental differences between C and Fortran. the size of an array is known a priori in Fortran, in C it isn t Fortran arrays are stored in column order (first index runs fastest), C arrays are stored in row order Fortran passes arguments by reference, whereas C passes them by value Scientific Programming in C, fall 2012 Susi Lehtola More features 26/58
27 Interfacing C with Fortran As you recall, there are some fundamental differences between C and Fortran. the size of an array is known a priori in Fortran, in C it isn t Fortran arrays are stored in column order (first index runs fastest), C arrays are stored in row order Fortran passes arguments by reference, whereas C passes them by value When you use Fortran functions or subroutines from C, you need to use pointer arguments. Scientific Programming in C, fall 2012 Susi Lehtola More features 27/58
28 Interfacing C with Fortran, cont d Conventionally it has been the case that people have wanted to use Fortran routines from C, mainly LAPACK (linear algebra such as matrix diagonalization). Scientific Programming in C, fall 2012 Susi Lehtola More features 28/58
29 Interfacing C with Fortran, cont d Conventionally it has been the case that people have wanted to use Fortran routines from C, mainly LAPACK (linear algebra such as matrix diagonalization). Nowadays also many Fortran users want to interface to C, as there are many extremely useful libraries available that have been written in C (or C++), such as the GNU Scientific Library (GSL) or FFTW. Scientific Programming in C, fall 2012 Susi Lehtola More features 29/58
30 Interfacing C with Fortran, cont d Although compiler-dependent interfacing has been around for a long time, the C interface has been only recently standardized in Fortran I will use the Fortran data types introduced in the 2003 standard in order to avoid defining the sizes manually. This has no effect on code portability, as if you don t have Fortran 2003 features you can just write a dummy ISO C BINDING module which defines the sizes of the datatypes in Fortran 90 code. Scientific Programming in C, fall 2012 Susi Lehtola More features 30/58
31 Interfacing C with Fortran, cont d In order to prevent unwanted mixed-language programming, the Fortran function names are usually suffixed with an underscore. However, this is dependent on the compiler: it might not be added it might be added two might be added You can adjust these with compiler flags, e.g. -fno-underscoring and -fsecond-underscore in gfortran. Scientific Programming in C, fall 2012 Susi Lehtola More features 31/58
32 Interfacing C with Fortran, cont d In order to prevent unwanted mixed-language programming, the Fortran function names are usually suffixed with an underscore. However, this is dependent on the compiler: it might not be added it might be added two might be added You can adjust these with compiler flags, e.g. -fno-underscoring and -fsecond-underscore in gfortran. Fortran 2003 added the BIND(C) attribute which eliminates the need to handle underscoring. Scientific Programming in C, fall 2012 Susi Lehtola More features 32/58
33 C to Fortran interface A simple example: using the Fortran intrinsic function nint(x), which returns the nearest integer to the floating point number x equivalent to ((int) round(x)) in C Fortran wrapper code wnint.f90 f u n c t i o n wnint ( x ) r e s u l t ( i ) use ISO C BINDING i m p l i c i t none r e a l (C DOUBLE), i n t e n t ( i n ) : : x i n t e g e r ( C INT ) : : i i=n i n t ( x ) end f u n c t i o n wnint Scientific Programming in C, fall 2012 Susi Lehtola More features 33/58
34 C to Fortran interface, cont d #i n c l u d e <s t d i o. h> #i n c l u d e < s t d l i b. h> / F o r t r a n f u n c t i o n / i n t w n i n t ( c o n s t double x ) ; i n t main ( i n t argc, char a r g v ) { double d ; i f ( a r g c!=2) { p r i n t f ( Usage : %s v a l u e \n, a r g v [ 0 ] ) ; r e t u r n 1 ; } d=a t o f ( a r g v [ 1 ] ) ; } p r i n t f ( %e rounded to t h e c l o s e s t i n t e g e r i s %i. \ n,\ d, w n i n t (&d ) ) ; r e t u r n 0 ; Note that you have to declare the prototypes for the Fortran functions you use in the C code. Scientific Programming in C, fall 2012 Susi Lehtola More features 34/58
35 C to Fortran interface using BIND(C) If you add bind(c) to the attributes of the Fortran function f u n c t i o n wnint ( x ) r e s u l t ( i ) b i n d ( c ) use ISO C BINDING i m p l i c i t none r e a l (C DOUBLE), i n t e n t ( i n ) : : x i n t e g e r ( C INT ) : : i i=n i n t ( x ) end f u n c t i o n wnint it will be available to C as / F o r t r a n f u n c t i o n / i n t wnint ( c o n s t double x ) ; and you would call it with p r i n t f ( %e rounded to t h e c l o s e s t i n t e g e r i s %i. \ n,\ d, wnint (&d ) ) ; Scientific Programming in C, fall 2012 Susi Lehtola More features 35/58
36 C to Fortran interface, compiling When you mix C with Fortran, you naturally have to use different compilers for the different parts of the program. Compile C part with gcc -c (and other flags) Compile Fortran part with gfortran -c (and other flags) Link module files together Scientific Programming in C, fall 2012 Susi Lehtola More features 36/58
37 C to Fortran interface, compile example Example: # Compile main program gcc g c n i n t. c # Compile F o r t r a n wrapper g f o r t r a n g c wnint. c # L i n k t h e s e t o g e t h e r gcc o n i n t. x n i n t. o wnint. o l g f o r t r a n Note that if you use gcc to perform the linking, you need to add -lgfortran so that the Fortran runtime libraries that are needed by wnint.o are linked in. You can also use gfortran to perform the linking, then you don t need the -lgfortran argument. $. / i n t. x e+00 rounded to the c l o s e s t i n t e g e r i s 2. $. / i n t. x e+00 rounded to the c l o s e s t i n t e g e r i s 1. Scientific Programming in C, fall 2012 Susi Lehtola More features 37/58
38 C to Fortran interface, strings Unlike C strings, Fortran strings are not terminated. Instead, their size is always known since Fortran includes array metadata. Fortran functions with string arguments are appended with the length of the string. Scientific Programming in C, fall 2012 Susi Lehtola More features 38/58
39 C to Fortran interface, strings cont d For example, you would call the Fortran subroutine s u b r o u t i n e f p r i n t s t r ( s t r ) use i s o c b i n d i n g i m p l i c i t none c h a r a c t e r ( k i n d=c c h a r, l e n = ), i n t e n t ( i n ) : : s t r w r i t e (, (A) ) F o r t r a n code w r i t e (, (AAA) ), s t r, end s u b r o u t i n e f p r i n t s t r in C code as v o i d f p r i n t s t r ( c o n s t char, i n t l e n ) ; N.B. len is here not a pointer. Scientific Programming in C, fall 2012 Susi Lehtola More features 39/58
40 C to Fortran interface, strings cont d The analogous C code is #i n c l u d e <s t d i o. h> v o i d c p r i n t s t r ( c o n s t char s t r ) { p r i n t f ( C code \n ) ; p r i n t f ( \ %s \ \n, s t r ) ; } and the calling main program in C is #i n c l u d e < s t r i n g. h> v o i d c p r i n t s t r ( c o n s t char ) ; v o i d f p r i n t s t r ( c o n s t char, i n t l e n ) ; i n t main ( v o i d ) { char s t r [80]= This i s a t e s t s t r i n g. ; } c p r i n t s t r ( s t r ) ; f p r i n t s t r ( s t r, s t r l e n ( s t r ) ) ; r e t u r n 0 ; Scientific Programming in C, fall 2012 Susi Lehtola More features 40/58
41 C to Fortran interface, strings cont d... and the main program in Fortran is program t e s t use ISO C BINDING i m p l i c i t none c h a r a c t e r ( k i n d=c c h a r, l e n =80) : : s t r! Set s t r, use e x p l i c i t t y p e s t r=c c h a r This i s a t e s t s t r i n g.! P r i n t u s i n g F o r t r a n r o u t i n e. Remember t h a t you need! to remove t h e padded s p a c e s i n F o r t r a n c a l l f p r i n t s t r ( t r i m ( s t r ) )! P r i n t u s i n g C r o u t i n e, t e r m i n a t e s t r i n g c a l l c p r i n t s t r ( t r i m ( s t r ) // C NULL CHAR) end program t e s t Scientific Programming in C, fall 2012 Susi Lehtola More features 41/58
42 C to Fortran interface, strings example Running the programs gives $. / c t o f. x C code This i s a t e s t s t r i n g. F o r t r a n code This i s a t e s t s t r i n g. $. / f t o c. x F o r t r a n code This i s a t e s t s t r i n g. C code This i s a t e s t s t r i n g. Scientific Programming in C, fall 2012 Susi Lehtola More features 42/58
43 C to Fortran interface, vectors Let s begin with using a Fortran routine from C. s u b r o u t i n e f p r i n t a r r ( n, a r r ) use ISO C BINDING i m p l i c i t none i n t e g e r ( C INT ), i n t e n t ( i n ) : : n r e a l (C DOUBLE), i n t e n t ( i n ), dimension ( n ) : : a r r i n t e g e r ( C INT ) : : i w r i t e (, (A) ) F o r t r a n a r r a y do i =1,n w r i t e (, ( AF8. 3 ), advance= no ), a r r ( i ) end do w r i t e (, ) end s u b r o u t i n e f p r i n t a r r The routine prints out the elements of the vector it is given. Note that the array size needs to be given explicitly, since Fortran arrays contain metadata not available in the C version. Scientific Programming in C, fall 2012 Susi Lehtola More features 43/58
44 C to Fortran interface, vectors cont d The corresponding C code is #i n c l u d e <s t d i o. h> v o i d c p r i n t a r r ( c o n s t i n t N, c o n s t double a r r ) { i n t i ; p r i n t f ( C a r r a y \n ) ; f o r ( i =0; i < N; i ++) p r i n t f ( %8.3 f, i, a r r [ i ] ) ; p r i n t f ( \n ) ; } Scientific Programming in C, fall 2012 Susi Lehtola More features 44/58
45 C to Fortran interface, vectors cont d A Fortran test program program t e s t use ISO C BINDING i m p l i c i t none r e a l (C DOUBLE), dimension ( : ), a l l o c a t a b l e : : a r r i n t e g e r ( C INT ), parameter : : N=9 i n t e g e r ( C INT ) : : i a l l o c a t e ( a r r (N) ) do i =1,N a r r ( i )= i 1 end do c a l l f p r i n t a r r ( n, a r r ) c a l l c p r i n t a r r ( n, a r r ) d e a l l o c a t e ( a r r ) end program t e s t Scientific Programming in C, fall 2012 Susi Lehtola More features 45/58
46 C to Fortran interface, vectors cont d.. and the corresponding C test program #i n c l u d e < s t d l i b. h> v o i d c p r i n t a r r ( c o n s t i n t N, c o n s t double a r r ) ; v o i d f p r i n t a r r ( c o n s t i n t N, c o n s t double a r r ) ; i n t main ( v o i d ) { c o n s t i n t N=9; double a r r ; i n t i ; a r r=m a l l o c (N s i z e o f ( double ) ) ; f o r ( i =0; i <N; i ++) a r r [ i ]= i ; } c p r i n t a r r (&N, a r r ) ; f p r i n t a r r (&N, a r r ) ; f r e e ( a r r ) ; r e t u r n 0 ; Scientific Programming in C, fall 2012 Susi Lehtola More features 46/58
47 C to Fortran interface, vectors cont d Running the programs gives the expected result $. / c t o f. x C a r r a y F o r t r a n a r r a y $. / f t o c. x F o r t r a n a r r a y C a r r a y so the Fortran routine has correct access to the array allocated and initialized in C, and vice versa. Scientific Programming in C, fall 2012 Susi Lehtola More features 47/58
48 C to Fortran interface, matrices The same can be applied to multidimensional arrays. Scientific Programming in C, fall 2012 Susi Lehtola More features 48/58
49 C to Fortran interface, matrices The same can be applied to multidimensional arrays. Some points to take into account: in the Fortran interface, you cannot use arrays of pointers. The memory region must be continguous! can use static arrays or multidimensional arrays with the manual indexing or the multidimensional arrays allocated as mentioned at the beginning of this lecture indices in Fortran run from 1 to N, whereas in C they run from 0 to N-1 Fortran storage order is the opposite of C Scientific Programming in C, fall 2012 Susi Lehtola More features 49/58
50 C to Fortran interface, matrices cont d s u b r o u t i n e f p r i n t a r r (m, n, a r r ) use ISO C BINDING i m p l i c i t none i n t e g e r ( C INT ), i n t e n t ( i n ) : : m, n r e a l (C DOUBLE), i n t e n t ( i n ), dimension (m, n ) : : a r r i n t e g e r ( C INT ) : : i, j w r i t e (, (A) ) F o r t r a n a r r a y do i =1,m do j =1,n w r i t e (, ( AF8. 3 ), advance= no ), a r r ( i, j ) end do w r i t e (, ) end do end s u b r o u t i n e f p r i n t a r r Scientific Programming in C, fall 2012 Susi Lehtola More features 50/58
51 C to Fortran interface, matrices cont d #i n c l u d e <s t d i o. h> v o i d c p r i n t a r r ( c o n s t i n t M, c o n s t i n t N, \ c o n s t double a r r [ ] [ N ] ) { / Need to d e f i n e t h e l e n g t h o f t h e l a s t d i m e n s i o n ( s )! } Other o p t i o n would be to use a p o i n t e r and manual i n d e x i n g. / i n t i, j ; p r i n t f ( C a r r a y \n ) ; f o r ( i =0; i < M; i ++) { f o r ( j =0; j < N; j ++) p r i n t f ( %8.3 f, i, a r r [ i ] [ j ] ) ; p r i n t f ( \n ) ; } Scientific Programming in C, fall 2012 Susi Lehtola More features 51/58
52 C to Fortran interface, matrices cont d program t e s t use ISO C BINDING i m p l i c i t none r e a l (C DOUBLE), dimension ( :, : ), a l l o c a t a b l e : : a r r i n t e g e r ( C INT ), parameter : : M=3, N=4 i n t e g e r ( C INT ) : : i, j a l l o c a t e ( a r r (M,N) ) do i =1,M do j =1,N a r r ( i, j )=( i 1) N+j 1 end do end do c a l l f p r i n t a r r (m, n, a r r ) c a l l c p r i n t a r r (m, n, a r r ) d e a l l o c a t e ( a r r ) end program t e s t Scientific Programming in C, fall 2012 Susi Lehtola More features 52/58
53 C to Fortran interface, matrices cont d #i n c l u d e <s t d l i b. h> v o i d c p r i n t a r r ( c o n s t i n t M, c o n s t i n t N, c o n s t double a r r ) ; v o i d f p r i n t a r r ( c o n s t i n t M, c o n s t i n t N, double a r r ) ; i n t main ( v o i d ) { const i n t M=3, N=4; i n t i, j ; } double a r r ; a r r=m a l l o c (M s i z e o f ( double ) ) ; a r r [0]= m a l l o c (M N s i z e o f ( double ) ) ; f o r ( i =1; i<m; i ++) a r r [ i ]= a r r [0]+ i N; f o r ( i =0; i<m; i ++) f o r ( j =0; j<n; j ++) a r r [ i ] [ j ]= i N+j ; c p r i n t a r r (&M, &N, a r r [ 0 ] ) ; / Fortran function i s given the s t a r t of the array a r r i s h e r e t h e s t a r t o f t h e p o i n t e r a r r a y whereas a r r [ 0 ] i s t h e s t a r t o f t h e a c t u a l a r r a y, i f you use s t a t i c a l l o c a t i o n t h i s would be j u s t a r r / f p r i n t a r r (&M, &N, a r r [ 0 ] ) ; f r e e ( a r r [ 0 ] ) ; f r e e ( a r r ) ; r e t u r n 0 ; Scientific Programming in C, fall 2012 Susi Lehtola More features 53/58
54 C to Fortran interface, matrices cont d $. / c t o f. x C a r r a y F o r t r a n a r r a y $. / f t o c. x F o r t r a n a r r a y C a r r a y As you can see, the elements end up in the transpose ordering. If you use manual indexing in C, you can use the Fortran ordering in the C code as well. Scientific Programming in C, fall 2012 Susi Lehtola More features 54/58
55 C to Fortran interface, matrices cont d Fortran also has the intrinsic transpose function, which you can use to convert the indexing when you mix code. Scientific Programming in C, fall 2012 Susi Lehtola More features 55/58
56 C to Fortran interface, matrices cont d Fortran also has the intrinsic transpose function, which you can use to convert the indexing when you mix code. This will result in some computational overhead. If your matrices are very large, this is a thing you really want to avoid. Scientific Programming in C, fall 2012 Susi Lehtola More features 56/58
57 C to Fortran interface, matrices cont d Fortran also has the intrinsic transpose function, which you can use to convert the indexing when you mix code. This will result in some computational overhead. If your matrices are very large, this is a thing you really want to avoid.... but if the matrices are not very big, the overhead is probably acceptable wrt to the cleaner code. Scientific Programming in C, fall 2012 Susi Lehtola More features 57/58
58 Loop optimizations Remember that the memory ordering in C is row major, and column major in Fortran. When writing loops you should keep this in mind. double f ( s i z e t i, s i z e t j ) ; c o n s t s i z e t M=1000, N=200; double mat [M] [ N ] ; s i z e t i, j ; f o r ( j =0; j <N; j ++) f o r ( i =0; i <M; i ++) mat [ i ] [ j ]= f ( i, j ) ; Here the loops are in wrong order. Changing to the correct memory order is beneficial. Scientific Programming in C, fall 2012 Susi Lehtola More features 58/58
Types, Operators and Expressions
Types, Operators and Expressions CSE 2031 Fall 2011 9/11/2011 5:24 PM 1 Variable Names (2.1) Combinations of letters, numbers, and underscore character ( _ ) that do not start with a number; are not a
More informationScientific Programming in C IV. Pointers
Scientific Programming in C IV. Pointers Susi Lehtola 1 November 2012 Pointers The feature at the heart of C are pointers, which are simply pointers to memory addresses. Scientific Programming in C, fall
More informationScientific Programming in C VI. Common errors
Scientific Programming in C VI. Common errors Susi Lehtola 6 November 2012 Beginner errors If you re a beginning C programmer, you might often make off-by one errors when you use arrays: #i n c l u de
More informationTypes, Operators and Expressions
Types, Operators and Expressions EECS 2031 18 September 2017 1 Variable Names (2.1) l Combinations of letters, numbers, and underscore character ( _ ) that do not start with a number; are not a keyword.
More informationFeatures of C. Portable Procedural / Modular Structured Language Statically typed Middle level language
1 History C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC
More informationCS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation
CS113: Lecture 3 Topics: Variables Data types Arithmetic and Bitwise Operators Order of Evaluation 1 Variables Names of variables: Composed of letters, digits, and the underscore ( ) character. (NO spaces;
More informationArrays III and Enumerated Types
Lecture 15 Arrays III and Enumerated Types Multidimensional Arrays & enums CptS 121 Summer 2016 Armen Abnousi Multidimensional Arrays So far we have worked with arrays with one dimension. Single dimensional
More informationFile Handling in C. EECS 2031 Fall October 27, 2014
File Handling in C EECS 2031 Fall 2014 October 27, 2014 1 Reading from and writing to files in C l stdio.h contains several functions that allow us to read from and write to files l Their names typically
More informationEEE145 Computer Programming
EEE145 Computer Programming Content of Topic 2 Extracted from cpp.gantep.edu.tr Topic 2 Dr. Ahmet BİNGÜL Department of Engineering Physics University of Gaziantep Modifications by Dr. Andrew BEDDALL Department
More informationData Structures and Algorithms (DSA) Course 4. Iulian Năstac
Data Structures and Algorithms (DSA) Course 4 Iulian Năstac 10. Functions for dynamic memory allocation (recapitulation) Dynamic allocation is a specific characteristic allowed by some computing languages,
More informationScientific Programming in C V. Strings
Scientific Programming in C V. Strings Susi Lehtola 1 November 2012 C strings As mentioned before, strings are handled as character arrays in C. String constants are handled as constant arrays. const char
More informationHigh Performance Computing
High Performance Computing MPI and C-Language Seminars 2009 Photo Credit: NOAA (IBM Hardware) High Performance Computing - Seminar Plan Seminar Plan for Weeks 1-5 Week 1 - Introduction, Data Types, Control
More informationCS1100 Introduction to Programming
CS1100 Introduction to Programming Arrays Madhu Mutyam Department of Computer Science and Engineering Indian Institute of Technology Madras Course Material SD, SB, PSK, NSN, DK, TAG CS&E, IIT M 1 An Array
More informationExcel Functions & Tables
Excel Functions & Tables SPRING 2016 Spring 2016 CS130 - EXCEL FUNCTIONS & TABLES 1 Review of Functions Quick Mathematics Review As it turns out, some of the most important mathematics for this course
More informationData Types. 9. Types. a collection of values and the definition of one or more operations that can be performed on those values
Data Types 1 data type: a collection of values and the definition of one or more operations that can be performed on those values C++ includes a variety of built-in or base data types: short, int, long,
More informationProgramming in C. What is C?... What is C?
C Programming in C UVic SEng 265 Developed by Brian Kernighan and Dennis Ritchie of Bell Labs Earlier, in 1969, Ritchie and Thompson developed the Unix operating system We will be focusing on a version
More informationProgramming in C UVic SEng 265
Programming in C UVic SEng 265 Daniel M. German Department of Computer Science University of Victoria 1 SEng 265 dmgerman@uvic.ca C Developed by Brian Kernighan and Dennis Ritchie of Bell Labs Earlier,
More informationProject 1 System Calls
Project 1 System Calls Introduction In this project, you will become familiar with: 1. Using the xv6 Makefile 2. Using conditional compilation. 3. The xv6 system call invocation path. 4. Implementing a
More informationProgramming in C. What is C?... What is C?
Programming in C UVic SEng 265 C Developed by Brian Kernighan and Dennis Ritchie of Bell Labs Earlier, in 1969, Ritchie and Thompson developed the Unix operating system We will be focusing on a version
More informationSyntax and Variables
Syntax and Variables What the Compiler needs to understand your program, and managing data 1 Pre-Processing Any line that starts with # is a pre-processor directive Pre-processor consumes that entire line
More informationMatrix Multiplication
Matrix Multiplication CPS343 Parallel and High Performance Computing Spring 2013 CPS343 (Parallel and HPC) Matrix Multiplication Spring 2013 1 / 32 Outline 1 Matrix operations Importance Dense and sparse
More informationCS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010
CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011 Lectures 1-22 Moaaz Siddiq Asad Ali Latest Mcqs MIDTERM EXAMINATION Spring 2010 Question No: 1 ( Marks: 1 ) - Please
More informationProgramming in C++ 4. The lexical basis of C++
Programming in C++ 4. The lexical basis of C++! Characters and tokens! Permissible characters! Comments & white spaces! Identifiers! Keywords! Constants! Operators! Summary 1 Characters and tokens A C++
More informationC Language, Token, Keywords, Constant, variable
C Language, Token, Keywords, Constant, variable A language written by Brian Kernighan and Dennis Ritchie. This was to be the language that UNIX was written in to become the first "portable" language. C
More informationCS Programming I: Arrays
CS 200 - Programming I: Arrays Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624 Array Basics
More informationMatrix Multiplication
Matrix Multiplication CPS343 Parallel and High Performance Computing Spring 2018 CPS343 (Parallel and HPC) Matrix Multiplication Spring 2018 1 / 32 Outline 1 Matrix operations Importance Dense and sparse
More informationProgramming for Engineers Structures, Unions
Programming for Engineers Structures, Unions ICEN 200 Spring 2017 Prof. Dola Saha 1 Structure Ø Collections of related variables under one name. Ø Variables of may be of different data types. Ø struct
More informationIntroduction. Primitive Data Types: Integer. Primitive Data Types. ICOM 4036 Programming Languages
ICOM 4036 Programming Languages Primitive Data Types Character String Types User-Defined Ordinal Types Array Types Associative Arrays Record Types Union Types Pointer and Reference Types Data Types This
More informationOBJECT ORIENTED PROGRAMMING USING C++
OBJECT ORIENTED PROGRAMMING USING C++ Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1 Introduction 10.2 Structure Definitions 10.3 Initializing Structures 10.4 Accessing
More informationBLM2031 Structured Programming. Zeyneb KURT
BLM2031 Structured Programming Zeyneb KURT 1 Contact Contact info office : D-219 e-mail zeynebkurt@gmail.com, zeyneb@ce.yildiz.edu.tr When to contact e-mail first, take an appointment What to expect help
More informationPointer Arithmetic and Lexical Scoping. CS449 Spring 2016
Pointer Arithmetic and Lexical Scoping CS449 Spring 2016 Review Pitfall 1 from previous lecture void foo(char *s) { s = "World"; int main() { char *str = "Hello"; foo(str); printf("%s\n", str); return
More informationBasic Elements of C. Staff Incharge: S.Sasirekha
Basic Elements of C Staff Incharge: S.Sasirekha Basic Elements of C Character Set Identifiers & Keywords Constants Variables Data Types Declaration Expressions & Statements C Character Set Letters Uppercase
More informationIntroduction to C++ with content from
Introduction to C++ with content from www.cplusplus.com 2 Introduction C++ widely-used general-purpose programming language procedural and object-oriented support strong support created by Bjarne Stroustrup
More informationINFORMATION TECHNOLOGY SPREADSHEETS. Part 1
INFORMATION TECHNOLOGY SPREADSHEETS Part 1 Page: 1 Created by John Martin Exercise Built-In Lists 1. Start Excel Spreadsheet 2. In cell B1 enter Mon 3. In cell C1 enter Tue 4. Select cell C1 5. At the
More informationLecture 14. Dynamic Memory Allocation
Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 14-1 Lecture 14. Dynamic Memory Allocation The number of variables and their sizes are determined at compile-time before a program runs /*
More informationENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I
ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, 2 0 1 0 R E Z A S H A H I D I Today s class Constants Assignment statement Parameters and calling functions Expressions Mixed precision
More informationFunctional Programming. Lecture 2: Algebra
Functional Programming by Łukasz Stafiniak Email: lukstafi@gmail.com, lukstafi@ii.uni.wroc.pl Web: www.ii.uni.wroc.pl/~lukstafi Lecture 2: Algebra Algebraic Data Types and some curious analogies 1 A Glimpse
More informationUNIT - I. Introduction to C Programming. BY A. Vijay Bharath
UNIT - I Introduction to C Programming Introduction to C C was originally developed in the year 1970s by Dennis Ritchie at Bell Laboratories, Inc. C is a general-purpose programming language. It has been
More informationThe component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science
The component base of C language Nguyễn Dũng Faculty of IT Hue College of Science Content A brief history of C Standard of C Characteristics of C The C compilation model Character set and keyword Data
More informationAIMMS Function Reference - Date Time Related Identifiers
AIMMS Function Reference - Date Time Related Identifiers This file contains only one chapter of the book. For a free download of the complete book in pdf format, please visit www.aimms.com Aimms 3.13 Date-Time
More informationLecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists
Lecture 5: Outline I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists Multidimensional arrays: 2D Declaration int a[3][4]; /*Conceptually 2D matrix
More informationDefinition: Data Type A data type is a collection of values and the definition of one or more operations on those values.
Data Types 1 Definition: Data Type A data type is a collection of values and the definition of one or more operations on those values. Base Data Types All the values of the type are ordered and atomic.
More informationProgramming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan
Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah Lecturer Department of Computer Science & IT University of Balochistan 1 Outline p Introduction p Program development p C language and beginning with
More informationIn this session we will cover the following sub-topics: 1.Identifiers 2.Variables 3.Keywords 4.Statements 5.Comments 6.Whitespaces 7.Syntax 8.
In this session we will cover the following sub-topics: 1.Identifiers 2.Variables 3.Keywords 4.Statements 5.Comments 6.Whitespaces 7.Syntax 8.Semantic www.tenouk.com, 1/16 C IDENTIFIERS 1. Is a unique
More informationChapter 6 part 1. Data Types. (updated based on 11th edition) ISBN
Chapter 6 part 1 Data Types (updated based on 11th edition) ISBN 0-321 49362-1 Chapter 6 Topics Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types Associative
More informationCS201 Some Important Definitions
CS201 Some Important Definitions For Viva Preparation 1. What is a program? A program is a precise sequence of steps to solve a particular problem. 2. What is a class? We write a C++ program using data
More informationScientific Programming in C XIV. Parallel programming
Scientific Programming in C XIV. Parallel programming Susi Lehtola 11 December 2012 Introduction The development of microchips will soon reach the fundamental physical limits of operation quantum coherence
More informationDECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.
DECLARATIONS Character Set, Keywords, Identifiers, Constants, Variables Character Set C uses the uppercase letters A to Z. C uses the lowercase letters a to z. C uses digits 0 to 9. C uses certain Special
More informationC Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee
C Language Part 1 (Minor modifications by the instructor) References C for Python Programmers, by Carl Burch, 2011. http://www.toves.org/books/cpy/ The C Programming Language. 2nd ed., Kernighan, Brian,
More informationReview of the C Programming Language for Principles of Operating Systems
Review of the C Programming Language for Principles of Operating Systems Prof. James L. Frankel Harvard University Version of 7:26 PM 4-Sep-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights
More informationIntroduction. Structures, Unions, Bit Manipulations, and Enumerations. Structure. Structure Definitions
Introduction Structures, Unions, Bit Manipulations, and Enumerations In C, we can create our own data types If programmers do a good job of this, the end user does not even have to know what is in the
More informationC# Fundamentals. Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh
C# Fundamentals Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh Semester 1 2018/19 H-W. Loidl (Heriot-Watt Univ) F20SC/F21SC 2018/19
More informationHistory. used in early Mac development notable systems in Pascal Skype TeX embedded systems
Overview The Pascal Programming Language (with material from tutorialspoint.com) Background & History Features Hello, world! General Syntax Variables/Data Types Operators Conditional Statements Functions
More informationFundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University
Fundamental Data Types CSE 130: Introduction to Programming in C Stony Brook University Program Organization in C The C System C consists of several parts: The C language The preprocessor The compiler
More informationHow to declare an array in C?
Introduction An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type. Representation of a large number of homogeneous values.
More informationCharacter Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Uppercase Alphabets Lowercase Alphabets Character Set A, B, C, Y, Z a, b, c, y, z Digits
More informationFixed-Point Math and Other Optimizations
Fixed-Point Math and Other Optimizations Embedded Systems 8-1 Fixed Point Math Why and How Floating point is too slow and integers truncate the data Floating point subroutines: slower than native, overhead
More informationCS3157: Advanced Programming. Outline
CS3157: Advanced Programming Lecture #12 Apr 3 Shlomo Hershkop shlomo@cs.columbia.edu 1 Outline Intro CPP Boring stuff: Language basics: identifiers, data types, operators, type conversions, branching
More informationIntroduction to Programming Using Java (98-388)
Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;
More informationBindel, Fall 2011 Applications of Parallel Computers (CS 5220) Tuning on a single core
Tuning on a single core 1 From models to practice In lecture 2, we discussed features such as instruction-level parallelism and cache hierarchies that we need to understand in order to have a reasonable
More informationIntroduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language
Introduction C++ widely-used general-purpose programming language procedural and object-oriented support strong support created by Bjarne Stroustrup starting in 1979 based on C Introduction to C++ also
More informationCSE 341 Section Handout #6 Cheat Sheet
Cheat Sheet Types numbers: integers (3, 802), reals (3.4), rationals (3/4), complex (2+3.4i) symbols: x, y, hello, r2d2 booleans: #t, #f strings: "hello", "how are you?" lists: (list 3 4 5) (list 98.5
More informationChapter 6. Data Types
Chapter 6 Data Types Introduction A data type defines a collection of data objects and a set of predefined operations on those objects A descriptor is the collection of the attributes of a variable Copyright
More informationType Definition. C Types. Derived. Function Array Pointer Structure Union Enumerated. EE 1910 Winter 2017/18
Enum and Struct Type Definition C Types Derived Function Array Pointer Structure Union Enumerated 2 tj Type Definition Typedef Define a new Type Inherits members and operations from a standard or previously
More informationCOMPSCI 210 Part II Data Structure
Agenda & Reading COMPSCI 210 Part II Data Structure Based on slides @ McGraw-Hill Agenda: Enum structs Nested structs Array of structs structs Parameters & Returning structs structs Pointers Exercises:
More informationObjectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program
Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates
More informationChapter 2: Basic Elements of C++
Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates
More informationChapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction
Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers
More informationC: How to Program. Week /Mar/05
1 C: How to Program Week 2 2007/Mar/05 Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers
More informationINTRODUCTION 1 AND REVIEW
INTRODUTION 1 AND REVIEW hapter SYS-ED/ OMPUTER EDUATION TEHNIQUES, IN. Programming: Advanced Objectives You will learn: Program structure. Program statements. Datatypes. Pointers. Arrays. Structures.
More informationCourse Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++
Introduction to Programming in C++ Course Text Programming in C++, Zyante, Fall 2013 edition. Course book provided along with the course. Course Description This course introduces programming in C++ and
More informationC++: Const Function Overloading Constructors and Destructors Enumerations Assertions
C++: Const Function Overloading Constructors and Destructors Enumerations Assertions Const const float pi=3.14159; const int* pheight; // defines pointer to // constant int value cannot be changed // pointer
More informationDuhok Polytechnic University Amedi Technical Institute/ IT Dept. Halkawt Rajab Hussain
Duhok Polytechnic University Amedi Technical Institute/ IT Dept. By Halkawt Rajab Hussain 2016-04-02 Overview. C# program structure. Variables and Constant. Conditional statement (if, if/else, nested if
More informationArrays and Pointers. CSC209: Software Tools and Systems Programming (Winter 2019) Furkan Alaca & Paul Vrbik. University of Toronto Mississauga
Arrays and Pointers CSC209: Software Tools and Systems Programming (Winter 2019) Furkan Alaca & Paul Vrbik University of Toronto Mississauga https://mcs.utm.utoronto.ca/~209/ Week 2 Alaca & Vrbik (UTM)
More informationCompiling and Running a C Program in Unix
CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 95 ] Compiling and Running a C Program in Unix Simple scenario in which your program is in a single file: Suppose you want to name
More informationSystems Programming. 05. Structures & Trees. Alexander Holupirek
Systems Programming 05. Structures & Trees Alexander Holupirek Database and Information Systems Group Department of Computer & Information Science University of Konstanz Summer Term 2008 Schedule for Today
More informationIntroduction to C An overview of the programming language C, syntax, data types and input/output
Introduction to C An overview of the programming language C, syntax, data types and input/output Teil I. a first C program TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 1 PROGRAMMING LANGUAGE C is
More informationCode optimization techniques
& Alberto Bertoldo Advanced Computing Group Dept. of Information Engineering, University of Padova, Italy cyberto@dei.unipd.it May 19, 2009 The Four Commandments 1. The Pareto principle 80% of the effects
More informationArrays: The How and the Why of it All Jane Stroupe
Arrays: The How and the Why of it All Jane Stroupe When you need to join data sets, do you even ask yourself Why don t I just use an array? Perhaps because arrays are a mystery or perhaps because you have
More informationcs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).
cs3157: another C lecture (mon-21-feb-2005) C pre-processor (1). today: C pre-processor command-line arguments more on data types and operators: booleans in C logical and bitwise operators type conversion
More information6. Pointers, Structs, and Arrays. 1. Juli 2011
1. Juli 2011 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 50 Outline Recapitulation Pointers Dynamic Memory Allocation Structs Arrays Bubble Sort Strings Einführung
More informationC interfaces to HSL routines. J. D. Hogg. Version 1.0 5th December Numerical Analysis Group Internal Report
2011-1 Numerical Analysis Group Internal Report C interfaces to HSL routines J. D. Hogg Version 1.0 5th December 2011 Copyright (c) 2011 Science and Technology Facilities Council C interfaces to HSL routines
More informationWhy C? Because we can t in good conscience espouse Fortran.
C Tutorial Why C? Because we can t in good conscience espouse Fortran. C Hello World Code: Output: C For Loop Code: Output: C Functions Code: Output: Unlike Fortran, there is no distinction in C between
More informationEECS 388 C Introduction. Gary J. Minden August 29, 2016
EECS 388 C Introduction Gary J. Minden August 29, 2016 1 C Developed at AT&T Bell Laboratories in the early 1970s by Dennis Richie Intended as a systems programming language, that is used to write operating
More informationAn array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.
Data Structures Introduction An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type. Representation of a large number of homogeneous
More informationBASIC ELEMENTS OF A COMPUTER PROGRAM
BASIC ELEMENTS OF A COMPUTER PROGRAM CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING LOGO Contents 1 Identifier 2 3 Rules for naming and declaring data variables Basic data types 4 Arithmetic operators
More informationCSCI 171 Chapter Outlines
Contents CSCI 171 Chapter 1 Overview... 2 CSCI 171 Chapter 2 Programming Components... 3 CSCI 171 Chapter 3 (Sections 1 4) Selection Structures... 5 CSCI 171 Chapter 3 (Sections 5 & 6) Iteration Structures
More informationEP241 Computer Programming
EP241 Computer Programming Topic 2 Dr. Ahmet BİNGÜL Department of Engineering Physics University of Gaziantep Modifications by Dr. Andrew BEDDALL Department of Electric and Electronics Engineering Sep
More informationComputational Methods of Scientific Programming. Lecturers Thomas A Herring Chris Hill
12.010 Computational Methods of Scientific Programming Lecturers Thomas A Herring Chris Hill Review of last lecture Start examining the FORTRAN language Development of the language Philosophy of language:
More informationIntroduction to Programming using C++
Introduction to Programming using C++ Lecture One: Getting Started Carl Gwilliam gwilliam@hep.ph.liv.ac.uk http://hep.ph.liv.ac.uk/~gwilliam/cppcourse Course Prerequisites What you should already know
More informationC# Types. Industrial Programming. Value Types. Signed and Unsigned. Lecture 3: C# Fundamentals
C# Types Industrial Programming Lecture 3: C# Fundamentals Industrial Programming 1 Industrial Programming 2 Value Types Memory location contains the data. Integers: Signed: sbyte, int, short, long Unsigned:
More informationChapter 2: Data Types and Arithmetic Expressions TRUE/FALSE
Chapter 2: Data Types and Arithmetic Expressions TRUE/FALSE 1. Data can take many forms, including numbers, individual alphabetic characters, strings of alphabetic characters, and numbers with specific
More informationC OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure
C Overview Basic C Program Structure C OVERVIEW BASIC C PROGRAM STRUCTURE Goals The function main( )is found in every C program and is where every C program begins speed execution portability C uses braces
More information6. Pointers, Structs, and Arrays. March 14 & 15, 2011
March 14 & 15, 2011 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 47 Outline Recapitulation Pointers Dynamic Memory Allocation Structs Arrays Bubble Sort Strings Einführung
More informationChapter 9 Technicalities: Classes, etc.
Chapter 9 Technicalities: Classes, etc. Dr. Hyunyoung Lee Based on slides by Dr. Bjarne Stroustrup www.stroustrup.com/programming Abstract This lecture presents language technicalities, mostly related
More informationIndustrial Programming
Industrial Programming Lecture 3: C# Fundamentals Industrial Programming 1 C# Types Industrial Programming 2 Value Types Memory location contains the data. Integers: Signed: sbyte, int, short, long Unsigned:
More informationWriting Functions in C
Writing Functions in C 1 Test 2, Problem 5 b. Write a function to allocate space for a new instance of your structure, as defined in part a. Write the C code for a function to get space from the heap using
More informationC Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017
C Concepts - I/O Lecture 19 COP 3014 Fall 2017 November 29, 2017 C vs. C++: Some important differences C has been around since around 1970 (or before) C++ was based on the C language While C is not actually
More informationC OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed
C Overview C OVERVIEW Goals speed portability allow access to features of the architecture speed C fast executables allows high-level structure without losing access to machine features many popular languages
More information2 nd Week Lecture Notes
2 nd Week Lecture Notes Scope of variables All the variables that we intend to use in a program must have been declared with its type specifier in an earlier point in the code, like we did in the previous
More information