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

Lect 6 Programming Logic Using ‘C’ Data Types

The document provides an overview of data types in C programming, detailing primary and derived types, their sizes, ranges, and format specifiers. It explains the characteristics of various data types such as int, float, double, char, and their modifiers like signed, unsigned, short, and long. Additionally, it discusses the cyclic property of certain data types when assigned values beyond their range.

Uploaded by

nayecool37
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lect 6 Programming Logic Using ‘C’ Data Types

The document provides an overview of data types in C programming, detailing primary and derived types, their sizes, ranges, and format specifiers. It explains the characteristics of various data types such as int, float, double, char, and their modifiers like signed, unsigned, short, and long. Additionally, it discusses the cyclic property of certain data types when assigned values beyond their range.

Uploaded by

nayecool37
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Programming Logic Using ‘C’

BCAC0001
Lect-7 Data Types
Presented by:
Atul Kumar Uttam
Assistant Professor
Computer Engineering & Applications Department,
GLA University, Mathura
[email protected], +91-8979593001
Data Types

String

Name: George
Age: 5 Integer
Weight: 15.2 Number

Floating Point
Number

BCAC0001 Programming Logic Using C


Atul Kr Uttam
Data Types
James, Rita, Alice
String
007, 123, 101
Name: George
Age: 5 Integer
Weight: 15.2 Number
7, 5, 30
VII, five, 3Not
Floating Point
Number 0.012, 15.2, 3.0

BCAC0001 Programming Logic Using C


Atul Kr Uttam
Data Types

• Data type specifies the


– Range of values and
– Type of values
that may be stored in a variable of that type.

BCAC0001 Programming Logic Using C


Atul Kr Uttam
Data Types
Data Types

Primary / Primitive Secondary / Derived

int Array

char Pointer

float Structure

double enumeration

void Union
BCAC0001 Programming Logic Using C
Atul Kr Uttam
The actual size of the types may vary from one system to another.
Table shows the usual ranges on a 32-bit system
Data Type Usual Size Range of Values Precision
(bytes) (min–max) Digits

char 1 -128….127
short int 2 -32768….32767
int 4 -2147483648…. 2147483647
long int 4 -2147483648…. 2147483647
float 4 6
double 8 15
long double 10, 12, 16
unsigned char 1 0….255
unsigned short int 2 0…65535
unsigned int 4 0…4294967295
BCAC0001 Programming Logic Using C
unsigned long int 4 0…4294967295
Atul Kr Uttam
Format
Type Size (bytes)
Specifier
int at least 2, usually 4 %d, %i
char 1 %c
float 4 %f
double 8 %lf
Format
Type Size (bytes)
Specifier
short int 2 usually %hd
unsigned int at least 2, usually 4 %u
long int at least 4, usually 8 %ld, %li
long long int at least 8 %lld, %lli
unsigned long int at least 4 %lu
unsigned long long
at least 8 %llu
int
signed char 1 %c
unsigned char 1 %c
long double at least 10, usually 12 or 16 %Lf
int
• Whole numbers
• Example
0, 10, -15
• 4 Byte
• Min value
-2147483648
• Max Value
2147483647
float and double
• Real numbers
• Example:
-2.0, 10.2, 0.02, 5.14e2
• float
– 4 Byte
– single precision (6 digit after decimal)
• double
– 8 Byte
– Double precision (Up to 15 digit after decimal)
float and double
• If you don’t care about precision, use the float
type, because it usually reserves fewer bytes
and calculations with float numbers tend to be
executed faster.
char
• character type variables
• Example:
‘a’, ‘5’, ‘@’
• 1 Byte
void
• Generic data type
• Empty data type
• Represents No value
• Used in
– If a function does not return a value->return type
– If a function does not take any argument
– generic pointer
Type Modifiers
• Prefixed with basic data types.
• Modifies the amount of memory space
reserved for variables
Type Modifiers
• short
• long
• signed
• unsigned
short
• Small integer values
• 2 Bytes
• Min Value
−32767
• Max Value
32767
long
• For large integer values:
– long
– long long
• For large floating values:
– long double
signed and unsigned
• Can change the data storage of a data type
• Example
unsigned int a;
– a can store only positive or zero values
signed int b
– b can store positive, negative and zero values
Byte Range
• unsigned char 1 0…255
• unsigned short int 2 0…65535
• unsigned int 4 0…4294967295
• unsigned long int 4 0…4294967295
• The memory space that a data type reserves
may vary from one system to another.

• To compute the number of bytes a data type


reserves on a particular system,

– sizeof operator
Cyclic Property of Data Types
• In C some data types when assigned a value
beyond range of that data type then,
– It will not any compiler error but assign a number
according to some cyclic order.
• Data type which shows cyclic nature:
(a) char
(b) int
(c) long int
• Data type which doesn’t show cyclic nature:
(a) float
(b) double
(c) long double
• For +ve Numbers
Data Type[min……….max]
Min+|Diff|-1
|Diff|= |Max-Given value|
• For –ve Numbers:
Max - |Diff| +1
|Diff| = |Min – Given Value|
Cyclic nature of unsigned char
• Range of unsigned char is 0 to 255

#include<stdio.h>
int main()
{
char a=257;
printf("%d",a);
}
Answer
1
Cyclic nature of signed char
• Range of unsigned char is -128 to +127

#include<stdio.h>
int main()
{
signed char a = 130;
printf("%d",a);
}
Answer
-126
Cyclic nature of signed int
#include<stdio.h>
int main()
{
int a = 2147483649;
printf("%d",a);
}
Answer
-2147483647

You might also like