CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.

Slides:



Advertisements
Similar presentations
1 C++ Syntax and Semantics The Development Process.
Advertisements

Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
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
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
JavaScript, Third Edition
Data Types.
Objectives You should be able to describe: Data Types
A First Book of ANSI C Fourth Edition
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.
Variables and Data Types
C Tokens Identifiers Keywords Constants Operators Special symbols.
Lecture 2 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout Data types.
Constants in C A Presentation On Department of Computer & Information Technology, M.S.P.V.L. Polytechnic College, Pavoorchatram.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
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.
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
CPS120: Introduction to Computer Science
Lecture #5 Introduction to C++
Week 1 Algorithmization and Programming Languages.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Characters and tokens Characters are the basic building blocks in C program, equivalent to ‘letters’ in English language Includes every printable character.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
 Character set is a set of valid characters that a language can recognise.  A character represents any letter, digit or any other sign  Java uses the.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
What is C? C is a programming language. It was developed in 1972 USA. It was designed and written by a man named dennis ritchie. C is the base for all.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
CHAPTER # 2 Part 2 PROGRAMS AND DATA 1 st semster King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
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 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Constants, Variables and Data types in C The C character Set A character denotes any alphabet, digit or special symbol used to represent information.
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.
Ajmer Singh PGT(IP) Programming Fundamentals. Ajmer Singh PGT(IP) Java Character Set Character set is a set of valid characters that a language can recognize.
Chapter2 Constants, Variables, and Data Types. 2.1 Introduction In this chapter, we will discuss –constants (integer, real, character, string, enum),symbolic.
Variables Symbol representing a place to store information
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
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.
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.
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.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Chapter # 2 Part 2 Programs And data
Chapter 2 Variables.
Chapter 2: Introduction to C++
Java Variables and Types
BASIC ELEMENTS OF A COMPUTER PROGRAM
ITEC113 Algorithms and Programming Techniques
Chapter 2: Introduction to C++
Variables ,Data Types and Constants
IDENTIFIERS CSC 111.
2.1 Parts of a C++ Program.
Chapter 2 Variables.
C++ Data Types Data Type
Chapter # 2 Part 2 Programs And data
Chapter 2: Introduction to C++.
Chapter 2 Variables.
Lexical Elements & Operators
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Variables and Constants
Programming Fundamental-1
Presentation transcript:

CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types of constants: 1.Numeric constants 2.Character constants The const keyword is used to declare a constant, as shown below: int const a = 1; const int a =2; The keyword const can be declared before or after the data type.

NUMERIC CONSTANTS Numeric constants are of two types: i.Integer constants ii.Floating point constants

Integer Constants The whole numbers which have no fraction part (decimal point) or comma is called integer constants. An integer represents a value that is counted like the number of students in a class. Negative integer constant must be preceded by a negative sign but a plus sign is optional for non-negative integers.

Some examples of valid integer constants are: Following are invalid integer constants for the reason indicated: # (blank) 9 34, 56 In above example, character (#), blank space,(.), (,),(-+) are illegal characters and 0 cannot be the first digits of decimal integer constants.

Floating Point Constants Floating point constants are also called real constants. Floating point constants are used to represent values that are measured like the height of a person which might have a value of cm, as opposed to integer constants which are used to represent values that are counted.

A floating point constant consists of either a fraction form or the exponent or the both. It may have either sign (+,-). Some valid floating points are as given; 0.5, 11.0, , 66668e2, , The following are some invalid floating point constant; 85——————– Missing decimal point (.) -1/2——————- Illegal characters (/).59, 45.4—————Illegal character (.,,) and no digits to left of the decimal point. Some valid floating pint constants in the exponent forms are given below. 8.85e5——is equivalent to E2——–is equivalent to e-6——-is equivalent to

CHARACTER CONSTANTS: Character constants are enclosed within single quotes i.e. apostrophes mark (‘) e.g. ‘a’, ‘cd’, ‘%’ represent character constants. It usually includes: Digits 0 through 9 Uppercase letters A through Z Lowercase letters a through z Punctuation symbols such as semicolon (;) Comma (,) Period (.) Special symbols such as +,-, =,> etc.

#include int main() { int const RED=5, YELLOW=6, GREEN=4, BLUE=5; printf("RED = %d\n", RED); printf("YELLOW = %d\n", YELLOW); printf("GREEN = %d\n", GREEN); printf("BLUE = %d\n", BLUE); getch(); } This will produce following results RED = 5 YELLOW = 6 GREEN = 4 BLUE = 5

VARIABLES A variable is a name of data storage location in computer memory that is used to store a certain kind of data. Variable has given a name for reference. In other words, when a variable is used in a computer program, the computer associates it with a particular memory location.

NAMING VARIABLES The name of variable can be called identifier or variable name. It has to follow these rules: The name can contain letters, digits and the underscore but the first letter has to be a letter or the underscore. Be avoided underscore as the first letter because it can be clashed with standard system variables. The underscore may be used to improve readability of the variable name e.g. subject_name. There is no restriction on the length of a variable name. The length of name can be up to 247 characters long in C but 31 characters are usually sufficient.

C language has a very small set of keywords such as int, do, if, while etc. which are part of the programming language. Keywords cannot be used as a variable name. These keywords are also known as reserved words. All the reserved words of c programs must be written as lower case letters, e.g. the reserved word if should not be written as IF or If. Both the upper and lower case letters are allowed in naming variables. An uppercase letter is considered different from a lower case letter. For example, the variable AVG is different from Avg or avg. There are four basic types of variable in C; they are: char, int, double, and float.

INTEGER VARIABLES Declaring Integer variables: In C programming language, declaring a variable is also defining a variable. All the variables used in a C program must be declared. The int variable can be declared as follows: – int counter; The declaration consists of the data type, int, followed by the variable name, counter. If you have more than one variable of the same type, you can separate the variables names with commas as illustrated below: – int counter, sum, age;

The variable declaration always ends with a semicolon (;). Declaring a variable tells the computer the name of the variable and its type. This enables the compiler to recognize the valid variable names in your program. Initialising Variables: You can also initialise a variable when you declare it, for example: – int x=10;

Type Qualifiers Type qualifiers refer to the number of bytes used for storing the integer on a particular computer. Declaration of an integer variable can be preceded by the type qualifiers short, long, unsigned or signed. Some examples are: short int num; long int sum,avg; unsigned int count; short unsigned int count;

To help promote safety in programs, values can be marked as being constant with the const type qualifier. Compilers must diagnose, usually with an error, attempts to modify such variables. const variables must be initialised at the point of declaration. int const black = 12; const int black = 12; The const type qualifier declares an object to be no modifiable.

The number of bytes set aside by the compiler for the type qualifiers are as given below: Variable DeclarationNo. of BytesRange int2-32,768 to +32,767 long int4-2,147,483,648 to -2,147,483,647 unsigned int20 to 65,535

Unsigned integers can only take non-negative values (positive or zero), while signed integers (default) can take both positive and negative values. The advantage of unsigned integers is to allow a greater range of positive values (e.g. 0 to ), whereas signed integers allow only up to half the same number as positive integers and the other half as negative integers (e.g. −32768 to ).

When declaring a short int, long int, it is permissible to omit the int. The following two declarations are equivalent. – long int brown; – long brown;

FLOATING POINT VARIABLES Floating point variables are used for floating point numbers. Floating point variables are stored in memory in two parts. The first part is the mantissa and the second part is the exponent. The mantissa is the value of the number and the exponent is the power to which it is raised. Some examples of floating point variable declaration statement are: Float height; Double float a,b,total; Long double float size,x,y;

The number of bytes set aside by the compiler for the above floating point qualifiers are as given below: (consult from the book) Variable Declaration No. of BytesRange float4 to (with 6 to 7 digits of precision) double float4 to (with 16 digits of precision) long double float2 to (with twice the precision of double)

Here, precision means the number of digits after the decimal point. Note that the word float may be omitted when preceded by a double, long double type qualifier in the declaration statements. The following two declarations are equivalent. – long double float f; – long double f;

There is another method of representing floating point numbers known as exponential notation. For example, the number 23,688 would be represented as e4. Here, is the value of the number (mantissa) and 4 is the exponent, the power to 10 to which the number will be raised. The exponent can also be negative. For example, the number is represented as 5.672e- 4 in exponential notation. The exponential form transfers the number one digit to the left of decimal point, followed by e and the appropriate power of 10.

In C, a floating point number of type float is stored in four bytes. Three bytes for the mantissa and one byte for the exponent and provides 6 or 7 digits of precision. Consider the C program to calculate the area of rectangle along with description at page 19 of the course text book.

CHARACTER VARIABLES Character variables are used to store character constants. Examples of declaration of character variables are: Char letter, c, answer; The compiler set aside one byte of memory for storing a character in a character variable. A character variable can only store one character.