C: Input, Variables, and Data Types - Data Types
(Page 3 of 4 )
There are four basic data types in C. They are Int, Char, Float, and Double.
Data Type: Int
This data type is used to store integers (whole numbers). They are usually 32 bits, though at this point I wouldn't concern myself with that. Variables of this type can hold values ranging from -2147483648 to 2147483647. Here is an example of how you would declare an int variable and store data in it:
int weight = 320;
Data Type: Char
This data type stores a single letter, number, or special character. You can also use the ASCII character set, but again, we won't be discussing that here; just know for now that the option is available. Here is a sample of a program using a char variable:
#include <stdio.h>
int main ()
{
char lyric1;
char lyric2;
char lyric3;
lyric1 = 'A';
lyric2 = 'B';
lyric3 = 'C';
printf("It's easy as %c%c%c ", lyric1,lyric2,lyric3);
return(0);
}
Data Type: Float
This data type is used for floating point values. To work, float literals must have a suffix of either f or F, as in: 3.26852f or 2.0F. Note that the float only stores single-precision floating point numbers.
Data Type: Double
This data type is very similar to the float, except that it allows you to store double-precision floating point numbers. It takes up 8 bytes typically and does not require the f or F as a suffix: 9.1298273646552717, 2.0, 3.098e+23 are all valid. If you use a number without a decimal, it will be interpreted as an int instead.
Next: Data Type Modifiers >>
More BrainDump Articles
More By James Payne