Week 3 Lecture 2. Types Constants and Variables

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

Basic Types and Formatted I/O

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

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

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

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.

Zheng-Liang Lu Java Programming 45 / 79

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

More about Binary 9/6/2016

CSc Introduction to Computing

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

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

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

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

BASIC ELEMENTS OF A COMPUTER PROGRAM

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Week 8 Lecture 3. Finishing up C

Computer System and programming in C

Fundamentals of C Programming

Programming and Data Structures

Lecture 2 Tao Wang 1

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

INTRODUCTION 1 AND REVIEW

CS149: Elements of Computer Science. Fundamental C++ objects

BSM540 Basics of C Language

Syntax and Variables

Programming in C++ 4. The lexical basis of C++

Full file at

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

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

Fundamental Data Types

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

VARIABLES AND CONSTANTS

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

DEPARTMENT OF MATHS, MJ COLLEGE

6.096 Introduction to C++ January (IAP) 2009

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

Variables and Constants

Variables and Literals

Chapter 2 THE STRUCTURE OF C LANGUAGE

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

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

CS107 Handout 13 Spring 2008 April 18, 2008 Computer Architecture: Take II

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

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

Exercise: Using Numbers

A Fast Review of C Essentials Part I

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

ARG! Language Reference Manual

Basic Elements of C. Staff Incharge: S.Sasirekha

Number Systems Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur Number Representation

Data Types and Variables in C language

1/29/2018. Starting a Program Executes its main Function. ECE 220: Computer Systems & Programming. The Function main Divides into Two Parts

Computer Organization & Systems Exam I Example Questions

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

Work relative to other classes

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

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

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

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

UNIT- 3 Introduction to C++

BLM2031 Structured Programming. Zeyneb KURT

C Language, Token, Keywords, Constant, variable

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

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Chapter 2: Basic Elements of C++

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

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

UNIT-2 Introduction to C++

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

Chapter-8 DATA TYPES. Introduction. Variable:

Basic Types, Variables, Literals, Constants

Lecture 3. More About C

UNIT 7A Data Representation: Numbers and Text. Digital Data

CS242 COMPUTER PROGRAMMING

LECTURE 02 INTRODUCTION TO C++

Dynamic Data Structures. CSCI 112: Programming in C

Chapter 3. Fundamental Data Types

Programming in C++ 6. Floating point data types

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

Arithmetic Expressions in C

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

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

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Objectives. In this chapter, you will:

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

Presented By : Gaurav Juneja

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Annotation Annotation or block comments Provide high-level description and documentation of section of code More detail than simple comments

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

CS240: Programming in C. Lecture 2: Overview

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

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

EL2310 Scientific Programming

THE FUNDAMENTAL DATA TYPES

Programming in C and C++

COMP2611: Computer Organization. Data Representation

Data Types & Variables

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

Transcription:

Lecture 2 Types Constants and Variables

Types Computers store bits: strings of 0s and 1s Types define how bits are interpreted They can be integers (whole numbers): 1, 2, 3 They can be characters 'a', '?', '_' They can be real numbers: 1.5, 3.2453 They can be strings Hello World In every case they are strings of bits. Scratch figures out what the bits mean by what you do with them 2

Types in C In C you must tell the compiler how to interpret the bits There are two kinds of types: Simple types, which are defined by the hardware Complex types, which are define by the language Complex types are collections of simple types 3

Simple Types There are three basic simple types: int, char, float Int: an integer E.g., 1, 2, 3 Char: a character E.g., 'a', '/', '_' Float: a real number E.g., 1.5, 2.354 4

Types: Size and Range Name Description Size* Range* char Character or small integer 1 byte signed: -128 to 127 unsigned: 0 to 255 short int (short) Short integer 2 bytes signed: -32768 to 32767 unsigned: 0 to 65535 int Integer 4 bytes signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 long int (long) Long integer 4 bytes signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 float Floating point number 4 bytes 3.4e +/- 38 (7 digits) double Double precision floating point number 8 bytes 1.7e +/- 308 (15 digits) long double Long double precision floating point number 8 bytes 1.7e +/- 308 (15 digits) 5

Fixed point number Binary number Sign bit Two's compliment Limited numbers [-214748646, 214748647] 6

Floating Point Numbers Represents fractions Represents large and small numbers Have limited precision 23 bits of significand up to about 8 decimal digits 8 bits of exponent About plus or minus: 2256 115792089237316195423570985008687907853269984665640564039 457584007913129639936 1 sign bit 7

Characters and Strings Characters are 8 bit values interpreted as ASCII. Strings a sequences and char's terminated by the null character '/0'. Strings are complex data types. Their length cannot be specified in the standard. 8

Constants Constants are representations of value of certain types. Int constant: 123 Comprised entirely of digits Float constant: 1.234 Includes a period, but otherwise only digits Char constant: 'a' A single character, surrounded by single quotes String constant: Hello world Zero or more characters surrounded by double quotes 9

Variables Variables are named memory locations of a size that can hold values of a certain type. Int variable: int counter; Float variable: float average; Char variable: char operator; String variable: char message[80]; The [80] indicates that this string is 79 characters or less. 10

Rules for Constructing Variables Names A variable name is any combination of letters, digits and Examples: /* OK */ _1993_tar_return /* system var */ Hello#there /* illegal: # */ double /* keyword */ 2fartogo /* illegal: 2 */. present, hello, y2x3, r2d3,... Manav Rachna College of Engg underscores The first character must be an letter or an underscore (system variable). No commas and blanks are allowed within a variable name. Case matters! C keywords cannot be be used as variable names. 11

Keywords Keywords, or reserved words, are defined by C They cannot be used as variable names There are 32: 12

Initialization Always initialize variables with constants. Int variable: int counter = -1; Float variable: float average = -1.0; Char variable: char operator = 'x'; String variable: char message[80] = Initialization ; 13

Initialization Tips If possible, initialize to a value that doesn't make sense so you know when you are seeing the initial value. When initializing a string initialization value is a good choice. On the other hand, sometimes you want to initialize to a default value If a numeric results is supposed to be positive -1 or -1.1 is a good choice. A character you do not expect is a good choice. 14

Compound types Arrays: all elements are the same size. e.g. int array[5] An array containing 5 integers. To use: array[0] = 5; Structs: elements may have different sizes e.g. struct circle {int x, y; float radius} A circle has two ints called x and y and a single float called radius. To use: circle.x = 5; 15

Strings are arrays. E.g. char *Name = Nat ; Name[0] == 'N' Name[1] = 'a' Name[2] = 't' Name[3] = '\0' 16

Arrays are pointers e.g. char* Name = Nat Name == &Name[0] *Name == Name[0] == 'N' &Name[1] == Name + sizeof(char) &Name[2] == Name + (2 * sizeof(char)) 17

Array initialization Arrays can be initialized. e.g. int data[5] = {2, 5, 3, 7, 1} 18

Structs are pointers e.g. struct circle {int x, y; float radius} &circle.x == circle circle.x == *circle &circle.y == circle + sizeof(int) &circle.radius == circle + (2 *sizeof(int)) 19

Define new types with typedef Type definition common with struct Define a circle type Ints x and y Float radius Allocates memory for each of the elements. 20

Struct initialization Structs can be initialized. Easiest with defined type 21

Kinds of variables Local variable: defined in the current block of code. Global variable: defined through the program. Static variable: allocated memory survives though program life. External variable: variable is defined in another file. 22

Scope and Lifetime Scope defines where a variable can be used Local variable: used only in block. Global variable: used anywhere in program. Lifetime defines how long a variable exists. Local variables: usually deleted when not accessible Static variables: exists until program ends. 23

Scope example Global variable Can be used anywhere in program. Avoid Difficult to find where it changes Local variable Can only be used in current block. 24