Chapter 2. Variable and Data type

Slides:



Advertisements
Similar presentations
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Advertisements

Constants and Data Types Constants Data Types Reading for this class: L&L,
Data Types and Expressions
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
JavaScript, Third Edition
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Variables, Constants and Built-in Data Types Chapter 2: Java Fundamentals.
Basic Elements of C++ Chapter 2.
Variable & Constants. A variable is a name given to a storage area that our programs can manipulate. Each variable in C has a specific type, which determines.
Objectives You should be able to describe: Data Types
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
C Tokens Identifiers Keywords Constants Operators Special symbols.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
CPS120: Introduction to Computer Science
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
CS212: Object Oriented Analysis and Design Lecture 2: Introduction to C++
CSCI 130 Chapter 3. Variables & Names Variable Declarations: –reserve a storage location in memory –identify the name of the variable –identify the type.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
CHAPTER # 2 Part 2 PROGRAMS AND DATA 1 st semster King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
VARIABLES AND DATA TYPES Chapter2:part1 1. Objectives: By the end of this section you should: Understand what the variables are and why they are used.
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
Chapter 5. Outline Some Exercises in C++ Names Variables Scope Lifetime.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
 constant represented by a name, just like a variable, but whose value cannot be changed  The const keyword precedes the type, name, and initialization.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Basics of ‘ C ’ By Gaikwad Varsha P. Asst. Prof. Information Technology Dept. Govt. College of Engg. Aurangabad.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Fundamentals 2.
Chapter # 2 Part 2 Programs And data
Chapter 2: Basic Elements of C++
Variables A variable is a placeholder for a value. It is a named memory location where that value is stored. Use the name of a variable to access or update.
Data Types, Identifiers, and Expressions
By: Syed Shahrukh Haider
Advanced Programming Basics
2.1 Parts of a C++ Program.
Basics of ‘C’.
C++ Data Types Data Type
Introduction to Programming - 3
Chapter 2: Java Fundamentals
Chapter # 2 Part 2 Programs And data
Chapter 2: Java Fundamentals
Chapter 2: Introduction to C++.
C Language B. DHIVYA 17PCA140 II MCA.
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Variables and Constants
Presentation transcript:

Chapter 2. Variable and Data type C++ Programing

Variable A variable provides us with named storage that our programs can manipulate. Each variable in C++ has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable. The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. Upper and lowercase letters are distinct because C++ is case-sensitive:

Fundamental Data Type While doing programming in any programming language, you need to use various variables to store various information. Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. You may like to store information of various data types like character, wide character, integer, floating point, double floating point, boolean etc. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory.

Fundamental Data Type (cont) C++ offer the programmer a rich assortment of built-in as well as user defined data types. Following table lists down seven basic C++ data types: Data Type Keyword Boolean bool Character char Interger int Floating point float Double floating point double Valueless void Wide character wchar_t

Scope of variables A scope is a region of the program and broadly speaking there are three places where variables can be declared: Inside a function or a block which is called local variables. In the definition of function parameters which is called formal parameters. Outside of all functions which is called global variables.

Initialization of variables int d = 3, f = 5; // definition and initializing d and f. byte z = 22; // definition and initializes z. char x = 'x'; // the variable x has the value 'x'.

Constant/Literal Constants refer to fixed values that the program may not alter and they are called literals. Constants can be of any of the basic data types and can be divided into Integer Numerals, Floating-Point Numerals, Characters, Strings and Boolean Values. Again, constants are treated just like regular variables except that their values cannot be modified after their definition.

Constant/Literal (cont) Defining Constants: There are two simple ways in C++ to define constants: Using #define preprocessor. Using const keyword. The #define Preprocessor: Following is the form to use #define preprocessor to define a constant: #define identifier value

Constant/Literal (cont), example #include<iostream> using namespace std; #define LENGTH 10 #define WIDTH 5 #define NEWLINE ‘\n’ Int main() { int area; area= LENGTH * WIDTH; cout<<area; cout<<NEWLINE; return 0; }

Constant/Literal (cont) The const Keyword: You can use const prefix to declare constants with a specific type as follows: const type variable=value;

Constant/Literal (cont), example #include<iostream> using namespace std; Int main() { const int LENGTH=10; const int WIDTH=5; const char NEWLINE=‘\n’; int area; area= LENGTH * WIDTH; cout<<area; cout<<NEWLINE; return 0; }