0% found this document useful (0 votes)
6 views

Data Types

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Data Types

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Overview

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

Some examples of basic data types in Care:

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

the int type is a signed integer


it must be an integer and it can be positive, negative, or zero

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;

thevalues 158,-10, and 0are all valid examples of integer constants


noembedded spaces are permitted between the digits
values larger than 999 cannot be expressed using commas (12,000 must be written as 12000)
CFORBEGINNERS
Overview Ccodemy
float
" Avariable to be of type float can be used for storing floating-point numbers
(values containing decimal places)

the values3., 125.8, and -.0001 are all valid examples of floating-point
constants that can be assigned to a variable

floating-point constants canalso be expressed in scientific notation


1.7e4 isa floating-point value expressed in this notation and represents
the value 1.7 x 10 to the power of 4

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

allfloating-point constants are taken as double values by the Ccompiler

To explicitly express afloat constant, append either anfor Fto the end of
number
12.5f

CFOR BEGINNERS

Overview {eon Coodemy


Bool
" the Bool data type can be used to store just the values 0or 1
used for indicating an on/off, yes/no, or true/false situation (binary
choices)

Bool variables are used in programsthat need to indicate a Boolean


condition
a variable of this type might be used to indicate whether all data has
been read from a file

0 is used toindicate afalse value

1indicates atrue value


CFOR BEGINNERS
Overview Coodemy
Example
#include <stdio.h>

int main (void)


{
int integerVar= 100;
float floatingVar =331.79;
double doubleVar = 8,44e+11;
Bool boolVar = 0;
return 0;
}
Other DataTypes
the int type will probably meet most of your integer needs when beginninginC

However, Coffers many other integer types


gives the programmer the optionof matching a type to aparticular use case
integer types vary in the range of values offered and in whether negative numbers can be used

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

long int numberOfPoints = 131071100L:

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

enum primaryColor {red, yellow, blue };


variables declared to be of this data type can be assigned the values red, yellow, and blue inside the
program, and no other values

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

enum primaryColor myColor, gregsColor;


defines the two variables myColor and gregsColor to be of type primaryColor
the only permissible values that can be assigned to these variables are the names red,
yellow, and blue
myColor =red;
Another example
enum month { January, February, March, April, May, June, July, August, September, October,
November, December };

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

enum month thisMonth,

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

enum direction { up. down, left = 10, right };


an enumerated data type direction is definedwith the values up, down, left, and right
up =0 because it appears first in the list
1to down because it appears next
10 1o left because it is explicitly assigned this value
11 to right because it appears immediately after left in the list

CFOR BEGINNERS
Enums and Char {con
Char
Chars represent a single character such as the letter 'a', the digit character '6', or asemicolon
(:")

Character literals use single quotes such as 'A 'or Z'

You can also declare char variables to be unsigned


can be used toexplicitly tell the compiler thata particular variable is asigned quantity
We will talk about acharacter string in another lecture, much different than asingle character

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

If you use double quotes, it thinks you are using a string

you can also use the numerical code to assign values

char grade = 65; /* ok for ASCIl, but poor style */


CFOR BEGINNERS
Enums and Char {lean Coodemy
Escape Characters
C contains special characters that represent actions
backspacing
going to the next line
making the terminal bell ring (or speaker beep)

We can represent these actions by using specialsymbol sequences


called escape sequences

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

You might also like