Comparison between FORTRAN and C C is case

Comparison between FORTRAN and C C is case sensitive, FORTRAN is not.

Scope delimiter Program myprog ……. . . . stop End program myprog if (a. ge. b) then …… else. . . . end if int main() { …. ; return 0; } do n = 1, 100, 2 …… end do for (i=1; i<100; i+=2) { …. ; } if ( a >= b ) { …. ; } else { …. . ; } Statement saparation: FORTRAN -- “EOL” C -- ”; ”

function format real*8 function expsum(x, n) real*8 x integer n ………. expsum = sum return end function expsum subroutine printx(x) real*8 x print *, “x=“, x return end subroutine printx double expsum(double x, int n) { …. . ; return sum; } void expsum(double x) { printf(“ x= %lfn”, x); return; } FORTRAN always passes vairables by reference, C by value.

I/O function Open(unit=11, file=‘my. dat’, status=‘old’) read(11, *) n read(11, *) (a(i), i=1, n) close(11) FILE *fp; fp = fopen(“my. dat”, ”r”); fscanf(fp, “%d”, &n); for(i=0; i<n; i++) fscanf(fp, “%lf”, &a[i]); fclose(fp); Open(unit=12, file=‘my. dat’, status=‘new’) write(12, *) n write(12, *) (a(i), i=1, n) close(12) FILE *fp; fp = fopen(“my. dat”, ”w”); fprintf (fp, “%dn”, n); for(i=0; i<n; i++) fprintf(fp, “%lf n”, a[i]); fclose(fp); fp is called a file pointer of (user-defined) struct FILE, which is designed to record the information of open files, including file name, memory buffer Size, address, … etc.

variable declaration 4 -byte floating 8 -byte floating 4 -byte integer character 2 -byte integer Unsigned integer real*4 real*8 integer character integer*2 -- float double int char short int unsigned int Variable prefix modification in C: float – long integer – long, short, unsigned long unsigned short

Logic expression Greater than ( less than) . GT. (. LT. ) > ( <) Greater or equal (less eq) . GE. (. LE. ) <= ( <= ) Equal (not equal) . eq. (. NE. ) == ( != ) And (or) . and. (. or. ) && ( || ) End of file test read(11, *, end=label) NOT end of file Loop: feof(fp) !feof(fp) For (i=0; i<=10; i++) {…. . ; } While loop: while ( !feof(fp) ) { fscaf(fp, ”%d”, &n); } do { sum = sum +a; } while (a<=20); Condiftion if: if (a > 0) {b=a; } else {b=-a; } b = (a<0)? (-a) : a;

array declaration program xxx real*8, a, b(10) character*12 name call mysub(a, b, name) end subroutine mysub(a, b, nm) real*8 a, b(10) chararcter*12 nm print *, a, b(5), nm return end main() { …. . double a, b[10]; char name[12]; mysub(a, b, name); return 0; } void mysub(double a, double *b, char *nm) { printf(“%lf %sn”, a, b[5], nm); return; }
![Array = pointer with a fixed address l float X[6]; value X[3] = 5; Array = pointer with a fixed address l float X[6]; value X[3] = 5;](http://slidetodoc.com/presentation_image_h/0873e8ee3fcfe7e230d5b277956663ab/image-8.jpg)
Array = pointer with a fixed address l float X[6]; value X[3] = 5; *(X+3) = 5; pointer X+3; &X[3] location 0 -- 3 X[0] X X[1] X+1 X[2] X+2 X[3] X+3 12 -- 15 X[4] X+4 16 -- 19 X[5] X+5 4 -- 7 8 -- 11 20 -- 23 ※ (x+1) 代表下一個變數、真正的位置增量看數的型態而定

Usage in C not in FORTRAN l Address of a variable (pointer) double a=5. 0, *b; // double variable a, pointer b b = &a ; // assign address of a to pointer b printf(“%lf %pn”, *b, b); &a : address of variable a. *b : value of the address pointed by b. l User-defined variable typedef unsigned int uint; // define new type uint. struct tag 1 { double x; double y; }; // define a struct typedef struct tag 1 twodim; // define structure as new type

Pass an argument l Pass by value y = twox(x); double twox(double x) { x = 2. 0 *x ; return x; } after return to calling function: twox = 2 x, x is not changed. l Pass by adress y = twox(&x); double twox(double *a) { double x=*a; x = 2. 0*x; *a=x; after return to calling function, *a = twox = 2 x. l Pass by reference y = twox(x); double twox(double &a) {a = 2. 0 *a; return a; } after return to calling function, twod = 2 x, x = 2 x. return x; }
![struct tag { members; …} variable; struct srecord { int nd; char name[16], idno[16]; struct tag { members; …} variable; struct srecord { int nd; char name[16], idno[16];](http://slidetodoc.com/presentation_image_h/0873e8ee3fcfe7e230d5b277956663ab/image-11.jpg)
struct tag { members; …} variable; struct srecord { int nd; char name[16], idno[16]; float t 1, t 2, t 3, avg; } student; // in c++ srecord is already a type Typedef struct srecord RECORD; //RECORD becomes a type (user-defined type) Member of a structure: RECORD sss; // sss now is a structre variable sss. nd = 20; // first member of sss fscanf(fp, “%s”, sss. name) // read name from file fp. // note that sss. name is a pointer, no need to add “&” fscanf(fp, “%f”, &sss. t 1); // sss. t 1 is a float, read into its address.
- Slides: 11