Variables and Data Types

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
Data types and variables
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
Programming C/C++ on Eclipe C Training Trình bày : Ths HungNM.
Objectives You should be able to describe: Data Types
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
C Tokens Identifiers Keywords Constants Operators Special symbols.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CPS120: Introduction to Computer Science
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Primitive Variables.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Primitive Variables.
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.
Chapter 2 Variables.
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.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
CPS120: Introduction to Computer Science Variables and Constants.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Introduction ABAP Fields and Variables. Slide 2 Fields (Introduction) In ABAP, fields (or data objects) are named locations in memory Variables store.
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
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.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
INTRODUCTION TO ‘C’ PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
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.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
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.
Constants, Data Types and Variables
Numbers in ‘C’ Two general categories: Integers Floats
User Interaction and Variables
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.
ITEC113 Algorithms and Programming Techniques
Variables ,Data Types and Constants
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.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Biến và Kiểu Dữ Liệu Chương 2.
Variables in C Topics Naming Variables Declaring Variables
Chapter 2 Variables.
C++ Data Types Data Type
Chapter 2: Java Fundamentals
Chapter 2: Java Fundamentals
WEEK-2.
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Variables in C Topics Naming Variables Declaring Variables
Variables and Constants
Programming Fundamental-1
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

Variables and Data Types Session 2

Objectives Discuss variables Differentiate between variables and constants List the different data types and make use of them in C programs Discuss arithmetic operators Elementary Programming with C/Session 2/ 2 of 22

Each location in the memory is unique Variables Memory 15 Data Data in memory 15 Each location in the memory is unique Variables allow to provide a meaningful name for the location in memory Elementary Programming with C/Session 2/ 3 of 22

DISPlAY ‘Enter 2 numbers’ Example BEGIN DISPlAY ‘Enter 2 numbers’ INPUT A, B C = A + B DISPLAY C END A, B and C are variables in the pseudocode Variable names takes away the need for a programmer to access memory locations using their address The operating system takes care of allocating space for the variables To refer to the value in the memory space, we need to only use the variable name Elementary Programming with C/Session 2/ 4 of 22

Constants A constant is a value whose worth never changes Examples 5 numeric / integer constant 5.3 numeric / float constant ‘Black’ string constant ‘C’ Character constant Variables hold constant values Elementary Programming with C/Session 2/ 5 of 22

Identifier Names ! is invalid The names of variables, functions, labels, and various other user defined objects are called identifiers Some correct identifier names arena s_count marks40 class_one Examples of erroneous identifiers 1sttest oh!god start... end Identifiers can be of any convenient length, but the number of characters in a variable that are recognized by a compiler varies from compiler to compiler Identifiers in C are case sensitive ! is invalid Elementary Programming with C/Session 2/ 6 of 22

Guidelines for Naming Identifiers Variable names should begin with an alphabet Proper names should be avoided while naming variables The first character can be followed by alphanumeric characters A variable name should be meaningful and descriptive Confusing letters should be avoided Some standard variable naming convention should be followed while programming Elementary Programming with C/Session 2/ 7 of 22

Keywords Keywords : All languages reserve certain words for their internal use Keywords hold a special meaning within the context of the particular language No problem of conflict as long as the keyword and the variable name can be distinguished. For example, having integer as a variable name is perfectly valid even though it contains the keyword int Elementary Programming with C/Session 2/ 8 of 22

Data Types-1 Different types of data are stored in variables. Some examples are: Numbers Whole numbers. For example, 10 or 178993455 Real numbers. For example, 15.22 or 15463452.25 Positive numbers Negative numbers Names. For example, John Logical values. For example, Y or N Elementary Programming with C/Session 2/ 9 of 22

Datatype variableName Data Types-2 A data type describes the kind of data that will fit into a variable The name of the variable is preceded with the data type For example, the data type int would precede the name varName Datatype variableName int varName Elementary Programming with C/Session 2/ 10 of 22

The basic data types are int float char double void Elementary Programming with C/Session 2/ 11 of 22

Type int Stores numeric data int num; Cannot then store any other type of data like “Alan” or “abc” 16 bits (2 bytes) Integers in the range -32768 to 32767 Examples: 12322, 0, -232 Elementary Programming with C/Session 2/ 12 of 22

Type float Stores values containing decimal places float num; Precision of upto 6 digits 32 bits (4 bytes) of memory Examples: 23.05, 56.5, 32 Elementary Programming with C/Session 2/ 13 of 22

Type double Stores values containing decimal places double num; Precision of upto 10 digits 64 bits (8 bytes) of memory Examples: ‘a’, ‘m’, ‘$’ ‘%’ , ‘1’, ’5’ Elementary Programming with C/Session 2/ 14 of 22

Type char Stores a single character of information char gender; gender='M'; 8 bits (1 byte) of memory Examples: ‘a’, ‘m’, ‘$’ ‘%’ , ‘1’, ’5’ Elementary Programming with C/Session 2/ 15 of 22

Type void Stores nothing Indicates the compiler that there is nothing to expect Elementary Programming with C/Session 2/ 16 of 22

Derived Data Types int unsigned int short int/double long Basic Data Modifiers Basic Data types Derived data type int unsigned int (Permits only positive numbers) unsigned int short short int (Occupies less memory space than int) int/double Long int /longdouble (Occupies more space than int/double) long Elementary Programming with C/Session 2/ 17 of 22

signed and unsigned Types unsigned type specifies that a variable can take only positive values unsigned int varNum; varNum=23123; varNum is allocated 2 bytes modifier may be used with the int and float data types unsigned int supports range from 0 to 65535 Elementary Programming with C/Session 2/ 18 of 22

long and short Types short int occupies 8 bits (1 byte) allows numbers in the range -128 to 127 long int occupies 32 bits (4 bytes) 2,147,483,647 and 2,147,483,647 long double occupies 128 bits (16 bytes) Elementary Programming with C/Session 2/ 19 of 22

Data Types and their range-1 Approximate Size in Bits Minimal Range char 8 -128 to 127 unsigned 0 to 255 signed char int 16 -32,768 to 32,767 unsigned int 0 to 65,535 signed int Same as int short int unsigned short int 0 to 65, 535 Elementary Programming with C/Session 2/ 20 of 22

Data Types and their range-2 Approximate Size in Bits Minimal Range signed short int 8 Same as short int long int 32 -2,147,483,647 to 2,147,483,647 signed long int 0 to 4,294,967,295 unsigned long int float Six digits of precision double 64 Ten digits of precision long double 128 Elementary Programming with C/Session 2/ 21 of 22

Sample Declaration main () { char abc; /*abc of type character */ int xyz; /*xyz of type integer */ float length; /*length of type float */ double area; /*area of type double */ long liteyrs; /*liteyrs of type long int */ short arm; /*arm of type short integer*/ } Elementary Programming with C/Session 2/ 22 of 22