Computers in Engineering. Moving From Fortran to C Michael A. Hawker

Similar documents
COMP 208 Computers in Engineering

Computers in Engineering. Moving From Fortran to C Part 2 Michael A. Hawker

Computers in Engineering COMP 208. Roots of a Quadratic in C 10/25/2007. Moving From Fortran to C Part 2 Michael A. Hawker

Object-Oriented Programming and Design D0010E. Interactive lecture 2. Overview of the lecture. Writing larger classes Documentation Recursion

Operators and Expressions:

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

C++ Programming: From Problem Analysis to Program Design, Third Edition

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

Computer System and programming in C

Objectives. In this chapter, you will:

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Data Types and Variables in C language

Functions. Systems Programming Concepts

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Variables and literals

C Program. Output. Hi everyone. #include <stdio.h> main () { printf ( Hi everyone\n ); }

UNIT 3 OPERATORS. [Marks- 12]

Tutorial No. 2 - Solution (Overview of C)

EL2310 Scientific Programming

Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)

UNIT- 3 Introduction to C++

C Language, Token, Keywords, Constant, variable

A Fast Review of C Essentials Part I

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Basics of Programming

C Functions. 5.2 Program Modules in C

Programming. C++ Basics

Pointers and scanf() Steven R. Bagley

Prepared by: Shraddha Modi

Structured Programming. Dr. Mohamed Khedr Lecture 4

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

CSCI 171 Chapter Outlines

Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP

Unit 3. Operators. School of Science and Technology INTRODUCTION

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

Informatica e Sistemi in Tempo Reale

Fundamentals of Programming

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Chapter 2: Overview of C. Problem Solving & Program Design in C

CS16 Exam #1 7/17/ Minutes 100 Points total

Work relative to other classes

Technical Questions. Q 1) What are the key features in C programming language?

Unit 1: Introduction to C Language. Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune

Computer Systems Lecture 9

Computers in Engineering. Subroutines Michael A. Hawker

Data types, variables, constants

CSC 1107: Structured Programming

Introduction to C Language

printf( Please enter another number: ); scanf( %d, &num2);

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

This exam is to be taken by yourself with closed books, closed notes, no calculators.

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Full file at

ANSI C Programming Simple Programs

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Introduction to C. Systems Programming Concepts

Motivation was to facilitate development of systems software, especially OS development.

Programming and Data Structure

Computers in Engineering COMP 208. Subprograms. Subroutines. Subroutines Michael A. Hawker

C Tutorial: Part 1. Dr. Charalampos C. Tsimenidis. Newcastle University School of Electrical and Electronic Engineering.

CS16 Week 2 Part 2. Kyle Dewey. Thursday, July 5, 12

C Programs: Simple Statements and Expressions

C Programming Language (Chapter 2 of K&R) Variables and Constants

Motivation was to facilitate development of systems software, especially OS development.

Lecture 04 FUNCTIONS AND ARRAYS

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Programming for Engineers Iteration

Lesson 3 Introduction to Programming in C

(Refer Slide Time 01:41 min)

Chapter 2 - Introduction to C Programming

Procedures, Parameters, Values and Variables. Steven R. Bagley

EC 413 Computer Organization

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

First of all, it is a variable, just like other variables you studied


Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

CS111: PROGRAMMING LANGUAGE II

Computers Programming Course 6. Iulian Năstac

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

Computers Programming Course 5. Iulian Năstac

C: How to Program. Week /Mar/05

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

Fundamental of Programming (C)

Transcription:

Computers in Engineering COMP 208 Moving From Fortran to C Michael A. Hawker

Remember our first Fortran program? PROGRAM hello IMPLICIT NONE!This is my first program WRITE (*,*) "Hello, World!" END PROGRAM hello Oct. 23rd, 2007 From Fortran to C 2

Concepts We Saw Blocks of code Bracketed by keywords such as PROGRAM END PROGRAM, DO END DO Program Block A special block identifying the program Comments From! to end of line Oct. 23rd, 2007 From Fortran to C 3

Concepts We Saw Statements Each statement begins on a new line WRITE statement is part of language Format of output is determined by compiler but can be specified by programmer Oct. 23rd, 2007 From Fortran to C 4

Here's the C version #include <stdio.h> void main() { /* This is my first program */ } printf("hello, World!\n"); Oct. 23rd, 2007 From Fortran to C 5

The Concepts Revisited Blocks of code in Fortran Bracketed by keywords such as PROGRAM END PROGRAM, DO END DO Blocks of code in C Bracketed by { } Oct. 23rd, 2007 From Fortran to C 6

The Concepts Revisited Program Block in Fortran A special block identifying the program C program structure A C program is just a collection of function definitions One of the functions must be called main When the program is run the main function is automatically invoked first Oct. 23rd, 2007 From Fortran to C 7

Here's the C version #include <stdio.h> void main() { /* This is my first program */ } printf("hello, World!\n"); Oct. 23rd, 2007 From Fortran to C 8

Subroutines and Functions In Fortran there are two types of subprograms Functions return a value Subroutines perform an action In C, functions are the only kind of subprogram If no value is to be returned, we specify a return type of void Oct. 23rd, 2007 From Fortran to C 9

Here's the C version #include <stdio.h> void main() { /* This is my first program */ } printf("hello, World!\n"); Oct. 23rd, 2007 From Fortran to C 10

The Concepts Revisited Comments in Fortran From! to end of line Comments in C Enclosed by /* */ Can appear anywhere in the program (even in the middle of other code on the same line) Comments preceded by // include everything to the end of the line Oct. 23rd, 2007 From Fortran to C 11

Here's the C version #include <stdio.h> void main() { /* This is my first C program */ } printf("hello, World!\n"); // It has comments in it Oct. 23rd, 2007 From Fortran to C 12

The Concepts Revisited Statements in Fortran Each statement begins on a new line Statements in C C is a free-format languages Statements can appear anywhere and must be terminated with a ; A new statement can appear on the same line following the ; The programmer is responsible for making the code readable to others Oct. 23rd, 2007 From Fortran to C 13

Free Format Code Free format allows the programmer to write very obscure code Hard for the human reader to understand The compiler doesn t care as long as C syntax rules are obeyed Some programmers like to compete as to who can write the most obscure code. See the web site http://www.ioccc.org/main.html Oct. 23rd, 2007 From Fortran to C 14

What Does This Do? #include <stdio.h> #include <stdlib.h> #include <math.h> #define _ ;double #define void x,x #define case(break,default) break[o]:default[o]: #define switch(bool) ;for(;x<bool; #define do(if,else) iniine(else)>int##if? #define true (--void++) #define false (++void--) char*o=" <60>!?\\\n"_ doubie[010]_ int0,int1 _ Iong=0 _ iniine(int eise){int O1O=!O _ l=!o;for(;o1o<010;++o1o)l+=(o1o[doubie]*pow(eise,o1o));return l;}int main(int booi,char*eise[]){int I=1,x=-*O;if(eIse){for(;I<010+1;I++)I[doubIe-1] =booi>i?atof(i[eise]):!o switch(*o)x++)abs(iniine(x))>iong&&(iong=abs(iniine(x )));int1=iong;main(-*o>>1,0);}else{if(booi<*o>>1){int0=int1;int1=int0-2*iong/0 [O]switch(5[O]))putchar(x-*O?(int0>=inIine(x)&&do(1,x)do(0,true)do(0,false) case(2,1)do(1,true)do(0,false)6[o]case(-3,6)do(0,false)6[o]-3[o]:do(1,false) case(5,4)x?booi?0:6[o]:7[o])+*o:8[o]),x++;main(++booi,0);}}} Oct. 23rd, 2007 From Fortran to C 15

The Concepts Revisited Fortran actions WRITE statement is part of language C actions Functions that return values or perform actions such as I/O are not an intrinsic part of C C has many libraries that contain groups of related functions (such as I/O operations) To access these libraries we must tell the compiler using a #include preprocessor command Oct. 23rd, 2007 From Fortran to C 16

The Concepts Revisited 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 called stdio.h The most common form is printf( format string, list of expressions); The format string is required Oct. 23rd, 2007 From Fortran to C 17

Here's the C version #include <stdio.h> void main() { /* This is my first program */ } printf("hello, World!\n"); Oct. 23rd, 2007 From Fortran to C 18

Case Sensitivity Unlike Fortran which is case insensitive, C is case sensitive. That means that main is different than Main in C A variable named first is different than a variable named First or FIRST Oct. 23rd, 2007 From Fortran to C 19

Roots of a Quadratic in C #include <stdio.h> #include <math.h> void main() { float a, b, c; float d; float root1, root2; scanf ("%f%f%f", &a, &b, &c); /* continued on next slide */ Oct. 23rd, 2007 From Fortran to C 20

} if (a == 0.0) { if (b == 0.0) { if (c == 0.0) { printf ("All numbers are roots \n"); } else { printf ("Unsolvable equation"); } } else { printf ("This is a linear form, root = %f\n", -c/b);} } else { d = b*b - 4.0*a*c ; if (d > 0.0) { d = sqrt (d); root1 = (-b + d)/(2.0 * a) ; root2 = (-b - d)/(2.0 * a) ; printf ("Roots are %f and %f \n", root1, root2); } else if (d == 0.0) { printf ("The repeated root is %f \n", -b/(2.0 * a)); } else { printf ("There are no real roots \n"); printf ("The discriminant is %f \n", d); } } Oct. 23rd, 2007 From Fortran to C 21

Roots of a Quadratic in C #include <stdio.h> #include <math.h> void main() { float a, b, c; float d; float root1, root2; scanf ("%f%f%f", &a, &b, &c); /* continued on next slide */ Oct. 23rd, 2007 From Fortran to C 22

Declarations The concept is the same as in Fortran Declarations tell the compiler: To allocate space in memory for a variable What shape the memory cell should be (i.e. what type of value is to be placed there) What name we will use to refer to that cell Syntax of C declarations <type> list of variable names; Oct. 23rd, 2007 From Fortran to C 23

Variable Types Fortran INTEGER REAL CHARACTER C int short long unsigned int unsigned short unsigned long float double char Oct. 23rd, 2007 From Fortran to C 24

What s Missing? Fortran has a Logical type C doesn t Fortran uses logical expressions in if statements and to control some loops What does C do? Oct. 23rd, 2007 From Fortran to C 25

Arithmetic Expressions Expressions in C are very much like those in Fortran Basic operations include +,-,*,/ The precedence rules are similar Type requirements and conversions are similar to Fortran Functions such as sqrt() are found in libraries such as math.h Oct. 23rd, 2007 From Fortran to C 26

What s Missing There is no exponentiation operator in C Many Processors didn't have built-in exponentiation function How can we accomplish it? math.h contains the pow function: 4.3**5 => pow(4.3, 5) Oct. 23rd, 2007 From Fortran to C 27

What s New? C has some other operators: % is the mod operator, x%y is like the Fortran function mod(x,y) Increment(++), decrement(--) i++ After using i, increase i by 1 i-- After using i, decrease i by 1 ++i Before using i, increase i by 1 --i Before using i, decrease i by 1 Oct. 23rd, 2007 From Fortran to C 28

Example #include <stdio.h> int main() { int x, y; x = 14; y = 5; } printf(" %i \n %i \n %i \n", ++x - y--, ++x - --y, x++ - y--); return 0; >operators 14 13 9 Oct. 23rd, 2007 From Fortran to C 29

Example The following expression x = ((++z) (w--)) % 100; is equivalent to z++; /* or z=z+1 */ x = (z-w) % 100; w--; /* or w=w-1; */ The increment operators are actually more efficient Oct. 23rd, 2007 From Fortran to C 30

Another Shorthand Expressions such as i = i+3; x = x*(y+2); Can be rewritten i += 3; x *= (y+2); Oct. 23rd, 2007 From Fortran to C 31

Another Shorthand In general, expressions of the form: var = var op exp; Can be rewritten in a form that is equivalent (but more efficient to execute: var op = exp; Oct. 23rd, 2007 From Fortran to C 32

Operator Precedence ( ) [ ] ->.! - * & sizeof cast ++ -- (right->left) / % + - < <= >= > ==!= & /\ &&?: (right->left) = += -= (right->left), (comma) Oct. 23rd, 2007 From Fortran to C 33

} if (a == 0.0) if (b == 0.0) if (c == 0.0) printf ("All numbers are roots \n"); else printf ("Unsolvable equation"); else printf ("This is a linear form, root = %f\n", -c/b); else { d = b*b - 4.0*a*c ; if (d > 0.0) { d = sqrt (d); root1 = (-b + d)/(2.0 * a) ; root2 = (-b - d)/(2.0 * a) ; printf ("Roots are %f and %f \n", root1, root2); } else if (d == 0.0) printf ("The repeated root is %f \n", -b/(2.0 * a)); else { printf ("There are no real roots \n"); printf ("The discriminant is %f \n", d); } } Oct. 23rd, 2007 From Fortran to C 34

If Statements in C Syntax: if (expression) statement_1 else statement_2 Other forms: and if (expression) statement if (expression) statement else if (expression) statement... else statement if (expression) { statements } else { statements } if (expression) {statements} if (expression) { statements } else if (expression) { statements } else { statements } Oct. 23rd, 2007 From Fortran to C 35

A Note On Syntax Grouping statements inside braces { } makes them into a block set of statements Easier to read Easier to expand There is no marker to end the if statement Only a single statement can appear in each clause if no braces used Oct. 23rd, 2007 From Fortran to C 36

Wait a Second! There is no logical type in C This isn t like Fortran What is the expression supposed to evaluate to? What is the meaning of an if statement? Oct. 23rd, 2007 From Fortran to C 37

Semantics of if Statements Evaluate the expression If it evaluates to any non-zero value, execute the first statement If it evaluates to 0, execute the second statement (after the else) Go on to the statement following the if Oct. 23rd, 2007 From Fortran to C 38

Still To Come We will see how other Fortran features are reflected in C Some are almost the same with syntactic modifications Some have sometimes subtle semantic differences Later we will see concepts which do not have direct Fortran counterparts Oct. 23rd, 2007 From Fortran to C 39