Programming in C Quick Start! Biostatistics 615 Lecture 4

Similar documents
C Programs: Simple Statements and Expressions

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from

Introduction to C Language

Preview from Notesale.co.uk Page 2 of 79

ESC101N Fundamentals of Computing

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Functions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction

ECET 264 C Programming Language with Applications

Data Type Fall 2014 Jinkyu Jeong

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

Stream Model of I/O. Basic I/O in C

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

ANSI C Programming Simple Programs

Functions. Systems Programming Concepts

Number Systems, Scalar Types, and Input and Output

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

Course organization. Course introduction ( Week 1)

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

1st and 3rd September 2015

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C Functions. 5.2 Program Modules in C

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

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

CSCI 171 Chapter Outlines

Functions. Autumn Semester 2009 Programming and Data Structure 1. Courtsey: University of Pittsburgh-CSD-Khalifa

CSE123. Program Design and Modular Programming Functions 1-1

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

(2-2) Functions I H&K Chapter 3. Instructor - Andrew S. O Fallon CptS 121 (January 18, 2019) Washington State University

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

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

Programming and Data Structure

211: Computer Architecture Summer 2016

sends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ;

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Computer System and programming in C

EL2310 Scientific Programming

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Professor Peter Cheung EEE, Imperial College

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

Language Design COMS W4115. Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science

Presented By : Gaurav Juneja

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

UNIT-I Input/ Output functions and other library functions

CSC 1107: Structured Programming

Lecture 3. More About C

File Handling in C. EECS 2031 Fall October 27, 2014

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme]

CS110: PROGRAMMING LANGUAGE I

Subject: Fundamental of Computer Programming 2068

A Fast Review of C Essentials Part I

Lecture 5. Functions II. Functions with Arguments. CptS 121 Summer 2016 Armen Abnousi

6.096 Introduction to C++ January (IAP) 2009

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Lecture 02 C FUNDAMENTALS

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

CSC 1107: Structured Programming

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

The C Language Reference Manual

Function. specific, well-defined task. whenever it is called or invoked. A function to add two numbers A function to find the largest of n numbers

EC 413 Computer Organization

today cs3157-fall2002-sklar-lect05 1

Lecture 03 Bits, Bytes and Data Types

Matlab? Chapter 3-4 Matlab and IPT Basics. Working Environment. Matlab Demo. Array. Data Type. MATLAB Desktop:

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

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

High Performance Programming Programming in C part 1

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Primitive Data Types: Intro

COSC2031 Software Tools Introduction to C

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

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


C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

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

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

Course Outline Introduction to C-Programming

Calling Prewritten Functions in C

Chapter 1 & 2 Introduction to C Language

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

Introduction to C An overview of the programming language C, syntax, data types and input/output

BLM2031 Structured Programming. Zeyneb KURT

CS Programming In C

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

C Libraries. Bart Childs Complementary to the text(s)

C FILE Type. Basic I/O in C. Accessing a stream requires a pointer variable of type FILE.

Chapter 7. Basic Types

ESC101N: Fundamentals of Computing End-sem st semester

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

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -VARIABLE AND DATA TYPES- SPRING 2015, SEON-JU AHN, CNU EE

PROGRAMMAZIONE I A.A. 2017/2018

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

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

Topic 6: A Quick Intro To C

BİL200 TUTORIAL-EXERCISES Objective:

The C standard library

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

Lecture 04 FUNCTIONS AND ARRAYS

Transcription:

Programming in C Quick Start! Biostatistics 615 Lecture 4

Last Lecture Analysis of Algorithms Empirical Analysis Mathematical Analysis Big-Oh notation

Today Basics of programming in C Syntax of C programs C function library Representation of numbers in a computer

Very Short History of C C was developed by Dennis Ritchie at Bell Labs (1969 72) Support the new UNIX operating system Successor to B and BCPL Strongly typed language Dynamic memory allocation User defined data structures

The Modern C Language Portable language C compilers are available for desktop computers, mainframes and mobile phones Very efficient C++ is the successor to C Simplifies grouping of functions and related data

Data Types in C Integer data types int, long Floating point data types float, double Character types char Pointers int *, double *, char * User defined types struct (short for structure) Void type

Integers For most purposes the int type will do unsigned int for strictly positive quantities Typically, store up to 31 or 63 digits in base 2 plus one digit for sign range -2.1 to 2.1 billion (32 bit)

Floating point Stored as exponent, mantissa and sign Representation varies between machines Limited range and precision 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 sign exponent mantissa

Anatomy of C Program A collection of functions Receive a set of parameters Declare local variables Carry out processing Return a value main() function Called to start the program

C libraries Most programs are not build from scratch Rely on pre-existing collections of functions e.g. the Standard C library Header (.h) files describe functions in these collections e.g. accessed through #include statements

A C function declaration type function(argument_list) { variable_declarations; statements; } Each function has a type Each function argument has a type Each local variable has a type

A simple C program #include <stdio.h> int main() { printf( Hello, I am a program\n ); return 0; }

Variables in C Must be declared before use Each variable has a specific type integer floating point character Names are case-sensitive

Another C Program #include <stdio.h> int Multiply(int x, int y) { int product = x * y; return product; } int main() { int x = 2; printf( %d * %d = %d\n, x, x, Multiply(x, x)); return 0; }

Another C Function int search(int a[], int value, int start, int stop) { // Variable declarations int i; // Search through each item for (i = start; i <= stop; i++) if (value == a[i]) return i; // Search failed return -1; }

Programming Constructs in C Function definitions and calls Compound statements Flow-control if else do while while for case

Compound Statements C statements can be grouped with { } Each compound statement starts with optional local variable declarations Individual statements separated by ;

if else if (expression) statement1; else statement2; When expression is true (or nonzero) statement1 is executed; otherwise statement2 is executed.

Example void Compare(int a, int b) { if (a == b) printf( Values Match!\n ); else printf( Values are different!\n ); }

do while do statement; while (expression); statement is executed until expression evaluates to false (or zero). statement is executed is executed at least once.

Example /* Calculate precision of double */ double precision() { double e = 1.0, temp; do { e = e * 0.5; temp = 1.0 + e; } while (temp > 1.0); return e * 2.0; }

while while (expression) statement; statement is executed while expression evaluates to true. statement may never be executed.

Example /* Calculate maximum integer */ int maximum_integer() { int a = 2, b = 1, bits = 1; while (a > b) { b = a; a = a + a; bits++; } printf( Looks like a %d-bit computer\n, bits); return b; }

for for ( initialization; condition; increment) statement; Executes initialization. While condition is true: Execute statement. Evaluate increment. statement may never be executed.

Example // Search through each item for (i = start; i <= stop; i++) if (value == a[i]) return i;

break and continue continue Re-evaluates loop condition. If not finished, start a new cycle. break Stop looping early.

Some Standard C Libraries Header File Functionality ctype.h Information about characters float.h Information about floating point limits.h Information about integers math.h Common mathematical functions stdio.h Basic input / output functions stdlib.h Kitchen Sink! string.h String manipulation functions time.h Time

Arithmetic Limits <float.h> DBL_MIN DBL_MAX DBL_EPSILON <limits.h> INT_MIN INT_MAX

Example

math.h, Mathematical Functions double exp(double x); exponential of x double log(double x); natural logarithm of x double log10(double x); base-10 logarithm of x double pow(double x, double y); x raised to power y double sin(double x); double cos(double x); Standard trigonometric functions double sqrt(double x); square root of x double ceil(double x); smallest integer not less than x double floor(double x); largest integer not greater than x double fabs(double x); absolute value of x

Example

Example Output Table of running times as a function of N O(N) O(N log N) O(N*N) O(e^N) 1N 1.0 0.0 1.0 2.7 2N 2.0 2.0 4.0 7.4 4N 4.0 8.0 16.0 54.6 8N 8.0 24.0 64.0 2981.0 16N 16.0 64.0 256.0 8886110.5

Input / Output Functions <stdio.h> Default int printf(char * format, ); int scanf(char * format, ); File based functions FILE * fopen(char * filename, char * mode); int fclose(file * file); int fprintf(file * file, char * format, ); int fscanf(file * file, char * format, );

printf Writes formatted output Format string controls how arguments are converted to text Arguments are output in order as fields are encountered %[flags][width][precision]type Otherwise, string is quoted

printf fields Flags: - to left justify result + to show sign in positive numbers Width Minimum number of characters to print Precision Number of digits after decimal (for floating point) Maximum number of characters (for strings) Type s for strings d for integers, x to print hexadecimal integers f for floating point, e for exponential notation, g for automatic

scanf Reads formatted input Format string controls text is stored in arguments Input converted as fields are encountered %type

scanf fields Types s for strings d for int variables ld for long variables f for float variables lf for double variables Addresses of variables should follow format string

Example

Opening and closing files FILE * fopen(char * filename, char * type); Opens file with filename If type is wt, a text file is opened for writing If type is rt, a text file is opened for reading Types rb and wb are analogous for binary files Returns NULL on failure int fclose(file * file); Closes file Returns 0 on success

Example

Today Brief introduction to C Some useful C library functions Introduced concept of floating point precision