# C PROGRAMS
C Basics
➤ Introduction to C
➤ Compile and Execute
➤ Hello, World! in C
➤ Identifiers & Keywords
➤ Data types in C
➤ Variables in C
➤ Comments in C
➤ Operators in C
➤ Bitwise Operators
➤ Printf() in C
➤ Input using scanf()
C Basic Programs
➤ Size & Range of Data Types
➤ C Programs for Beginners
➤ Add Subtract Multiply & divide
➤ Sum & Average of 3 Numbers
➤ Simple and Compound Interest
➤ Distance between two Points
➤ Find Circle Triangle Area
➤ ASCII Value of Character
➤ Find Square root of Number
➤ Different Swapping Programs
Size and Range of data types in C. The size is calculated using sizeof(). The range of data types can be found by manually or using <limits.h> and <float.h>
The size of data types in C is dependent on the compiler or you can say that the system architecture i.e. 32-bit compiler or 64-bit compiler. The size of data type
Another factor on which the size of data type depends is the compiler on which you perform any program i.e. In turbo c/c++ the size of
For finding the size we need a sizeof() function defined under stdio.h. sizeof() function find the size in bytes. 0 or 1 takes 1 bit space. 1 byte = 8 bits . Using sizeof() we can find size of data-types or a variable also.
C Program to Find Size of Data Types
#include<stdio.h>
int main()
{
printf("Size of short is %ld bytes\n",sizeof(short));
printf("Size of int is %ld bytes\n",sizeof(int));
printf("Size of long is %ld bytes\n",sizeof(long));
printf("Size of float is %ld bytes\n",sizeof(float));
printf("Size of double is %ld bytes\n",sizeof(double));
printf("Size of long double is %ld bytes\n",sizeof(long double));
printf("Size of char is %ld bytes\n",sizeof(char));
printf("Size of void is %ld bytes\n",sizeof(void));
return 0;
}
Output:-
Size of short is 2 bytes
Size of int is 4 bytes
Size of long is 8 bytes
Size of float is 4 bytes
Size of double is 8 bytes
Size of long double is 16 bytes
Size of char is 1 byte
Size of void is 1 byte
Note:- Sizes of unsigned and signed data types are the same.
Size of a Variable
In a similar way, we can also find the size of any variables. The size of a variable completely depends on the data type of variable. If a variable is of data type int then the size of the variable will be 2 or 4 bytes ( Because the size of int is 2 or 4 bytes).
#include<stdio.h>
int main()
{
int x = 10;
char c;
printf("Size of variable x = %ld bytes\n",sizeof(x));
printf("Size of variable c = %ld byte\n",sizeof(c));
return 0;
}
Output:-
Size of variable x = 4 bytes
Size of variable c = 1 byte
Size of Structure data type
#include<stdio.h>
struct employee
{
int number;
char name[20];
float salary;
};
int main()
{
struct employee x;
//Calculate size using variable name
printf("Size of employee: %ld\n",sizeof(x));
//Calculate size using data type
printf("Size of employee: %ld\n",sizeof(struct employee));
return 0;
}
Output:-
Size of employee: 28
Size of employee: 28
To store more than one primitive data types, Structure (user defined data type) was introduced. In above example, struct employee is a data type. As int is a data type, similarly struct employee is also a data type. Notice that struct alone or employee alone is not a data type.
Whenever we write int than it does not allocate memory because it is only a data-type. Similarly, happens with struct employee. When we write, struct employee x (where x is a variable) then it allocates memory.
Primitive data type of number is int, so, it will hold 4 bytes. name[20] is char of array [20], so it will hold 20*1 = 20 bytes (1 char holds 1 bytes), and salary is of float data type. Hence it holds 4 bytes. finally 4+20+4 = 28 bytes.
One variable x holds 28 bytes. If 2 varibles will be present than it will hold 28*2 bytes i.e (28* number of variables).
Range of Data types <limits.h> and <float.h> in C
To find the limit of primitive data types we need <limits.h>. In unsigned, the minimum value is not present because in unsigned, limits start with zero which can be remembered easily. The system file limits.h available in C-Compliant compilers contains the upper and lower limits of integer types. The user may include before main().
C Program to Find Range of Data Types
#include<stdio.h>
#include<limits.h>
int main()
{
printf("The number of bits in a byte = %d\n", CHAR_BIT);
printf("\nThe minimum value of Signed CHAR is = %d\n", SCHAR_MIN);
printf("The maximum value of Signed CHAR is = %d\n", SCHAR_MAX);
printf("The minimum value of CHAR is = %d\n", CHAR_MIN);
printf("The maximum value of CHAR is = %d\n", CHAR_MAX);
printf("The maximum value of Unsigned CHAR is = %u\n", UCHAR_MAX);
printf("\nThe minimum value of Signed Short is = %d\n", SHRT_MIN);
printf("The maximum value of Signed Short is = %d\n", SHRT_MAX);
printf("The maximum value of Unsigned Short is = %u\n",
USHRT_MAX);
printf("\nThe minimum value of Signed INT is = %d\n", INT_MIN);
printf("The maximum value of Signed INT is = %d\n", INT_MAX);
printf("The maximum value of Unsigned INT is = %u\n", UINT_MAX);
printf("\nThe minimum value of Signed LONG is = %ld\n", LONG_MIN);
printf("The maximum value of Signed LONG is = %ld\n", LONG_MAX);
printf("The maximum value of Unsigned LONG is = %lu\n",
ULONG_MAX);
return 0;
}
Output:-
The number of bits in a byte = 8
The minimum value of Signed CHAR is = -128
The maximum value of Signed CHAR is = 127
The minimum value of CHAR is = -128
The maximum value of CHAR is = 127
The maximum value of Unsigned CHAR is = 255
The minimum value of Signed Short is = -32768
The maximum value of Signed Short is = 32767
The maximum value of Unsigned Short is = 65535
The minimum value of Signed INT is = -2147483648
The maximum value of Signed INT is = 2147483647
The maximum value of Unsigned INT is = 4294967295
The minimum value of Signed LONG is = -9223372036854775808
The maximum value of Signed LONG is = 9223372036854775807
The maximum value of Unsigned LONG is = 18446744073709551615
Table
Data Type | Constant | Value |
Signed char | SCHAR_MIN SCHAR_MAX | -128 127 |
char | CHAR_MIN CHAR_MAX | -128 127 |
Unsigned char | UCHAR_MAX | 255 |
Short int | SHRT_MIN SHRT_MAX | -32768 32767 |
Unsigned short int | USHRT_MAX | 65535 |
int | INT_MIN INT_MAX | -2147483648 2147483647 |
Unsigned int | UINT_MAX | 4294967295 |
Long int | LONG_MIN LONG_MAX | -9223372036854775808 9223372036854775807 |
Unsigned long int | ULONG_MAX | 18446744073709551615 |
The following are the constants defined in the header file <float.h>
Data Type | Constant |
float | FLT_MIN FLT_MAX |
double | DBL_MIN DBL_MAX |
long double | LDBL_MIN LDBL_MAX |
#include<stdio.h>
#include<float.h>
int main()
{
printf("The minimum value of float is = %.10e\n", FLT_MIN);
printf("The maximum value of float is = %.10e\n", FLT_MAX);
printf("\nThe minimum value of Double is = %.10e\n", DBL_MIN);
printf("The maximum value of Double is = %.10e\n", DBL_MAX);
printf("\nThe minimum value of LONG Double is = %.10Le\n",
LDBL_MIN);
printf("The maximum value of LONG Double is = %.10Le\n",
LDBL_MAX);
return 0;
}
Output:-
The minimum value of float is = 1.1754943508e-38
The maximum value of float is = 3.4028234664e+38
The minimum value of Double is = 2.2250738585e-308
The maximum value of Double is = 1.7976931349e+308
The minimum value of LONG Double is = 3.3621031431e-4932
The maximum value of LONG Double is = 1.1897314954e+4932
The size of data types in C depends on the compiler, and the range also depends on the compiler.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!
That is a good tip particularly to those new to the blogosphere. Simple but very precise info. Thanks for sharing this one. A must-read post!
Like!! I blog quite often and I genuinely thank you for your information. The article has truly peaked my interest.