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
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
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
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
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
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

What's hot

Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
Sowmya Jyothi
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C Programming
Qazi Shahzad Ali
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
tanmaymodi4
 
C++
C++C++
Data types in C language
Data types in C languageData types in C language
Data types in C language
kashyap399
 
Programming in C Presentation upto FILE
Programming in C Presentation upto FILEProgramming in C Presentation upto FILE
Programming in C Presentation upto FILE
Dipta Saha
 
Function in c
Function in cFunction in c
Function in c
Raj Tandukar
 
Structure in C language
Structure in C languageStructure in C language
Structure in C language
CGC Technical campus,Mohali
 
Control structures in c
Control structures in cControl structures in c
C functions
C functionsC functions
C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)
indrasir
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
MohammadSalman129
 
C basics
C   basicsC   basics
Functions in c
Functions in cFunctions in c
Functions in c
sunila tharagaturi
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
Kamal Acharya
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ Language
Way2itech
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
tanmaymodi4
 
C_Programming_Notes_ICE
C_Programming_Notes_ICEC_Programming_Notes_ICE
C_Programming_Notes_ICE
Gilbert NZABONITEGEKA
 
String in c
String in cString in c
String in c
Suneel Dogra
 

What's hot (20)

Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C Programming
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
 
C++
C++C++
C++
 
Data types in C language
Data types in C languageData types in C language
Data types in C language
 
Programming in C Presentation upto FILE
Programming in C Presentation upto FILEProgramming in C Presentation upto FILE
Programming in C Presentation upto FILE
 
Function in c
Function in cFunction in c
Function in c
 
Structure in C language
Structure in C languageStructure in C language
Structure in C language
 
Control structures in c
Control structures in cControl structures in c
Control structures in c
 
C functions
C functionsC functions
C functions
 
C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
 
C basics
C   basicsC   basics
C basics
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ Language
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
C_Programming_Notes_ICE
C_Programming_Notes_ICEC_Programming_Notes_ICE
C_Programming_Notes_ICE
 
String in c
String in cString in c
String in c
 

Similar to Constants Variables Datatypes by Mrs. Sowmya Jyothi

Data Types and Variables In C Programming
Data Types and Variables In C ProgrammingData Types and Variables In C Programming
Data Types and Variables In C Programming
Kamal Acharya
 
C programming tutorial
C programming tutorialC programming tutorial
C programming tutorial
Mohit Saini
 
Constant, variables, data types
Constant, variables, data typesConstant, variables, data types
Constant, variables, data types
Pratik Devmurari
 
Module 1:Introduction
Module 1:IntroductionModule 1:Introduction
Module 1:Introduction
nikshaikh786
 
Bsc cs i pic u-2 datatypes and variables in c language
Bsc cs i pic u-2 datatypes and variables in c languageBsc cs i pic u-2 datatypes and variables in c language
Bsc cs i pic u-2 datatypes and variables in c language
Rai University
 
Btech i pic u-2 datatypes and variables in c language
Btech i pic u-2 datatypes and variables in c languageBtech i pic u-2 datatypes and variables in c language
Btech i pic u-2 datatypes and variables in c language
Rai University
 
Diploma ii cfpc u-2 datatypes and variables in c language
Diploma ii  cfpc u-2 datatypes and variables in c languageDiploma ii  cfpc u-2 datatypes and variables in c language
Diploma ii cfpc u-2 datatypes and variables in c language
Rai University
 
Mca i pic u-2 datatypes and variables in c language
Mca i pic u-2 datatypes and variables in c languageMca i pic u-2 datatypes and variables in c language
Mca i pic u-2 datatypes and variables in c language
Rai University
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c language
Rai University
 
Data type
Data typeData type
Data type
Isha Aggarwal
 
C presentation book
C presentation bookC presentation book
C presentation book
krunal1210
 
5-Lec - Datatypes.ppt
5-Lec - Datatypes.ppt5-Lec - Datatypes.ppt
5-Lec - Datatypes.ppt
AqeelAbbas94
 
Structure of c_program_to_input_output
Structure of c_program_to_input_outputStructure of c_program_to_input_output
Structure of c_program_to_input_output
Anil Dutt
 
CProgrammingTutorial
CProgrammingTutorialCProgrammingTutorial
CProgrammingTutorial
Muthuselvam RS
 
constants, variables and datatypes in C
constants, variables and datatypes in Cconstants, variables and datatypes in C
constants, variables and datatypes in C
Sahithi Naraparaju
 
CONSTANTS, VARIABLES & DATATYPES IN C
CONSTANTS, VARIABLES & DATATYPES IN CCONSTANTS, VARIABLES & DATATYPES IN C
CONSTANTS, VARIABLES & DATATYPES IN C
Sahithi Naraparaju
 
Constants variables data_types
Constants variables data_typesConstants variables data_types
Constants variables data_types
NAVEEN SHARMA'S CLASSROOM
 
Chapter 13.1.1
Chapter 13.1.1Chapter 13.1.1
Chapter 13.1.1
patcha535
 
PSPC--UNIT-2.pdf
PSPC--UNIT-2.pdfPSPC--UNIT-2.pdf
PSPC--UNIT-2.pdf
ArshiniGubbala3
 
Mql4 manual
Mql4 manualMql4 manual
Mql4 manual
naveendnk22
 

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

Data Types and Variables In C Programming
Data Types and Variables In C ProgrammingData Types and Variables In C Programming
Data Types and Variables In C Programming
 
C programming tutorial
C programming tutorialC programming tutorial
C programming tutorial
 
Constant, variables, data types
Constant, variables, data typesConstant, variables, data types
Constant, variables, data types
 
Module 1:Introduction
Module 1:IntroductionModule 1:Introduction
Module 1:Introduction
 
Bsc cs i pic u-2 datatypes and variables in c language
Bsc cs i pic u-2 datatypes and variables in c languageBsc cs i pic u-2 datatypes and variables in c language
Bsc cs i pic u-2 datatypes and variables in c language
 
Btech i pic u-2 datatypes and variables in c language
Btech i pic u-2 datatypes and variables in c languageBtech i pic u-2 datatypes and variables in c language
Btech i pic u-2 datatypes and variables in c language
 
Diploma ii cfpc u-2 datatypes and variables in c language
Diploma ii  cfpc u-2 datatypes and variables in c languageDiploma ii  cfpc u-2 datatypes and variables in c language
Diploma ii cfpc u-2 datatypes and variables in c language
 
Mca i pic u-2 datatypes and variables in c language
Mca i pic u-2 datatypes and variables in c languageMca i pic u-2 datatypes and variables in c language
Mca i pic u-2 datatypes and variables in c language
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c language
 
Data type
Data typeData type
Data type
 
C presentation book
C presentation bookC presentation book
C presentation book
 
5-Lec - Datatypes.ppt
5-Lec - Datatypes.ppt5-Lec - Datatypes.ppt
5-Lec - Datatypes.ppt
 
Structure of c_program_to_input_output
Structure of c_program_to_input_outputStructure of c_program_to_input_output
Structure of c_program_to_input_output
 
CProgrammingTutorial
CProgrammingTutorialCProgrammingTutorial
CProgrammingTutorial
 
constants, variables and datatypes in C
constants, variables and datatypes in Cconstants, variables and datatypes in C
constants, variables and datatypes in C
 
CONSTANTS, VARIABLES & DATATYPES IN C
CONSTANTS, VARIABLES & DATATYPES IN CCONSTANTS, VARIABLES & DATATYPES IN C
CONSTANTS, VARIABLES & DATATYPES IN C
 
Constants variables data_types
Constants variables data_typesConstants variables data_types
Constants variables data_types
 
Chapter 13.1.1
Chapter 13.1.1Chapter 13.1.1
Chapter 13.1.1
 
PSPC--UNIT-2.pdf
PSPC--UNIT-2.pdfPSPC--UNIT-2.pdf
PSPC--UNIT-2.pdf
 
Mql4 manual
Mql4 manualMql4 manual
Mql4 manual
 

Recently uploaded

The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 

Recently uploaded (20)

The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 

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
  • 5.
  • 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
  • 8.
  • 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
  • 13.
  • 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
  • 25.
  • 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
  • 31.
  • 32.
  • 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.