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

CHAPTER4

Uploaded by

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

CHAPTER4

Uploaded by

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

CHAPTER 3 :CONSTANTS,

VARIABLES AND DATA TYPES


Introduction :-
• The Sequence of the Instruction written to perform specific task is called the
Program.
• This Instructions are formed using special symbols and words according to some
rigid rule is known as syntax rules.
• Every Instruction must be written according to the syntax rule of the languages.
• Like any other languages C languages has its vocabulary and grammar. They are
follow up for making the program.
In this Chapter we will discuss about various constants, variables and data types
are used in C Programming
Character set:

The characters that are used to form statements and expressions in a


programming language is known as character set. The characters set in
C are grouped as follows.
1. Letter- uppercase A-Z and lowercase a-z
2. Digits-0-9
3. Special characters (, > < ? ‘ “ ; : )
4. Whitespaces (blank space, new line, tab)
C TOKENS
• In a passage of text, individual words and punctuation marks are called tokens,
• In C program the smallest individual units are known as tokens
• C programs are written using these tokens and the syntax of the language . C
supports 6 types of tokens. They are
• Keywords
• Identifiers
• Constants
• Strings
• Special symbols
• Operators
Keywords and identifiers:
• Every C word is classified either as a keyword or an identifier
• All keywords have a fixed predefined meaning that cannot be
changed. Hence keywords are also known as reserve words.
• All keywords must be written in lowercase.
• C supports a set of 32keywords
Ex: int, float, double, extern, static, auto, continue, if, goto ,short, long
etc. are the keywords
Identifiers
Identifiers refer to the names of variables, functions and arrays
These are user defined names and consists of a sequence of letters and
digits, with a letter as a first character, both uppercase and lowercase
letters are permitted.
Rules for identifiers
1 First character must be an alphabet (or underscore)
2 Must consist 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 referred as a fixed value that don't change during the
execution of the program.
Integer Constants:
Integer constants refer to a sequence of digits preceded by an optional +/- sign.
E.g.+123,-567,
• Decimal integers consist of a set of digits 0-9.
• An octal integer consists of combination of digits from 0 to 7 with a leading 0.
E.g.032,056,0435.
• A hexadecimal integer consists of 0-9 digits and alphabets A through F to
represent numbers from 10-15. A hexadecimal integer is preceded by 0x or
0X. E.g. 0xF2, 0X35A,0x56
• Numbers containing fractional part are called real or floating point constants.
• These can be represented either in decimal notation or exponential notation.
• In decimal notation a whole number is followed by a decimal point and a
fractional part. E.g. 0.045,-0.25
• A real number can also be expressed in scientific or exponential notation as
mantissa exponent.
Exponential notation is a way of express a very large number or very small number
using a base number(mantissa).it’s commonly used and takes the form a*10^b.
• The mantissa could be either a real number expressed in decimal notation or an
integer.
• The exponent is an integer with an optional ^/-sign.
• E.g.0.65e4,1.25e-5,56.23E-1
Eg: number 3500 can be written as 3.5*10^3.
Character constants:
• Character constants are either single character constants or string
constants
• Single character constants consist of a single character in single
quotes.
E.g.‘a’,‘Q’,‘5’
• String constants are a group of characters enclosed in double quotes.
The characters may include letters, numbers or special characters.
E.g.“abc”,
“1956”,“Hello!”
Backslash character constants:
• C supports backslash character constants that are used in output function.
• A backslash character constant consists of a backslash followed by a character.
These character combinations are also known as escape sequences
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.
• The value of a variable changes during the execution of
program.
Rules for naming a variable:
• Variable names should begin with a letter.
• Variable names should not be keywords.
• Blank spaces are not allowed.
• Uppercase and lowercase letters are significant because C is a case
sensitive language.
• Length of a variable name should not exceed 8 characters
DECLARATION OF VARIABLE
• After designing suitable variable names, we must declare them to the
compiler. Declaration does two things.
1. It tells the compiler what the variable name is.
2. It specifies what type of data the variable will hold.
3. The declaration must be done before they are used in the program.
DATA TYPE VAR1,VAR 2,VAR3;
• Variables are separated by commas
• The declaration statement must end with a semicolon
Assigning values toVariables:
• Values are assigned to variables using the assignment Operator ‘=’.
Syntax: var_name=value;
E.g.: x=10;
INITIALIZATION
It is also possible to assign values to a variable during declaration. The
process of assigning initial values to variables is known as initialization.
Syntax: data_type var_name=value;
E.g.: int x=10;
char x=‘y’;
Declaring variable as a constant:
Difference between constant variable and variable
• The variable whose value does not change during the execution of
program is known as “constant variable.”

• on the other hand a variable may take different value at different


time. and changes it’s value dependent on the equation.
DEFINING SYMBOLIC CONSTANTS
• A symbolic constant is a name is used to represent some fixed value that
never changes through out the program.
• which substitutes either a numeric constant or a character or a string
constant.
• When the program is compiled, each occurrence of a symbolic constant is
replaced by its corresponding character sequence.
• Symbolic constants are usually defined at the beginning of a program.
• Symbolic constant can be defined using #define statement.
Syntax: #define symbolic-name value-of constant
Ex:-
• #define PI 3.142
• #define MAX 100
• #define CLASS “IBCA”
• Using #define preprocessor directive: -They are responsible for adding
header-files , libraries and modules before the primary function.

• Using a const keyword :To ensures that its value remains unchanged
in the program.
Syntax: const data_type var_name=value;
EX: const int x=40;
Following are the rules used to
define the symbolic constant:
• Symbolic name is similar to variable but it is not variable.
Symbolic name is reference or information. But variable name represents what
information the variable contains.
• No blank space is allow between ‘#’ and ‘define’ word.
• ‘#’ must be first in the line.
• A blank space is require between ‘#define’ and ‘symbolic name’ & between
‘symbolic name’ and ‘constant value’
• It is not statement so it does not end with semicolon
• You cannot put assignment operator ‘=’ between ‘symbolic name’ and constant
value.
• You can put anywhere in program but before it is used in the program.
Data Types:
• The 4 basic data types supported by C are
1. Fundamental / Primary Data type
2. User defined Data type
3. Derived data type
4. Empty dataset
MEMORY SIZE:
Bit= 0 or 1
1nibble=4bits
1byte=8bits
1KB (kilo byte)=1024bytes
1MB(mega byte)=1024KB
1GB(giga byte)=1024MB
1TB(tera byte)=1024GB
1PB(peta byte)=1024TB
1HB(Hexa byte)=1024PB
Primary Data Types:
All C compilers support this data type. Primary data types can be
classified as follows
1. Integer
2. Floating point
3. Character
Integer Data Type:
• Integers are whole numbers with a range of values. Integers occupy one word of
storage.
• In a 16-bit machine, the range of integers is -32768 to+32767
• In order to provide some control over a range of numbers and storage space, C
has 3 classes of storage for integers namely, short int, int and long int in both
signed and unsigned format
• Short int is used for small range of values and requires half the amount of
storage as a regular int number uses.
• Long int requires double the storage as an int value.
• In unsigned integers, all the bits in the storage are used to store the magnitude
and are always positive.
Floating point DataType:
• All floating point numbers require 32 bits with 6 digits of precision
• They are defined in C using the keyword float
• For higher precision, double data type can be used.
• A double type number uses 64 bits (8 bytes) giving a precision of 14
digits.
• Range of values for double type is 1.7E-308 to 1.7E+308
• To extend the precision further, we can use long double which uses 80
bits.
Character DataType:
• A single character can be defined as a character data type using the
keyword char
• Characters usually need 8 bits for storage
• The qualifier signed or unsigned can be explicitly applied to char.
You can explicitly specify signed char or unsigned char to avoid
ambiguity and ensure consistent behavior across different platforms.
• While unsigned characters have values between 0 and 255,
• signed characters have values from -128 to 127.

You might also like