Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
17 views27 pages

Data Types and Variables v1

Uploaded by

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

Data Types and Variables v1

Uploaded by

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

Data Types and Variables Prof.(Dr.

) Ganesh Gupta
Introduction to
Problem Solving

Course Objectives
The course aims to provide exposure to problem-
solving through programming.
The course aims to raise the programming skills
of students via logic building capability.
With knowledge of C programming language,
students would be able to model real world
problems.
2
Course Outcomes
CO Course Outcome
Number

CO1 demonstrate the algorithm, Pseudo-code and flow chart for the given
problem.
CO2 develop better understanding of basic concepts of C programming.

CO3 create and implement logic using array and function.


CO4 construct and implement the logic based on the concept of strings
and pointers.
CO5 apply user-defined data types and I/O operations in file.
CO6 design and develop solutions to real world problems using C.

3
Contents
C0NTENTS

Data Types Variables Constants

4
Data
Data types
Types
Data type is used to specify the range of values that a
particular variable can take during program execution

Each variable in C is associated with data type that


determines memory space for its storage

Data type determines which type of value a particular


variable can take

5
Data
Data types
Types- Categories
Primary Data Types: These data types are predefined into C system
Their storage space , range of values and format specifiers are all fixed.

Derived Data Types : The data types which are derived from
primary data types are called derived data types .

User Defined Data Types : These data type are defined by users
according to their requirements with the combination of primary data
types .
6
Data types

7
Data types Size Chart
Data Type Memory Space Range Of Values Format Specifiers
Signed char 1 Byte -128 To +127 %c
Unsigned char 1 Byte 0 To 255 %c
signed int 2 Bytes -32768 to +32767 %d
unsigned int 2 Bytes 0 to 65535 %u
-2147483648
signed long 4 Bytes To %ld
2147483647

unsigned long 4 Bytes 0 To 4294967295 %lu

8
Data types Size Chart
Data Type Memory Space Range Of Values Format Specifiers
-9223372036854775808
signed long long int 8 Bytes To %lld
9223372036854775807
0
unsigned long long int 8 Bytes To %llu
18446744073709551615
-3.4 E38
float 4 Bytes To %f
+3.4E38
-1.7E308
double 8 Bytes To %lf
+1.7E308 9
Data types
Steps to remember

1. The formula to calculate range is -2n-1 To 2n-1-1 where n is the no. of bits taken by a
particular data type.

2. Char takes one byte in memory in all C compilers .

3. The Ranges of data types are specified here according to 16-bit compiler .

4. Ranges for int and long data types are compiler dependent that will vary from 16 bit
compiler to 64 bit compiler .

10
Constants
Constants

Constants are the quantities that do not change their value


during execution of the program .

11
Integer Constants

An integer constant is that which can be decimal , hexadecimal and


octal constant . An Octal is preceded by O and hexadecimal is preceded
by 0X and decimal by none .
321 // Valid decimal constant
O458 // Invalid octal as 8 is not a octal digit
OX234

12
Real Constants

These are known as real constants . These constants include decimal


point to represent real values . Real constants can also be represented
in exponential form which has
mantissa and exponents. Exponent can be represented with e or E .
3.142 // Valid real constant
341E-5 // valid real constant represents 341 X 10-5
123E // Invalid exponent part is missing

13
Character Constant
Character Constants
These constants are represented in ‘ ‘ as ‘a’ . Special characters are
used in C language that are preceded by \ like ‘\t’ and ‘\n’ are
characters which are used as tab and new line Characters
characters in C
‘a’ // valid character constant
‘5’ // digit can be represented in char form
‘&’ // special symbol can be represented as character constant
‘\t’ // Special Character represents tab
‘an’ // Invalid it should include only single char
14
String Constant
String Constants

Any group of characters enclosed within “ “ is known as string constant


“Happy” // Valid string Constant
“#876 Janakpuri New Delhi” // Valid
“Happy //Valid, two parts of strings can be separated into two lines
Diwali”

15
How to Define Constants ?

In C ,constants can be defined by two ways


a) Using const keyword b) Through Macros using #define
a)Const keyword can be used to define constants as:
const double pi=3.142;
Now value of pi will remain 3.142 till end of the program and cannot be
modified

16
How to Define Constants ?

Macro can be used to define constants as :


#define pi 3.142
each occurrence of pi within the program will be replaced by 3.142 and
this value can
never get changed during execution of the program .

17
Variables
Variables

Variables are the quantities whose value changes during the execution
of the program Variable name is always preceded with some valid
data type that fixes the memory space for the variable to store it .
Actually variable is the name given to memory location whose value
can be manipulated according to the program requirements .
example : int a=5;
a is the variable of type integer in which initial value as 5 is placed

18
Naming The Variables
The variable name may include alphabets , digits or underscore .The name of the
variable is also known as identifier .
Following are the rules for naming the variables
1.Any keyword cannot be used for naming the variable
int break; // Invalid identifier as break is a keyword in C .
2.Space is not allowed in a variable name
int a b // space is not allowed in a variable name .

3.The first character of variable name cannot be digit


int 8a_b; // Invalid as first char is digit .

19
Lva Rvalue
Lvalue
lvalue − Expressions that may appear on L.H.S as well as on R.H.S of the
assignment operator
rvalue – it is the expression that evaluated to constant value and can
appear only on the R.H.S of the assignment operator .
Example :
int a,b;
b=10; // Valid as rvalue constant 10 is on R.H.S of =
10=a; // Invalid as rvalue 10 is on L.H.S of =
a=b; // Valid as both are variables may appear on either side of =

20
Summary
Are used to declare variables .It specifies range , format specifier
Data types and memory space required by corresponding variable

Are names given to memory where value can be manipulated


Variables during program execution

Naming Variables It can include alphabets , digits or underscore only

Constants Are quantities that do not change its value during program
execution.

Constants can be defined either by macros or by using const


Defining constants keyword
21
Q3:I have read that int take two bytes in
Q1:Why int and long are two data types for memory but when I use sizeof() operator to
storing integer values in memory ? Is long alone check size of int on online compiler it prints 4
sufficient ? .Why online compiler takes more space ?
Ans: No , Using long waste our memory space Ans: size of integer data types is compiler
like using long to store no. of students in a dependent . Most of online compiler are 32 bit(4
single class of your university is merely wastage Bytes) so int takes 4 bytes on all these compilers
of memory and memory is one of the valuable but on turboc it takes 2 bytes .
resource of our computer system .

FAQs
Q2: Identifiers can take alphabets , digits and Q4: Character constant should take single
underscore but when I take 2020_roll as character but ‘\n’ and ‘\t’ takes two characters
identifier Compiler generates an error message . ,why ?
Kindly tell me why it happened ?
Ans: The first character of identifier cannot be
digit that is the rule of naming variables , so Ans: All escape sequences like ‘\n and ‘\t’ are
compiler generates an error message . special characters preceded by \ .
22
Q7: What are the memory space requirements
of derived data types ?
Q5: Can we change value of constant defined by
#define ? Ans: Derived data types are the combination of
predefined data types so their space is
Ans: No, Constant of any type cannot be
calculated by adding up space of all primary data
modified
types from which derived data type composed
of .

FAQs
Q6: I store my city population in int variable it is
Q8 : When I use short x; compiler did not
50000 but when I print it was some garbage
generate any error message.Ccan we use short
value why it happened ?
without int ?
Ans: You worked on 16 bit compiler. int take two
Ans: short x; or short int x ; have same meaning
bytes and can store value in range -32768 to
so compiler will not generate any error message
32767 so 50000 cannot be stored in this variable
.
you can take long to store this value .
23
Test Yourself

long long Int a=145;


int 8ab;
printf(“%d”,sizeof(a));
scanf(“%d”,&8ab);
printf(“8ab=%d”,&8ab);

char a=‘a’; const flaot a=123e3;


char b[]=“a”; printf(“%2f”,a);
printf(“%d%d”,sizeof(a),sizeof( a=a+1.5;
b)); printf(“%f”,a);

24
References
S.No Title Content link
1 Book Programming in C by Reema Thareja.
2 Book Programming with C (Schaum's Outline Series) by Byron
Gottfried Jitender Chhabra, Tata McGraw Hill.
3 Book The C Programming Language by Brian W. Kernighan, Dennis
Ritchie, Pearson education.
4 Book Programming in ANSI C by E. Balaguruswamy, Tata McGraw
Hill.
5 Weblink https://www.tutorialspoint.com/cprogramming/c_operators.htm

6 Weblink https://www.programiz.com/c-programming
7 Weblink https://fresh2refresh.com/c-programming/
References
S.No Title Content link
8 Weblink https://www.studytonight.com/c/
9 Weblink https://www.javatpoint.com/c-operators
10 Video Link https://www.youtube.com/watch?v=OSyjOvFbAGI
11 Video Link https://www.youtube.com/watch?v=d7tdL-ZEWdE

12 Online Course Link https://www.coursera.org/


13 Online Course Link https://www.udemy.com/

14 Online Course Link https://www.niit.com/


THANK YOU

You might also like