Data Types.

Slides:



Advertisements
Similar presentations
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
Advertisements

C++ Basics Variables, Identifiers, Assignments, Input/Output.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Constants and Data Types Constants Data Types Reading for this class: L&L,
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Structure of a C program
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
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.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
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)
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CPS120: Introduction to Computer Science
Lecture #5 Introduction to C++
C++ Character Set It is set of Characters/digits/symbol which is valid in C++. Example – A-Z, (white space) C++ Character Set It is set of.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
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.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
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.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
Chapter2 Constants, Variables, and Data Types. 2.1 Introduction In this chapter, we will discuss –constants (integer, real, character, string, enum),symbolic.
C++ for Java Programmers Chapter 2. Fundamental Daty Types Timothy Budd.
1.2 Primitive Data Types and Variables
CPS120: Introduction to Computer Science Variables and Constants.
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.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
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.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
Variables and Types. Primitive Built-in Type Type Meaning Minimum Size bool boolean NA char character 8 bits wchar_t wide character 16 bits char16_t Unicode.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Asst.Prof.Dr. Tayfun ÖZGÜR
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
User Interaction and Variables
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Data types Data types Basic types
BASIC ELEMENTS OF A COMPUTER PROGRAM
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.
Multiple variables can be created in one declaration
CSE101-Lec#3 Components of C Identifiers and Keywords Data types.
DATA HANDLING.
Advanced Programming Basics
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
C AS A LANGUAGE Interface between computer & human being. ALPHABETS
Chapter 2: Java Fundamentals
Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
Chap 2. Identifiers, Keywords, and Types
C Language B. DHIVYA 17PCA140 II MCA.
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Presentation transcript:

Data Types

Objectives of this session Basic Data Types bool & wchar_t Built-in Data Types User-defined Data Types Derived Data Types Symbolic Constants Dynamic Initialization of Variables Reference Variables

Variables and Data Types Tokens The smallest individual units in a program are known as tokens. Keywords Identifiers Constants Strings Operators

Keywords

Identifiers A valid identifier is a sequence of one or more letters, digits or underscore characters (_). Neither spaces nor punctuation marks or symbols can be part of an identifier. Only letters, digits and single underscore characters are valid. In addition, variable identifiers always have to begin with a letter. They can also begin with an underline character (_ ).

Identifiers The name of a variable: continue… The name of a variable: Starts with an underscore “_” or a letter, lowercase or uppercase, such as a letter from a to z or from A to Z. Examples are Name, gender, _Students, pRice. Can include letters, underscore, or digits. Examples are: keyboard, Master, Junction, Player1, total_grade, _ScoreSide1. Cannot include special characters such as !, %, ], or $. Cannot include an empty space. Cannot be any of the reserved words. Should not be longer than 32 characters (although allowed).

Basic Data Types C++ Data Types User-defined Type Built-in Type Derived Type structure union class enumeration array function pointer reference Integral Type Void Floating Type int char float double

Basic Data Types ANSI C++ added two more data types bool wchar_t continue… ANSI C++ added two more data types bool wchar_t

Data Type - bool A variable with bool type can hold a Boolean value true or false. Declaration: bool b1; // declare b1 as bool type b1 = true; // assign true value to b1 bool b2 = false; // declare and initialize The default numeric value of true is 1 and false is 0.

Data Type – wchar_t The character type wchar_t has been defined to hold 16-bit wide characters. wide_character uses two bytes of memory. wide_character literal in C++ begin with the letter L L‘xy’ // wide_character literal

Built-in Data Types int, char, float, double are known as basic or fundamental data types. Signed, unsigned, long, short modifier for integer and character basic data types. Long modifier for double.

Built-in Data Types Type void was introduced in ANSI C. continue… Type void was introduced in ANSI C. Two normal use of void: To specify the return type of a function when it is not returning any value. To indicate an empty argument list to a function. eg:- void function-name ( void )

Built-in Data Types continue… Type void can also used for declaring generic pointer. A generic pointer can be assigned a pointer value of any basic data type, but it may not be de-referenced. void *gp; // gp becomes generic pointer int *ip; // int pointer gp = ip; // assign int pointer to void pointer Assigning any pointer type to a void pointer is allowed in C & C++.

Built-in Data Types void *gp; // gp becomes generic pointer continue… void *gp; // gp becomes generic pointer int *ip; // int pointer ip = gp; // assign void pointer to int pointer This is allowed in C. But in C++ we need to use a cast operator to assign a void pointer to other type pointers. ip = ( int * ) gp; // assign void pointer to int pointer // using cast operator *ip = *gp;  is illegal

User-Defined Data Types Structures & Classes: struct union class Legal data types in C++. Like any other basic data type to declare variables. The class variables are known as objects.

User-Defined Data Types continue… Enumerated Data Type: Enumerated data type provides a way for attaching names to numbers. enum keyword automatically enumerates a list of words by assigning them values 0, 1, 2, and so on. enum shape {circle, square, triangle}; enum colour {red, blue, green, yellow}; enum position {off, on};

User-Defined Data Types continue… Enumerated Data Type: enum colour {red, blue, green, yellow}; In C++ the tag names can be used to declare new variables. colour background; In C++ each enumerated data type retains its own separate type. C++ does not permit an int value to be automatically converted to an enum value.

User-Defined Data Types continue… Enumerated Data Type: colour background = blue; // allowed colour background = 3; // error in C++ colour background = (colour) 3; // OK int c = red; // valid

User-Defined Data Types continue… Enumerated Data Type: By default, the enumerators are assigned integer values starting with 0. We can override the default value by explicitly assigning integer values to the enumerators. enum colour { red, blue=4, green =8}; enum colour {red=5, blue, green};

User-Defined Data Types continue… Enumerated Data Type: C++ also permits the creation of anonymous enum (i.e., enum with out tag name). enum {off, on}; here off  0 and on  1 int switch_1 = off; int switch_2 = on;

Derived Data Types Arrays Functions The application of arrays in C++ is similar to that in C. Functions top-down - structured programming ; to reduce length of the program ; reusability ; function over-loading.

Derived Data Types Pointers continue… Pointers Pointers can be declared and initialized as in C. int * ip; // int pointer ip = &x; // address of x assigned to ip *ip = 10; // 10 assigned to x through indirection

Derived Data Types Pointers continue… Pointers C++ adds the concept of constant pointer and pointer to a constant. char * const ptr1 = “GOODS”; // constant pointer int const * ptr2 = &m; // pointer to a constant

Symbolic Constants Two ways of creating symbolic constant in C++. Using the qualifier const Defining a set of integer constants using enum keyword. Any value declared as const can not be modified by the program in any way. In C++, we can use const in a constant expression. const int size = 10; char name[size]; // This is illegal in C.

Symbolic Constants const allows us to create typed constants. continue… const allows us to create typed constants. #define - to create constants that have no type information. The named constants are just like variables except that their values can not be changed. C++ requires a const to be initialized. A const in C++ defaults, it is local to the file where it is declared. To make it global the qualifier extern is used.

Symbolic Constants extern const int total = 100; enum { X, Y, Z }; continue… extern const int total = 100; enum { X, Y, Z }; This is equivalent to const int X = 0; const int Y = 1; const int Z = 2;

Reference Variables A reference variable provides an alias for a previously defined variable. For eg., if we make the variable sum a reference to the variable total, then sum and total can be used interchangeably to represent that variable. data-type & reference-name = variable-name float total = 100; float &sum = total;

Reference Variables continue… A reference variable must be initialized at the time of declaration. This establishes the correspondence between the reference and the data object which it names. int x ; int *p = &x ; int & m = *p ;

When the function call f(m) is executed, Reference Variables continue… void f ( int & x ) { x = x + 10; } int main ( ) int m = 10; f (m); When the function call f(m) is executed, int & x = m;