SlideShare a Scribd company logo
C Programming - Data Types, Variables and Constants 1 Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
Programming in C: A Practical Approach Data Types, Variables and Constants Introduction C  is a: General-purpose Block structured Procedural Case sensitive Free flow Portable High level programming language developed by  Dennis Ritchie  at the  Bell Telephone Laboratories .  The selection of ‘C’ as a name of a programming language seems to be an odd choice but it was named C because it was evolved from earlier languages  BCPL  ( B asic  C ombined  P rogramming  L anguage) and  B .  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
Basic source character set includes: Letters: Uppercase Letters: A,B,C…,Z Lowercase Letters:  a, b, c…,z Digits: 0,1,2…9 Special Characters: ,.:;!@#$% ..etc. White Space Characters: Blank space Character Horizontal tab space character Carriage return New line character Form Feed Character Programming in C: A Practical Approach Data Types, Variables and Constants  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
Identifiers An identifier refers to the name of the object. The syntactic rules to write an identifier name  in C are: Can have letters, digits or underscores. First character must be a letter or an underscore but can’t be a digit. No special character except underscore can be used. Keywords or reserved words cannot form a valid identifier name. Maximum number of characters that form an identifier name is compiler dependent.  Programming in C: A Practical Approach Data Types, Variables and Constants  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
Keywords Keyword refers to a reserved word that has a particular meaning in programming language. It cannot be used as an identifier name in C. There are 32 keywords available in C language. Programming in C: A Practical Approach Data Types, Variables and Constants  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
Declaration Statement Every identifier (except label name) needs to be declared before it is used.  An identifier can be declared by making use of  declaration statement .  ROLE:  To introduce the name of an identifier along with its data type (or just type) to the compiler before its use.  The general form of declaration statement is:  [storage_class_specifier ][type_qualifier|type_modifier   ]type  identifier [=value [, ...]]; The terms enclosed within [] (i.e. square brackets) are optional and might not be present in a declaration statement. The type name and identifier name are mandatory parts of a declaration statement.  Programming in C: A Practical Approach Data Types, Variables and Constants  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
Examples of valid declaration in C: int variable;  ( type int and identifier name variable present) static int variable; ( Storage class specifier static, type int and  identifier name variable present) int a=20, b=10; ( type int, identifier name a and its initial value  20 present, another identifier name b  and its  initial value 10 present ) Programming in C: A Practical Approach Data Types, Variables and Constants  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
Declaration Statements:  2 Types Programming in C: A Practical Approach Data Types, Variables and Constants  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.   SHORTHAND DECLARATION STATEMENT LONGHAND DECLARATION STATEMENT Declaration statements in which more than one identifier is declared is known as  shorthand declaration statement .  e.g. int a=20, b=10;  The corresponding  longhand declaration statements  are: e.g. int a=20; int b=10; 2.  Can only be used to declare identifiers of the same type.  e.g. int a=10, float b=2.3; is an invalid statement.  2.  It can be used to declare identifiers of different types  e.g. int a=10;float b=2.3; are valid  statements.
Programming in C: A Practical Approach Data Types, Variables and Constants  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.   Data Types
Programming in C: A Practical Approach Data Types, Variables and Constants  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
Programming in C: A Practical Approach Data Types, Variables and Constants  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
Data Object, L-value And R-value An identifier is allocated some space in memory depending upon its data type and the working environment. This memory allocation gives rise to two important concepts known as L -value concept  and R -value concept .  DATA OBJECT  is a term that is used to specify the region of data storage that is used to hold values. Once an identifier is allocated memory space, it will be known as a data object.  Programming in C: A Practical Approach Data Types, Variables and Constants  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
L-value L-value  is a data object locator. It is an expression that locates an object. Variable is a sort of name given to the memory location (say2000). Variable here refers to L-value, an object locator. The term L-value can be further categorized as: Modifiable L-value :  A modifiable L-value is an expression that refers to an object that can be accessed and legally changed in the memory. Non-modifiable L-value : A non-modifiable L-value refers to an object that can be accessed but cannot be changed in the memory. ---> L  in  L-value  stands for  “left” , this means that L-value could legally stand on the left side of assignment operator. Programming in C: A Practical Approach Data Types, Variables and Constants  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
R-value R in  R-value  stands for  “right” or “read” , this means that if an identifier name appears on the right side of assignment operator it refers to R-value. Consider the expression:  variable= variable +20 variable on the left side of assignment operator refers to L-value.  variable  on the right side of assignment operator (in bold) refers to R-value. variable  appearing on the right side refers to 20. 20 is added to 20 and the value of expression comes out to be 40 (R-value). This outcome (40) is assigned to variable on the left side of assignment operator, which signifies L-value.  Programming in C: A Practical Approach Data Types, Variables and Constants  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
L-value variable locates the memory location where this value is to be placed i.e. at 2000 say.  Remember it as: L-value  refers to location value i.e. location of the object and  R-value  refers to read value i.e. value of the object. Programming in C: A Practical Approach Data Types, Variables and Constants  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
Variables A  variable  is an entity whose value can vary (i.e. change) during the execution of a program.  The value of a variable can be changed because it has a modifiable l-value. Since, it has modifiable l-value, it can be placed on the left side of assignment operator. Variables and Constants Programming in C: A Practical Approach Data Types, Variables and Constants  Constants A  constant  is an entity whose value remains same throughout the execution of a program. It cannot be placed on the left-side of assignment operator because it does not have a modifiable l-value. Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
Variables Variable can also be placed on the right side of assignment operator. Hence, it has r-value too. Thus, a variable has both l-value and r-value.  Constants It can only be placed on the right side of assignment operator. Thus, a constant has an r-value only Programming in C: A Practical Approach Data Types, Variables and Constants  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
Constants Programming in C: A Practical Approach Data Types, Variables and Constants  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
Literal   constant  or just  literal  denotes a fixed value, which may be an integer, floating point number, character or a string. The type of literal constant is determined by its value. Symbolic constants  are created with the help of define   preprocessor directive. For example: #define PI 3.14124 defines PI as a symbolic constant with value 3.14124. Each symbolic constant is replaced by its actual value during the preprocessing stage. Qualified constants  are created by using const qualifier. The following statement creates a qualified character constant named as:  const char a=’A’; Programming in C: A Practical Approach Data Types, Variables and Constants  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
Since, qualified constants are placed in the memory, they have l-value. But, as it is not possible to modify them, this means that they do not have modifiable l-value i.e. they have non-modifiable l-value. E.g. int a=10;  // It is possible to modify the value of a.  const int a=10;.  // It is possible read the value placed within the memory location, but it is not possible to modify  the value.   Programming in C: A Practical Approach Data Types, Variables and Constants  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
Integer Literal Constants Integer literal   constants  are integer values like -1, 2, 8 etc.  The following are the rules for writing integer literal constants: An integer literal constant must have at least one digit. It should not have any decimal point. It can be either positive or negative. If no sign precedes an integer literal constant, then it is assumed to be positive. No special characters (even underscore) and blank spaces are allowed within an integer literal constant. If an integer literal constant starts with 0, then it assumed to be in octal number system e.g. 023 is a valid integer literal constant, which means 23 in octal number system and is equivalent to 19 in decimal number system. Programming in C: A Practical Approach Data Types, Variables and Constants  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
If an integer literal constant starts with 0x or 0X, then it is assumed to be in hexadecimal number system e.g. 0x23 or 0X23 is a valid integer literal constant, which means 23 in hexadecimal number system and is equivalent to 35 in decimal number system. The size of integer literal constant can be modified by using a length modifier. The length modifier can be a suffix character l, L, u, U, f or F. If the integer literal constant is terminated with l or L then it is assumed to be long. If it is terminated with u or U then it is assumed to be an unsigned integer e.g. 23l is a long integer and 23u is an unsigned integer. The length modifier f or F can only be used with floating point literal constant and not with integer literal constant. Programming in C: A Practical Approach Data Types, Variables and Constants  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
Floating Point Literal Constants Floating point literal   constants  are values like -23.1, 12.8, -1.8e12 etc. Floating point literal constants can be written in:  fractional form  or in  exponential form .  Fractional Form The following are the rules for writing floating point literal constants in fractional form: A fractional floating point literal constant must have at least one digit. It should have a decimal point. It can be either positive or negative. If no sign precedes a floating point literal constant then it is assumed to be positive. Programming in C: A Practical Approach Data Types, Variables and Constants  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
No special characters (even underscore) and blank spaces are allowed within a floating point literal constant. A floating point literal constant by default is assumed to be of type double, e.g. the type of 23.45 is double. The size of floating point literal constant can be modified by using the length modifier f or F i.e. if 23.45 is written as 23.45f or 23.45F, then it is considered to be of type float instead of double. Following are valid floating point literal constants in fractional form: -2.5, 12.523, 2.5f, 12,5F Exponential Form The following are the rules for writing floating point literal constants in exponential form: Programming in C: A Practical Approach Data Types, Variables and Constants  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
A floating point literal constant in exponential form has two parts: the mantissa part and the exponent part. Both parts are separated by e or E. The mantissa can be either positive or negative. The default sign is positive. The mantissa part should have at least one digit.  The mantissa part can have a decimal point but it is not mandatory. The exponent part must have at least one digit. It can be either positive or negative. The default sign is positive. The exponent part cannot have a decimal point. No special characters (even underscore) and blank spaces are allowed within the mantissa part and the exponent part. Following are valid floating point literal constants in exponential form: –2.5E12, –2.5e–12, 2e10 (i.e. equivalent to 2×10 10 ) Programming in C: A Practical Approach Data Types, Variables and Constants  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
Character Literal Constant A  character literal constant  can have one or at most two characters enclosed within single quotes e.g. ‘A’, ‘a’, ‘\n’ etc. Character literal constants are classified as: Printable character literal constants Non-Printable character literal constants Printable Character Literal Constant: All characters of source character set except quotation mark, backslash and new line character when enclosed within single quotes form  printable character literal constant .  Following are the examples of printable character literal constants: ‘A’, ‘#’, ‘6’. Programming in C: A Practical Approach Data Types, Variables and Constants  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
Non-printable Character Literal Constant: Non-printable character literal constants  are represented with the help of  escape sequences . An escape sequence consists of a backward slash (i.e. \) followed by a character and both enclosed within single quotes. An escape sequence is treated as a single character. It can be used in a string like any other printable character.  Programming in C: A Practical Approach Data Types, Variables and Constants  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
List of Escape Sequences Programming in C: A Practical Approach Data Types, Variables and Constants  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.   ESCAPE SEQUENCES CHARACTER VALUE ACTION ON OUTPUT DEVICE ‘ \'’ Single quotation mark Prints ‘ ‘ \“’ Double quotation mark (“) Prints “ ‘ \?’ Question mark (?) Prints ? ‘ \\’ Backslash character (\) Prints \ ‘ \a’ Alert Alerts by generating beep ‘ \b’ Backspace Moves the cursor one position to the left of its current position. ‘ \f’ Form feed Moves the cursor to the beginning of next page. ‘ \n’ New line Moves the cursor to the beginning of the next line. ‘ \r’ Carriage return Moves the cursor to the beginning of the current line. ‘ \t’ Horizontal tab Moves the cursor to the next horizontal tab stop. ‘ \v’ Vertical tab Moves the cursor to the next vertical tab stop. ‘ \0’ Null character Prints nothing
Structure of a C Program Programming in C: A Practical Approach Data Types, Variables and Constants  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
C Program Programming in C: A Practical Approach Data Types, Variables and Constants  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.   LINE PROGRAM OUTPUT WINDOW 1 //Comment: First C program Hello Readers!! 2 #include<stdio.h> 3 main() 4 { 5 printf(“Hello Readers!!”); 6 }
Comments Single line comment  starts with two forward slashes (i.e. //) and is automatically terminated with the end of line.  Multi-line comment  starts with /* and terminates with */. Multi-line comment is used when multiple lines of text are to be commented out. Programming in C: A Practical Approach Data Types, Variables and Constants  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
Preprocessor Directives #include<stdio.h> is a preprocessor directive which includes standard input/output (i.e. stdio) header (.h) file. This file is to be included if standard input/output functions like printf or scanf are to be used in a program. The following points must be remembered while writing preprocessor directives: The preprocessor directive always starts with a pound symbol(i.e. #) The pound symbol # should be the first non-white space character in a line. Programming in C: A Practical Approach Data Types, Variables and Constants  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
The preprocessor directive is terminated with a newline character and not with a semicolon. Preprocessor directives are executed before the compiler compiles the source code. These will change the source code usually to suit the operating environment (#pragma directive) or to add the code (#include directive) that will be required by the calls to library functions. Programming in C: A Practical Approach Data Types, Variables and Constants  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
Executing a C Program Programming in C: A Practical Approach Data Types, Variables and Constants  Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.

More Related Content

What's hot (20)

PPT
Variables in C Programming
programming9
 
PPTX
C language unit-1
Malikireddy Bramhananda Reddy
 
PPTX
File in C language
Manash Kumar Mondal
 
PDF
Algorithm and c language
kamalbeydoun
 
PDF
Keywords, identifiers ,datatypes in C++
Ankur Pandey
 
PPTX
C if else
Ritwik Das
 
PPTX
DBMS: Types of keys
Bharati Ugale
 
PPTX
Object Oriented Programming Using C++
Muhammad Waqas
 
PPTX
CPU : Structures And Unions
Dhrumil Patel
 
PPTX
Programming in C Presentation upto FILE
Dipta Saha
 
PPTX
Data Type in C Programming
Qazi Shahzad Ali
 
PDF
Arrays in Java
Naz Abdalla
 
PPTX
Switch statement, break statement, go to statement
Raj Parekh
 
PPTX
C Programming Language Tutorial for beginners - JavaTpoint
JavaTpoint.Com
 
PPTX
C keywords and identifiers
Akhileshwar Reddy Ankireddy
 
PPTX
Dynamic memory allocation
Viji B
 
PPTX
Introduction to C Programming
Amr Ali (ISTQB CTAL Full, CSM, ITIL Foundation)
 
PPT
Function overloading(c++)
Ritika Sharma
 
PPTX
What is keyword in c programming
Rumman Ansari
 
PPT
Basic concept of OOP's
Prof. Dr. K. Adisesha
 
Variables in C Programming
programming9
 
File in C language
Manash Kumar Mondal
 
Algorithm and c language
kamalbeydoun
 
Keywords, identifiers ,datatypes in C++
Ankur Pandey
 
C if else
Ritwik Das
 
DBMS: Types of keys
Bharati Ugale
 
Object Oriented Programming Using C++
Muhammad Waqas
 
CPU : Structures And Unions
Dhrumil Patel
 
Programming in C Presentation upto FILE
Dipta Saha
 
Data Type in C Programming
Qazi Shahzad Ali
 
Arrays in Java
Naz Abdalla
 
Switch statement, break statement, go to statement
Raj Parekh
 
C Programming Language Tutorial for beginners - JavaTpoint
JavaTpoint.Com
 
C keywords and identifiers
Akhileshwar Reddy Ankireddy
 
Dynamic memory allocation
Viji B
 
Function overloading(c++)
Ritika Sharma
 
What is keyword in c programming
Rumman Ansari
 
Basic concept of OOP's
Prof. Dr. K. Adisesha
 

Viewers also liked (20)

PPT
constants, variables and datatypes in C
Sahithi Naraparaju
 
PPTX
Constants and variables in c programming
Chitrank Dixit
 
PPSX
INTRODUCTION TO C PROGRAMMING
Abhishek Dwivedi
 
PPTX
Variable, constant, operators and control statement
Eyelean xilef
 
PPTX
Data types
Zahid Hussain
 
PDF
Constants, Variables and Data Types in Java
Abhilash Nair
 
PPTX
Java basics variables
JoeReddieMedia
 
PDF
Data struture lab
krishnamurthy Murthy.Tt
 
PDF
Using Variables in Programming
flippanthorse6864
 
PDF
Python Programming: Variables
Leena Levashvili
 
PPT
02 functions, variables, basic input and output of c++
Manzoor ALam
 
PDF
ITFT-Constants, variables and data types in java
Atul Sehdev
 
PDF
C++ L01-Variables
Mohammad Shaker
 
PPTX
Programming Variables
Andrew Willetts
 
PDF
PHP Basic & Variables
M.Zalmai Rahmani
 
PPSX
Data type
Frijo Francis
 
PPTX
Unit 1 introduction to visual basic programming
Abha Damani
 
PDF
Writing algorithms
Krishna Chaytaniah
 
PPT
C ppt
jasmeen kr
 
PPT
Basic variables ppt
Shaker Middle School
 
constants, variables and datatypes in C
Sahithi Naraparaju
 
Constants and variables in c programming
Chitrank Dixit
 
INTRODUCTION TO C PROGRAMMING
Abhishek Dwivedi
 
Variable, constant, operators and control statement
Eyelean xilef
 
Data types
Zahid Hussain
 
Constants, Variables and Data Types in Java
Abhilash Nair
 
Java basics variables
JoeReddieMedia
 
Data struture lab
krishnamurthy Murthy.Tt
 
Using Variables in Programming
flippanthorse6864
 
Python Programming: Variables
Leena Levashvili
 
02 functions, variables, basic input and output of c++
Manzoor ALam
 
ITFT-Constants, variables and data types in java
Atul Sehdev
 
C++ L01-Variables
Mohammad Shaker
 
Programming Variables
Andrew Willetts
 
PHP Basic & Variables
M.Zalmai Rahmani
 
Data type
Frijo Francis
 
Unit 1 introduction to visual basic programming
Abha Damani
 
Writing algorithms
Krishna Chaytaniah
 
C ppt
jasmeen kr
 
Basic variables ppt
Shaker Middle School
 
Ad

Similar to Chapter1 c programming data types, variables and constants (20)

PPTX
Basic Of C language
PriyaPatil451572
 
PPT
Basics of C.ppt
Ashwini Rao
 
ODP
CProgrammingTutorial
Muthuselvam RS
 
PDF
Interview Questions For C Language
Rowank2
 
PPT
Basics of c
vinothini1996
 
PDF
C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...
Rowank2
 
PPTX
Interview Questions For C Language .pptx
Rowank2
 
PPTX
C basics
sridevi5983
 
PPTX
C basics
sridevi5983
 
PPTX
Compiler Construction.pptx
BilalImran17
 
PPTX
C programming
DipjualGiri1
 
PPTX
C programming Training in Ambala ! Batra Computer Centre
jatin batra
 
PPT
C Prog. - Operators and Expressions
vinay arora
 
PPTX
C
PRADEEPA R
 
PPTX
Programming Fundamentals
Hassan293
 
PPTX
Structured Languages
Mufaddal Nullwala
 
PPTX
presentation_data_types_and_operators_1513499834_241350.pptx
KrishanPalSingh39
 
PDF
C programming.pdf
JitendraYadav351971
 
PPTX
programming for problem solving in C and C++.pptx
BamaSivasubramanianP
 
PPTX
Introduction to C Programming: History, Applications, Variables, and Operator...
Mahmud Hasan Tanvir
 
Basic Of C language
PriyaPatil451572
 
Basics of C.ppt
Ashwini Rao
 
CProgrammingTutorial
Muthuselvam RS
 
Interview Questions For C Language
Rowank2
 
Basics of c
vinothini1996
 
C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...
Rowank2
 
Interview Questions For C Language .pptx
Rowank2
 
C basics
sridevi5983
 
C basics
sridevi5983
 
Compiler Construction.pptx
BilalImran17
 
C programming
DipjualGiri1
 
C programming Training in Ambala ! Batra Computer Centre
jatin batra
 
C Prog. - Operators and Expressions
vinay arora
 
Programming Fundamentals
Hassan293
 
Structured Languages
Mufaddal Nullwala
 
presentation_data_types_and_operators_1513499834_241350.pptx
KrishanPalSingh39
 
C programming.pdf
JitendraYadav351971
 
programming for problem solving in C and C++.pptx
BamaSivasubramanianP
 
Introduction to C Programming: History, Applications, Variables, and Operator...
Mahmud Hasan Tanvir
 
Ad

More from vinay arora (20)

PDF
Search engine and web crawler
vinay arora
 
DOCX
Use case diagram (airport)
vinay arora
 
DOCX
Use case diagram
vinay arora
 
DOCX
Lab exercise questions (AD & CD)
vinay arora
 
PDF
SEM - UML (1st case study)
vinay arora
 
PPT
6 java - loop
vinay arora
 
PPT
4 java - decision
vinay arora
 
PPT
3 java - variable type
vinay arora
 
PPT
2 java - operators
vinay arora
 
PPT
1 java - data type
vinay arora
 
PPT
Uta005 lecture3
vinay arora
 
PPT
Uta005 lecture1
vinay arora
 
PPT
Uta005 lecture2
vinay arora
 
PPT
Security & Protection
vinay arora
 
PPT
Process Synchronization
vinay arora
 
PDF
CG - Output Primitives
vinay arora
 
PDF
CG - Display Devices
vinay arora
 
PDF
CG - Input Output Devices
vinay arora
 
PDF
CG - Introduction to Computer Graphics
vinay arora
 
PDF
C Prog. - Strings (Updated)
vinay arora
 
Search engine and web crawler
vinay arora
 
Use case diagram (airport)
vinay arora
 
Use case diagram
vinay arora
 
Lab exercise questions (AD & CD)
vinay arora
 
SEM - UML (1st case study)
vinay arora
 
6 java - loop
vinay arora
 
4 java - decision
vinay arora
 
3 java - variable type
vinay arora
 
2 java - operators
vinay arora
 
1 java - data type
vinay arora
 
Uta005 lecture3
vinay arora
 
Uta005 lecture1
vinay arora
 
Uta005 lecture2
vinay arora
 
Security & Protection
vinay arora
 
Process Synchronization
vinay arora
 
CG - Output Primitives
vinay arora
 
CG - Display Devices
vinay arora
 
CG - Input Output Devices
vinay arora
 
CG - Introduction to Computer Graphics
vinay arora
 
C Prog. - Strings (Updated)
vinay arora
 

Recently uploaded (20)

PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
community health nursing question paper 2.pdf
Prince kumar
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 

Chapter1 c programming data types, variables and constants

  • 1. C Programming - Data Types, Variables and Constants 1 Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 2. Programming in C: A Practical Approach Data Types, Variables and Constants Introduction C is a: General-purpose Block structured Procedural Case sensitive Free flow Portable High level programming language developed by Dennis Ritchie at the Bell Telephone Laboratories . The selection of ‘C’ as a name of a programming language seems to be an odd choice but it was named C because it was evolved from earlier languages BCPL ( B asic C ombined P rogramming L anguage) and B . Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 3. Basic source character set includes: Letters: Uppercase Letters: A,B,C…,Z Lowercase Letters: a, b, c…,z Digits: 0,1,2…9 Special Characters: ,.:;!@#$% ..etc. White Space Characters: Blank space Character Horizontal tab space character Carriage return New line character Form Feed Character Programming in C: A Practical Approach Data Types, Variables and Constants Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 4. Identifiers An identifier refers to the name of the object. The syntactic rules to write an identifier name in C are: Can have letters, digits or underscores. First character must be a letter or an underscore but can’t be a digit. No special character except underscore can be used. Keywords or reserved words cannot form a valid identifier name. Maximum number of characters that form an identifier name is compiler dependent. Programming in C: A Practical Approach Data Types, Variables and Constants Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 5. Keywords Keyword refers to a reserved word that has a particular meaning in programming language. It cannot be used as an identifier name in C. There are 32 keywords available in C language. Programming in C: A Practical Approach Data Types, Variables and Constants Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 6. Declaration Statement Every identifier (except label name) needs to be declared before it is used. An identifier can be declared by making use of declaration statement . ROLE: To introduce the name of an identifier along with its data type (or just type) to the compiler before its use. The general form of declaration statement is:  [storage_class_specifier ][type_qualifier|type_modifier ]type identifier [=value [, ...]]; The terms enclosed within [] (i.e. square brackets) are optional and might not be present in a declaration statement. The type name and identifier name are mandatory parts of a declaration statement. Programming in C: A Practical Approach Data Types, Variables and Constants Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 7. Examples of valid declaration in C: int variable; ( type int and identifier name variable present) static int variable; ( Storage class specifier static, type int and identifier name variable present) int a=20, b=10; ( type int, identifier name a and its initial value 20 present, another identifier name b and its initial value 10 present ) Programming in C: A Practical Approach Data Types, Variables and Constants Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 8. Declaration Statements: 2 Types Programming in C: A Practical Approach Data Types, Variables and Constants Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd. SHORTHAND DECLARATION STATEMENT LONGHAND DECLARATION STATEMENT Declaration statements in which more than one identifier is declared is known as shorthand declaration statement . e.g. int a=20, b=10; The corresponding longhand declaration statements are: e.g. int a=20; int b=10; 2. Can only be used to declare identifiers of the same type. e.g. int a=10, float b=2.3; is an invalid statement. 2. It can be used to declare identifiers of different types e.g. int a=10;float b=2.3; are valid statements.
  • 9. Programming in C: A Practical Approach Data Types, Variables and Constants Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd. Data Types
  • 10. Programming in C: A Practical Approach Data Types, Variables and Constants Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 11. Programming in C: A Practical Approach Data Types, Variables and Constants Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 12. Data Object, L-value And R-value An identifier is allocated some space in memory depending upon its data type and the working environment. This memory allocation gives rise to two important concepts known as L -value concept and R -value concept . DATA OBJECT is a term that is used to specify the region of data storage that is used to hold values. Once an identifier is allocated memory space, it will be known as a data object. Programming in C: A Practical Approach Data Types, Variables and Constants Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 13. L-value L-value is a data object locator. It is an expression that locates an object. Variable is a sort of name given to the memory location (say2000). Variable here refers to L-value, an object locator. The term L-value can be further categorized as: Modifiable L-value : A modifiable L-value is an expression that refers to an object that can be accessed and legally changed in the memory. Non-modifiable L-value : A non-modifiable L-value refers to an object that can be accessed but cannot be changed in the memory. ---> L in L-value stands for “left” , this means that L-value could legally stand on the left side of assignment operator. Programming in C: A Practical Approach Data Types, Variables and Constants Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 14. R-value R in R-value stands for “right” or “read” , this means that if an identifier name appears on the right side of assignment operator it refers to R-value. Consider the expression: variable= variable +20 variable on the left side of assignment operator refers to L-value. variable on the right side of assignment operator (in bold) refers to R-value. variable appearing on the right side refers to 20. 20 is added to 20 and the value of expression comes out to be 40 (R-value). This outcome (40) is assigned to variable on the left side of assignment operator, which signifies L-value. Programming in C: A Practical Approach Data Types, Variables and Constants Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 15. L-value variable locates the memory location where this value is to be placed i.e. at 2000 say. Remember it as: L-value refers to location value i.e. location of the object and R-value refers to read value i.e. value of the object. Programming in C: A Practical Approach Data Types, Variables and Constants Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 16. Variables A variable is an entity whose value can vary (i.e. change) during the execution of a program. The value of a variable can be changed because it has a modifiable l-value. Since, it has modifiable l-value, it can be placed on the left side of assignment operator. Variables and Constants Programming in C: A Practical Approach Data Types, Variables and Constants Constants A constant is an entity whose value remains same throughout the execution of a program. It cannot be placed on the left-side of assignment operator because it does not have a modifiable l-value. Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 17. Variables Variable can also be placed on the right side of assignment operator. Hence, it has r-value too. Thus, a variable has both l-value and r-value. Constants It can only be placed on the right side of assignment operator. Thus, a constant has an r-value only Programming in C: A Practical Approach Data Types, Variables and Constants Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 18. Constants Programming in C: A Practical Approach Data Types, Variables and Constants Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 19. Literal constant or just literal denotes a fixed value, which may be an integer, floating point number, character or a string. The type of literal constant is determined by its value. Symbolic constants are created with the help of define preprocessor directive. For example: #define PI 3.14124 defines PI as a symbolic constant with value 3.14124. Each symbolic constant is replaced by its actual value during the preprocessing stage. Qualified constants are created by using const qualifier. The following statement creates a qualified character constant named as: const char a=’A’; Programming in C: A Practical Approach Data Types, Variables and Constants Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 20. Since, qualified constants are placed in the memory, they have l-value. But, as it is not possible to modify them, this means that they do not have modifiable l-value i.e. they have non-modifiable l-value. E.g. int a=10; // It is possible to modify the value of a. const int a=10;. // It is possible read the value placed within the memory location, but it is not possible to modify the value. Programming in C: A Practical Approach Data Types, Variables and Constants Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 21. Integer Literal Constants Integer literal constants are integer values like -1, 2, 8 etc. The following are the rules for writing integer literal constants: An integer literal constant must have at least one digit. It should not have any decimal point. It can be either positive or negative. If no sign precedes an integer literal constant, then it is assumed to be positive. No special characters (even underscore) and blank spaces are allowed within an integer literal constant. If an integer literal constant starts with 0, then it assumed to be in octal number system e.g. 023 is a valid integer literal constant, which means 23 in octal number system and is equivalent to 19 in decimal number system. Programming in C: A Practical Approach Data Types, Variables and Constants Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 22. If an integer literal constant starts with 0x or 0X, then it is assumed to be in hexadecimal number system e.g. 0x23 or 0X23 is a valid integer literal constant, which means 23 in hexadecimal number system and is equivalent to 35 in decimal number system. The size of integer literal constant can be modified by using a length modifier. The length modifier can be a suffix character l, L, u, U, f or F. If the integer literal constant is terminated with l or L then it is assumed to be long. If it is terminated with u or U then it is assumed to be an unsigned integer e.g. 23l is a long integer and 23u is an unsigned integer. The length modifier f or F can only be used with floating point literal constant and not with integer literal constant. Programming in C: A Practical Approach Data Types, Variables and Constants Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 23. Floating Point Literal Constants Floating point literal constants are values like -23.1, 12.8, -1.8e12 etc. Floating point literal constants can be written in: fractional form or in exponential form . Fractional Form The following are the rules for writing floating point literal constants in fractional form: A fractional floating point literal constant must have at least one digit. It should have a decimal point. It can be either positive or negative. If no sign precedes a floating point literal constant then it is assumed to be positive. Programming in C: A Practical Approach Data Types, Variables and Constants Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 24. No special characters (even underscore) and blank spaces are allowed within a floating point literal constant. A floating point literal constant by default is assumed to be of type double, e.g. the type of 23.45 is double. The size of floating point literal constant can be modified by using the length modifier f or F i.e. if 23.45 is written as 23.45f or 23.45F, then it is considered to be of type float instead of double. Following are valid floating point literal constants in fractional form: -2.5, 12.523, 2.5f, 12,5F Exponential Form The following are the rules for writing floating point literal constants in exponential form: Programming in C: A Practical Approach Data Types, Variables and Constants Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 25. A floating point literal constant in exponential form has two parts: the mantissa part and the exponent part. Both parts are separated by e or E. The mantissa can be either positive or negative. The default sign is positive. The mantissa part should have at least one digit. The mantissa part can have a decimal point but it is not mandatory. The exponent part must have at least one digit. It can be either positive or negative. The default sign is positive. The exponent part cannot have a decimal point. No special characters (even underscore) and blank spaces are allowed within the mantissa part and the exponent part. Following are valid floating point literal constants in exponential form: –2.5E12, –2.5e–12, 2e10 (i.e. equivalent to 2×10 10 ) Programming in C: A Practical Approach Data Types, Variables and Constants Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 26. Character Literal Constant A character literal constant can have one or at most two characters enclosed within single quotes e.g. ‘A’, ‘a’, ‘\n’ etc. Character literal constants are classified as: Printable character literal constants Non-Printable character literal constants Printable Character Literal Constant: All characters of source character set except quotation mark, backslash and new line character when enclosed within single quotes form printable character literal constant . Following are the examples of printable character literal constants: ‘A’, ‘#’, ‘6’. Programming in C: A Practical Approach Data Types, Variables and Constants Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 27. Non-printable Character Literal Constant: Non-printable character literal constants are represented with the help of escape sequences . An escape sequence consists of a backward slash (i.e. \) followed by a character and both enclosed within single quotes. An escape sequence is treated as a single character. It can be used in a string like any other printable character. Programming in C: A Practical Approach Data Types, Variables and Constants Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 28. List of Escape Sequences Programming in C: A Practical Approach Data Types, Variables and Constants Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd. ESCAPE SEQUENCES CHARACTER VALUE ACTION ON OUTPUT DEVICE ‘ \'’ Single quotation mark Prints ‘ ‘ \“’ Double quotation mark (“) Prints “ ‘ \?’ Question mark (?) Prints ? ‘ \\’ Backslash character (\) Prints \ ‘ \a’ Alert Alerts by generating beep ‘ \b’ Backspace Moves the cursor one position to the left of its current position. ‘ \f’ Form feed Moves the cursor to the beginning of next page. ‘ \n’ New line Moves the cursor to the beginning of the next line. ‘ \r’ Carriage return Moves the cursor to the beginning of the current line. ‘ \t’ Horizontal tab Moves the cursor to the next horizontal tab stop. ‘ \v’ Vertical tab Moves the cursor to the next vertical tab stop. ‘ \0’ Null character Prints nothing
  • 29. Structure of a C Program Programming in C: A Practical Approach Data Types, Variables and Constants Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 30. C Program Programming in C: A Practical Approach Data Types, Variables and Constants Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd. LINE PROGRAM OUTPUT WINDOW 1 //Comment: First C program Hello Readers!! 2 #include<stdio.h> 3 main() 4 { 5 printf(“Hello Readers!!”); 6 }
  • 31. Comments Single line comment starts with two forward slashes (i.e. //) and is automatically terminated with the end of line. Multi-line comment starts with /* and terminates with */. Multi-line comment is used when multiple lines of text are to be commented out. Programming in C: A Practical Approach Data Types, Variables and Constants Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 32. Preprocessor Directives #include<stdio.h> is a preprocessor directive which includes standard input/output (i.e. stdio) header (.h) file. This file is to be included if standard input/output functions like printf or scanf are to be used in a program. The following points must be remembered while writing preprocessor directives: The preprocessor directive always starts with a pound symbol(i.e. #) The pound symbol # should be the first non-white space character in a line. Programming in C: A Practical Approach Data Types, Variables and Constants Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 33. The preprocessor directive is terminated with a newline character and not with a semicolon. Preprocessor directives are executed before the compiler compiles the source code. These will change the source code usually to suit the operating environment (#pragma directive) or to add the code (#include directive) that will be required by the calls to library functions. Programming in C: A Practical Approach Data Types, Variables and Constants Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.
  • 34. Executing a C Program Programming in C: A Practical Approach Data Types, Variables and Constants Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.