C Programming Data Types - primitive, bool, enum, struct, array, pointer

<<Previous

Next >>





A c program takes data as input and processes that data to provide meaningful insights. Many different kinds of data exist. Data types are used to determine the type and size of the values in C programming. Data Types are also used to specify the space required for variables in C programs. The following data types are commonly used in C programming.

  • Basic data types: int, char, float, double
  • Boolean data type: bool
  • Enumeration Data type: enum
  • Derived data types: array, pointer, structure, union
  • void data type

Integer Data Types

An Integer is always a whole number and can be positive or negative. Integer data does not have a decimal point.

For example:
42 245 -89 -3 125

The int keyword is used to declare an integer variable. Various integer data types, the memory size they occupy, the range of values they represent, and the format specifier are as shown in the table below. Format specifier used to specify the format of data for gathering input from devices like keyword and also used to specify the format of data to be displayed or printed in output devices like monitor, printer or plotter

Type Memory RequirementRange Format specifier
int 2 bytes -32,768 to 32,767%d or %ld
short int2 bytes-32,768 to 32,767%d or %I
long int4 bytes -2,147,483,648 to 2,147,483,647%ld
signed short int2 bytes-32,768 to 32,767%d
signed long int4 bytes-2,147,483,648 to 2,147,483,647%ld
unsigned short2 bytes0 to 65,535%d or %hu
unsigned long 4 bytes0 to 4,294,967,295%lu

Floating Point Data Type

Numbers with decimal points are floating-point numbers in C programming. Floating-point numbers require twice as much memory space as an integer. The float data type takes 4 bytes of storage whereas the double data type takes 8 bytes of storage. float data type guarantees precision for 6 digits after the decimal point. double data types guarantee precision for 15 digits after the decimal point.

For example:

255.12    0.666      -523.1      25.23     0.0    2.586745

The float and double keywords are used to define the float and double variables repectively. Following are the types of floating point data type.


TypeMemory RequirementRangeFormat Specifier
float4 bytes3.4E-38 to 3.8E+38%f or %E or %e
double8 bytes1.7E308 to 1.7E+308%lf or %E or %e
long double10 bytes3.4E-4932 to 3.4E+4932%Lf

If you want to print the range value of any data type, we can use the parameters such as INT_MIN, INT_MAX, FLT_MIN, and so on. These parameters are presented in limits.h and float.h header file

#include <stdio.h>
#include <limits.h>
#include <float.h>
main()
{
     printf("Integer range:\t%d,\t%d \n ", INT_MIN,INT_MAX);

     printf("Float range:\t%E,\t%E \n ", FLT_MIN,FLT_MAX);

     printf("Double range:\t%lf,\t%lf \n ", DBL_MIN,DBL_MAX);
}

When you compile and execute the above program, you will get the following result.

Integer range: -32768, 32767

Float range: 1.175494E-38, 3.402823E+38

Double range: 2.225074E-308, 1.797693E+308

bool data type

bool data type was not available in earlier versions of C language. bool is available from C99 using the stdbool.h


#include 

void main(){
bool flag = false;
bool newFlag = true;
	if(newFlag){
	    printf("Yes it is true.");
	}
}


char Data Type

The char data type can store a single character. C language knows 256 different characters as defined in the ASCII table. In C, the single character is written enclosed by a single quotation. The char keyword is used to define the character variable. The following are the types of character data type.

TypeMemory RequirementRangeFormat specifier
char 1 byte-128 to 127 or 0 to 255 %c
signed char1 byte-128 to 127%c
unsigned char 1 byte0 to 255%c

Following are examples of character data.

'a'      'B'   '10'     '%'   '-'

A group of multiple characters or the sequence of characters are called a string which is enclosed by double-quotes. Examples of C string:

" C is a Programming language "
" easy to learn "

void Data Type

  • void data type is an empty data type. That means, it does not have value.
  • void data type is mainly used for function definition and pointer declaration.
  • In the later section, we see how void will be used in c program.

The sizeof() function

The sizeof() function is used to determine the required memory space that is allocated to each data type in C Programming Language.

#include <stdio.h>
#include <limits.h>
#include <float.h>
main()
{
     int num;

     unsigned int a;

     float b;

     char m;

     printf("Integer Type:\t %d \n ",sizeof(num));

     printf("Unsigned Integer Type:\t %d \n ",sizeof(a));

     printf("Float Type:\t %f\n ", sizeof(b));

     printf("Character Type:\t %c\n ", sizeof(m));

}

Output:

Integer Type: 2
Unsigned Integer Type: 4
Float Type: 4
Character Type: 1

<< Previous

Next >>




strcat() - String Concatenation        strcmp() - String Compare


strcpy() - String Copy        strlen() - String Length














C Programming Data Types - primitive, bool, enum, struct, array, pointer

<<Previous

Next >>





A c program takes data as input and processes that data to provide meaningful insights. Many different kinds of data exist. Data types are used to determine the type and size of the values in C programming. Data Types are also used to specify the space required for variables in C programs. The following data types are commonly used in C programming.

  • Basic data types: int, char, float, double
  • Boolean data type: bool
  • Enumeration Data type: enum
  • Derived data types: array, pointer, structure, union
  • void data type

Integer Data Types

An Integer is always a whole number and can be positive or negative. Integer data does not have a decimal point.

For example:
42 245 -89 -3 125

The int keyword is used to declare an integer variable. Various integer data types, the memory size they occupy, the range of values they represent, and the format specifier are as shown in the table below. Format specifier used to specify the format of data for gathering input from devices like keyword and also used to specify the format of data to be displayed or printed in output devices like monitor, printer or plotter

Type Memory RequirementRange Format specifier
int 2 bytes -32,768 to 32,767%d or %ld
short int2 bytes-32,768 to 32,767%d or %I
long int4 bytes -2,147,483,648 to 2,147,483,647%ld
signed short int2 bytes-32,768 to 32,767%d
signed long int4 bytes-2,147,483,648 to 2,147,483,647%ld
unsigned short2 bytes0 to 65,535%d or %hu
unsigned long 4 bytes0 to 4,294,967,295%lu

Floating Point Data Type

Numbers with decimal points are floating-point numbers in C programming. Floating-point numbers require twice as much memory space as an integer. The float data type takes 4 bytes of storage whereas the double data type takes 8 bytes of storage. float data type guarantees precision for 6 digits after the decimal point. double data types guarantee precision for 15 digits after the decimal point.

For example:

255.12    0.666      -523.1      25.23     0.0    2.586745

The float and double keywords are used to define the float and double variables repectively. Following are the types of floating point data type.


TypeMemory RequirementRangeFormat Specifier
float4 bytes3.4E-38 to 3.8E+38%f or %E or %e
double8 bytes1.7E308 to 1.7E+308%lf or %E or %e
long double10 bytes3.4E-4932 to 3.4E+4932%Lf

If you want to print the range value of any data type, we can use the parameters such as INT_MIN, INT_MAX, FLT_MIN, and so on. These parameters are presented in limits.h and float.h header file

#include <stdio.h>
#include <limits.h>
#include <float.h>
main()
{
     printf("Integer range:\t%d,\t%d \n ", INT_MIN,INT_MAX);

     printf("Float range:\t%E,\t%E \n ", FLT_MIN,FLT_MAX);

     printf("Double range:\t%lf,\t%lf \n ", DBL_MIN,DBL_MAX);
}

When you compile and execute the above program, you will get the following result.

Integer range: -32768, 32767

Float range: 1.175494E-38, 3.402823E+38

Double range: 2.225074E-308, 1.797693E+308

bool data type

bool data type was not available in earlier versions of C language. bool is available from C99 using the stdbool.h


#include 

void main(){
bool flag = false;
bool newFlag = true;
	if(newFlag){
	    printf("Yes it is true.");
	}
}


char Data Type

The char data type can store a single character. C language knows 256 different characters as defined in the ASCII table. In C, the single character is written enclosed by a single quotation. The char keyword is used to define the character variable. The following are the types of character data type.

TypeMemory RequirementRangeFormat specifier
char 1 byte-128 to 127 or 0 to 255 %c
signed char1 byte-128 to 127%c
unsigned char 1 byte0 to 255%c

Following are examples of character data.

'a'      'B'   '10'     '%'   '-'

A group of multiple characters or the sequence of characters are called a string which is enclosed by double-quotes. Examples of C string:

" C is a Programming language "
" easy to learn "

void Data Type

  • void data type is an empty data type. That means, it does not have value.
  • void data type is mainly used for function definition and pointer declaration.
  • In the later section, we see how void will be used in c program.

The sizeof() function

The sizeof() function is used to determine the required memory space that is allocated to each data type in C Programming Language.

#include <stdio.h>
#include <limits.h>
#include <float.h>
main()
{
     int num;

     unsigned int a;

     float b;

     char m;

     printf("Integer Type:\t %d \n ",sizeof(num));

     printf("Unsigned Integer Type:\t %d \n ",sizeof(a));

     printf("Float Type:\t %f\n ", sizeof(b));

     printf("Character Type:\t %c\n ", sizeof(m));

}

Output:

Integer Type: 2
Unsigned Integer Type: 4
Float Type: 4
Character Type: 1

<< Previous

Next >>






strncat() - String n Concatenation        strlwr() - String Lower       

strncmp() - String n Compare       strncpy() - String n Copy