Basic Types, Variables, Literals, Constants

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

Variables. Data Types.

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

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

Tokens, Expressions and Control Structures

Introduction to C++ Systems Programming

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

APPENDIX A : KEYWORDS... 2 APPENDIX B : OPERATORS... 3 APPENDIX C : OPERATOR PRECEDENCE... 4 APPENDIX D : ESCAPE SEQUENCES... 5

EEE145 Computer Programming

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Introduction to Programming

Basics of C++ // my first program in C++ Hello World! #include <iostream> using namespace std; int main () { cout << "Hello World!

PROGRAMMAZIONE I A.A. 2018/2019

Programming with C++ Language

6.096 Introduction to C++ January (IAP) 2009

Compiler Construction. Lecture 10

Programming. C++ Basics

Chapter 15 - C++ As A "Better C"

IS 0020 Program Design and Software Tools

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

Introduction to C# Applications

Short Notes of CS201

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

CS201 - Introduction to Programming Glossary By

C++ INDEX. Introduction: Instructions for use. Basics of C++: Structure of a program Variables. Data Types. Constants Operators Basic Input/Output

CSCI 123 Introduction to Programming Concepts in C++

C: How to Program. Week /Mar/05

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

Assignment Operations

Chapter 2 - Control Structures

A Fast Review of C Essentials Part I

Chapter 2 - Introduction to C Programming

EP241 Computer Programming

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

UEE1302 (1102) F10: Introduction to Computers and Programming

1

Manual. Subject Code: CS593. Computer Science and Engineering

3. Java - Language Constructs I

Axivion Bauhaus Suite Technical Factsheet AUTOSAR

INTRODUCTION 1 AND REVIEW

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

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

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

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

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

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

CS3157: Advanced Programming. Outline

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

EP578 Computing for Physicists

LEXICAL 2 CONVENTIONS

Syntax and Variables

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

C Language, Token, Keywords, Constant, variable

Basic Elements of C. Staff Incharge: S.Sasirekha

Data Types Literals, Variables & Constants

Program Fundamentals

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

UNIT- 3 Introduction to C++

Fundamental of Programming (C)

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

C++ Programming Basics

This tutorial adopts a simple and practical approach to describe the concepts of C++.

Chapter 2: Basic Elements of C++

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

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

XC Specification. 1 Lexical Conventions. 1.1 Tokens. The specification given in this document describes version 1.0 of XC.

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... C++ vs Java identifiers.

CSC 1107: Structured Programming

VARIABLES AND CONSTANTS

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Basics of Java Programming

BLM2031 Structured Programming. Zeyneb KURT

Lecture 2 Tao Wang 1

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

C++11/14 Rocks. Clang Edition. Alex Korban

Chapter 1 & 2 Introduction to C Language

Basic Types and Formatted I/O

Interview Questions of C++

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

Programming Fundamentals and Methodology. COMP104: C++ Basics

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

BITG 1233: Introduction to C++

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Data Types and Variables in C language

DEPARTMENT OF MATHS, MJ COLLEGE

Chapter 2 - Control Structures

A First Program - Greeting.cpp

Lecture 02 C FUNDAMENTALS

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

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

Java Notes. 10th ICSE. Saravanan Ganesh

Fundamentals of Programming

Computer System and programming in C

In this session we will cover the following sub-topics: 1.Identifiers 2.Variables 3.Keywords 4.Statements 5.Comments 6.Whitespaces 7.Syntax 8.

>B<82. 2Soft ware. C Language manual. Copyright COSMIC Software 1999, 2001 All rights reserved.

1.1 Introduction to C Language. Department of CSE

Transcription:

Basic Types, Variables, Literals, Constants

What is in a Word? A byte is the basic addressable unit of memory in RAM Typically it is 8 bits (octet) But some machines had 7, or 9, or... A word is the basic unit of operation by the CPU Most registers are this size Largest unit of transfer between RAM and CPU in single instruction

What is a Type? A type is a qualifier that is used by the compiler Machine languages do not have types The type of a variable or constant tells the compiler: How much space the object occupies What operations on the object mean

What is a Type? Given an address in RAM, what does it mean? Could be anything! Types tell the compiler Which instruction to apply Integer addition, floating pt addition How to increment pointers Array references, fields

some Arithmetic Types in C++ Type Meaning Minimum Size bool Boolean NA char character 8 bits wchar_t wide character 16 bits short short integer 16 bits int integer 16 bits long long integer 32 bits long long very long integer 64 bits float 1-prec. floating point 6 (7) sig. digits double double-prec. FP 10 (16) sig. digits long double Extended-prec FP 10 sig. digits

Which Type to Use? General Usage int most integer arithmetic double for floating point computation char only for chars bool only for Booleans Unsigned Used to save space who cares? Do not mix signed and unsigned! Can cause headaches easily - avoid

Type Conversion Casting, or conversion Needed when different type expected Compiler handles automatically Bool LHS = {false if 0, true if non-0} RHS = {0 if false, 1 if true}

Type Conversion Integer Floating point number Truncate FPN when int on LHS Fractional part 0 when int on RHS Can lose precision Out-of-Range LHS unsigned, then residue mod size LHS signed, then undefined (bad)

What is a Literal? A literal is a fixed, explicit value that is known at compile time Can be used to initialize variables Can be used to initialize constants Can be used in expressions Generally bad programming style It may be int, char, bool, etc. 5, 5.0, -3, 'a', a, Go Gators!, '\n'

Special Characters Some characters are not printable Some characters have special meaning to the language For these, we need escape sequences All start with backslash \ Some predefined: \n newline Any by \x where x is a number

Special Characters newline \n horizontal tab \t alert (bell) \a vertical tab \v backslash \b double quote \ backslash \] question mark \? single quote \' carriage return \r form feed \f Can use as single character: std::cout << '\n'; std::cout << \thello!\n ; Generalized escape sequence: \12 = \014 = x0c = newline in decimal, octal, hex Note: only the first 3 octal digits are accepted Note: hex uses all the following digits (!)

Special Literals Boolean true false Pointer nullptr preferred literal 0 NULL (must #include cstdlib) Never any other

Variables A variable is a logically named, typed, structured piece of storage Name allows us to refer to the stored structure Type allows us to know structure Variables can be assigned new values Program can manipulate them!!

Variables Definition: allocates space for storage Declaration: specifies name and type So variable can be referenced here and defined elsewhere Type var_name, var_name, ; All vars have type given at start Good practice: one per line of code!

Variable Definition int sum = 0, value, total = 0; Sales_item item; // all type int /* sum and total initialized to 0 */ /* type Sales_item initialized to default value */ std::string name( Dr. Newman ); /* string is a type from std library variable length character sequence */

Initialization Good idea: ALWAYS INITIALIZE!!!! Initialization object gets value at time of definition (when created) May be any expression that can be evaluated at time of creation Name becomes visible immediately Hence can be used in subsequent initializations in same line!

Variable Initialization int i = 0, j = 2*i; /* j init uses value of i immediately */ int k = sizeof(double); /* value is a function that can be evaluated when k is defined */

List Initialization int i = 0; /* i initialized with literal value */ int i(0); /* here also */ int i = {0}; /* i initialized with literal value, but restricted */ int i{0}; /* same here */ double pi = 3.14; int a{pi}, b = {pi}; int c(pi), d = pi; /* floating pt */ /* fail - requires narrowing */ /* OK, but value truncated */

Declaration vs. Definition Definition allocate space Declaration state type and name Name can be used in current file Makes promise it will be defined later Only define in ONE file extern int i; /* declares but doesn't define i*/ int j; // declares and defines j

Identifiers Identifier = name for variable, function, constant, class, type, etc. Cannot be a keyword in C++ Identifiers may be composed of letters, digits, and underscore char Must begin with _ or letter Identifiers are case-sensitive Main is not main is not MAIN!

C++ Keywords alignas alignof asm auto bool break case catch char char16_t char32_t class const constexpr const_cast continue decltype default delete do double dynamic_cast else enum explicit export extern false float for friend goto if inline int long mutable namespace new

C++ Keywords noexcept nullptr operator private protected public register reinterpret-cast return short signed sizeof static static_assert static_cast struct switch template this thread_local throw true try typedef typeid typename union unsigned using virtual void volatile wchar_t while

C++ Alternative Operator Names and and_eq bitand bitor compl not not_eq or or_eq xor xor_eq

Identifier Conventions Identifier should hint toward purpose Constants are all upper case Variables are lower case Classes start with upper case Multi-word identifiers should distinguish each word using a capital or underscore Sales_item, bookssold, totalrbis

Scoping When is a name visible/usable? Most scopes delimited by {} blocks Names can be reused across scopes Global scope defined outside a fcn Block scope accessible from point defined onward within block Nested scopes name visible in outer scope can be redefined in inner scope