Variables ,Data Types and Constants

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

1 C++ Syntax and Semantics The Development Process.
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.
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.
Programming Declarations. COMP102 Prog. Fundamentals I: Declarations/ Slide 2 General form of a C++ program // Program description #include directives.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Structure of a C program
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
Data types and variables
CS150 Introduction to Computer Science 1
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
String Escape Sequences
Data Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
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.
Variables and Data Types
Input & Output: Console
C Tokens Identifiers Keywords Constants Operators Special symbols.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
CPS120: Introduction to Computer Science
Lecture #5 Introduction to C++
GUIDED BY- A.S.MODI MADE BY- 1. SHWETA ALWANI 2. PRIYANKA.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
Data Types and Operations On Data Objective To understand what data types are The need to study data types To differentiate between primitive types and.
CHAPTER # 2 Part 2 PROGRAMS AND DATA 1 st semster King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
Ch Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Lecture 3 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout from Lecture 1 Background.
COMP 102 Programming Fundamentals I Presented by : Timture Choi COMP102 Lab 011.
CPS120: Introduction to Computer Science Variables and Constants.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
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.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Numbers in ‘C’ Two general categories: Integers Floats
Chapter # 2 Part 2 Programs And data
Last week: We talked about: History of C Compiler for C programming
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
User Interaction and Variables
Introduction to the C Language
BASIC ELEMENTS OF A COMPUTER PROGRAM
ITEC113 Algorithms and Programming Techniques
Lecture2.
Arrays in C.
DATA HANDLING.
Introduction to the C Language
Introduction to the C Language
IDENTIFIERS CSC 111.
Computers & Programming Languages
C AS A LANGUAGE Interface between computer & human being. ALPHABETS
C++ Data Types Data Type
Chapter 2: Java Fundamentals
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Chapter # 2 Part 2 Programs And data
WEEK-2.
B065: PROGRAMMING Variables 1.
B065: PROGRAMMING Variables 2.
Data Types and Maths Programming Guides.
Variables in C Topics Naming Variables Declaring Variables
DATA TYPES There are four basic data types associated with variables:
Variables and Constants
Programming Fundamental-1
Data in a C++ Program Numeric data and character data
Getting Started With Coding
Presentation transcript:

Variables ,Data Types and Constants Introduction to C++ Variables ,Data Types and Constants

Variables A variable is a memory location that is used to store values . To understand what this means let us take an example. Consider the memory of the computer to contain of equal sized cells capable of storing data. These cells have unique addresses. Since we store data in these we need to refer to them in our program. It is humanly impossible to remember all the addresses

Memory locations having unique memory addresses

age So We can name these Memory locations with some user defined names(identifier) like we have named this one as age. Here age is the name given to memory location AFXX011 and is called the variable age. We shall see how to declare a variable discussing data types

How do we use Variables ? In order to be able to use the memory location as a variable we need to declare it ie allocate memory to it. A variable can be declared using a data type and a valid identifier. Let us first discuss data types.

What is a Data Type? A data type defines a set of values and the operations that can be performed on that data. Data Operations Data Type

Why do we need a data type? As all of you must be aware that a computer is just a machine. It cannot by itself distinguish between various types of Data. This means that it cannot distinguish between the number 20 and the letter ‘A’ or the word “good” . For the computer all of this is just a piece of data. It is the programmer who must tell the computer that 20 is a number, ‘A’ is a character and ‘good’ is a word. How do we do it? By using data types we can classify the different data for the computer so that it can be stored and processed in a certain manner.

C++ Data Types : Categories The C++ data types can be categorized as C++ Data Types In-Built int, char, float, void User Defined Classes , structures Derived Arrays

C++ Data Types: In-built C++ supports four basic data types : Basic Data types int char float void

void The void type has no values and no operations. In other words, both the set of values and the set of operations are empty. Although this might seem unusual, we will see later that it is a very useful data type.

Characteristics of data types All data types have a certain size associated with them. This essentially means that data of each type has to be stored in a certain no of bytes. Each data type has a range of permissible values associated with it which is also its domain. All data type have some modifiers to accommodate various ranges for eg int data type has short long

int data type Variables declared as integers are capable of storing whole numbers These are numbers without decimal point. There are three variants of integer type. These differ in size. The following is a table showing the size and domain of integer data types. Type modifier Size (bytes) Min value Max value Sample data (To Store) short int Signed Unsigned 2 -32768 32767 65535 Marks Age int signed unsigned long int 4 -2,147,483,648 2,147,483,647 4,294,967,295 Population

Floating Point A floating-point variable is capable of storing a number with a fractional part. The C++ language supports three different sizes of floating-point: float, double and long double. Type Size (bytes) Min value Max value Sample data (To Store) float 4 3.4 E -38 3.4 E 38 Average double 6 1.7 E -308 1.7 E 308 Huge fractional calculation Long double 10 3.4 E -4932 3.4 E 4932 Stellar distances

Char data type A char variable is capable of storing any character on the keyboard. The ASCII code of the character is stored.eg the symbol ‘+’, the letter ‘A’ etc. Type Size (bytes) Domain Sample data (To Store) char 1 Any character on the keyboard Symbol operator

Variable declarations A variable declaration specifies the type and the name of the variable. A variable has a type and it can contain only values of that type. For example, a variable of the type int can only hold integer values. Syntax: data type identifier ; //declaration data type identifier = value; // initialization Examples: int age; int no_of_books = 45; char letter= 'y'; double price = 2493.14; float temp = -24.5;

Variable declarations When initializing a constant or a variable of char type, or when changing the value of a variable of char type, the value is enclosed in single quotation marks. Examples: const char star = '*'; char letter = ‘D';

Declaration vs Initialization When a variable is given a value at the time of declaration itself this is known as initialization int num; int num=89 Variables are not automatically initialized. For example, after declaration int sum; the value of the variable sum can be anything (garbage). Thus, it is good practice to initialize variables when they are declared.

Constant declarations Constants are used to store values that never change during the program execution. Using constants makes programs more readable and maintainable. Syntax: const data type identifier = value; Examples: const double rate = 7.8; const int x= 45;