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

3.0 Data Types in C

Uploaded by

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

3.0 Data Types in C

Uploaded by

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

Data Types in C


••
Each variable in C has an associated data type. It specifies the type of data that the variable
can store like integer, character, floating, double, etc. Each data type requires different
amounts of memory and has some specific operations which can be performed over it.
The data types in C can be classified as follows:
Types Description Data Types

Primitive data types are the most basic data types that are used
Primitive Data int, char, float,
for representing simple values such as integers, float,
Types double, void
characters, etc.

The data types that are derived from the primitive or built-in array, pointers,
Derived Types
datatypes are referred to as Derived Data Types. function

User Defined structure, union,


The user-defined data types are defined by the user himself.
Data Types enum

The following are some main primitive data types in C:


Table of Content
• Integer Data Type
• Character Data Type
• Float Data Type
• Double Data Type
• Void Data Type
Integer Data Type
The integer datatype in C is used to store the integer numbers (any number including
positive, negative and zero without decimal part). Octal values, hexadecimal values, and
decimal values can be stored in int data type in C.
• Range: -2,147,483,648 to 2,147,483,647
• Size: 4 bytes
• Format Specifier: %d
Syntax of Integer
We use int keyword to declare the integer variable:
int var_name;
The integer data type can also be used as
1. unsigned int: Unsigned int data type in C is used to store the data values from zero
to positive numbers but it can’t store negative values like signed int.
2. short int: It is lesser in size than the int by 2 bytes so can only store values from -
32,768 to 32,767.
3. long int: Larger version of the int datatype so can store values greater than int.
4. unsigned short int: Similar in relationship with short int as unsigned int with int.
Note: The size of an integer data type is compiler-dependent. We can use sizeof operator to
check the actual size of any data type.
Example of int
C
// C program to print Integer data types.
#include <stdio.h>

int main()
{
// Integer value with positive data.
int a = 9;

// integer value with negative data.


int b = -9;

// U or u is Used for Unsigned int in C.


int c = 89U;

// L or l is used for long int in C.


long int d = 99998L;

printf("Integer value with positive data: %d\n", a);


printf("Integer value with negative data: %d\n", b);
printf("Integer value with an unsigned int data: %u\n",
c);
printf("Integer value with an long int data: %ld", d);

return 0;
}

Output
Integer value with positive data: 9
Integer value with negative data: -9
Integer value with an unsigned int data: 89
Integer value with an long int data: 99998

Character Data Type


Character data type allows its variable to store only a single character. The size of the
character is 1 byte. It is the most basic data type in C. It stores a single character and requires
a single byte of memory in almost all compilers.
• Range: (-128 to 127) or (0 to 255)
• Size: 1 byte
• Format Specifier: %c
Syntax of char
The char keyword is used to declare the variable of character type:
char var_name;
Example of char
C
// C program to print Integer data types.
#include <stdio.h>

int main()
{
char a = 'a';
char c;

printf("Value of a: %c\n", a);

a++;
printf("Value of a after increment is: %c\n", a);

// c is assigned ASCII values


// which corresponds to the
// character 'c'
// a-->97 b-->98 c-->99
// here c will be printed
c = 99;

printf("Value of c: %c", c);

return 0;
}

Output
Value of a: a
Value of a after increment is: b
Value of c: c

Float Data Type


In C programming float data type is used to store floating-point values. Float in C is used to
store decimal and exponential values. It is used to store decimal numbers (numbers with
floating point values) with single precision.
• Range: 1.2E-38 to 3.4E+38
• Size: 4 bytes
• Format Specifier: %f
Syntax of float
The float keyword is used to declare the variable as a floating point:
float var_name;
Example of Float
C
// C Program to demonstrate use
// of Floating types
#include <stdio.h>

int main()
{
float a = 9.0f;
float b = 2.5f;
// 2x10^-4
float c = 2E-4f;
printf("%f\n", a);
printf("%f\n", b);
printf("%f", c);

return 0;
}

Output
9.000000
2.500000
0.000200

Double Data Type


A Double data type in C is used to store decimal numbers (numbers with floating point
values) with double precision. It is used to define numeric values which hold numbers with
decimal values in C.
The double data type is basically a precision sort of data type that is capable of holding 64 bits
of decimal numbers or floating points. Since double has more precision as compared to that
float then it is much more obvious that it occupies twice the memory occupied by the floating-
point type. It can easily accommodate about 16 to 17 digits after or before a decimal point.
• Range: 1.7E-308 to 1.7E+308
• Size: 8 bytes
• Format Specifier: %lf
Syntax of Double
The variable can be declared as double precision floating point using the double keyword:
double var_name;
Example of Double
C
// C Program to demonstrate
// use of double data type
#include <stdio.h>

int main()
{
double a = 123123123.00;
double b = 12.293123;
double c = 2312312312.123123;

printf("%lf\n", a);

printf("%lf\n", b);

printf("%lf", c);

return 0;
}
Output
123123123.000000
12.293123
2312312312.123123

Void Data Type


The void data type in C is used to specify that no value is present. It does not provide a result
value to its caller. It has no values and no operations. It is used to represent nothing. Void is
used in multiple ways as function return type, function arguments as void, and pointers to
void.
Syntax:
// function return type void
void exit(int check);
// Function without any parameter can accept void.
int print(void);
// memory allocation function which
// returns a pointer to void.
void *malloc (size_t size);
Example of Void
C
// C program to demonstrate
// use of void pointers
#include <stdio.h>

int main()
{
int val = 30;
void* ptr = &val;
printf("%d", *(int*)ptr);
return 0;
}

Output
30

Size of Data Types in C


The size of the data types in C is dependent on the size of the architecture, so we cannot
define the universal size of the data types. For that, the C language provides the sizeof()
operator to check the size of the data types.
Example
C
// C Program to print size of
// different data type in C
#include <stdio.h>

int main()
{
int size_of_int = sizeof(int);
int size_of_char = sizeof(char);
int size_of_float = sizeof(float);
int size_of_double = sizeof(double);

printf("The size of int data type : %d\n", size_of_int);


printf("The size of char data type : %d\n",
size_of_char);
printf("The size of float data type : %d\n",
size_of_float);
printf("The size of double data type : %d",
size_of_double);

return 0;
}

Output
The size of int data type : 4
The size of char data type : 1
The size of float data type : 4
The size of double data type : 8
Different data types also have different ranges up to which they can store numbers. These
ranges may vary from compiler to compiler. Below is a list of ranges along with the memory
requirement and format specifiers on the 32-bit GCC compiler.
Data Type Size (bytes) Range Format Specifier

short int 2 -32,768 to 32,767 %hd

unsigned short int 2 0 to 65,535 %hu

unsigned int 4 0 to 4,294,967,295 %u

int 4 -2,147,483,648 to 2,147,483,647 %d

long int 4 -2,147,483,648 to 2,147,483,647 %ld

unsigned long int 4 0 to 4,294,967,295 %lu

long long int 8 -(2^63) to (2^63)-1 %lld

unsigned long long int 8 0 to 18,446,744,073,709,551,615 %llu

signed char 1 -128 to 127 %c


Data Type Size (bytes) Range Format Specifier

unsigned char 1 0 to 255 %c

float 4 1.2E-38 to 3.4E+38 %f

double 8 1.7E-308 to 1.7E+308 %lf

long double 16 3.4E-4932 to 1.1E+4932 %Lf

Note: The long, short, signed and unsigned are datatype modifier that can be used with some
primitive data types to change the size or length of the datatype.

Literals in C
•••
In C, Literals are the constant values that are assigned to the variables. Literals represent
fixed values that cannot be modified. Literals contain memory but they do not have
references as variables. Generally, both terms, constants, and literals are used
interchangeably.
For example, “const int = 5;“, is a constant expression and the value 5 is referred to as a
constant integer literal.
Types of C Literals
There are 4 types of literal in C:
• Integer Literal
• Float Literal
• Character Literal
• String Literal

1. Integer Literals
Integer literals are used to represent and store the integer values only. Integer literals are
expressed in two types i.e.
A) Prefixes: The Prefix of the integer literal indicates the base in which it is to be read.
For Example:
0x10 = 16
Because 0x prefix represents a HexaDecimal base. So 10 in HexaDecimal is 16 in Decimal. Hence
the value 16.
There are basically represented into 4 types:
a. Decimal-literal(base 10): A non-zero decimal digit followed by zero or more decimal
digits(0, 1, 2, 3, 4, 5, 6, 7, 8, 9).
Example:
56, 78
b. Octal-literal(base 8): a 0 followed by zero or more octal digits(0, 1, 2, 3, 4, 5, 6, 7).
Example:
045, 076, 06210
c. Hex-literal(base 16): 0x or 0X followed by one or more hexadecimal digits(0, 1, 2, 3, 4, 5,
6, 7, 8, 9, a, A, b, B, c, C, d, D, e, E, f, F).
Example:
0x23A, 0Xb4C, 0xFEA
d. Binary-literal(base 2): 0b or 0B followed by one or more binary digits(0, 1).
Example:
0b101, 0B111
B) Suffixes: The Suffixes of the integer literal indicates the type in which it is to be read.
For example:
12345678901234LL
indicates a long long integer value 12345678901234 because of the suffix LL
These are represented in many ways according to their data types.
• int: No suffix is required because integer constant is by default assigned as an int
data type.
• unsigned int: character u or U at the end of an integer constant.
• long int: character l or L at the end of an integer constant.
• unsigned long int: character ul or UL at the end of an integer constant.
• long long int: character ll or LL at the end of an integer constant.
• unsigned long long int: character ull or ULL at the end of an integer constant.
Example:
C
#include <stdio.h>

int main()
{

// constant integer literal


const int intVal = 10;

printf("Integer Literal:%d \n", intVal);


return 0;
}

Output
Integer Literal:10

2. Floating-Point Literals
These are used to represent and store real numbers. The real number has an integer part, real
part, fractional part, and exponential part. The floating-point literals can be stored either in
decimal form or exponential form. While representing the floating-point decimals one must
keep two things in mind to produce valid literal:
• In the decimal form, one must include the integer part, or fractional part, or both,
otherwise, it will lead to an error.
• In the exponential form, one must include both the significand and exponent part,
otherwise, it will lead to an error.
A few floating-point literal representations are shown below:
Valid Floating Literals:
10.125
1.215e-10L
10.5E-3
Invalid Floating Literals:
123E
1250f
0.e879
Example:
C
#include <stdio.h>

int main()
{
// constant float literal
const float floatVal = 4.14;

printf("Floating point literal: %.2f\n",


floatVal);
return 0;
}

Output
Floating point literal: 4.14

3. Character Literals
This refers to the literal that is used to store a single character within a single quote. To store
multiple characters, one needs to use a character array. Storing more than one character
within a single quote will throw a warning and display just the last character of the literal. It
gives rise to the following two representations:
• char type: This is used to store normal character literal or narrow-character
literals.
Example:
char chr = 'G';
Example:
C
#include <stdio.h>

int main()
{
// constant char literal
const char charVal = 'A';

printf("Character Literal: %c\n",


charVal);
return 0;
}

Output
Character Literal: A

Escape Sequences: There are various special characters that one can use to perform various
operations.
4. String Literals
String literals are similar to that character literals, except that they can store multiple
characters and uses a double quote to store the same. It can also accommodate the special
characters and escape sequences mentioned in the table above. We can break a long line into
multiple lines using string literal and can separate them with the help of white spaces.
Example:
char stringVal[] = "GeeksforGeeks";
Example:
C
#include <stdio.h>

int main()
{
const char str[]
= "Welcome\nTo\nGeeks\tFor\tGeeks";
printf("%s", str);
return 0;
}

Output
Welcome
To
Geeks For Geeks
Escape Sequence in C

The•• escape sequence in C is the characters or the sequence of characters that can be used
inside the string literal. The purpose of the escape sequence is to represent the characters
that cannot be used normally using the keyboard. Some escape sequence characters are the
part of ASCII charset but some are not.
Different escape sequences represent different characters but the output is dependent on the
compiler you are using.
Escape Sequence List
The table below lists some common escape sequences in C language.
Escape
Sequence Name Description

\a Alarm or Beep It is used to generate a bell sound in the C program.

\b Backspace It is used to move the cursor one place backward.

It is used to move the cursor to the start of the next logical


\f Form Feed
page.

\n New Line It moves the cursor to the start of the next line.

\r Carriage Return It moves the cursor to the start of the current line.

It inserts some whitespace to the left of the cursor and moves


\t Horizontal Tab
the cursor accordingly.

\v Vertical Tab It is used to insert vertical space.

\\ Backlash Use to insert backslash character.

\’ Single Quote It is used to display a single quotation mark.

\” Double Quote It is used to display double quotation marks.

\? Question Mark It is used to display a question mark.


Escape
Sequence Name Description

\ooo Octal Number It is used to represent an octal number.

Hexadecimal
\xhh It represents the hexadecimal number.
Number

\0 NULL It represents the NULL character.

\e Escape sequence It represents the ASCII escape character.

\s Space Character It represents the ASCII space character.

\d Delete Character It represents the ASCII DEL character.

Out of all these escape sequences, \n and \0 are used the most. In fact, escape sequences like
\f, \a, are not even used by programmers nowadays.
Escape Sequence in C Examples
The following are the escape sequence examples that demonstrate how to use different
escape sequences in C language.
1. Example to demonstrate how to use \a escape sequence in C
C
// C program to illustrate \a escape sequence
#include <stdio.h>

int main(void)
{
// output may depend upon the compiler
printf("My mobile number "
"is 7\a8\a7\a3\a9\a2\a3\a4\a0\a8\a");
return (0);
}

Output
My mobile number is 7873923408
2. Example to demonstrate how to use \b escape sequence in C
C
// C program to illustrate \b escape sequence
#include <stdio.h>

int main(void)
{
// \b - backspace character transfers
// the cursor one character back with
// or without deleting on different
// compilers.
printf("Hello \b\b\b\b\b\bHi Geeks");
return (0);
}

Output
Hello Hi Geeks
3. Example to demonstrate how to use \n escape sequence in C
C
// C program to illustrate \n escape sequence
#include <stdio.h>
int main(void)
{
// Here we are using \n, which is a new line character.
printf("Hello\n");
printf("GeeksforGeeks");
return (0);
}

Output
Hello
GeeksforGeeks
4. Example to demonstrate how to use \t escape sequence in C
C
// C program to illustrate \t escape sequence
#include <stdio.h>

int main(void)
{
// Here we are using \t, which is
// a horizontal tab character.
// It will provide a tab space
// between two words.
printf("Hello \t GFG");
return (0);
}

Output
Hello GFG
The escape sequence “\t” is very frequently used in loop-based pattern printing programs.
5. Example to demonstrate how to use \v escape sequence in C
C
// C program to illustrate \v escape sequence
#include <stdio.h>

int main(void)
{
// Here we are using \v, which
// is vertical tab character.
printf("Hello friends\v");

printf("Welcome to GFG");

return (0);
}

Output
Hello friends
Welcome to GFG
6. Example to demonstrate how to use \r escape sequence in C
C
// C program to illustrate \r escape sequence
#include <stdio.h>

int main(void)
{
// Here we are using \r, which
// is carriage return character.
printf("Hello Geeks \rGeeksfor");
return (0);
}

Output
Hello Geeks
Geeksfor
7. Example to demonstrate how to use \\ escape sequence in C
C
// C program to illustrate \\(Backslash)
// escape sequence to print backslash.
#include <stdio.h>

int main(void)
{
// Here we are using \,
// It contains two escape sequence
// means \ and \n.
printf("Hello\\GFG");
return (0);
}

Output
Hello\GFG
Explanation: It contains two ‘\’ which means we want print ‘\’ as output.
8. Example to demonstrate how to use \’ and \” escape sequence in C
C
// C program to illustrate \' escape
// sequence/ and \" escape sequence to
// print single quote and double quote.
#include <stdio.h>
int main(void)
{
printf("\' Hello Geeks\n");
printf("\" Hello Geeks");
return 0;
}

Output
' Hello Geeks
" Hello Geeks
9. Example to demonstrate how to use \? escape sequence in C
C
// C program to illustrate
// \? escape sequence
#include <stdio.h>

int main(void)
{
// Here we are using \?, which is
// used for the presentation of trigraph
// in the early of C programming. But
// now we don't have any use of it.
printf("\?\?!\n");
return 0;
}

Output
??!
10. Example to demonstrate how to use \ooo escape sequence in C
C
// C program to illustrate \OOO escape sequence
#include <stdio.h>

int main(void)
{
// we are using \OOO escape sequence, here
// each O in "OOO" is one to three octal
// digits(0....7).
char* s = "A\072\065";
printf("%s", s);
return 0;
}

Output
A:5
Explanation: Here 000 is one to three octal digits(0….7) means there must be at least one
octal digit after \ and a maximum of three. Here 072 is the octal notation, first, it is converted
to decimal notation which is the ASCII value of char ‘:’. At the place of \072, there is: and the
output is A:5.
11. Example to demonstrate how to use \xhh escape sequence in C
C
// C program to illustrate \XHH escape
// sequence
#include <stdio.h>
int main(void)
{
// We are using \xhh escape sequence.
// Here hh is one or more hexadecimal
// digits(0....9, a...f, A...F).
char* s = "B\x4a";
printf("%s", s);
return 0;
}

Output
BJ
Explanation: Here hh is one or more hexadecimal digits(0….9, a…f, A…F). There can be more
than one hexadecimal number after \x. Here, ‘\x4a’ is a hexadecimal number and it is a single
char. Firstly it will get converted into decimal notation and it is the ASCII value of the char ‘J’.
Therefore at the place of \x4a, we can write J. So the output is BJ.

You might also like