0% found this document useful (0 votes)
56 views9 pages

Lecture Handout 4

The document discusses various data types in C language including basic types like integer and floating point types. It describes integer types like char, int, short, long etc and their ranges. For floating point types it discusses float, double and long double. It also covers enumerated types, type void and derived types like pointers, arrays and structures.

Uploaded by

Kirubai D
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)
56 views9 pages

Lecture Handout 4

The document discusses various data types in C language including basic types like integer and floating point types. It describes integer types like char, int, short, long etc and their ranges. For floating point types it discusses float, double and long double. It also covers enumerated types, type void and derived types like pointers, arrays and structures.

Uploaded by

Kirubai D
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/ 9

TYPOLOGY:

A data type specifies the type of data that a variable can store such as integer, floating,
character, etc.

There are the following data types in C language.

Sr.No. Types & Description

1 Basic Types
They are arithmetic types and are further classified into: (a) integer types and (b) floating-point
types.

2 Enumerated types
They are again arithmetic types and they are used to define variables that can only assign certain
discrete integer values throughout the program.

3 The type void


The type specifier void indicates that no value is available.

4 Derived types
They include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union types and (e)
Function types.
The basic types and the enumerated types together make up the arithmetic types.
The arithmetic types and the pointer types together are called the scalar types.
Finally, array types and structure types are referred to collectively as the aggregate types.

We will see the basic types in the following section,

Integer Types:

Signed Values- if we want to take signed or unsigned values both we will go for signed.

Unsigned Value- if we only looking for positive or negative number we use Unsigned.

The following table provides the details of standard integer types with their storage sizes and
value ranges −

Type Storage size Value range

char 1 byte -128 to 127 or 0 to 255

unsigned char 1 byte 0 to 255

signed char 1 byte -128 to 127

-32,768 to 32,767 or -2,147,483,648 to


int 2 or 4 bytes
2,147,483,647

unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295

short 2 bytes -32,768 to 32,767

unsigned short 2 bytes 0 to 65,535

long 8 bytes or (4bytes for 32 bit OS) -9223372036854775808 to


9223372036854775807

unsigned long 8 bytes 0 to 18446744073709551615


To get the exact size of a type or a variable on a particular platform, you can use
the sizeof operator. The expressions sizeof(type) yields the storage size of the object or type in
bytes. Given below is an example to get the size of various type on a machine using different
constant defined in limits.h header file.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>

int main(int argc, char** argv) {

printf("CHAR_BIT : %d\n", CHAR_BIT);


printf("CHAR_MAX : %d\n", CHAR_MAX);
printf("CHAR_MIN : %d\n", CHAR_MIN);
printf("INT_MAX : %d\n", INT_MAX);
printf("INT_MIN : %d\n", INT_MIN);
printf("LONG_MAX : %ld\n", (long) LONG_MAX);
printf("LONG_MIN : %ld\n", (long) LONG_MIN);
printf("SCHAR_MAX : %d\n", SCHAR_MAX);
printf("SCHAR_MIN : %d\n", SCHAR_MIN);
printf("SHRT_MAX : %d\n", SHRT_MAX);
printf("SHRT_MIN : %d\n", SHRT_MIN);
printf("UCHAR_MAX : %d\n", UCHAR_MAX);
printf("UINT_MAX : %u\n", (unsigned int) UINT_MAX);
printf("ULONG_MAX : %lu\n", (unsigned long) ULONG_MAX);
printf("USHRT_MAX : %d\n", (unsigned short) USHRT_MAX);
return 0;
}
Output:
C99 introduced the unsigned integer type _Bool to represent Boolean truth values. The Boolean
value true is coded as 1, and false is coded as 0. If you include the header file stdbool.h in a
program, you can also use the identifiers bool, true, and false.

Floating-Point Types

C also includes special numeric types that can represent nonintegers with a decimal
point in any position. The standard floating-point types for calculations with real numbers are as
follows:
Float:

For variables with single precision

Double:

For variables with double precision

long double:

For variables with extended precision.

In C, arithmetic operations with floating-point numbers are performed internally with double or
greater precision. For example, the following product is calculated using the double type.
float height = 1.2345, width = 2.3456; // Float variables have single
// precision.
double area = height * width; // The actual calculation is
// performed with double
// (or greater) precision

The following table provide the details of standard floating-point types with storage sizes and
value ranges and their precision −

Type Storage size Value range Precision

float 4 byte 1.2E-38 to 3.4E+38 6 decimal places

double 8 byte 2.3E-308 to 1.7E+308 15 decimal places

long double 10 byte 3.4E-4932 to 1.1E+4932 19 decimal places


The header file float.h defines macros that allow you to use these values and other details about
the binary representation of real numbers in your programs. The following example prints the
storage space taken by a float type and its range values −
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>
int main(int argc, char** argv) {
printf("Storage size for float : %d \n", sizeof(float));
printf("FLT_MAX : %g\n", (float) FLT_MAX);
printf("FLT_MIN : %g\n", (float) FLT_MIN);
printf("-FLT_MAX : %g\n", (float) -FLT_MAX);
printf("-FLT_MIN : %g\n", (float) -FLT_MIN);
printf("DBL_MAX : %g\n", (double) DBL_MAX);
printf("DBL_MIN : %g\n", (double) DBL_MIN);
printf("-DBL_MAX : %g\n", (double) -DBL_MAX);
printf("Precision value: %d\n", FLT_DIG );

return 0;
}

Output:

C99 supports mathematical calculations with complex numbers.

In C, a complex number is represented by a pair of floating-point values for the


real and imaginary parts.

These are the three complex floating-point types:


• float _Complex

• double _Complex

• long double _Complex

Enumerated Types:

Enumerations are integer types the definition of an enumeration begins with the
keyword enum,

each value:
enum [identifier] { enumerator-list };
The following example defines the enumerated type enum color:
enum color { black, red, green, yellow, blue, white=7, gray };

Enumeration (or enum) is a user defined data type in C. It is mainly used to


assign names to integral constants, the names make a program easy to read and
maintain.

enum State {Working = 1, Failed = 0};


The keyword ‘enum’ is used to declare new enumeration types in C.
Following is an example of enum declaration.
// The name of enumeration is "flag" and the constant
// are the values of the flag. By default, the values
// of the constants are as follows:
// constant1 = 0, constant2 = 1, constant3 = 2 and
// so on.
enum flag{constant1, constant2, constant3, ....... };
Variables of type enum can also be defined. They can be defined in two ways:
// In both of the below cases, "day" is
// defined as the variable of type week.
enum week{Mon, Tue, Wed};
enum week day;
// Or
enum week{Mon, Tue, Wed}day;

// An example program to demonstrate working

#include<stdio.h>

enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};

int main()

{ enum week day;

day = Sun;

printf("%d",day);

return 0;}

OUTPUT:
The Type void

The type specifier void indicates that no value is available. Consequently, you
cannot declare variables or constants with this type. You can use the type void for the purposes
described in the following sections.

Types & Description

1 Function returns as void


There are various functions in C which do not return any value or you can say they return void. A
function with no return value has the return type as void. For example, void exit (int status);

2 Function arguments as void


There are various functions in C which do not accept any parameter. A function with no parameter
can accept a void. For example, int rand(void);

3 Pointers to void
A pointer of type void * represents the address of an object, but not its type. For example, a memory
allocation function void *malloc( size_t size ); returns a pointer to void which can be casted to any
data type.
int a = 10;

char b = 'x';

void *p = &a; // void pointer holds address of int 'a'

p = &b; // void pointer holds address of char 'b'

The following program compiles and runs fine.

#include<stdio.h>

int main()

int a = 10;

void *ptr = &a;

printf("%d", *(int *)ptr);

return 0;

Output:
10

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy