Data types
In C programming, data types are declarations for variables. This determines the type and size of data associated with variables.
Data types in c refer to an extensive system used for declaring variables or functions of different types. The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted.
In the C programming language, data types constitute the semantics and characteristics of storage of data elements. They are expressed in the language syntax in form of declarations for memory locations or variables. Data types also determine the types of operations or methods of processing of data elements.
C language is rich in it’s data types C supports different types of data types C supports several differently types of data, each of which may be represented with in the computer memory. Data type is specified or we say to computer what kind of data you want to requited in C. mainly we are having four data types.
Int [integer type]
Float [real type]
Char [character type]
Double [precision floating points]
Void [value less]
INTEGER :
Integer types can be signed (with negative values) or unsigned values (only positive). Int values are always signed unless specifically mentioned.
Int means integer. It is used to define or declare normal number 0-9.
Integer types are further classified as –
Data type Range
int
signed int −32,768 to 32,767
unsigned int 0 to 65,535
short int
signed short int -2,147,483,648 to 2,147,483,647 (4 bytes)
unsigned short int 0 to 4,294,967,295 (4 bytes)
long int
signed long int -2,147,483,648 to 2,147,483,647 (4 bytes)
unsigned long int 0 to 4,294,967,295 (4 bytes)
We have used %hd for short, %d for int, and so on for printing each data type.
Note that we have used ‘long long’ for sum, which is 8 bytes, whereas long is 4 bytes. Though in practical situations, we may not use numbers that are this big, it is good to know the range and what data type we should use for programs with exponential calculations. We can use %u in place of %d for unsigned int but even %d works. Let us say the value of long notprime = -2300909090909933322; has a minus, but we print it as notprime is %lu, the correct value will not be printed. This is why it is safe to use %ld, unless you want the values to be always unsigned. If we add more digits to short int num1 = 10000, it will be out of range and will print wrong value. ‘short int’ can be used to limit the size of the integer data type.
Size : 2 bytes
Represent : %d
Range :-32, 768 to +32, 767
Syntax : data type variable name [size]
Example : int a. b
FLOAT :
The floating point data type allows the user to type decimal values. For example, average marks can be 97.665. if we use int data type, it will strip off the decimal part and print only 97. To print the exact value, we need ‘float’ data type. However, you will get the result of the mark as 67.00000, which may not be a pleasant sight with a lot of redundant zeroes. If you try to print the value of mark as %d after declaring it as float, you will not get 67. Try to run this program and see what value you get. Float is 4 bytes, and we can print the value using %f. The float can contain int values too. Float data type is used to represent real values containing decimal portion.
Size : 4 bytes
Represent : %f
Range : 3.4e-38 to 3.4e38
Precision : 6[1.000011]
Example : float a, b
CHAR :
char stores a single character. Char consists of a single byte.
For example,
char group = ‘B’;
To print a name or a full string, we need to define char array.
char group = 'B';
char name[30] = "Student1";
printf("group is %c, name is %s", group, name);
Note that for a single character, we use single quotes, but for String (character array), we use double-quotes. Since its an array, we have to specify the length (30 in this case).
Just like the int data type, char can be signed (range from -128 to +127) or unsigned (0 to 255). C stores the binary equivalent of the Unicode/ASCII value of any character that we type. In our above example, the char group will be stored as a value ‘066’.
You can think of char also as an int value, as char takes int values too. The importance of signed and unsigned comes when you store an int between the specified range in a char.
Here is an example to help understand signed and unsigned chars better –
signed char char1 = -127;
unsigned char char2 = -127;
printf("char1 is %d, char2 is %d", char1, char2);
Note that since we are taking in int values, we will print as %d and not %c. Since char1 is signed, the printf will give value as -127. However, char2 is unsigned, which means the range is from 0 to 255, -127 is out of range. So, it will print 129. Same way, if you assign char2 as -1, you will get a value of 255. Char data type is used to represent characters. In this data type we are having two specifies but memory allocation be same, which means 1 byte.
%s: this specifier is used to accepts or print strings.
%c: this specifier is used to represents singal character.
Size : 1 byte
Range : -128 to +127
Example : char name [10]
Example : char name;
You can think of float, double and long double similar to short int, int, and long int. Double is 8 bytes, which means you can have more precision than float. This is useful in scientific programs that require precision. Float is just a single-precision data type; double is the double-precision data type. Long Double is treated the same as double by most compilers; however, it was made for quadruple data precision. Double data is used type is used to represent real value that means it will take decimal points. the average is 679999999.454000, the score is 680000000.000000
Note the difference in outputs – while double prints the exact value, float value is rounded off to the nearest number.
Size : 8 bytes
Range : 1.7e-308 to 1.7e+308
Precision : 10 [1.0000011001]
Example : double
VOID :
The void is just an empty data type used as a return type for functions. The absence of any other data type is void. When you declare a function as void, it doesn’t have to return anything.
For example –
void swapNumbers(int a, int b)
{
//multiple lines of code here
}
Same way, if a function does not have any parameters, that can be indicated with the void.
int getNumbers(void)
{
// some code
}
We can declare a void pointer so that it can take a variable of any data type. A pointer declared as void becomes a general-purpose pointer –
char *ptr;
int value;
ptr = &value; //this will give error because we cannot point a char pointer to an int value
However,
void *ptr;
will solve this problem and now we can write
ptr = &value;
without any compilation errors. You can assign any data type to the void pointer.
Void is another data type available in C.It decease function as returning No value or creates generic points.
Void means nothing.
No comments:
Post a Comment