0% found this document useful (0 votes)
9 views

Chapter 2

Chapter 2 discusses constants, variables, and data types in programming, specifically focusing on the C language. It covers the classification of C tokens, keywords, identifiers, constants (including integer, real, single character, and string constants), and the various data types supported by C. Additionally, it explains variable declaration, assignment, and the use of symbolic constants to enhance code readability and maintainability.

Uploaded by

czoremsanga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Chapter 2

Chapter 2 discusses constants, variables, and data types in programming, specifically focusing on the C language. It covers the classification of C tokens, keywords, identifiers, constants (including integer, real, single character, and string constants), and the various data types supported by C. Additionally, it explains variable declaration, assignment, and the use of symbolic constants to enhance code readability and maintainability.

Uploaded by

czoremsanga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

Chapter-2

Constants, Variables, and


Data Types

Copyright © 2016 McGraw Hill Education, All Rights Reserved.

PROPRIETARY MATERIAL © 2016 The McGraw Hill Education, Inc. All rights reserved. No part of this PowerPoint slide may be displayed, reproduced
or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and
educators permitted by McGraw Hill for their individual course preparation. If you are a student using this PowerPoint slide, you are using it without
permission.
• A programming language is designed to help process certain
kinds of data consisting of numbers, characters and strings
and to provide useful output known as information.
• The task of processing of data is accomplished by executing a
sequence of precise instructions called a program.
• The instructions are formed using certain symbols and words
according to some rigid rules known as syntax rules (or
grammar).
C TOKENS
• In a passage of text, individual words and punctuation marks are called
tokens.
• Similarly in a C program, the smallest individual units are known as C
tokens.

Fig. 2.1 C tokens and examples


KEYWORDS
• Every C word is classified as either a keyword or identifier.
• All keywords have fixed meanings and these meanings cannot be changed.
• Keywords served as basic building blocks for program statement.
• All keywords must be written in lowercase.

IDENTIFIERS
• Identifiers refer to the name of variables, functions and arrays.
• These are user-defined names and consists of a sequence of letters and digits.
• Rules for identifiers:-
1) First alphabet must be an alphabet (or underscore)
2) Must consists of only letters, digits or underscore.
3) Only first 31 characters are significant.
4) Cannot use a keyword.
5) Must not contain white space.
CONSTANTS
• Constants in C refer to fixed values that do not change during the
execution of a program.

Fig. 2.2 Basic types of C constants


Integer Constants
• Integer Constants refers to a sequence of digits.
• Three type- decimal integer, octal integer and hexadecimal integer
• Decimal integer- 0 through 9 (123, -321, 0, 654321, +78), Embedded
spaces, commas and non-digit characters not permitted between digits.
• Octal integer- 0 through 7 with leading 0 (037, 0, 0435)
• Hexadecimal integer- 0 through F preceded by0x or 0X (0X2, 0x9F,
0xbcd)
Real Constants
• Numbers containing fractional parts (17.567, 0.0083, -0.75, +247.0) are
called real or floating point constants.
• May also be expressed in exponential (or scientific) notation. (e.g. 215.65
may be expressed as 2.1565e2 in exponential notation.
• e2 means multiply by 102.
Mantissa e exponent
• Mantissa- either real number expressed in decimal notation or integer.
• Exponent- integer number with optional plus or minus.
• e – either uppercase or lowercase.
Single Character Constants
• A single character constant (or simply character constant) contains a single
character enclosed within a pair of single quote marks.
‘5’ ‘X’ ‘;’ ‘ ’
• 5 is not equal to ‘5’
• Character constants have integer values known as ASCII values.
• e.g. ASCII value of ‘a’ is 97.
String Constant
• A string constant is a sequence of characters enclosed in double quotes.
• The characters may be letters, numbers, special characters and blank
space.
e.g. “Hello!” “WELL DONE” “?....!” “5+3” “X”
• ‘X’ is not equal to “X”
• String character constant does not have equivalent integer value.
Backslash Character Constants
(Also known as escape sequence)
Variables
• A variable is a data name that may be used to store a data value.
• A variable may take different values at different times during execution.
Data Types
• A particular kind of data item, as defined by the values it can take, the
programming language used, or the operations that can be performed on it.
• A data type is a classification of data which tells the compiler or interpreter
how the programmer intends to use the data.
• A Data type provides a set of values from which an expression (i.e. variable,
function ...) may take its values.
• The type defines the operations that can be done on the data, the meaning
of the data, and the way values of that type can be stored.
• ANSI C supports 3 class of data types:
• Primary (or fundamental) data types
• Derived data types
• User-defined data types
Fig. 2.4 Primary data types in C
Integer Types
• Integers are whole numbers with a range of values supported by a particular
machine.
• Generally, integers occupy one word of storage (one word defer on 16 bits
and 32 bits machine).
• Three class of integer storage:-

Fig. 2.5 Integer types


Fig. 2.3 Representation of integer constants on 16-bit machine
Floating Point Types
• Floating point (or real) numbers are stored in 32 bits, with 6 digits of
precision.
• Defined in C by the keyword float.
• When accuracy not sufficient double or long double can be used.
• A double data type number used 64 bits giving a precision of 14 digits.

Fig. 2.6 Floating-point types


Void Types
• The void type has no values.
• Used to specify the type of functions that does not return any value to the
calling function.
• It can also play the role of a generic type (i.e. it can represent any of the other
standard types)
• E.g. void main()
Character Types
• A single character can be defined as a character (char) type data.
• Usually stored in 8 bits (one byte) of internal storage.
• The qualifier signed and unsigned may be explicitly applied to char.
• Signed char : -128 to 127
• Unsigned char : 0 to 255
Declaration of Variables
• Variables must be declared before using them in the program.
• Declaration-
• It tells the compiler what the variable name is.
• It specifies what type of data the variable will hold.
• A variable can be used to store a value of any data type.
• Syntax of variable declaration-
• Data-type v1, v2,…vn;
• v1, v2, vn are name of variables.
• Declaration statement must end with semicolon.
• E.g.
• int count; int number, total; double ratio;
Fig. 2.7 Declaration of variables
To Remember
• When an adjective (qualifier) short, long, or unsigned is used without a
basic data type specifier, C compiler treat the data type as an int.
• For unsigned data type unsigned qualifier must be used.
• In
Assigning values to Variables
value = amount + inrate * amount;
while (year <= PERIOD){
…….
…..
year = year + 1;
}

• Assignment statement: variable_name = constant;


Examples:-
initial_value = 0;
balance = 75.84;
or
initial_value = 0; balance = 75.84;

• Value of variable on the left of equal sign is set equal to the value of
quantity (or expression) on the right.
e.g. year = year +1;
• During assignment operation, C converts the type of value on RHS to the
type on the LHS.

• It is also possible to assign value to a variable at the time the variable is


declared.
data-type variable_name = constant;
e.g: int final_value = 100;
char balance = 75.84;

• The process of giving initial values to variables is called initialization.

• C permits initialization of more than one variables in one statement:


p = q = s = 0;
Fig. 2.8 Examples of assignments
Reading data from Keyboard
• Input data from keyboard can be done using scanf() function. General format
is:
scanf (“control string”, &variable1, &variable2,…);
• The control string contains the format of data being executed.
• The ampersand symbol & before each variable name is an operator that
specifies the variable name’s address.
Fig. 2.9 Use of scanf function for interactive computing
Fig. 2.10 Interactive investment program
Defining Symbolic Constants
• Used of constants repeatedly in a number of places in a program couses
the problem of:-
• Modifiability
• Understandability
• We can assign symbolic names to a constant defined as follows:-
• #define symbolic-name value_of_constant
• e.g. #define STRENGTH 100
• #define PI 3.14159
• Symbolic names are sometimes called constant identifiers.
Rules for defining Symbolic Constants
• Symbolic names have the same form as variables names. (Written in
CAPITAL LETTERS).
• No blank space between # and define.
• # must be first character in the line.
• Blank space between #define, symbolic name and constant.
• #define statement must not end with semicolon.
• After definition, the symbolic name should not be assigned any other value
within the program by using assignment statement.
• Symbolic names are not declared for data types. Its data type depends on
the type of constant.
• #define statement may appear anywhere in the program but before it is
referenced in the program. Usually placed at the beginning.
Fig. 2.11 Average of N numbers
Fig. 2.12 Temperature conversion—fahrenheit-celsius

You might also like