12.010 Computational Methods of Scientific Programming Lecture 8 Today’s lecture Start C/C++ Basic language features Web page

Slides:



Advertisements
Similar presentations
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Advertisements

Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control.
Computational Methods of Scientific Programming Lecture 9 Today’s lecture C in more detail Web page
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
Introduction to C Programming CE Lecture 1 Introduction to C.
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
. Compilation / Pointers Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
Guide To UNIX Using Linux Third Edition
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C. A Brief History Created by Dennis Ritchie at AT&T Labs in 1972 Originally created to design and support the Unix operating system.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
 2007 Pearson Education, Inc. All rights reserved C Preprocessor.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 19 - The Preprocessor Outline 19.1 Introduction 19.2 The #include Preprocessor Directive 19.3.
Computer Science 210 Computer Organization Introduction to C.
CS 11 C track: lecture 1 Preliminaries Need a CS cluster account cgi-bin/sysadmin/account_request.cgi Need to know UNIX ITS.
Windows Programming Lecture 05. Preprocessor Preprocessor Directives Preprocessor directives are instructions for compiler.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Programming With C.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room , Chris Hill, Room ,
CSCI 171 Presentation 8 Built-in Functions, Preprocessor Directives, and Macros.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
Introduction to Programming
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
1 Programming a Computer Lecture Ten. 2 Outline  A quick introduction to the programming language C  Introduction to software packages: Matlab for numerical.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Minimal standard C program int main(void) { return 0 ; }
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Programming in C++
THE PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Introduction to C Programming
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
Windows Programming Lecture 03. Pointers and Arrays.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Last week: We talked about: History of C Compiler for C programming
The Machine Model Memory
Computer Science 210 Computer Organization
INC 161 , CPE 100 Computer Programming
Chapter 13 - The Preprocessor
Functions Separate Compilation
C Basics.
Introduction to C Programming Language
Computer Science 210 Computer Organization
Preprocessor C program → Modified C program → Object Code
C Preprocessor(CPP).
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
C Programming Getting started Variables Basic C operators Conditionals
C Preprocessor Seema Chandak.
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

Computational Methods of Scientific Programming Lecture 8 Today’s lecture Start C/C++ Basic language features Web page

10/05/ Lec 082 C History and Background Origins 1973, Bell Labs Public K&R C “The C Programming Language”, [Kernighan 1978] ANSI C – standardized 1989, X Ritchie “C is quirky, flawed and an enormous success” – Compiled language ( gcc, cc ) –Good runtime performance, more control e.g memory utilisation –Portability, licensing, versatility –C apps: Matlab, Mathematica, + Linux netscape, IE, … C++ superset of C i.e. C plus some additional concepts – more on these later

10/05/ Lec 083 C Variables (and C++) Variable names –Lower or upper case + lower, upper, digit, _ … –e.g. x, CO2, DENSITY, area_of_polygon –Names ARE case sensitive: CO2 and co2 not same –Keywords are reserved (also case sensitive) if, for, while, return, int, float ……..

10/05/ Lec 084 Data types and basic arrays int, float, double, char, short, uint, long int int – 4 byte integer (long = 8 byte), short – 2 byte integer, float 32-bit, double 64-bit, char – 1 byte [] for arrays Examples –int a [10], b[10][10]; –char c[20]; –double x, area_of_circle, radius; Also macros –#define PI Everything must be declared /* */ comments

10/05/ Lec 085 Executable Statements 1 Statement terminator is the ;. All C-statements end with this character (common compile error is to forget to put ; at end of a statement. Assignment –#define PI double x, radius, area_of_circle; radius=2.; area_of_circle = PI*radius*radius; Assignment operators: variable op= expression is equivalent to Variable = variable op epression Operators are: = += -= *= /= %/ >>= <<= &= ^= |= Example: k *= 3+x is the same as k=k*(3+x) Some of the operators above (>> << & | are bit operators and rarely seen. % is the modulus operator (a%b is a modulus b; remainder after removing as many b’s are possible from a e.g. 7%3 = 1) Multiple = and be used on a line e.g., a=b=c-0; right to left evaluation

10/05/ Lec 086 Executables: Conditionals Conditional statements are like fortran except no endif statement. The code to be executed in contained in {}’s unless it is just one statement. –if ( radius == 0. ) { inv_radius = 0.; } else { inv_radius = 1./radius; } –We could above used ‘} else inv_radius = 1./radius; ‘ –If( radius == 0. ) { code } else if ( condition ) { code } –It is allowed to have to an empty statement by just having ; after the if or in a sequence of if else if statements.

10/05/ Lec 087 Executable Statements 2 Increment int type by 1 methods in c: –Postfix evaluated after expression –Prefix evaluated before expression int i; i = i+1.; ++i; /* prefix mode */ i++; /* postfix mode */ –When used in an expression prefix mode increments first e.g., c = ++a + ++b; gives difference answer to c = a++ + b++; –These commands are used because increment by 1 is a machine instruction (faster than load 1 to register and add to another register) Changing variable type: cast – double x; int i; – x = (double) i; /* changes integer i to double type)

10/05/ Lec 088 Executable Statements 3 Loops using the “for” construction. int i,j,k; double b[10][10]; k=0; for (j=0;j<10;++j) { for (i=0;i<10;++i) { b[j][i] = (double) k++; } Fortran style “do while structure” but the while appears at the end of the construction do { statements;} while (condition);

10/05/ Lec 089 Standard libraries no math functions, no I/O functions etc are included in standard code. Header files are need to define constants and functions. #include x = cos(y); z = cos(PI); #include printf(“Hello\n”); fprintf(stdout,“Hello\n”); == /usr/include/math.h – C source files == /usr/include/stdio.h

10/05/ Lec 0810 A C Program #include int i=1; main() { int j; j = 2; printf(“Hello\n”); fprintf(stdout,”Hello\n”); fprintf(stdout,”pi == %f\n”,M_PI); fprintf(stdout,”i == %d\n”,i); fprintf(stdout,”j == %d\n”,j); } Header files Global constants and types Program heading Local declarations Executable statements

10/05/ Lec 0811 Functions Definition method. All modules are functions in c and may or may not return a result (type void if no return). type fname(type arg1, type arg2) { /* Local variables and executable code */ } Calling a function fname(arg1, arg2); /* type void call */ result = fname( arg1, arg2); /* result and fname same type*/ Prototype defines how a function should be called type fname(type, type); In C, none of the arguments passed to a functions can be changed -- call by value. Addresses can be passed and the values stored at these addresses can be changed.

10/05/ Lec 0812 Function Example int mymax(float, float); /* Prototype */ main () { float a,b; int ans; a=b=2.; ans= mymax(a,b) /* returns 1 if a > b, 2 if b > a, 0 otherwise */ } int mymax(float a, float b) { if ( a > b ) return 1; if ( b > a ) return 2; return 0; }

10/05/ Lec 0813 Call by reference int mymax(*float, *float); /* Prototype. The *float is a pointer to (address of) a floating point number */ main () { float a,b; int ans; a=b=2.; ans= mymax(&a,&b); /* 1 if a > b, 2 if b > a, 0 otherwise */ /* set a and b = to max. value */ } int mymax(float *a, float *b) { if ( *a > *b ) {*b=*a;return 1;} if ( *b > *a ) {*a=*b;return 2;} return 0; }

10/05/ Lec 0814 Addresses - *, & C allows very explicit addressing of memory locations with the concept of “pointers” (points to memory location) short a; short *ptr_to_a; a = 1; ptr_to_a = &a; Computer Memory x000xFF &a a (value stored at &a)

10/05/ Lec 0815 Compiling and linking Source code is created in a text editor. To compile and link: cc prog.c funcs.c -llibraries -o prog Where prog.c is main program plus maybe functions funcs.c are more subroutines and functions libraries.a are indexed libraries of subroutines and functions (see ranlib) prog is name of executable program to run. depend on specific machine (see man cc or cc --help) -llibraries refers to precompiled library in file lib libraries.a

10/05/ Lec 0816 C Basic Summary Origins of C – Compiled language; K&R, ANSI –V. versatile i.e Matlab, Mathematica, Fortran compiler, Linux, Netscape cores - all are mostly in C. Basic Syntax == –case sensitive, semi-colon required at end of statements, loops, conditionals ( == ). Simple program –Standard libraries ( stdio.h, math.h ) –Calling a function Call by reference v. call by value –double a; double *ptrToA; ptrToA = &a;

10/05/ Lec 0817 C preprocessor (CPP) precompile macros and options; “compiler” proper does not see CPP code. Also stand alone cpp; other compilers e.g..F files fortran – (not in java!) #include - file inclusion #define- macro definition #undef- undefine macro #line- compiler messages line number (not really for general use) #if, #ifdef, #ifndef,- Conditional compilation #else, #elif, #endif __FILE__, __LINE___ (ANSI C).

10/05/ Lec 0818 C preprocessor (CPP) #include “fred.h” - includes contents of file fred.h in program. –I cpp flag sets path to search for fred.h #define PI substitutes everywhere PI occurs in program source. (except in quotes). #undef PI - stops substitution #ifdef PI printf(“pi is set to %f in file %s\n”,PI,__FILE__); #else printf(“pi is not set. Line %d file %s\n”, __LINE__,__FILE__); #endif

10/05/ Lec 0819 C preprocessor (CPP) Macros with args #define _getaddress(a) (&a) /* This macro returns address of a */ main() { double n; double *ptrToN; ptrToN = _getadress(n); } Compiler actually sees code below main() { double n; double *ptrToN; ptrToN = &n; } Often used for debuging #ifdef debug #define _D(a) a #else #define _D(a) #endif

10/05/ Lec 0820 Summary C programming language. Similar to fortran in many ways but with: –Somewhat less rigid syntax –More explicit memory addressing methods –“short-cut” ways of doing operations that can be very fast on some CPU’s. Next lecture we go into more detail in pointers and call by reference and call by value.