SlideShare a Scribd company logo
CONSTANTS, VARIABLES AND
DATA TYPES in C
Reference: Programming in C by Balaguruswamy
MRS. SOWMYA JYOTHI, SDMCBM, MANGALORE
Introduction:-
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. These instructions are formed using certain symbols and words according to some rigid
rules known as syntax rules (or grammar).
Character set:-
The characters that can be used to form words, numbers and expressions depend upon the computer on
which the program is run.
The character set in C Language can be grouped into the following categories:
1.Letters
2.Digits
3.SpecialCharacters
4. White Spaces
Letters Digits
Upper Case A to Z 0 to 9
Lower Case a to z .
C Character-Set Table
Constants Variables Datatypes by Mrs. Sowmya Jyothi
White Spaces
Blank Space
Horizontal Tab
Carriage return
New Line
Form feed
White Spaces are ignored by the compiler until they are a part of string constant. White Space may be
used to separate words, but are strictly prohibited while using between characters or keywords or
identifiers.
C Tokens:
In the passage of text, individual words and punctuation marks
are called tokens.
In C program, the smallest individual units are known as C
tokens.
C has six type of tokens.
Every C program is written using these tokens and the syntax of
the language
Constants Variables Datatypes by Mrs. Sowmya Jyothi
Keywords and Identifiers
All Keywords have fixed meanings and these meanings cannot
be changed.
There are 32 keywords.
Some compiler may use additional keywords that must be
identified from the C manual.
Keywords serve as basic building block for a program
statement.
All keyword must be written in lowercase.
auto else register union
break enum return unsigned
case extern short void
char float signed volatile
const for size of while
continue goto static .
default if struct .
do int switch .
double long typedef .
Identifier refers to the names of user-defined variables, array and
functions.
A variable should be essentially a sequence of letters and or digits and the
variable name should begin with a character. Both uppercase and
lowercase letters are permitted, although lowercase letters are commonly
used. The underscore character is also permitted in identifiers.
The identifiers must conform to the following rules.
1. First character must be an alphabet (or underscore)
2. Identifier names must consists of only letters, digits and underscore.
3. A identifier name should have less than 31 characters.
4. Any standard C language keyword cannot be used as a variable
name.
5. A identifier should not contain a space.
3. Constants
Constants in C refer to fixed values that do not change during
the execution of a program. C supports several types of
constants.
1. Integer Constants
2.Real Constants
3.Single Character Constants
4. String Constants
Constants Variables Datatypes by Mrs. Sowmya Jyothi
Integer Constants
An integer constant is a sequence of digits.
There are 3 types of integers namely decimal integer, octal integer
and hexadecimal integer.
Decimal Integers consists of a set of digits 0 to 9 preceded by an
optional + or - sign. Spaces, commas and non digit characters are not
permitted between digits. Examples for valid decimal integer
constants are
123 -31 0 562321 + 78
Embedded spaces, commas and non-digit characters are not
permitted between digits. Some examples for invalid integer
constants are
15 750 20,000 Rs. 1000
Octal Integers constant consists of any combination of digits from 0
through 7 with a 0 at the beginning. Some examples of octal integers
are
026 0 0347 0676
Hexadecimal integer constant is preceded by 0X or 0x, they
may contain alphabets from A to F or a to f.
The alphabets A to F refers to 10 to 15 in decimal digits.
Example of valid hexadecimal integers are
0X2 0X8C 0Xbcd 0x
Real Constants
Real constants are used to represent quantities that
are very continuously, such as distances, temperature
etc. These quantities are represented by numbers.
These numbers are shown in decimal notation, having
a whole number followed by a decimal point and the
fractional part.
Real Numbers can also be represented by exponential
or scientific notation. For example, the value 215.65 may
be written as 2.1565e2 in exponential notation.e2 means
multiply by 102
The general form for exponential notation is
mantissa e exponent.
The mantissa is either a real number expressed in decimal notation or an integer. The
exponent is an integer number with an optional plus or minus sign. The letter e separating the
mantissa and the exponent can be written.
Exponential notation is useful for representing numbers that are either very large or very small in
magnitude. Floating-point constants are normally represented as double-precision quantities. However,
the suffixes f or F may be used to force single-precision and l or L to extend double precision further in
either lowercase or uppercase.
Examples of legal floating-point constants are:- 0.65e4 1.2e-1
ASCII, abbreviation of American Standard Code For Information Interchange, a standard data-
transmission code that is used by smaller and less-powerful computers to represent both textual data
(letters, numbers, and punctuation marks) and noninput-device commands (control characters like
Carriage return, line feed, back space etc..).
It's a 7-bit character code where every single bit represents a unique character
Single Character Constants
A single character constants contains a single character enclosed within a pair
of single quote marks.
Example, ‘5’ ‘X’ ‘;’.
The character constant ‘5’ is not the same as the number 5.
All character constants have an equivalent integer value which are called ASCII
Values.
For example:- printf(“%d”, ‘a’);
Would print the number 97, the ASCII value of the letter a.
Similarly, the statement
printf(“%c”,’97’)
would output the letter ‘a’.
char data type is used to store ASCII characters in C.
String Constants
A string constant contains a string of
characters enclosed within a pair of double
quote marks.
Examples: “Hello !” “1987” “?....!”
Backslash character constants/Escape Sequences are
special characters used in output functions.
Although they contain two characters they represent only one
character.
These characters combinations are known as escape
sequences.
Constant Meaning
'a' Audible Alert
'b' Backspace
'f' Formfeed
'n' New Line
'r' Carriage
't' Horizontal tab
'v' Vertical Tab
''' Single Quote
'"' Double Quote
'?' Question Mark
'' Back Slash
'0' Null
Variables
A variable is a data name that may be used to store data
value.
A variable may take different value at different times during
execution. Some examples are: average, height,
class_strength, abc123.
Variable names may consist of letters, digits and the
underscore character, subject to the following conditions.
1. They must always begin with a letter, although some
systems permit underscore as the first character.
2. The length of a variable must not be more than 8 characters.
3. White space is not allowed and
4. A variable should not be a Keyword
5. It should not contain any special characters.
6. Uppercase and lowercase are significant. That is, the variable
Total is not the same as total or TOTAL.
Data Types
C language is rich in its data types.
Storage representations and machine instructions to handle constants differ from machine to
machine.
The variety of data types available allow the programmer to select the type appropriate to the needs
of the application as well as the machine.
It supports 3 classes of data types. They are:-
1. Primary data type
2. Derived data type
3. User-defined data type
Constants Variables Datatypes by Mrs. Sowmya Jyothi
1.Integer int
2.Character char
3.Floating Point float
4.Double precision floating point double
5.Void void
Primary data type
All C compilers support five fundamental data types.
DATA RANGE OF
char -128 to 127
Int -32768 to +32767
float 3.4 e-38 to 3.4 e+38
double 1.7 e-308 to 1.7
The size and range of each data type is given in the
table below
Integer Type :
Integer are whole numbers with a range of values
supported by a particular machine. If we use a 16 bit(2
bytes) word length, the size of the integer value is limited
to the range -32768 to +32767.
C has 3 classes of integer storage namely short int, int
and long int. All of these data types have signed and
unsigned forms.
Floating Point Types:
Floating point number represents a real number with 6 digits precision.
Floating point numbers are denoted by the keyword float.
When the accuracy of the floating point number is insufficient, we can use the double to define the
number.
The double is same as float but with longer precision.
A double data type number uses 64 bits giving a precision of 14 digits. To extend the precision
further we can use long double which consumes 80 bits of memory space.
Float(4 bytes) Double(8 bytes) Long Double(10 bytes)
void Type :
The void type has no values.
Using void data type, we can specify the type of a function.
The type of a function is said to be void when it does not return
any value to the calling function. Keyword used is void.
Character Type :
A single character can be defined as a defined as a character type
of data. Characters are usually stored in 8 bits of internal storage.
The qualifier signed or unsigned can be explicitly applied to char.
While unsigned characters have values between 0 and 255, signed
characters have values from –128 to 127.
Keyword used for character data type is char
Constants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya Jyothi
Declaration of Variables
Every variable used in the program should be declared
to the compiler. The declaration does two things.
1. Tells the compiler the variables name.
2. Specifies what type of data the variable will hold.
The declaration of variables must be done before they
are used in the program.
The general format of any declaration
datatype v1, v2, v3, ……….. vn;
Where v1, v2, v3 are variable names. Variables are separated by commas.
A declaration statement must end with a semicolon.
Example:
int sum;
int number, salary;
double average, mean;
User defined type declaration
In C language a user can define an identifier that represents an
existing data type.
The user defined data type identifier can later be used to declare
variables.
The general syntax is
typedef type identifier;
where type represents existing data type and
‘identifier’ refers to the ‘new’ name given to the data type.
Example:
typedef int units;
typedef float average;
Here salary symbolizes int and average symbolizes float. They can be later used to declare variables as
follows:
units dept1, dept2;
average section1, section2;
Therefore dept1 and dept2 are indirectly declared as integer datatype and section1 and section2 are
indirectly float data type.
The second type of user defined datatype is enumerated data
type which is defined as follows.
enum identifier {value1, value2 …. Value n};
The identifier is a user defined enumerated datatype which can be used to declare variables that have one of the values
enclosed within the braces.
After the definition we can declare variables to be of this ‘new’ type as below.
enum identifier V1, V2, V3, ……… Vn
The enumerated variables V1, V2, ….. Vn can have only one of the values
value1, value2 ….. value n
Example:
enum day {Monday, Tuesday, …. Sunday};
enum day week_st, week end;
week_st = Monday;
week_end = Friday;
Assignment Statement:-
Values can be assigned to variables using the assignment operator = as follows:-
variable_name= constant;
For example:
initial_value = 0;
final_value=100;
An assignment statement implies that the value of the variable on the left of the ‘equal sign’ is set
equal to the value of the quantity( or the expression) on the right.
year= year+1;
means that the ‘new value ‘ of year is equal to the ‘old value’ of year plus 1.
It is also possible to assign a value to a variable at the time the variable is declared.
data-type variable-name = constant;
For example:-
int final_value= 100;
char yes = ‘x’;
The process of giving initial values to variables is called initialization. C permits the initialization of more
than one variables in one statement using multiple assignment operators.
For example:-
p = q = s = 0;
x = y = z = MAX;
Reading data from keyboard:-
Another way of giving values to variables is to input data through
keyboard using the scanf function. It is a general input function
available in C.
The general format of scanf is as follows:-
scanf(“control string”, &variable1, &variable2,…….);
The control string contains the format of data being received.
The ampersand symbol & before each variable name is an operator
that specifies the variable name’s address.
We must always use this operator, otherwise unexpected results may
occur.
For example:- scanf(“%d”, &number);
When this statement is encountered by the computer, the execution stops and waits for the value of
the variable number to be types in.
Since the control string “%d” specifies that an integer value is to be read from the terminal, we
have to type in the value in integer form.
Once the number is typed in and the ‘Return’ key is pressed, the computer then proceeds to the next
statement.
Thus, the use of scanf provides an interactive feature and makes the program ‘user friendly’. The
value is assigned to the variable number.
Defining Symbolic Constants
Constants may appear repeatedly in a number of places in the program.
A symbolic constant value can be defined as a preprocessor statement and used in the program as any other
constant value.
The general form of a symbolic constant is
Syntax:-
#define symbolic_name valueofconstant
Valid examples of constant definitions are :
#define marks 100
#define total 50
#define pi 3.14159
These values may appear anywhere in the program, but must come before it is referenced in the program. It is
a standard practice to place them at the beginning of the program.
Declaring Variable as Constant
The values of some variable may be required to remain constant through-out the program. We can
do this by using the qualifier const at the time of initialization.
Example:
const int class_size = 40;
The const data type qualifier tells the compiler that the value of the int variable class_size may not
be modified in the program.

More Related Content

PPTX
Overview of C Mrs Sowmya Jyothi
PDF
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
PDF
Unit II chapter 4 Loops in C
PDF
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
PDF
Unit ii chapter 1 operator and expressions in c
PDF
Arrays in c unit iii chapter 1 mrs.sowmya jyothi
PDF
Unit ii chapter 2 Decision making and Branching in C
PDF
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
Overview of C Mrs Sowmya Jyothi
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
Unit II chapter 4 Loops in C
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
Unit ii chapter 1 operator and expressions in c
Arrays in c unit iii chapter 1 mrs.sowmya jyothi
Unit ii chapter 2 Decision making and Branching in C
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf

What's hot (20)

PDF
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
PDF
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
PPTX
Function C programming
PPT
Constants in C Programming
PPTX
Loops in c programming
PPTX
Looping statements in C
PPTX
Union in c language
PPTX
Character set of c
PPT
Variables in C Programming
PPTX
C tokens
PPTX
While , For , Do-While Loop
PPSX
Break and continue
PPTX
CONDITIONAL STATEMENT IN C LANGUAGE
PPSX
Function in c
PPTX
Storage classes in C
PPTX
Loops in c language
PPT
C program
PPTX
Functions in C.pptx
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
Function C programming
Constants in C Programming
Loops in c programming
Looping statements in C
Union in c language
Character set of c
Variables in C Programming
C tokens
While , For , Do-While Loop
Break and continue
CONDITIONAL STATEMENT IN C LANGUAGE
Function in c
Storage classes in C
Loops in c language
C program
Functions in C.pptx
Ad

Similar to Constants Variables Datatypes by Mrs. Sowmya Jyothi (20)

PPTX
Introduction to C language programming.pptx
PPTX
Data Types and Variables In C Programming
PPTX
Constant, variables, data types
PPTX
C Programming Lecture 3 - Elements of C.pptx
PPTX
Module 1:Introduction
PPT
CONSTANTS, VARIABLES & DATATYPES IN C
PPT
constants, variables and datatypes in C
PPTX
FUNDAMENTAL OF C
DOCX
C programming tutorial
PPT
C presentation book
PPT
C the basic concepts
PDF
C Tutorial
PPTX
COM1407: Variables and Data Types
PPT
All C ppt.ppt
PPTX
Diploma ii cfpc u-2 datatypes and variables in c language
PPSX
Programming in c
PPTX
Btech i pic u-2 datatypes and variables in c language
PPTX
PPTX
datatypes and variables in c language
PPTX
Bsc cs i pic u-2 datatypes and variables in c language
Introduction to C language programming.pptx
Data Types and Variables In C Programming
Constant, variables, data types
C Programming Lecture 3 - Elements of C.pptx
Module 1:Introduction
CONSTANTS, VARIABLES & DATATYPES IN C
constants, variables and datatypes in C
FUNDAMENTAL OF C
C programming tutorial
C presentation book
C the basic concepts
C Tutorial
COM1407: Variables and Data Types
All C ppt.ppt
Diploma ii cfpc u-2 datatypes and variables in c language
Programming in c
Btech i pic u-2 datatypes and variables in c language
datatypes and variables in c language
Bsc cs i pic u-2 datatypes and variables in c language
Ad

Recently uploaded (20)

PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Trump Administration's workforce development strategy
PPTX
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Lesson notes of climatology university.
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Yogi Goddess Pres Conference Studio Updates
Microbial disease of the cardiovascular and lymphatic systems
Module 4: Burden of Disease Tutorial Slides S2 2025
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
LDMMIA Reiki Yoga Finals Review Spring Summer
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
2.FourierTransform-ShortQuestionswithAnswers.pdf
202450812 BayCHI UCSC-SV 20250812 v17.pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Microbial diseases, their pathogenesis and prophylaxis
Trump Administration's workforce development strategy
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
STATICS OF THE RIGID BODIES Hibbelers.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Lesson notes of climatology university.

Constants Variables Datatypes by Mrs. Sowmya Jyothi

  • 1. CONSTANTS, VARIABLES AND DATA TYPES in C Reference: Programming in C by Balaguruswamy MRS. SOWMYA JYOTHI, SDMCBM, MANGALORE
  • 2. Introduction:- 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. These instructions are formed using certain symbols and words according to some rigid rules known as syntax rules (or grammar).
  • 3. Character set:- The characters that can be used to form words, numbers and expressions depend upon the computer on which the program is run. The character set in C Language can be grouped into the following categories: 1.Letters 2.Digits 3.SpecialCharacters 4. White Spaces
  • 4. Letters Digits Upper Case A to Z 0 to 9 Lower Case a to z . C Character-Set Table
  • 6. White Spaces Blank Space Horizontal Tab Carriage return New Line Form feed White Spaces are ignored by the compiler until they are a part of string constant. White Space may be used to separate words, but are strictly prohibited while using between characters or keywords or identifiers.
  • 7. C Tokens: In the passage of text, individual words and punctuation marks are called tokens. In C program, the smallest individual units are known as C tokens. C has six type of tokens. Every C program is written using these tokens and the syntax of the language
  • 9. Keywords and Identifiers All Keywords have fixed meanings and these meanings cannot be changed. There are 32 keywords. Some compiler may use additional keywords that must be identified from the C manual. Keywords serve as basic building block for a program statement. All keyword must be written in lowercase.
  • 10. auto else register union break enum return unsigned case extern short void char float signed volatile const for size of while continue goto static . default if struct . do int switch . double long typedef .
  • 11. Identifier refers to the names of user-defined variables, array and functions. A variable should be essentially a sequence of letters and or digits and the variable name should begin with a character. Both uppercase and lowercase letters are permitted, although lowercase letters are commonly used. The underscore character is also permitted in identifiers. The identifiers must conform to the following rules. 1. First character must be an alphabet (or underscore) 2. Identifier names must consists of only letters, digits and underscore. 3. A identifier name should have less than 31 characters. 4. Any standard C language keyword cannot be used as a variable name. 5. A identifier should not contain a space.
  • 12. 3. Constants Constants in C refer to fixed values that do not change during the execution of a program. C supports several types of constants. 1. Integer Constants 2.Real Constants 3.Single Character Constants 4. String Constants
  • 14. Integer Constants An integer constant is a sequence of digits. There are 3 types of integers namely decimal integer, octal integer and hexadecimal integer. Decimal Integers consists of a set of digits 0 to 9 preceded by an optional + or - sign. Spaces, commas and non digit characters are not permitted between digits. Examples for valid decimal integer constants are 123 -31 0 562321 + 78 Embedded spaces, commas and non-digit characters are not permitted between digits. Some examples for invalid integer constants are 15 750 20,000 Rs. 1000
  • 15. Octal Integers constant consists of any combination of digits from 0 through 7 with a 0 at the beginning. Some examples of octal integers are 026 0 0347 0676 Hexadecimal integer constant is preceded by 0X or 0x, they may contain alphabets from A to F or a to f. The alphabets A to F refers to 10 to 15 in decimal digits. Example of valid hexadecimal integers are 0X2 0X8C 0Xbcd 0x
  • 16. Real Constants Real constants are used to represent quantities that are very continuously, such as distances, temperature etc. These quantities are represented by numbers. These numbers are shown in decimal notation, having a whole number followed by a decimal point and the fractional part. Real Numbers can also be represented by exponential or scientific notation. For example, the value 215.65 may be written as 2.1565e2 in exponential notation.e2 means multiply by 102
  • 17. The general form for exponential notation is mantissa e exponent. The mantissa is either a real number expressed in decimal notation or an integer. The exponent is an integer number with an optional plus or minus sign. The letter e separating the mantissa and the exponent can be written. Exponential notation is useful for representing numbers that are either very large or very small in magnitude. Floating-point constants are normally represented as double-precision quantities. However, the suffixes f or F may be used to force single-precision and l or L to extend double precision further in either lowercase or uppercase. Examples of legal floating-point constants are:- 0.65e4 1.2e-1
  • 18. ASCII, abbreviation of American Standard Code For Information Interchange, a standard data- transmission code that is used by smaller and less-powerful computers to represent both textual data (letters, numbers, and punctuation marks) and noninput-device commands (control characters like Carriage return, line feed, back space etc..). It's a 7-bit character code where every single bit represents a unique character
  • 19. Single Character Constants A single character constants contains a single character enclosed within a pair of single quote marks. Example, ‘5’ ‘X’ ‘;’. The character constant ‘5’ is not the same as the number 5. All character constants have an equivalent integer value which are called ASCII Values. For example:- printf(“%d”, ‘a’); Would print the number 97, the ASCII value of the letter a. Similarly, the statement printf(“%c”,’97’) would output the letter ‘a’. char data type is used to store ASCII characters in C.
  • 20. String Constants A string constant contains a string of characters enclosed within a pair of double quote marks. Examples: “Hello !” “1987” “?....!”
  • 21. Backslash character constants/Escape Sequences are special characters used in output functions. Although they contain two characters they represent only one character. These characters combinations are known as escape sequences. Constant Meaning 'a' Audible Alert 'b' Backspace 'f' Formfeed 'n' New Line 'r' Carriage 't' Horizontal tab 'v' Vertical Tab ''' Single Quote '"' Double Quote '?' Question Mark '' Back Slash '0' Null
  • 22. Variables A variable is a data name that may be used to store data value. A variable may take different value at different times during execution. Some examples are: average, height, class_strength, abc123. Variable names may consist of letters, digits and the underscore character, subject to the following conditions.
  • 23. 1. They must always begin with a letter, although some systems permit underscore as the first character. 2. The length of a variable must not be more than 8 characters. 3. White space is not allowed and 4. A variable should not be a Keyword 5. It should not contain any special characters. 6. Uppercase and lowercase are significant. That is, the variable Total is not the same as total or TOTAL.
  • 24. Data Types C language is rich in its data types. Storage representations and machine instructions to handle constants differ from machine to machine. The variety of data types available allow the programmer to select the type appropriate to the needs of the application as well as the machine. It supports 3 classes of data types. They are:- 1. Primary data type 2. Derived data type 3. User-defined data type
  • 26. 1.Integer int 2.Character char 3.Floating Point float 4.Double precision floating point double 5.Void void Primary data type All C compilers support five fundamental data types.
  • 27. DATA RANGE OF char -128 to 127 Int -32768 to +32767 float 3.4 e-38 to 3.4 e+38 double 1.7 e-308 to 1.7 The size and range of each data type is given in the table below
  • 28. Integer Type : Integer are whole numbers with a range of values supported by a particular machine. If we use a 16 bit(2 bytes) word length, the size of the integer value is limited to the range -32768 to +32767. C has 3 classes of integer storage namely short int, int and long int. All of these data types have signed and unsigned forms.
  • 29. Floating Point Types: Floating point number represents a real number with 6 digits precision. Floating point numbers are denoted by the keyword float. When the accuracy of the floating point number is insufficient, we can use the double to define the number. The double is same as float but with longer precision. A double data type number uses 64 bits giving a precision of 14 digits. To extend the precision further we can use long double which consumes 80 bits of memory space. Float(4 bytes) Double(8 bytes) Long Double(10 bytes)
  • 30. void Type : The void type has no values. Using void data type, we can specify the type of a function. The type of a function is said to be void when it does not return any value to the calling function. Keyword used is void. Character Type : A single character can be defined as a defined as a character type of data. Characters are usually stored in 8 bits of internal storage. The qualifier signed or unsigned can be explicitly applied to char. While unsigned characters have values between 0 and 255, signed characters have values from –128 to 127. Keyword used for character data type is char
  • 33. Declaration of Variables Every variable used in the program should be declared to the compiler. The declaration does two things. 1. Tells the compiler the variables name. 2. Specifies what type of data the variable will hold. The declaration of variables must be done before they are used in the program.
  • 34. The general format of any declaration datatype v1, v2, v3, ……….. vn; Where v1, v2, v3 are variable names. Variables are separated by commas. A declaration statement must end with a semicolon. Example: int sum; int number, salary; double average, mean;
  • 35. User defined type declaration In C language a user can define an identifier that represents an existing data type. The user defined data type identifier can later be used to declare variables. The general syntax is typedef type identifier; where type represents existing data type and ‘identifier’ refers to the ‘new’ name given to the data type.
  • 36. Example: typedef int units; typedef float average; Here salary symbolizes int and average symbolizes float. They can be later used to declare variables as follows: units dept1, dept2; average section1, section2; Therefore dept1 and dept2 are indirectly declared as integer datatype and section1 and section2 are indirectly float data type.
  • 37. The second type of user defined datatype is enumerated data type which is defined as follows. enum identifier {value1, value2 …. Value n}; The identifier is a user defined enumerated datatype which can be used to declare variables that have one of the values enclosed within the braces. After the definition we can declare variables to be of this ‘new’ type as below. enum identifier V1, V2, V3, ……… Vn The enumerated variables V1, V2, ….. Vn can have only one of the values value1, value2 ….. value n Example: enum day {Monday, Tuesday, …. Sunday}; enum day week_st, week end; week_st = Monday; week_end = Friday;
  • 38. Assignment Statement:- Values can be assigned to variables using the assignment operator = as follows:- variable_name= constant; For example: initial_value = 0; final_value=100; An assignment statement implies that the value of the variable on the left of the ‘equal sign’ is set equal to the value of the quantity( or the expression) on the right. year= year+1; means that the ‘new value ‘ of year is equal to the ‘old value’ of year plus 1.
  • 39. It is also possible to assign a value to a variable at the time the variable is declared. data-type variable-name = constant; For example:- int final_value= 100; char yes = ‘x’; The process of giving initial values to variables is called initialization. C permits the initialization of more than one variables in one statement using multiple assignment operators. For example:- p = q = s = 0; x = y = z = MAX;
  • 40. Reading data from keyboard:- Another way of giving values to variables is to input data through keyboard using the scanf function. It is a general input function available in C. The general format of scanf is as follows:- scanf(“control string”, &variable1, &variable2,…….); The control string contains the format of data being received. The ampersand symbol & before each variable name is an operator that specifies the variable name’s address. We must always use this operator, otherwise unexpected results may occur.
  • 41. For example:- scanf(“%d”, &number); When this statement is encountered by the computer, the execution stops and waits for the value of the variable number to be types in. Since the control string “%d” specifies that an integer value is to be read from the terminal, we have to type in the value in integer form. Once the number is typed in and the ‘Return’ key is pressed, the computer then proceeds to the next statement. Thus, the use of scanf provides an interactive feature and makes the program ‘user friendly’. The value is assigned to the variable number.
  • 42. Defining Symbolic Constants Constants may appear repeatedly in a number of places in the program. A symbolic constant value can be defined as a preprocessor statement and used in the program as any other constant value. The general form of a symbolic constant is Syntax:- #define symbolic_name valueofconstant Valid examples of constant definitions are : #define marks 100 #define total 50 #define pi 3.14159 These values may appear anywhere in the program, but must come before it is referenced in the program. It is a standard practice to place them at the beginning of the program.
  • 43. Declaring Variable as Constant The values of some variable may be required to remain constant through-out the program. We can do this by using the qualifier const at the time of initialization. Example: const int class_size = 40; The const data type qualifier tells the compiler that the value of the int variable class_size may not be modified in the program.