C / C++

Mostly used powerful Datatypes in C

Good morning to all , here is my sixth blog before it i have covered some basics About C programming . Now we will discuss about Datatypes in C which are mostly used and are very useful in C programming. So here we start Firstly we discuss about what is data and then we will look at this topic.

What is Data ?

A collection of information obtained through measurements, study, observations, or analysis is referred to as data.

Facts, numbers, names, figures, or even descriptions of things may be included. In the form of graphs, charts, or tables, data is arranged.

Data can we of many types but in C we discuss about the data in the form of integer , decimal , words , character etc.

Datatypes in C

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

Basically , there are four types of datatype in C that are : Basic type , Derived type , Enumeration type (User Defined) , Void type (Valueless) .

Datatypes in c
Datatypes in C

So from above figure we can easily find that each class have listed datatypes as below :-

Basic Datatypeint, char, float, double
Derived Datatypearray, pointer, structure, union
Enumeration Datatype (User Defined)enum
Void Datatype (Valueless)void
Bool Datatypetrue, false

They are described below :-

Sr.No.Types & Description
1Basic Types ->They are arithmetic types and are further classified into: (a) integer types and (b) floating-point types.
2Enumerated types ->They are again arithmetic types and they are used to define variables that can only assign certain discrete integer values throughout the program.
3The type void ->The type specifier void indicates that no value is available.
4Derived types ->They include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union types and (e) Function types.

In this article we only read about primitive data types .

Primitive Datatypes in C

Primitive datatype in C

The five basic datatypes found in the C programming language are as follows:

  1. Integer – These are used to store different whole numbers, including 5, 8, 67, 2390, etc.
  2. Character – This term includes all ASCII character sets in addition to individual alphabets like “x,” “Y,” etc.
  3. Double – These huge numeric value types that do not fall within either the floating-point or integer data types are included here. To learn more, go to Double Data Type in C.
  4. Floating-point – These are all the decimal points or real numerical values, such as 40.1, 820.673, 5.9, etc.
  5. Void – This term refers to no values at all. We mostly use this data type when defining the functions in a program.

The aforementioned datatypes are specified in a program using a variety of keywords. The keywords we employ are as follows:

Keyword UsedDatatype
intInteger
floatFloating-point
voidVoid
charCharacter
doubleDouble

Each datatype’s size is specified in bytes or bits. Additionally, a very broad range of values can be stored in these data types.

Different Datatypes Values

The size of any particular datatype in a programme is greatly influenced by the compiler and kind of processor. To put it another way, the size of the data type fully depends on the machine that we are using to run the C language and the version of the C program compiler that we have installed.

Int datatype

  • It can be of 4 bytes / 2 bytes .
  • The size of the int datatype is extremely simple to recall. The supplied size typically corresponds to the program’s execution environment’s word length. In other words, if the environment is a 16-bit environment, int will be 2 bytes or 16 bits. In a 32-bit environment, however, int will be 4 bytes or 32 bits.

char datatype

  • It is of 1 byte.
  • The char datatype is essentially 8 bits or 1 byte in size. With several interpreters and compilers, no variance would exist. It implies that there will be absolutely no difference in its size depending on the compiler or processor utilised.

float datatype

  • It is of 4 bytes.
  • The float datatype is essentially 32 bits or 4 bytes in size. We utilise the single-precision float data type to store the decimal numbers because of its nature. Although it aids in the storage of very large values, floats are often faster than doubles. The reason for this is because double operates on comparably considerably larger data values. Thus, it moves more slowly in comparison.

double datatype

  • It is of 8 bytes.
  • The double datatype is essentially 64 bits or 8 bytes in size. It can store numbers that are roughly twice as large as what the float data type can store in terms of bytes. It is referred to as the double for this reason.
  • The software employs 1 bit for sign representation, 11 bits for the exponent, and the remaining 52 bits for the mantissa, for a total of 64 bits.
  • This data type can store up to 15–17 digits, both after and before the data type’s decimal.

void datatype

  • It is of 0 bytes.
  • The void datatype has no size at all because it has no purpose.
  • All of these datatypes encompass a variety of values. But first, let’s take a closer look at the modifiers that are applied to the data types.

Format specifiers in C

In C, the format specifiers are utilised for both input and output. When collecting input using the scanf() function and publishing using the printf() method, the compiler can determine the type of data in a variable by applying this idea.

Here is a list of format specifiers.

Format SpecifierType
%cCharacter
%dSigned integer
%e or %EScientific notation of floats
%fFloat values
%g or %GSimilar as %e or %E
%hiSigned integer (short)
%huUnsigned Integer (short)
%iUnsigned integer
%l or %ld or %liLong
%lfDouble
%LfLong double
%luUnsigned int or unsigned long
%lli or %lldLong long
%lluUnsigned long long
%oOctal representation
%pPointer
%sString
%uUnsigned int
%x or %XHexadecimal representation
%nPrints nothing
%%Prints % character

Modifiers in C

For all of the datatypes used in the C language, there are essentially four types of modifiers. For further categorization, we employ these in addition to all the fundamental data kinds.

In a similar way, the modifiers in the C language assist in greatly increasing the specificity of the main or primitive data types.

Here are a few modifiers:

  • short
  • long
  • unsigned
  • signed

We utilize the unsigned and signed modifiers, as the names imply, to provide unsigned (only +) and signed (- and +) values for any given data type. Additionally, the value range of any particular data type is impacted by the short and long modifiers.

For instance, the C programming language accepts many perfectly acceptable data types, such as long int, short int, unsigned int, signed int, etc.

Range of Values of C Datatypes

DatatypeFormat SpecifierMinimal RangeTypical Bit Size
unsigned char%c0 to 2558
char%c-127 to 1278
signed char%c-127 to 1278
int%d, %i-32,767 to 32,76716 or 32
unsigned int%u0 to 65,53516 or 32
signed int%d, %iSame as intSame as int16 or 32
short int%hd-32,767 to 32,76716
unsigned short int%hu0 to 65,53516
signed short int%hdSame as short int16
long int%ld, %li-2,147,483,647 to 2,147,483,64732
long long int%lld, %lli-(263 – 1) to 263 – 1 (It will be added by the C99 standard)64
signed long int%ld, %liSame as long int32
unsigned long int%lu0 to 4,294,967,29532
unsigned longlong int%llu264 – 1 (It will be added by the C99 standard)64
float%f1E-37 to 1E+37 along with six digits of the precisions here32
double%lf1E-37 to 1E+37 along with six digits of the precisions here64
long double%Lf1E-37 to 1E+37 along with six digits of the precisions here80

The different ranges and combinations of the modifiers and data types provide a range of changing values, as you can see in the table above.

When printing any value of a variable in a program, one must use the format specifier. The printf() statement is where they have to do it.

When Value of data is out of range

The C language compiler will produce an error whenever we attempt to assign any value to a specific data type in a programme that is greater than the permitted value range.

To further grasp this, let’s look at an illustration.

int main() {

// the maximum value allowed in the signed short int is 32767

signed short int x = 34767;

return 0;

}

The generated output for this program would be:

warning: very large integer value implicitly truncated to signed type [-Woverflow]

signed short int x = 34767;

^

The software sets the int datatype as a default data type whenever we use a type modifier, even when we don’t use any data types. Here, signed would be equivalent to a signed int, unsigned to an unsigned int, short to a short int, and long to a long int.

Meaning of Unsigned as well as Signed

Unsigned

In essence, it is a little difficult to comprehend. To put it another way, the unsigned modifier refers to all positive values, while the signed modifier refers to both positive and negative numbers.

Every time a compiler receives a numeric value from the program, it turns that numeric value into a binary number. This indicates that it will consist of a variety of 1s and 0s. For example, the binary value of 1 is either 0001 or 01, the binary value of 2 is 0010, the binary value of 32767 is 01111111 11111111, and many more.

Signed

The highest bit of order or the first digit starting from the left in binary numbers is used as the sign flag when referring to signed integers. When the sign flag is set to 1, the number will always be negative. Conversely, whenever the sign flag is set to 0, it will be positive.

Additionally, since we utilize one bit to indicate whether a particular number is positive or negative, the program will require one less bit to represent the number. As a result, the range is relatively limited.

Purposes of Datatypes

The term “datatype” in the context of the C programming language refers to a comprehensive system that we employ to define different sorts of functions or variables in a program. Here, we decide how much storage space a variable will require and how the stored bit pattern will be interpreted based on the type of variable that the program contains.

The kind of data that a variable can contain, such as character, integer, or floating-point data, is specified by its data type.

Escape Sequence in C

An escape sequence in C language is a sequence of characters that doesn’t represent itself when used inside string literal or character.

It is composed of two or more characters starting with backslash \. For example: \n represents new line.

List of Escape Sequences in C

Escape SequenceMeaning
\aAlarm or Beep
\bBackspace
\fForm Feed
\nNew Line
\rCarriage Return
\tTab (Horizontal)
\vVertical Tab
\\Backslash
\’Single Quote
\”Double Quote
\?Question Mark
\nnnoctal number
\xhhhexadecimal number
\0Null

Summary

Here we learn all the types of datatypes in c , format specifiers , modifiers & Escape sequence used in C . Hope you loved and enjoyed this blog . Follow our site for more blogs like this.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button