Datatype of Variable

A variable in C language must be given a type, which defines what type of data the variable will hold. It can be:

  • char: Can hold/store a character in it.
  • int: Used to hold an integer.
  • float: Used to hold a float value.
  • double: Used to hold a double value.
  • void

CHAR holds characters- things like letters, punctuation, and spaces. In a computer, characters are stored as numbers, so char holds integer values that represent characters. The actual translation is described by the ASCII standard. Here’s a handy table for looking up that.

The actual size, like all other data types in C, depends on the hardware you’re working on. By minimum, it is at least 8 bits, so you will have at least 0 to 127. Alternatively, you can use signed char to get at least -128 to 127.

INT  The amount of memory that a single int takes depends on the hardware. However, you can expect an int to be at least 16 bits in size. This means that it can store values from -32,768 to 32,767, or more depending on hardware.

Like all of these other data types, there is an unsigned variant that can be used. The unsigned int can be positive and zero but not negative, so it can store values from 0 to 65,535, or more depending on hardware.

SHORT The long data type stores integers like int, but gives a wider range of values at the cost of taking more memory. Long stores at least 32 bits, giving it a range of -2,147,483,648 to 2,147,483,647. Alternatively, use unsigned long for a range of 0 to 4,294,967,295.

LONG 

Variable declaration refers to the part where a variable is first declared or introduced before its first use. Variable definition is the part where the variable is assigned a memory location and a value. Most of the times, variable declaration and definition are done together.


Is it possible to have separate declaration and definition? It is possible in case of extern variables and functions. See question 1 of this for more details.

 

Even longer integers: long long:

The long long data type is overkill for just about every application, but C will let you use it anyway. It’s capable of storing at least −9,223,372,036,854,775,807 to 9,223,372,036,854,775,807. Alternatively, get even more overkill with unsigned long long,
which will give you at least 0 to 18,446,744,073,709,551,615.

FLOAT  float takes at least 32 bits to store, but gives us 6 decimal places from 1.2E-38 to 3.4E+38.

DOUBLE double takes double the memory of float (so at least 64 bits). In return, double can provide 15 decimal place from 2.3E-308 to 1.7E+308.

sizeof operator in C

Sizeof is a much used operator in the C programming language. It is a compile time unary operator which can be used to compute the size of its operand. The result of sizeof is of unsigned integral type which is usually denoted by size_t. sizeof can be applied to any data-type, including primitive types such as integer and floating-point types, pointer types, or compound datatypes such as Structure, union etc.

WHERE TO USE  

sizeof() operator is used in different way according to the operand type.

1. When operand is a Data Type. When sizeof() is used with the data types such as int, float, char… etc it simply returns the amount of memory is allocated to that data types.

Lets See an Example :

#include <stdio.h>
int main()
{
printf("%lu\n"sizeof(char));
printf("%lu\n"sizeof(int));
printf("%lu\n"sizeof(float));
printf("%lu"sizeof(double));
return 0;
}
OUTPUT
1
4
4
8
 
2.  When operand is an expression

When sizeof() is used with the expression, it returns size of the expression. Let see example:

#include <stdio.h>
int main()
{
int a = 0;
double d = 10.21;
printf("%lu"sizeof(a + d));
return 0;
}
OUTPUT 
8
 

In this way we can use size of do some exercises of coding to get more hands on for size_of .

Exercise 1 . Write a c program to find size of each data types using size_of in c .

Exercise 2 . Write a c program to Find the length of a string using sizeof .
Note : If you have any doubts feel free to contact us If You Didnt understand any concept ..