180 likes | 199 Views
Learn about integral data types, pointers, variable declaration, constants, type casting & the ASCII character set in C++. Includes examples and syntax guidelines.
E N D
CPCS202- LAB3 Data Types I.Mona Alshehri
Data Types: • Data Type: A set of values together with a set of operations is called a data type. • C++ data types fall into three categories • Simple Data Type. • Structured Data Type. • Pointers.
Simple Data Type: C++ simple data can be classified into three categories: 1. Integral, which is a data type that deals with integers, or numbers without a decimal part. 2. Floating-point, which is a data type that deals with decimal numbers. 3. Enumeration type, which is a user-defined data type.
Simple Data Types: charData Type • charis the smallest integral data type. • char data type is used to represent characters, that is letters, digits and special symbols. • Each character is enclosed within single quote marks. Some of the values belonging tochardata type are: 'A', 'a', '0', '*', '+', '$', '&' • Blank space is a character and is written ' ', with a space left between the single quotes. • Example: char blank = ' ';
Simple Data Types: • boolData Type • The data type bool has two values, true and false. • The central purpose of this data type is to manipulate logical (Boolean) expressions. We call true and false the logical (Boolean) values. • In C++, bool, true, and false are reserved words. • Example: • boolb = false;
Simple Data Types: int Data Type -6728, -67, 0, 78, 36782, +763,... • The memory allocated for the int, unsigned int, longandunsigned longdata types are 4 bytes. • Positive integers do not have to have a + sign in front of them. • No commas are used within an integer. Example int num; long num; Unsigned int num;
Floating-Point Data Types: • Scientific notation 43872918 = 4.3872918 *10^7 {10 to the power of seven}, .0000265 = 2.65 * 10^(-5) {10 to the power of minus five}, 47.9832 = 4.7983 * 10^1 {10 to the power of one} • There are two notation to represent real numbers in C++: • Decimal notation • Exponential notation
Floating-Point Data Types: • float: The data type floatis used in C++ to represent any real number between -3.4E+38 and 3.4E+38.The memory allocated for the floatdata type is 4 bytes. • double: The data typedoubleis used in C++ to represent any real number between -1.7E+308 and 1.7E+308.The memory allocated for the doubledata type is 8 bytes. Example float conversion = 2.54; double payRate = 0.45e-4;
Rules for defining variable name: • A variable name can have one or more letters or digits or underscore for example character _. • White space, punctuation symbols or other characters are not permitted to denote variable name. . • A variable name must begin with a letter. • Variable names cannot be keywords or any reserved words of the C++ programming language. • C++ is a case-sensitive language. Variable names written in capital letters differ from variable names with the same name but written in small letters. For example, the variable name EXFORSYS differs from the variable name exforsys.
Declaring Variables: In order for a variable to be used in C++ programming language, the variable must first be declared. The syntax for declaring variable names isdata type variable name; The date type can be int or float or any of the data types listed above. A variable name is given based on the rules for defining variable name (refer above rules).Example:int a; This declares a variable name a of type int. If there exists more than one variable of the same type, such variables can be represented by separating variable names using comma. For instance int x,y,z This declares 3 variables x, y and z all of data type int.
Declaring Conastant: • Constants have fixed value. Constants, like variables, contain data type. • In C++,constis a reserved word. • A memory location whose content is not allowed to change during program execution. • The syntax to declare a named constant is: • constdataType identifier = value; Example 2-11 const double conversion = 2.54; const intnoOfStudents = 20; const char blank = ' '; const doublepayRate = 15.75;
Lab Work: Write a C++ program which reads an integer number that represents number of minutes and display the equivalent number of hours.
Type Casting: • Converting an expression of a given type into another type is known as type-casting. • short a=2000; • int b; • b = (int) a; // c-like • or • b = int (a); //c++ • Example1: • char z=‘A’; • cout<<z; // print A or • int b=int (z);//A=65 in Ascii code • cout<<b; //print 65 int b=int(‘A’);//x=65 int x = (int) 89.99; //x = 89
ASCII Character Set: • Each of the 128 values of the ASCII character set represents a different character. • The value 65 represents 'A', and the value 43 represents '+'. • The value representing 'B' is 66, so 'A' is smaller than 'B'. • '+' is smaller than 'A' since 43 is smaller than 65. • The 14th character in the set is the new line character. • In C++, the new line character is represented as '\n'. • The null character is represented as '\0'.
ASCII (American Standard Code for Information Interchange) 0 1 2 3 4 5 6 7 8 9 0 nulsohstxetxeotenqackbelbs ht 1 lf vt ff cr so si del dc1 dc2 dc3 2 dc4 naksynetb can em sub esc fsgs 3 rs us b ! “ # $ % & ‘ 4 ( ) * + , - . / 0 1 5 2 3 4 5 6 7 8 9 : ; 6 < = > ? @ A B C D E 7 F G H I J K L M N O 8 P Q R S T U V W X Y 9 Z [ \ ] ^ _ ` a b c 10 d e f g h i j k l m 11 n o p q r s t u v w 12 x y z { | } ~ del