Types, Variables, and Constants

Similar documents
Number Systems, Scalar Types, and Input and Output

Program Flow. Instructions and Memory. Why are these 16 bits? C code. Memory. a = b + c. Machine Code. Memory. Assembly Code.

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Types, Operators and Expressions

Computer System and programming in C

Programming. Elementary Concepts

Types, Operators and Expressions

Internal representation. Bitwise operators

Java Notes. 10th ICSE. Saravanan Ganesh

C Introduction. Comparison w/ Java, Memory Model, and Pointers

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

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

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Internal representation. Bitwise operators

Internal representation. Bitwise operators

BASIC ELEMENTS OF A COMPUTER PROGRAM

Type Conversion. and. Statements

Number Representation 3/2/01 Lecture #

ECE2049 E17 Lecture 2: Data Representations & C Programming Basics

1.1 Introduction to C Language. Department of CSE

JAVA Programming Fundamentals

The type of all data used in a C++ program must be specified

Integer Data Types. Data Type. Data Types. int, short int, long int

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

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

Data Type Fall 2014 Jinkyu Jeong

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

Variables and literals

XSEDE Scholars Program Introduction to C Programming. John Lockman III June 7 th, 2012

Data Types and Computer Storage Arrays and Pointers. K&R, chapter 5

The type of all data used in a C (or C++) program must be specified

Declaration and Memory

11 'e' 'x' 'e' 'm' 'p' 'l' 'i' 'f' 'i' 'e' 'd' bool equal(const unsigned char pstr[], const char *cstr) {

Chapter 7. Basic Types

VARIABLES AND CONSTANTS

Types. C Types. Floating Point. Derived. fractional part. no fractional part. Boolean Character Integer Real Imaginary Complex

Room 3P16 Telephone: extension ~irjohnson/uqc146s1.html

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

Chapter 2: Overview of C++

PROGRAMMAZIONE I A.A. 2017/2018

Lecture 03 Bits, Bytes and Data Types

Programming in C++ 5. Integral data types

Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

ESC101N Fundamentals of Computing

Reserved Words and Identifiers

LECTURE 02 INTRODUCTION TO C++

1.3b Type Conversion

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

INTRODUCTION 1 AND REVIEW

UNIT- 3 Introduction to C++

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Structures, Unions Alignment, Padding, Bit Fields Access, Initialization Compound Literals Opaque Structures Summary. Structures

Java Programming Fundamentals. Visit for more.

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

Lecture 02 C FUNDAMENTALS

Department of Computer Applications

Type Checking. Prof. James L. Frankel Harvard University

Lecture 2 Tao Wang 1

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

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

Unit 3. Constants and Expressions

Topic 6: A Quick Intro To C

CS242 COMPUTER PROGRAMMING

CS1500 Algorithms and Data Structures for Engineering, FALL Virgil Pavlu, Jose Annunziato,

Language Reference Manual

Chapter 2: Introduction to C++

Embedded Systems - FS 2018

A flow chart is a graphical or symbolic representation of a process.

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Programming Language A

CS300 Data Structrures (Fall 2014) Abstract data types (ADT s)

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

DEPARTMENT OF MATHS, MJ COLLEGE

ANSI C Changes. Jonathan Hoyle Eastman Kodak 10/5/00

Numeric Data Types in C

EECS 388 C Introduction. Gary J. Minden August 29, 2016

Data Representation and Storage. Some definitions (in C)

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

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

A First Program - Greeting.cpp

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

TYPES, VALUES AND DECLARATIONS

Getting started with Java

CS 354 Midterm 1a, 33.33% Monday, October 9th, 2000 Solution

Exercise: Using Numbers

Language Fundamentals Summary

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

Advanced use of the C language

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

The C++ Language. Arizona State University 1

Chapter 2: Using Data

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

l l l l l l l Base 2; each digit is 0 or 1 l Each bit in place i has value 2 i l Binary representation is used in computers

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Basic data types. Building blocks of computation

BITG 1233: Introduction to C++

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Transcription:

, Variables, and Constants

What is a Type The space in which a value is defined Space All possible allowed values All defined operations Integer Space whole numbers +, -, x No divide 2 tj

Why Types No room for confusion in a computer Must get the same answer every time All data must be stored into memory somewhere Program Memory Data Memory Memory used to be expensive Minimize the amount needed 3 tj

C Types Void Integral Floating Point Derived Boolean Character Integer Real Imaginary Complex no fractional part fractional part 4 tj

C Types - void No values No defined operations Used when we want to indicate that nothing is here Examples MyFunction(void); // call a function with no input parameters void YourFunction(int val){ // indicate that a function returns nothing 5 tj

C Types - bool - boolean 2 values true, false Logical operations and (&&), or ( ), not (!) Examples bool in1, in2, in3; in1 && in2; // in1 AND in2 // declare 3 variables of type bool in3 =!(in1 in2); // in3 = in1 NOR in2 6 tj

C Types char - character ASCII - 128 values a,b,c,1,2,3,$,%,*, English language characters Unicode millions of values Stored in the computer as integers Same operations as integers Become characters when visualized Require a single quote Examples char initial1 = t ; char initial2 = j ; printf( %c%c, initial1, initial2); // print - tj printf( %c, (initial1 - initial2) ); // print (line feed) // 116 106 = 10 linefeed 7 tj

NULL no character numbers add 0x30 upper and lower case differ by hex 0x20 8 tj

C Types int - integer Values are system dependent integers only 1, 2, 4, 8 bytes short int, int, long int, long long int Operations Arithmetic operations +, -, *, / Comparison operations <, >, <=, >=, ==,!= Bitwise operations ~,, &, ^, <<, >> Examples int aa, bb, cc; aa + bb; // add aa to bb // declare 3 variables of type int 9 tj

C Types int - integer Special considerations with type int Range is defined and limited SIGNED and UNSIGNED variants 14 15 0 1 2-2 -1 0 1 2 65534 65535 0 1 2-2 -1 0 1 2-7 -8 7 6-32767 -32768 32766 32767 8 bit unsigned 8 bit signed 16 bit unsigned 16 bit signed 10 tj

C Types int - integer Consider 3 16bit variables of type short int aa = 32,500 bb = 300 cc = 15,000 signed unsigned aa + bb = -32,736 32,800 aa/cc = 2 11 tj

C Types special integers A special set of integers are defined for embedded systems Designed to allow register/memory access Not system dependent #include <stdint.h> signed char unsigned char short unsigned short int unsigned long long unsigned long long int8_t; uint8_t; int16_t; uint16_t; int32_t; uint32_t; int64_t; uint64_t; 12 tj

C Types int - integer TI MSP432 Laptop 13 tj

C Types float real Values are system dependent SIGNED 4, 8 bytes 1,8,23-1,11,52 float, double, long double Operations Arithmetic operations +, -, *, / Comparison operations <, >, <=, >=, ==,!= Examples float aa, bb, cc; aa / bb; // aa divided by bb // declare 3 variables of type float 14 tj

C Types float imaginary Values are system dependent SIGNED 4, 8 bytes 1,8,23-1,11,52 float imaginary, double imaginary, long double imaginary Operations Arithmetic operations +, -, *, / Comparison operations <, >, <=, >=, ==,!= Not supported in many systems 15 tj

C Types float complex Values are system dependent SIGNED 4, 8 bytes 1,8,23-1,11,52 float complex, double complex, long double complex Operations Arithmetic operations +, -, *, / Comparison operations <, >, <=, >=, ==,!= Real and imaginary parts must be the same size 16 tj

C Types float - real Consider 3 variables of type float aa = 2.5 bb = 300.5 cc = 0.035 signed aa + bb = 303.0 aa/cc = 71.428571 17 tj

Special Details Functions: sizeof(type) sizeof expression typeof(expression) Include <limits.h> <float.h> <stdint.h> SHRT_MIN SHRT_MAX USHRT_MAX INT_MIN INT_MAX Minimum value for an object of type short int Maximum value for an object of type short int Maximum value for an object of type unsigned short int Minimum value for an object of type int Maximum value for an object of type int defines max and min values for standard types 18 tj

Variables Variable Symbolic representation for a value - name Stored in memory (data) Can be modified during execution Since it requires space in memory it must have a type to tell the compiler how much space to reserve 19 tj

Variables Variable Declaration Specify the type and name for a variable Must be declared before it can be used int foo; float rate; char initial1; int var1, this, is, not, a, good, practice; int AccountBalance; int annual_interest_rate; ** Note: name length has no impact on compiled program size focus on readable code 20 tj

Variables Variable Initialization Variables are not initialized just by declaring them They do not automatically have a value of 0 They may well have garbage values Nothing stops you from using an un-initialized variable int foo = 23; int foo, boo = 23; int count; int foo = 23, boo = 23; count = 0; char = A ; float pie = 3.14259; 21 tj

Variables Variables and Memory int foo; char initial1 = t ; float rate = 2.5; char initial2; int boo = 255; reserved for foo has garbage in it initial 1 t hex 0x74 initial 2 - garbage unused - alignment 2.5 10.1 binary 1.01 x 2 1 0 sign 01 mantissa 1000000 exponent boo x x x x x x x x x x x 0 1 1 1 0 1 0 0 x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 x Memory Address 0x0001 1000 foo references this memory address 0x0001 1004 intial1 references this memory address 0x0001 1008 0x0001 100C 0x0001 1010 22 tj

Constants Constant Symbolic representation for a value - name Stored in memory (program) Cannot be modified during execution Since it requires space in memory it must have a type to tell the compiler how much space to reserve 23 tj

Constants Constant Types Same as variable types + string Boolean true, false must include stdbool.h Character requires single quotes Integer can use a modifier to declare type 123 int 123L long int 123UL unsigned long int 24 tj

Constants Constant Types Real - Floating Point default is double can use a modifier to declare type 12.3 double 12.3F float 12.3L long double Complex - Floating Point default is double can use a modifier to declare type 12.3 + 45.6*I double complex 12.3F + 45.6F*I float complex 12.3L + 45.6L*I long double complex 25 tj

Constants Constant Types String Series of characters enclosed in double quotes this is a string constant Special considerations empty string \0 null character 26 tj

Constants Defining Constants Literal un-named constant a = b + 5; // 5 is a literal constant Defined Pre-processor constant #define INTEREST_RATE 0.01 Note ALL CAPS Memory Similar to a variable but cannot be changed const type identifier = value; const float interestrate = 0.01; Why bother with types? 27 tj