Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

Similar documents
2. Numbers In, Numbers Out

2. Numbers In, Numbers Out

COMP s1 Lecture 1

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

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators

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

CpSc 111 Lab 3 Integer Variables, Mathematical Operations, & Redirection

Computer System and programming in C

A Fast Review of C Essentials Part I

Arithmetic Expressions in C

CpSc 1011 Lab 3 Integer Variables, Mathematical Operations, & Redirection

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

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

Operators and Expressions:

BLM2031 Structured Programming. Zeyneb KURT

Work relative to other classes

The C++ Language. Arizona State University 1

Structured programming

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

1. The Mac Environment in Sierra Hall 1242

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

Programming and Data Structures

1. The Mac Environment in SIE 1222

Fundamental of Programming (C)

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

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

Basic Types and Formatted I/O

Expressions. Eric McCreath

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

C Programming

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

Formatted Input/Output

ET156 Introduction to C Programming

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

CS102: Variables and Expressions

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Decimal Representation

Program Organization and Comments

Numbers In, Numbers Out

Lectures 4 and 5 (Julian) Computer Programming: Skills & Concepts (INF-1-CP1) double; float; quadratic equations. Practical 1.

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

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

Datatypes, Variables, and Operations

Reserved Words and Identifiers

Computer Programming

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Python for Astronomers. Week 1- Basic Python

Basics of Programming

UIC. C Programming Primer. Bharathidasan University

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

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

Welcome! COMP s1. Programming Fundamentals

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

BSM540 Basics of C Language

Learning to Program with Haiku

Programming Language A

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

Fundamentals of C Programming

Basic Assignment and Arithmetic Operators

CHAPTER 3: CORE PROGRAMMING ELEMENTS

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

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

Programming for Engineers Introduction to C

Lecture 3. More About C

Topic 2: Introduction to Programming

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019

BSM540 Basics of C Language

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

BASIC ELEMENTS OF A COMPUTER PROGRAM

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

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

C Program Structures

Variables, Data Types, and Arithmetic Expressions Learning Objectives:

Module 2: Computer Arithmetic

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

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

C: How to Program. Week /Mar/05

Have the same meaning as variables in algebra Single alphabetic character Each variable needs an identifier that distinguishes it from the others a =

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

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

Number Systems, Scalar Types, and Input and Output

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

CSCI 2132 Software Development. Lecture 8: Introduction to C

ANSI C Programming Simple Programs

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

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Display Input and Output (I/O)

COMP 2001/2401 Test #1 [out of 80 marks]

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

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Fundamentals of Programming

A) Central Processing Unit (CPU = Arithmetic-Logic Unit (ALU) + Control Unit (CU))

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

3/13/2012. ESc101: Introduction to Computers and Programming Languages

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

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

Compilation and Execution Simplifying Fractions. Loops If Statements. Variables Operations Using Functions Errors

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

Transcription:

Variables Integer Representation Variables are used to store a value. The value a variable holds may change over its lifetime. At any point in time a variable stores one value (except quantum computers!) C variables have a type We ll only use 2 types of variable for the next few weeks: int for integer values, e.g.: 42, -1 double for decimal numbers 3.14159, 2.71828 typically 4 bytes used to store an int variable 4 bytes 32 bits 2 32 possible values (bit patterns) only 2 32 integers can be represented - which ones? 2 31 to 2 31 1 i.e. -2,147,483,648 to +2,147,483,647 Why are limits assymetric? zero needs a pattern (all zeros) can print bit values see: https://cgi.cse.unsw.edu.au/~cs1511/code/c_basics/ print_bits_of_int.c More later and in COMP1521 Integer Overflow/Underflow Real Representation storing a value in an īnt outside the range that can be represented is illegal unexpected behaviour from most C implementations e.g the sum of 2 large positive integers is negative may cause programs to halt, or not to terminate can creates security holes bits used for int can be different on other platforms C on tiny embedded CPU in washing machine may use 16 bits 2 15 to 2 15 1 i.e. -32,768 to +32767 we ll show later how to handle this, for now assume 32 bit ints also arbitrary precision libraries available for C manipulate integers of any size (memory permitting) commonly 8 bytes used to store a double variable 8 bytes 64 bits 2 64 possible values (bit patterns) 64-bits gives huge number of patterns but infinite number of reals use of bit patterns more complex, if you want to know now https://en.wikipedia.org/wiki/double-precision_ floating-point_format reals in (absolute) range 10 308 to 10 308 can be approximated approximation errors can accumulate More later and in COMP1521

Variables Variable Names (and other Identifiers) Declare The first time a variable is mentioned, we need to specify its type. Initialise Before using a variable we need to assign it a value. // Declare // Initialise answer = 42; // Use printf("%d", num); Variable names can made up of letters, digits and underscores Use a lower case letter to start your variable names Beware variable names are case sensitive, e.g. hello and hello are different names) Beware certain words can t be used as variable names: e.g.: if, while, return, int, double These keywords have special meanings in C programs. You ll learn what many of them are as we go on. Output using printf() Using values in printf() No variables: printf("hello World\n"); A single variable: int num = 5; printf("num is %d\n", num); More than one variable: int j = 5; int k = 17; printf("j is %d and k is %d\n", j, k); Use %d to print an int (integer) value answer = 42; printf("the answer is %d\n", answer); Use %lf or %g to print a double (floating point) value double pi; pi = 3.14159265359; printf("pi is %lf\n", pi);

Input using scanf() Numbers and Types scanf uses a format string like printf. Use %d to read an int (integer) value printf("enter the answer: "); scanf("%d", &answer); Use %lf to read a double (floating point) value double e; printf("enter e: "); scanf("%lf", &e); use only %d and %lf format strings with scanf read only 1 value at a time with scanf scanf can be used in other ways - don t do it we ll show you better ways to do other input Numbers in programs have types. Numbers with a decimal point are type double, e.g. 3.14159-34.56 42.0 C also lets write numbers in scientific notation: 2.4e5 = 2.4 10 5 = 240000.0 Numbers in scientific notation are also type double Numbers without decimal point or exponent are type int, e.g. 42 0-24 Numbers in programs are often called constants (unlike variables they don t change) Giving Constants Names Arithmetic Operators It can be useful to give constants (numbers) a name. It often makes your program more readable. It can make your program easier to update particularly if the constant appears in many places One method is #define statement e.g. #define SPEED OF LIGHT 299792458.0 #define statements go at the top of your program after #include statements #define names should be all capital letters + underscore C supports the usual maths operations: + / Precedence is as you would expect from high school, e.g.: a + b c + d/e = a + (b c) + (d/e) Associativity (grouping) is as you would expect from high school, e.g.: a b c d = ((a b) c) d Use brackets if in doubt about order arithemtic will be evaluated. Beware division may not do what you expect.

Division in C Mathematical functions C division does what you expect if either operand is a double If either operand is a double the result is a double. 2.6/2 = 1.3 (not 2!) C division may not do what you expect if both arguments are integers. The result of dividing 2 integers in C is an integer. The fractional part is discarded (not rounded!). 5/3 = 1 (not 2!) C also has the % operator (integers only). computes the modulo (remainder after division) 14 % 3 = 2 Mathematical functions not part of standard library Essentially because tiny CPUs may not support them A library of mathematical functions is available including: sqrt(), sin(), cos(), log(), exp() Above functions take a double as argument and return a double Functions covered fully later in course Extra include line needed at top of program: #include <math.h> (explained later in course) dcc includes maths library by default most compilers need extra option: gcc needs -lm e.g.: gcc -o heron heron.c -lm Other functions - printf & scanf Linux Command: cp printf & scanf are functions scanf returns a value returns number of items read Use this value to determine if scanf successfully read number. scanf could fail e.g. if the user enters letters OK for now to assume scanf succeeds Good programmers always check Linux Command cp: copies files and directories. cp sourcefile destination If the destination is an existing file, the file is overwritten if the destination is an existing directory the file is copied into the directory To copy a directory use cp -r sourcedir destination

Linux Command: mv Linux Command: rm Linux Command mv moves or renames a file. mv source destination If the destination is an existing file, the file is overwritten if the destination is an existing directory the file is moved into the directory. Linux Command rm removes a file. Usually no undo or recycle bin - be careful & have backups rm filename rm -r directoryname This will delete a whole directory. Be extra careful with this command