l 08 Variables Inc
-
Upload
tessy-joseph -
Category
Documents
-
view
214 -
download
0
Transcript of l 08 Variables Inc
-
7/27/2019 l 08 Variables Inc
1/22
CMSC 104, Version 9/01 1
Variables in C
Topics
Naming Variables
Declaring Variables
Using Variables
The Assignment Statement
Reading
Sections 2.3 - 2.4
-
7/27/2019 l 08 Variables Inc
2/22
CMSC 104, Version 9/01 2
What Are Variables in C?
Variables in C have the same meaning asvariables in algebra. That is, they representsome unknown, or variable, value.
x = a + b
z + 2 = 3(y - 5)
Remember that variables in algebra arerepresented by a single alphabeticcharacter.
-
7/27/2019 l 08 Variables Inc
3/22
CMSC 104, Version 9/01 3
Naming Variables
Variables in C may be given representationscontaining multiple characters. But there arerules for these representations.
Variable names in Co May only consist of letters, digits, and
underscores
o May be as long as you like, but only the first 31characters are significant
o May not begin with a number
o May not be a C reserved word (keyword)
-
7/27/2019 l 08 Variables Inc
4/22
CMSC 104, Version 9/01 4
Reserved Words (Keywords) in C
auto break
case char
const continue default do
double else
enum extern float for
goto if
int long
register return
short signedsizeof static
struct switch
typedef unionunsigned void
volatile while
-
7/27/2019 l 08 Variables Inc
5/22
CMSC 104, Version 9/01 5
Naming Conventions
C programmers generally agree on thefollowing conventions for naming variables.
o Begin variable names with lowercase letters
o Use meaningful identifiers
o Separate words within identifiers with
underscores or mixed upper and lower case.
o Examples: surfaceArea surface_Area
surface_areao Be consistent!
-
7/27/2019 l 08 Variables Inc
6/22
CMSC 104, Version 9/01 6
Naming Conventions (cont)
Use all uppercase forsymbolic constants(used in #define preprocessor directives).
Examples:
#define PI 3.14159
#define AGE 52
-
7/27/2019 l 08 Variables Inc
7/22CMSC 104, Version 9/01 7
Case Sensitivity
C is case sensitiveo It matters whether an identifier, such as a
variable name, is uppercase or lowercase.
o Example:area
Area
AREAArEa
are all seen as different variables by thecompiler.
-
7/27/2019 l 08 Variables Inc
8/22CMSC 104, Version 9/01 8
Which Are Legal Identifiers?
AREA area_under_the_curve3D num45
Last-Chance #values
x_yt3 pi
num$ %done
lucky***
-
7/27/2019 l 08 Variables Inc
9/22CMSC 104, Version 9/01 9
Declaring Variables
Before using a variable, you must give thecompiler some information about thevariable; i.e., you must declare it.
The declaration statement includes thedata type of the variable.
Examples of variable declarations:
int meatballs ;float area ;
-
7/27/2019 l 08 Variables Inc
10/22CMSC 104, Version 9/01 10
Declaring Variables (cont)
When we declare a variableo Space is set aside in memory to hold a value of
the specified data type
o That space is associated with the variable name
o That space is associated with a unique address
Visualization of the declaration
int meatballs ;meatballs
FE07
garbage
-
7/27/2019 l 08 Variables Inc
11/22CMSC 104, Version 9/01 11
More About Variables
C has three basic predefined data types:
Integers (whole numbers)
o int, long int, short int, unsigned int
Floating point (real numbers)
o float, double
Characters
o char
At this point, you need only be concernedwith the data types that are bolded.
-
7/27/2019 l 08 Variables Inc
12/22CMSC 104, Version 9/01 12
Using Variables: Initialization
Variables may be be given initial values, orinitialized, when declared. Examples:
int length = 7 ;
float diameter = 5.9 ;
char initial = A ;
7
5.9
A
length
diameter
initial
-
7/27/2019 l 08 Variables Inc
13/22CMSC 104, Version 9/01 13
Using Variables: Initialization (cont)
Do not hide the initializationo put initialized variables on a separate line
o a comment is always a good idea
o Example:int height ; /* rectangle height */
int width = 6 ; /* rectangle width */
int area ; /* rectangle area */
NOT int height, width = 6, area ;
-
7/27/2019 l 08 Variables Inc
14/22
CMSC 104, Version 9/01 14
Using Variables: Assignment
Variables may have values assigned to them throughthe use of an assignment statement.
Such a statement uses the assignment operator =
This operator does not denote equality. It assigns
the value of the righthand side of the statement (theexpression) to the variable on the lefthand side.
Examples:
diameter = 5.9 ;area = length * width ;
Note that only single variables may appear on thelefthand side of the assignment operator.
-
7/27/2019 l 08 Variables Inc
15/22
-
7/27/2019 l 08 Variables Inc
16/22
CMSC 104, Version 9/01 16
Example: Declarations and Assignments (contd)
printf (Its depth at sea: \n) ;printf ( %d fathoms \n, fathoms) ;printf ( %d feet \n, feet) ;printf ( %d inches \n, inches) ;
return 0 ;}
-
7/27/2019 l 08 Variables Inc
17/22
CMSC 104, Version 9/01 17
Enhancing Our Example
What if the depth were really 5.75 fathoms?Our program, as it is, couldnt handle it.
Unlike integers, floating point numbers can
contain decimal portions. So, lets usefloating point, rather than integer.
Lets also ask the user to enter the number
of fathoms, rather than hard-coding it in.
-
7/27/2019 l 08 Variables Inc
18/22
CMSC 104, Version 9/01 18
Enhanced Program
#include
int main ( ){
float inches, feet, fathoms ;
printf (Enter the depth in fathoms : ) ;
scanf (%f, &fathoms) ;
feet = 6 * fathoms ;
inches = 12 * feet ;
printf (Its depth at sea:\n) ;
printf ( %f fathoms\n, fathoms) ;printf ( %f feet\n, feet) ;
printf ( %f inches\n, inches) ;
return 0 ;
}
-
7/27/2019 l 08 Variables Inc
19/22
CMSC 104, Version 9/01 19
Final Clean Program
#include
int main( ){
float inches ; /* number of inches deep */float feet ; /* number of feet deep */
float fathoms ; /* number of fathoms deep */
/* Get the depth in fathoms from the user */
printf (Enter the depth in fathoms : ) ;
scanf (%f, &fathoms) ;/* Convert the depth to inches */
feet = 6 * fathoms ;
inches = 12 * feet ;
-
7/27/2019 l 08 Variables Inc
20/22
CMSC 104, Version 9/01 20
Final Clean Program (cont)
/* Display the results */
printf (Its depth at sea:\n) ;
printf ( %f fathoms\n, fathoms) ;
printf ( %f feet\n, feet);printf ( %f inches\n, inches);
return 0 ;
}
-
7/27/2019 l 08 Variables Inc
21/22
CMSC 104, Version 9/01 21
Good Programming Practices
Place each variable declaration on its ownline with a descriptive comment.
Place a comment before each logicalchunk of code describing what it does.
Do not place a comment on the same line ascode (with the exception of variabledeclarations).
Use spaces around all arithmetic andassignment operators.
Use blank lines to enhance readability.
-
7/27/2019 l 08 Variables Inc
22/22
CMSC 104 Version 9/01 22
Good Programming Practices (cont)
Place a blank line between the last variabledeclaration and the first executablestatement of the program.
Indent the body of the program 3 to 4 tabstops -- be consistent!