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)

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of.
Advertisements

Variables in C Amir Haider Lecturer.
Class 2 ssh, memory, data types, variables
C++ Basics Variables, Identifiers, Assignments, Input/Output.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Structure of a C program
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. The Fundamentals of C++ Basic programming elements and concepts.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
Data types and variables
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Chapter 2 Data Types, Declarations, and Displays
Data Types.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
KEAN UNIVERSITY Visual C++ Dr. K. Shahrabi. Developer studio Is a self-contain environment for creating, compiling, linking and testing windows program.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
CS 192 Lecture 3 Winter 2003 December 5, 2003 Dr. Shafay Shamail.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
C Tokens Identifiers Keywords Constants Operators Special symbols.
C-Language Keywords(C99)
 Programming Languages  First Generation Languages (Machine Language)  We Actually have to do a few things. First we have to find the operating code,
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
CPS120: Introduction to Computer Science
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
C++ Programming, Namiq Sultan1 Chapter 2 Introduction to C++ Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.
Copyright Curt Hill Variables What are they? Why do we need them?
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Data Types, Primitive Types in C++, Variables – Declaration, Initialization, Scope Telerik Software Academy academy.telerik.com Learning and Development.
CPS120: Introduction to Computer Science Variables and Constants.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
C++ Lesson 1.
Asst.Prof.Dr. Tayfun ÖZGÜR
Variables, Identifiers, Assignments, Input/Output
Basics (Variables, Assignments, I/O)
Data types Data types Basic types
Compiler Construction
Reserved Words.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Basics (Variables, Assignments, I/O)
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
C++ Basics.
درس برنامه‌سازي کامپيوتر
Basics of ‘C’.
Keywords.
Variables, Identifiers, Assignments, Input/Output
Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
Variables in C Declaring , Naming, and Using Variables.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Programming Language C Language.
Chap 2. Identifiers, Keywords, and Types
Module 2 Variables, Data Types and Arithmetic
C Language B. DHIVYA 17PCA140 II MCA.
Module 2 - Part 1 Variables, Assignment, and Data Types
Presentation transcript:

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

Arithmetic Types in C++ TypeMeaningMinimum Size boolBooleanNA charcharacter8 bits wchar_twide character16 bits shortshort integer16 bits intinteger16 bits longlong integer32 bits long very long integer64 bits float1-prec. floating point 6 (7) sig. digits doubledouble-prec. FP 10 (16) sig. digits long doubleExtended-prec FP 10 sig. digits some

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\nhorizontal tab\talert (bell)\a vertical tab\vbackspace\bdouble quote\” backslash\\question mark\?single quote\' carriage return\rform 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,// all type int total = 0; /* sum and total initialized to 0 */ Sales_item item;/* 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