Chapter2 Constants, Variables, and Data Types. 2.1 Introduction In this chapter, we will discuss –constants (integer, real, character, string, enum),symbolic.

Slides:



Advertisements
Similar presentations
Fundamentals of Computer and programming in C
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Data Types and Expressions
Types and Variables. Computer Programming 2 C++ in one page!
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
CMT Programming Software Applications
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
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.
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
Data Types.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Simple Data Type Representation and conversion of numbers
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.
C-Language Keywords(C99)
Constants in C A Presentation On Department of Computer & Information Technology, M.S.P.V.L. Polytechnic College, Pavoorchatram.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Lecture #5 Introduction to C++
Primitive Variables.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Variables Symbol representing a place to store information
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Objects Variables and Constants. Our Scuba Problem #include // cin, cout, > using namespace std; int main() { const double FEET_PER_ATM = 33.0, LBS_PER_SQ_IN_PER_ATM.
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.
Characters and Strings
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 Objects Types, Variables, and Constants Chapter 3.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
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
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Chapter 2 Variables.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Wel come.
ITEC113 Algorithms and Programming Techniques
Fundamental Data Types
Chapter 2: Introduction to C++
CSE101-Lec#3 Components of C Identifiers and Keywords Data types.
INTRODUCTION c is a general purpose language which is very closely associated with UNIX for which it was developed in Bell Laboratories. Most of the programs.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
INPUT & OUTPUT scanf & printf.
C++ Basics.
Basics of ‘C’.
C AS A LANGUAGE Interface between computer & human being. ALPHABETS
CS111 Computer Programming
Chapter 2: Java Fundamentals
Variables in C Declaring , Naming, and Using Variables.
Fundamental Data Types
Module 2 Variables, Data Types and Arithmetic
Lexical Elements & Operators
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Variables and Constants
Presentation transcript:

Chapter2 Constants, Variables, and Data Types

2.1 Introduction In this chapter, we will discuss –constants (integer, real, character, string, enum),symbolic constants –Variables (name, declare, assign) –Fundamental data type ( int, float, double, char, void)

Objective To be able to distinguish the constants of different data type To be able to name, declare , initialize and assign values to variables To become familiar with fundamental data types.

2.1 Character set Letters –uppercase A … Z –lowercase a … z Digits :All decimal digits 0 … 9 Special characters –Table2.1 on page23 White spaces

2.3 C Tokens Keywords Identifiers Constants Strings Special symbols operators

2.4 Keywords and Identifiers Keywords –Table2.3 on page24 –Must in lowercase Identifiers –User-defined names –Consist of letters, underscore(_), and digits –First character must not be digit –Can ’ t use a keyword –Can ’ t contain white space

2.4 Keywords and Identifiers Which of the following are valid identifiers Maxfirst_namen1/n2 3rowdouble_34 Boy numgirl-num

2.7 Data Types Primary( fundamental) data types –Integer –Floating point ( float and double ) –Character –Void Fig.2.4 on page31

2.7 Data Types Integer –signed /unsigned int short int long int Floating point –float –double –long double Character –signed / unsigned void

2.7 Data Types Size and range of basic data types Table 2.7 on page31 Table 2.8 on page32

2.5 Constants Integer Real Character String

Integer constants Decimal integer –Consist of 0 through 9, +, - Octal integer –Consist of 0 through 7, with a leading 0 Hexadecimal integer –Consist of0 to 9, and a through f preceded by 0x or 0X

Integer constant The largest integer value is machine- dependent Qualifiers –U or u: unsigned integer –l or L: long integer –UL or ul: unsigned long integer –Short integer

short is no longer than int and that long is no shorter than int.

Integer constant Example2.1 Representation of integer constants on a 16-bit computer. #include main() { printf("Integer values\n\n"); printf("%d %d %d\n",32767, , ); printf("\n"); printf("Long integer values\n\n"); printf("%ld %ld %ld\n",32767L, L, L); }

Real constant (double) (1) decimal point –Consist of 0 through 9,.,+, - – ,.94, -.58, (2)Exponential notation mantissa e exponent –.56e3 2.3e-3-2.3E-3 –The exponent must be an integer number Suffixes f or F indicate a float constant L or l indicate a long double “ Default values of constants ” on page35

Floating-Point Round-off Errors Take a number, add 1 to it, and subtract the original number. What do you get? You get 1. A floating- point calculation, may give another answer:

Character constant A character enclosed within ‘ ’ –‘6’‘=‘‘;’‘ ‘–‘6’‘=‘‘;’‘ ‘ –Character constants have integer values, For example –‘ a ’ and 97 –‘ 0 ’ and 48 Backslash character constants –Table2.5 on page28 –‘ \ooo ’ and ‘ \xhh ’

String constants a sequence of characters enclosed in “ ”, for example –“ hello ” “ 34 ” “ +() ” “ x ” “ \n ” “ x ” and ’ x ’

2.6 variables A variable is data name that may be used to store a data value. –Variable names correspond to locations in the computer's memory –Every variable has a name, a type, a size and a value –Whenever a new value is placed into a variable, it replaces (and destroys) the previous value –Reading variables from memory does not change them

2.8 Declaration of variables The declaration of variables must be done before they are used. declaration form data-type v1,v2, …,vn; for example –int count; –float sum; –double ratio; –char ch;

Initialization of variables To initialize a variable means to assign it a starting, or initial, value. In C, this can be done as part of the declaration. char ch= ‘ ’ ; int cows = 32, int dogs, cats = 94;

2.10 Assignment statement values can be assigned to variables using the assignment operator = as variable_name=value;

User-defined type declaration the keyword “ typedef ” is used to rename an existing data type. typedef type identifier; for example –typedef int integer; –typedef float real; –real sum1, sum2; –integer count;

Enumerated Types You can use the enumerated type to declare symbolic names to represent integer constants. declarations: –enum spectrum {red, orange, yellow, green, blue, violet}; –enum spectrum color;