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
int main() {
printf( “Hello World!\n”);
return 0;
}C is space insensitive
but case sensitive.
\>gfortran hello.f90\>gfortran hello.f90 –o hello\>hello
\>gcc hello.c\>gcc hello.c –o hello\>hello
Programming language in general
Variable, expression, statement Conditional statement (if-else)
Iteration statement (loop)
Sub-program units (functions)
Log Exp
1st Block 2nd block
T F
i < n
1st Block
T
F
Comparison of Fortran and C, variable types
INTEGER
REAL
CHARACTER
intshortlongunsigned intunsigned shortunsigned long
floatdouble
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)
8(64bits)
(-263 ~ (263 –1))
(-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
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
What is the difference from a Fortran program?
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 stringint 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-91printf(“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=bc1=a, c2=bc1=A, c2=Bstr=Hello world!
Arithmetic Operators
Arithmetic operators ++, --, -(minus) right to left *, /, % left to right +, -(sub) 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
low
high
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=4j=i++; printf(“i=%d, j=%d\n”, i,j); //Ouput:i=5, j=4i=3; printf(“i=%d\n”, i++); //Output: i=3j=i++; printf(“i=%d, j=%d\n”, i,j); //Ouput:i=5, j=4return 0;
}
Operators (cont.)
Relational operators ==, != (eqv. to /= in Fortran), <, <=, >, >=
Logical operators ! (eqv. to .NOT. in Fortran) R to L && (eqv. to .AND. in Fortran) L to R || (eqv. to .OR. in Fortran) L to R
Bitwise operators (Not required!) &, |, ^, ~, <<, >>
high
low
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 Fortranj=(i>0) ? i : -i; // if i is positive j=i, otherwise, j=-iprintf(“i=%d, j=%d\n”, i, j);return 0;
}READ in value: -5OUTPUT: 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=3i=2; j=3; i-=j; printf(“i=%d, j=%d\n”, i, j); //output: i=-1, j=3i=2; j=3; i*=j; printf(“i=%d, j=%d\n”, i, j); //output: i=6, j=3i=2; j=3; i/=j; printf(“i=%d, j=%d\n”, i, j); //output: i=0, j=3i=2; j=3; i%=j; printf(“i=%d, j=%d\n”, i, j);//output: i=2, j=3return 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 printfint 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, 53##4###5 3, 4, 534#5 3, 4, 53<TAB>4<TAB>5 3, 4, 5
Invalid input:3,4,5
Example 10, 11: scanf and printf
int main(){ // example 10int 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, 53,#4,##5 3, 4, 5
int main(){ // example 11
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 written in one line.
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 intprintf(“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)
statement1
else
statement2
IF (logical-exp) statement
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;
}
PROGRAM test
IMPLICIT NONE
INTEGER::i
READ(*,*) i
IF(i<0) THEN
WRITE(*,*) i
ELSE
WRITE(*,*) –i
END IF
END PROGRAM test
INPUT: -5OUTPUT: 5
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;
}
PROGRAM testINTEGER::a, b, t=0READ(*,*) a, b IF(a>b) THEN
t=aa=bb=t
END IFWRITE(*,*) a, b
END PROGRAM test
INPUT: 3, 1OUTPUT: 1 3 3
INPUT: 1, 3OUTPUT: 1 3 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=0READ(*,*) a, b IF(a>b) &
t=aa=bb=tWRITE(*,*) a, b
END PROGRAM test
INPUT: 3, 1OUTPUT: 1 3 3
INPUT: 1, 3OUTPUT: 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;
}
01
00
01
x
x
x
y
Switch case, example 17 int main() {
char grade;scanf(“%c”, &grade);switch (grade){ case ‘A’:
printf(“85~100”\n”); break; case ‘B’:
printf(“70~84\n”); break; case ‘C’:
printf(“60~69\n”); break; case ‘D’:
printf(“<60\n”); break; default:
printf(“error\n”);}return 0;
}
PROGRAM testIMPLICIT NONECHARACTER::gradeREAD(*,*) gradeSELECT 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 SELECTEND PROGRAM
“break” is very important! Without it, if grade==‘A’, output:85~10070~8460~69<60error
ITERATION C (i<n as an example logical-
exp)
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 18int i, sum=0;i=1;while(i<=10){ sum +=i;
// eqv. to sum=sum+i;
i++; // eqv. to i=i+1; or ++i;
}prinf(“sum=%d\n”,sum);return 0;
}
int main(){ // example 19int i, sum=0;i=1;do { sum +=i;
// eqv. to sum=sum+1;
i++; // eqv. to i=i+1; or ++i;
} while(i<=10) ;prinf(“sum=%d\n”,sum);return 0;
}
Example 20: while v.s. do-whileint main(){ // example 20
int i, sum=0;scanf(“%d”, &i);while(i<=10){ sum +=i;
// eqv. to sum=sum+i;
i++; // eqv. to i=i+1; or ++i;
}prinf(“sum=%d\n”,sum);return 0;
}
int main(){ // example 21int i, sum=0;scanf(“%d”, &i);do { sum +=i;
// eqv. to sum=sum+i;
i++; // eqv. to i=i+1; or ++i;
} while(i<=10);prinf(“sum=%d\n”,sum);return 0;
}INPUT: 1OUTPUT: 55
INPUT: 11OUTPUT: 0
INPUT: 1OUTPUT: 55
INPUT: 11OUTPUT: 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;
}
Syntaxfor(exp1; log-exp2; exp3)
statement(s)
Log-exp2
exp1
T
F
statement(s)
exp3
Example 23, 24: for
// Same as example 22int main(){ // example 23
int i, sum=0;i=1;for(; i<=10; i++) {
sum+=i;}printf(“sum=%d\n”,sum);return 0;
}
// Same as example 22int main(){ // example 24
int i, sum;i=1;for(sum=0; i<=10; i++) {
sum+=i;}printf(“sum=%d\n”,sum);return 0;
}
Example 25,26: for
// Same as example 22int main(){ // example 25
int i, sum;for(i=1, sum=0;
i<=100; i++) {sum+=i;
}printf(“sum=%d\n”,sum);return 0;
}
// Same as example 22int main(){ // example 26
int i, sum=0;i=1;for(; i<=100; ) {
sum+=i;i++;
}printf(“sum=%d\n”,sum);return 0;
}
Example 27: break
int main(){ // example 27int i;for(i=1; i<10; i++){
if(i == 8) break; // Loop terminated when i==8printf(“%d\n”, i)// write numbers 1 to 7 only
}return 0;
}
PROGRAM testDO i=1,10
if(i == 8) EXIT !Loop terminated when i==8 WRITE(*,*) i !write numbers 1 to 7 only
END DOEND PROGRAM
“continue” is similar.