(13)From Fortran to C

(13)From Fortran to C - Nathan Friedman 2009 How are...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Nathan Friedman 2009 ` How are Fortran and C similar? ` What are the differences? ` How can I use my knowledge of Fortran to program in C? HI i l C ? ` How can I compile a C program? 2009 From Fortran to C 2 PROGRAM hello IMPLICIT NONE !This is my first program WRITE (*,*) “Hello World!” END PROGRAM hello 2009 From Fortran to C 3 #include <stdio.h> void main() { /* This is my first program */ printf("Hello World! \n"); } 2009 From Fortran to C 4 ` Give the file a name with the extension .c (instead of .f90) ` With Scite, the extension specifies which compiler to use and we compile and run the same way as before ` In the command window you can use the command gcc to compile C programs ` On a Mac you can run it in xcode (click compile and then go) ` On a Mac you can use the command prompt and gcc compiler 2009 From Fortran to C 5 ` Blocks of code in Fortran Bracketed by keywords such as PROGRAM…END PROGRAM, DO…END DO ` Blocks of code in C Bracketed by { … } ` In Fortran, the main program is in a PROGRAM…END PROGRAM block ` In C, the main program goes inside a function called main() 2009 From Fortran to C 6
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 ` In Fortran we use WRITE (*,*) to output values ` In C, we use a function called printf() ; ` The printf() function is not a statement that is part of the language ` It is defined inside a file called stdio.h that contains a collection of various input and output commands ` To access functions in this file we have to tell the compiler to “include” it 2009 From Fortran to C 7 #include <stdio.h> void main() { /* This is my first program */ printf("Hello World! \n"); } 2009 From Fortran to C 8 ` I/O in Fortran Format of output is determined by compiler Can be specified by programmer using formats ` I/O in C Functions that perform I/O are found in a library Functions that perform I/O are found in a library called stdio.h The most common form is printf(“format string”, list of expressions); The format string is required 2009 From Fortran to C 9 ` In Fortran there are two types of subprograms Functions return a value Subroutines perform an action ` In C functions are the only kind of In C, functions are the only kind of subprogram If no value is to be returned, we specify a return type of void 2009 From Fortran to C 10 #include <stdio.h> void main() { /* This is my first program */ printf("Hello World! \n"); } 2009 From Fortran to C 11 ` Comments in Fortran From “!” to end of line ` Comments in C Enclosed by /* … */ Can appear anywhere in the program (even in the Can appear anywhere in the program (even in the
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/15/2010 for the course COMP COMP 206 taught by Professor Vybihal during the Spring '04 term at McGill.

Page1 / 9

(13)From Fortran to C - Nathan Friedman 2009 How are...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online