Transcript LECT12.PPT
Variables in C
Declaring ,
Naming, and
Using Variables
CMSC 104
1
Using Variables
You may declare variables in C.
The declaration includes the data type
you need.
Examples of variable declarations:
int meatballs ;
float area ;
CMSC 104
2
Declaring Variables
When we declare a variable:
o space in memory is set aside to hold that
data type
o That space is associated with the variable
name
o Visualization of the declaration
int meatballs ;
meatballs
CMSC 104
FE07
3
Legal Variable Names
Variable names in C must be valid
identifiers
o Consists of letters, digits and underscores.
o May be as long as you like, but only the
first 31 characters are significant.
o May NOT begin with a number
o May not be a C keyword
CMSC 104
4
Naming Conventions
Begin variable names with lowercase
letters
Use meaningful identifiers
Separate “words” within identifiers with
underscores or mixed upper and lower
case.
Example: surfaceArea surface_Area
or surface_area
CMSC 104
Be consistent !!
5
Naming Conventions
(continued)
Use all uppercase for symbolic
constants ( #define )
Example: PI (#define PI 3.14159 )
Function names follow the same rules
as variables
CMSC 104
6
Case Sensitive
C is case sensitive
o It matters whether something is upper or
lower case
o Example: area is different than Area
which is different than AREA
CMSC 104
7
More About Variables
C has 3 basic predefined data types
Integers
o int, long int, short int, unsigned int
Floating point numbers
o float, double
Characters
o char
CMSC 104
8
Initializing Variables
Variables may be initialized
o int x = 7;
o float y = 5.9;
o char c = ‘A’;
CMSC 104
Do not “hide” the initialization
o put initialized variables on a separate line
o a comment is probably a good idea
o int y = 6; /* feet per fathom */
o NOT int x, y = 6, z;
9
Keywords in C
CMSC 104
auto
case
const
default
double
enum
float
goto
break
char
continue
do
else
extern
for
if
int
register
short
signed
sizeof
struct
typedef
unsigned
volatile
long
return
static
switch
union
void
while
10
Which Are Legal Identifiers ?
AREA
3D
Last-Chance
x_yt3
num$
lucky***
CMSC 104
area_under_the_curve
num45
#values
pi
%done
11
Declarations and assignments
wreck.c
#include <stdio.h>
main ( )
{
int inches, feet, fathoms ;
fathoms = 7 ;
feet = 6 * fathoms ;
inches = 12 * feet ;
inches
feet
fathoms
fathoms
7
feet
42
inches
504
CMSC 104
}
12
wreck.c (cont’d)
main ( )
{
printf (“Its depth at sea: \n”) ;
printf (“ %d fathoms \n”, fathoms) ;
printf (“ %d feet \n”, feet);
printf (“ %d inches \n”, inches);
}
%d is a place holder - indicates that the
value of the integer variable is to be printed in
decimal form (rather than binary or hex) at
CMSC 104 that location.
13
Floating point numbers
Are numbers that can contain decimal
points.
What if the depth were really 5.75
fathoms ? ... Our program, as it is,
couldn’t handle it.
We can declare floating point variables
like this :
float fathoms ;
float feet ;
CMSC 104
14
Floating point version of wreck.c
(works for any depth shipwreck)
#include <stdio.h>
main ( )
{
float fathoms, feet;
printf (“Enter the depth in fathoms : ”);
scanf (“%f”, &fathoms);
feet = 6.0 * fathoms;
printf (“She’s %f feet down.\n”, feet);
}
CMSC 104
15