Data Types
Data Types
We understand that Csupports many different types of variables and each type of variable is used for storing kind of data
types that store integers
types that store nonintegral numerical values
types that store characters
int
float
double
char
Bool
the difference between the types is in the amount of memory they occupy and the range of values they can hold
the amount of storage that is allocated to store aparticular type of data
depends on the computer you are running (machine-dependent)
an integer might take up 32 bits on your computer, or perhaps it might be stored in 64
CFOR BEGINNERS
Overview Ccodemy
int
avariable of type int can be usedto contain integral values only (values that do not contain decimal
places)
a minus sign preceding the data type and variable indicates that the value is negative
if an integer is preceded by a zero and the letter x(either lowercase or uppercase), the value is taken
as being expressed in hexadecimal (base 16) notation
int rgbColor = 0xFFEFOD;
the values3., 125.8, and -.0001 are all valid examples of floating-point
constants that can be assigned to a variable
CFOR BEGINNE r
Overview
double
the double type is the same as type float, only with roughly twice the
precision
used whenever the range provided by a float variable is not sufficient
can store twice as many significant digits
most computers represent double values using 64 bits
To explicitly express afloat constant, append either anfor Fto the end of
number
12.5f
CFOR BEGINNERS
Coffers three adjective keywords to modify the basic integer type (can also be used by itself)
short, long,and unsigned
The type short int, or short may use less storage than int, thus saving space when only small numbers are needed
can be used when the program needs a lot of memory and the amount of available memory is limited
The type long int, or long, may use more storage than int, thus enabling youto express larger integer values
The type long long int,or long long may use more storage than long
Aconstantvalue of type long int is formed by optionally appending the letter L(upper-or lowercase) onto the end of an integer
Constant
C FOR BEGINNERS
Overview }eon
Other Data Types (contd)
Type specifiers can also be applied to doubles
longdouble US_deficit_2017:
Along double constant is written as afloating constant with the letter lor Limmediately following
1.234e+7L
The type unsigned int, or unsigned, is used for variables that have only nonnegative values (positive)
unsigned int counter;
the accuracy of the integer variable is extended
The keyword signed can be used with any of the signedtypes to make your intent explicit
short, short int, signed short, and signed short int are all names for the same type
CFOR BEGINNERS
Overview lean CCodemy
Enums
" a data type that allows a programmer to define a variable and specify the valid values that could be
stored into that variable
can create a variable named "myColor" and it can only contain one of the primary colors, red,
yellow, or blue, and no other values
" Your first have to define the enum type and give it a name
initiated by the keyword enum
then the name of the enumerated data type
then list of identifiers (enclosed in aset of curly braces) that define the permissible values that
can be assigned to the type
CFOR BEGINNERS
Enums and Char
oon Goodeny
Enums (cont'd)
To declare a variable to be of type enum primaryColor
use the keyword enum
followed by the enumerated type nan
followed by the variable list. So the statement
CFOR BEGINNERS
Enums and Char lean Coodemy
Enums as ints
the compiler actually treats enumeration identifiers as integer constants
first name in list is 0
thisMonth = February:
the value 1is assigned to thisMonth (and not the name February) because it is the second ident1fier listed inside the
enumeration list
if you want to have a specific integer value associated with an enumeration identifier, the integer can be assigned to the
identifier when the data type is defined
CFOR BEGINNERS
Enums and Char {con
Char
Chars represent a single character such as the letter 'a', the digit character '6', or asemicolon
(:")
CFOR BEGINNERS
Enums and Char Lean Coodemy
Declaring achar
char broiled; /* declare achar variable */
broiled =T': * OK */
broiled =T; * NO! Thinks T is avariable */
broiled="T": * NO! Thinks "T" is a string */
If youomit the quotes, the compiler thinks that Tis the name of a variable
Escape sequences must be enclosed in single quotes when assignedto a character variable
char x \n';
and then print the variable x to advance the printer or screen one line
C FOR BEGINNERS
Enums and Char
leano
Coodemy
Escape Characters (contd)
Sequence Meaning
\a Alert (ANSI C).
\b Backspace.
\f Form feed.
\n Newiine.
\r Carriage return.
\t Horizontal tab:
Vertical tab.
Backslash (\).
Single quote (').
Double quote (" ).
Question mark (?).
\000 Octal value. (o represents an octal digit.)
\xhh Hexadecimal value. (h represents a hexadecimal digit.) Taken from "C Primer Plus", Prata
C FOR BEGINNERS
Enums and Char { }loon Oco0my