Lecture 13 Transition from Fortran to C

Lecture 13 Transition
from Fortran to C
Yi Lin
Feb 27, 2007
Hello World!
PROGRAM hello
IMPLICIT NONE
!This is my first program
WRITE (*,*) “Hello World!“
END PROGRAM hello
\>gfortran hello.f90
\>gfortran hello.f90 –o hello
\>hello
int main() {
printf( “Hello World!\n”);
return 0;
}
C is space insensitive
but case sensitive.
\>gcc hello.c
\>gcc hello.c –o hello
\>hello
Programming language in general


Variable, expression, statement
Conditional statement (if-else)
T Log Exp
1st Block

F
2nd block
Iteration statement (loop)
i<n
T

Sub-program units (functions)
1st Block
F
Comparison of Fortran and C,
variable types

INTEGER


REAL


CHARACTER

int
short
long
unsigned int
unsigned short
unsigned long
float
double
char
Variable size in C
TYPE
bytes
Value range
int
4
-2147483648 ~ 2147483647 (-215 ~ (215 –1))
short
2
-32768 ~ 32767 (-215 ~ (215 –1))
long
4(32bits) (-263 ~ (263 –1))
8(64bits) (-231 ~ (231 –1))
unsigned short
2
0 ~ 65535 (0 ~ 216-1)
unsigned long
8
0 ~ 4294967295 (0 ~ 232-1)
float
4
1 bit sign,8 bits exp, 23 bits fraction
double
8
1 bit sign,11 bits exp,52bits fraction
char
1
Represented in ASCII code
Example 2: int
int main(){
int a, b, c, d;
unsigned u;
a=12; b=-24;u=10;
c=a+u; d=b+u;
printf(“a+u=%d, b+u=%d\n”,
c, d);
return 0;
}
OUTPUT:
a+u=22, b+u=-14
What is the difference from
a Fortran program?
PROGRAM test
IMPLICIT NONE
INTEGER::a,b,c,d,u
a=12
b=-24
u=10
c=a+u
d=b+u
WRITE(*,*) “a+u=“,c, “b+u=“,d
END PROGRAM test
Example 3: double
int main(){
double x;
int i;
x=3.6;
i=(int)x; //Force type casting from double to int. Not int(x)
/* (int)x+i: force x to an int and then plus i */
printf(“x=%f, i=%d”, x, i); // x doesn’t change
return 0;
}
OUTPUT:
x=3.600000, i=3
Example 4: char and string
int main(){
char c1, c2;
char* str;
// more about “*” in the next class.
c1=‘a’; c2=‘b’;
printf(“c1=%c, c2=%c\n”, c1, c2);
c1=97; c2=98; // In ASCII codes, a-z: 97-123, A-Z:65-91
printf(“c1=%c, c2=%c\n”, c1, c2);
c1 = c1 - 32; c2 = c2 - 32; // equivalent to: c1 -=32; c2 -=32;
printf(“c1=%c, c2=%c\n”, c1, c2);
str = “Hello world!”;
printf(“str=%s\n”, str);
return 0;
}
OUTPUT:
c1=a, c2=b
c1=a, c2=b
c1=A, c2=B
str=Hello world!
Arithmetic Operators

Arithmetic operators




++, --, -(minus)
*, /, %
+, -(sub)
high
low
right to left
left to right
left to right
post-, pre-, Incrementing(++), decrementing(--)
i++
After using i, increasing i by 1
i-After using i, decreasing i by 1
++i
Before using i, increasing i by 1
--i
Before using i, decreasing i by 1
Example 5: ++, -int main(){
int i, j;
i=3; j=++i; printf(“i=%d, j=%d\n”, i,j); // output: i=4,j=4
i=3; j=i++; printf(“i=%d, j=%d\n”, i,j); // output: i=4,j=3
i=3; j=--i; printf(“i=%d, j=%d\n”, i,j); // output: i=2,j=2
i=3; j=i--; printf(“i=%d, j=%d\n”, i,j); // output: i=2,j=3
return 0;
}
Example 6: ++, -int main(){
int i, j;
i=3; printf(“i=%d\n”, ++i); //Output: i=4
j=i++; printf(“i=%d, j=%d\n”, i,j); //Ouput:i=5, j=4
i=3; printf(“i=%d\n”, i++); //Output: i=3
j=i++; printf(“i=%d, j=%d\n”, i,j); //Ouput:i=5, j=4
return 0;
}
Operators (cont.)

Relational operators


Logical operators




==, != (eqv. to /= in Fortran), <, <=, >, >=
! (eqv. to .NOT. in Fortran)
&& (eqv. to .AND. in Fortran)
|| (eqv. to .OR. in Fortran)
high
low
Bitwise operators (Not required!)

&, |, ^, ~, <<, >>
R to L
L to R
L to R
Operators (cont.)


Ternary operator
 <logical-exp> ? <exp1> : <exp2>
 If the logical-exp is true, the value of this expression is exp1’s
value, otherwise, it is the value of exp2.
Example 7:
int main(){
int i, j;
scanf(“%d”, &i); // eqv to READ(*,*) in Fortran
j=(i>0) ? i : -i;
// if i is positive j=i, otherwise, j=-i
printf(“i=%d, j=%d\n”, i, j);
return 0;
}
READ in value: -5
OUTPUT: i=-5, j=5
Operators (cont.)

Condensed operators

+=, -=, *=, /=, %=


E.g. a += b; // eqv. to a=a+b
&=, |=, ^=, <<=, >>= (Not. Required!)
Example 8:
int main(){
int i, j;
i=2; j=3; i+=j; printf(“i=%d, j=%d\n”, i, j); //output: i=5, j=3
i=2; j=3; i-=j; printf(“i=%d, j=%d\n”, i, j); //output: i=-1, j=3
i=2; j=3; i*=j; printf(“i=%d, j=%d\n”, i, j); //output: i=6, j=3
i=2; j=3; i/=j; printf(“i=%d, j=%d\n”, i, j); //output: i=0, j=3
i=2; j=3; i%=j; printf(“i=%d, j=%d\n”, i, j);//output: i=2, j=3
return 0;
}
scanf and printf

Syntax:



scanf(<formats>, <list of variables>);
printf(<formats>, <list of variables>);
Formats:







d: decimal int
o: octal int
x: hexdecimal int
c: character
s: string
f: real number, floating point
e: real number, exponential format
Example 9: scanf and printf
int main(){
int a, b, c;
scanf(“%d%d%d”, &a, &b, &c);
printf(“%d, %d, %d\n”, a, b, c);
return 0;
}
Valid input:
3#4#5
3##4###5
3
4#5
3<TAB>4<TAB>5
Invalid input:
3,4,5
 3, 4, 5
 3, 4, 5
 3, 4, 5
 3, 4, 5
Example 10, 11: scanf and printf
int main(){ // example 10
int a, b, c;
scanf(“%d,%d,%d”, &a,
&b, &c);
printf(“%d, %d, %d\n”, a,
b, c);
return 0;
}
Valid input:
3,4,5
 3, 4, 5
3,#4,##5  3, 4, 5
It doesn’t matter if
example
written in11one line.
int main(){ //
int a, b, c;
scanf(“%d:%d:%d”, &a, &b,
&c);
printf(“%d, %d, %d\n”, a, b,
c);
return 0;
}
Valid input:
3:4:5
 3, 4, 5
3:#4:##5  3, 4, 5
Example 12: scanf and printf
int main() {
float a, b;
double c,d;
scanf(“%f%f%lf%lf”, &a, &b, &c, &d);
// %lf for double, %ld for long int
printf(“a=%f, b=%6.2f, c=%f, d=%6.2f”, a, b, c, d);
/* 6.2f: output b with 6 digits width and 2 decimal digits */
return 0;
}
Valid input:
1.3##2.4#3.5#6.2
 a=1.300000, b=##2.40, c=3.500000, d=##6.20
Example 13: scanf and printf
int main() {
int a, b;
scanf(“%3d%3d”, &a, &b);
printf(“a=%4d, b=%4d\n”, a, b);
return 0;
}
Valid input:
123456  a=123, b=456
12#34#  a=12, b=34
Conditional statement
if (logical-exp)
statement
IF (logical-exp) statement
if (logical-exp)
statement1
else
statement2
IF (logical-exp) THEN
1st block of statements
ELSE
2nd block of statements
END IF
Example 14: if, if-else
int main(){
int i;
scanf(“%d”, &i);
if(i<0)
printf(“%d”, i);
else
printf(“%d”, -i);
return 0;
}
INPUT: -5
OUTPUT: 5
PROGRAM test
IMPLICIT NONE
INTEGER::i
READ(*,*) i
IF(i<0) THEN
WRITE(*,*) i
ELSE
WRITE(*,*) –i
END IF
END PROGRAM test
What if there are more than 1
statement between if and else?
Example 15: scope {}
int main() {
int a, b, t=0;
scanf(%d,%d”,&a,&b);
if(a>b){
t=a;
a=b;
b=t;
}
printf(“%d %d %d\n”, a, b, t);
return 0;
}int main() {
int a, b, t=0;
scanf(%d,%d”,&a,&b);
if(a>b)
t=a;
a=b;
b=t;
printf(“%d %d %d\n”, a, b, t);
return 0;
}
PROGRAM test
INTEGER::a, b, t=0
READ(*,*) a, b
IF(a>b) THEN
t=a
a=b
b=t
END IF
WRITE(*,*) a, b
END PROGRAM test
PROGRAM test
INTEGER::a, b, t=0
READ(*,*) a, b
IF(a>b) &
t=a
a=b
b=t
WRITE(*,*) a, b
END PROGRAM test
INPUT: 3, 1
OUTPUT: 1 3 3
INPUT: 1, 3
OUTPUT: 1 3 0
INPUT: 3, 1
OUTPUT: 1 3 3
INPUT: 1, 3
OUTPUT: 3 0 0
Example 16: if-else if-else
Problem: write a program to read an integer x and output the
corresponding y:
int main() {
int x, y;
scanf(“%d”, &x);
if(x<0)
y=-1;
else if(x==0)
y=0;
else
y=1;
printf(“x=%d,y=%d\n”, x, y);
return 0;
}

 1 x  0

y0 x0
1 x0

Switch case, example 17
int main() {
char grade;
scanf(“%c”, &grade);
switch (grade){
case ‘A’:
printf(“85~100”\n”);
case ‘B’:
printf(“70~84\n”);
case ‘C’:
printf(“60~69\n”);
case ‘D’:
printf(“<60\n”);
default:
printf(“error\n”);
}
return 0;
}
break;
break;
break;
break;
PROGRAM test
IMPLICIT NONE
CHARACTER::grade
READ(*,*) grade
SELECT CASE (grade)
CASE ‘A’:
WRITE(*,*) “85~100”
CASE ‘B’:
WRITE(*,*) “70~84”
CASE ‘C’:
WRITE(*,*) “60~69”
CASE ‘D’:
WRITE(*,*) “<60”
DEFAULT:
WRITE(*,*) “error”
END SELECT
END PROGRAM
“break” is very important! Without it, if grade==‘A’, output:
85~100
70~84
60~69
<60
error
ITERATION

C (i<n as an example logicalexp)
while(i<n) {
<loop body>
i++;
}
do {
<loop body>
i++;
} while(i<n);
for(i=1; i<n; i++){
<loop body>
}

Fortran (i<n as an example logical-exp)
DO count=1, n, 1
<loop body>
END DO
DO
IF(i>n) EXIT
<loop body>
i=i+1
END DO
DO WHILE(i<n)
<loop body>
i=i+1
END DO
Example 19: do while
int main(){ // example 18
int i, sum=0;
i=1;
while(i<=10){
sum +=i;
// eqv. to sum=sum+i;
int main(){ // example 19
int i, sum=0;
i=1;
do {
sum +=i;
// eqv. to sum=sum+1;
i++;
// eqv. to i=i+1; or ++i;
i++;
// eqv. to i=i+1; or ++i;
} while(i<=10) ;
prinf(“sum=%d\n”,sum);
return 0;
}
prinf(“sum=%d\n”,sum);
return 0;
}
}
Example 20: while v.s. do-while
int main(){ // example 20
int i, sum=0;
scanf(“%d”, &i);
while(i<=10){
sum +=i;
// eqv. to sum=sum+i;
int i, sum=0;
scanf(“%d”, &i);
do {
sum +=i;
// eqv. to sum=sum+i;
i++;
// eqv. to i=i+1; or ++i;
i++;
// eqv. to i=i+1; or ++i;
} while(i<=10);
prinf(“sum=%d\n”,sum);
return 0;
}
prinf(“sum=%d\n”,sum);
return 0;
}
int main(){ // example 21
INPUT:
1
OUTPUT: 55
INPUT:
11
OUTPUT: 0
}
INPUT:
1
OUTPUT: 55
INPUT:
11
OUTPUT: 11
for: example 22
int main(){ // example 22
int i, sum=0;
for(i=1; i<=10; i++) {
sum+=i;
}
printf(“sum=%d\n”,sum);
return 0;
}

Syntax
for(exp1; log-exp2; exp3)
statement(s)
exp1
Log-exp2
T
statement(s)
exp3
F
Example 23, 24: for
// Same as example 22
// Same as example 22
int main(){ // example 23
int main(){ // example 24
int i, sum=0;
int i, sum;
i=1;
i=1;
for(; i<=10; i++) {
for(sum=0; i<=10; i++) {
sum+=i;
sum+=i;
}
}
printf(“sum=%d\n”,sum);
printf(“sum=%d\n”,sum);
return 0;
return 0;
}
}
Example 25,26: for
// Same as example 22
// Same as example 22
int main(){ // example 26
int main(){ // example 25
int i, sum=0;
int i, sum;
i=1;
for(i=1, sum=0;
for(; i<=100; ) {
i<=100; i++) {
sum+=i;
i++;
sum+=i;
}
}
printf(“sum=%d\n”,sum);
printf(“sum=%d\n”,sum);
return 0;
return 0;
}
}
Example 27: break
int main(){ // example 27
int i;
for(i=1; i<10; i++){
if(i == 8) break;
// Loop terminated when i==8
printf(“%d\n”, i)
// write numbers 1 to 7 only
}
return 0;
}
“continue” is similar.
PROGRAM test
DO i=1,10
if(i == 8) EXIT
!Loop terminated when i==8
WRITE(*,*) i
!write numbers 1 to 7 only
END DO
END PROGRAM