Outline Announcements Differences between FORTRAN and C

Slides:



Advertisements
Similar presentations
Exposure C++ Chapter XVI C++ Data Structures, the Record.
Advertisements

CSE 105 Structured Programming Language (C)
Introduction to Programming in C++ John Galletly.
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
1 C++ Syntax and Semantics The Development Process.
Chapter 7:: Data Types Programming Language Pragmatics
Objective Revision of data structures and their implementation.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Guide To UNIX Using Linux Third Edition
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Language Issues Misunderstimated? Sublimable? Hopefuller? "I know how hard it is for you to put food on your family.” "I know the human being and fish.
Introduction to FORTRAN
Introduction of C++ language. C++ Predecessors Early high level languages or programming languages were written to address a particular kind of computing.
Runtime Environments Compiler Construction Chapter 7.
Chapter 1 Introduction to Computers and C++ Programming Goals: To introduce the fundamental hardware and software components of a computer system To introduce.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
Software Engineering Algorithms, Compilers, & Lifecycle.
©2004 Joel Jones 1 CS 403: Programming Languages Lecture 3 Fall 2004 Department of Computer Science University of Alabama Joel Jones.
Windows Programming Lecture 03. Pointers and Arrays.
1 C++ Programming C++ Basics ● C++ Program ● Variables, objects, types ● Functions ● Namespace ● Tests ● Loops ● Pointers, references.
Chapter 5 Names, Bindings, Type Checking CSCE 343.
Chapter 15 - C++ As A "Better C"
Implementing Subprograms
Unit 2 Technology Systems
Chapter 1.2 Introduction to C++ Programming
Chapter 10 : Implementing Subprograms
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
APPENDIX a WRITING SUBROUTINES IN C
Introduction to the C Language
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Alberto Fassò Endre Futó
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
What's a Computer? Monitor Disk Main mouse Memory Keyboard Network
Student Book An Introduction
Pointers.
C Basics.
Objective Revision of data structures and their implementation.
Pointers Psst… over there.
Pointers Psst… over there.
Chapter 10: Implementing Subprograms Sangho Ha
Implementing Subprograms
Data Structures and Analysis (COMP 410)
Introduction to the C Language
Learning to Program in Python
Code Generation.
Use of Mathematics using Technology (Maltlab)
Object-Oriented Programming Using C++ Second Edition
Pointers C#, pointers can only be declared to hold the memory addresses of value types int i = 5; int *p; p = &i; *p = 10; // changes the value of i to.
Classes and Objects.
Lecture 2 SCOPE – Local and Global variables
CS150 Introduction to Computer Science 1
Functions continued.
Compiler Construction
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
Programming Languages and Paradigms
Symbol Table 薛智文 (textbook ch#2.7 and 6.5) 薛智文 96 Spring.
The C Language: Intro.
In this class, we will cover:
CS1201: Programming Language 2
Compiler Construction
ENERGY 211 / CME 211 Lecture 29 December 3, 2008.
Implementing Subprograms
Arrays and Pointers.
Presentation transcript:

Outline Announcements Differences between FORTRAN and C HWI due on Friday Differences between FORTRAN and C Calling FORTRAN from C

Comparing C and FORTRAN

FORTRAN FORmula TRANslator One of the first programming languages Most common strain was standardized in 1977 Designed for “Scientific Computing” (e.g. physics) complex type fully implemented, integrated lots of legacy code simple fast!

FORTRAN: Disadvantages F77 is ancient Missing “modern” features like pointers, novel data structures (or even records) Missing not-so-modern features like recursion! Encourages bad programming: heavy use of goto-statement common blocks

C In many ways, C is similar to FORTRAN But more modern Procedural few built-in types and data structures But more modern pointers--allows you to manipulate memory directly structs--allows you to implement records Together, pointers and structs allow you to create new data structures supports recursion can do everything you could ever want to do (math, CS, graphics)

C: Key disadvantages Programming with pointers can be complicated and even dangerous No complex type or functions LESS LEGACY CODE! Calling this old code from C would allow us to have the best of both worlds!

Calling FORTRAN from C In theory, we should be able to Compile FORTRAN code to object code (-c option) Compile C code to object code Link objects together However, there are a few wrinkles: Namespace problem C needs to refer to the routines using the correct names ANSI C code needs prototypes Call-by-value problem C can use call-by-value, FORTRAN uses only call-by-reference In general, need to make sure we’re sending the FORTRAN routines the type of data they expect

Namespace Problem The section of a .o file for a specific routine is given a name. The name is used by the linker to figure out how the executable is put together We must ensure that calls to FORTRAN routines in C object code use the same name as in the FORTRAN .o file

Namespace Problem Routine FooBar in a FORTRAN .o file could be foobar_ (g77) To call FooBar from C, you will need to use the correct case and add the underscore Some compilers provide a -f option which forces the names in the .o to be all lower case CAUTION: Every system/compiler is different! Read the documentation!

Call-by-Value Problem In C, a variable can be passed to a subroutine by value or reference. call-by-value: the number stored in the variable is passed to the subroutine. The value in the calling routine WILL NOT CHANGE! int m = 4 Foo(m); /* m won’t change */ prototype for Foo: void Foo(int m);

Call-by-reference: call-by-reference: the memory address is passed. If the subroutine modifies the value, the value WILL CHANGE in the calling routine. Use “&” to pass a scalar by value: Foo(&m) /* m might change */ prototype for Foo: void Foo(int *m); /* “*”==pointer */ Arrays are already pointers, so they are automatically passed by reference: int m[10],tot; tot=SumArray(m,10); prototype for SumArray: int Foo(int *m, int n); /* n=length(m) */

Type Equivalences FORTRAN C character*n c char c[n] integer (integer *4) int real (real *4) float double (real *8) double complex *16 c struct dcomp{ double real; double cmplx; };struct dcomp c;

Multidimensional Arrays 1D arrays of equivalent type are represented identically by C and FORTRAN not true for multidimensional arrays 1 2 3 4 5 6 A= C: Column-major FORTRAN: Row-major 1 2 3 4 5 6 1 4 2 5 3 6