DOC PREVIEW
UMBC CMSC 104 - Variables in C

This preview shows page 1-2-3 out of 10 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Variables in CTopics Naming Variables Declaring Variables Using Variables The Assignment StatementReading Sections 2.3 - 2.4What Are Variables in C? Variables in C have the same meaning asvariables in algebra. That is, they representsome unknown, or variable, value.x = a + bz + 2 = 3(y - 5) Remember that variables in algebra arerepresented by a single alphabetic character.Naming Variables Variables in C may be givenrepresentations containing multiplecharacters. But there are rules for theserepresentations. Variable names (identifiers) in C May only consist of letters, digits, andunderscores May be as long as you like, but only the first31 characters are significant May not begin with a digit May not be a C reserved word (keyword)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 signed sizeof static struct switch typedef union unsigned void volatile while C programmers generally agree on the followingconventions for naming variables. Begin variable names with lowercase letters Use meaningful identifiers Separate “words” within identifiers with underscores ormixed upper and lower case. Examples: surfaceArea surface_Area surface_area Be consistent!Naming ConventionsNaming Conventions (con’t) Use all uppercase for symbolic constants(used in #define preprocessor directives). Note: symbolic constants are not variables,but make the program easier to read. Examples: #define PI 3.14159 #define AGE 52Case Sensitivity C is case sensitive It matters whether an identifier, such as avariable name, is uppercase or lowercase. Example:areaAreaAREAArEaare all seen as different variables by thecompiler.Which Are Legal Identifiers? AREA area_under_the_curve 3D num45 Last-Chance #values x_yt3 pi num$ %done lucky***Declaring Variables Before using a variable, you must give thecompiler some information about the variable;i.e., you must declare it. The declaration statement includes the datatype of the variable. Examples of variable declarations: int meatballs ; float area ;Declaring Variables (con’t) When we declare a variable Space is set aside in memory to hold a value of thespecified data type That space is associated with the variable name That space is associated with a unique address Visualization of the declaration int meatballs ;meatballsFE07garbage intMore About VariablesC has three basic predefined data types: Integers (whole numbers) int, long int, short int, unsigned int Floating point (real numbers) float, double Characters char At this point, you need only be concerned withthe data types that are bolded.Notes About Variables You must not use a variableuntil you somehow give it avalue. You can not assume that thevariable will have a valuebefore you give it one. Some compilers do, others donot! This is the source of manyerrors that are difficult to find.Using Variables: Initialization Variables may be be given initial values, orinitialized, when declared. Examples:int length = 7 ;float diameter = 5.9 ;char initial = ‘A’ ;75.9‘A’lengthdiameterinitialUsing Variables: Initialization Do not “hide” the initialization put initialized variables on a separate line a comment is always a good idea Example:int height ; /* rectangle height */int width = 6 ; /* rectangle width */int area ; /* rectangle area */ NOT int height, width = 6, area ; Variables may have values assigned to themthrough the use of an assignment statement. Such a statement uses the assignment operator= This operator does not denote equality. It assignsthe value of the right-hand side of the statement(the expression) to the variable on the left-handside. Examples:diameter = 5.9 ;area = length * width ;Note that only single variables may appear on theleft-hand side of the assignment operator.Using Variables: AssignmentFunctions It is necessary for us to use some functions to write ourfirst programs, but we are not going to explain functionsin great detail at this time.Functions are parts of programs that perform a certaintask and we have to give them some information so thefunction can do the task. We will show you how to use the functions as we gothrough the course and later on will show you how tocreate your own.Displaying Variables Variables hold values that we occasionally wantto show the person using the program. We have a function called printf( ) that will allowus to do that. The function printf needs two pieces ofinformation to display things. How to display it What to display printf( “%f\n”, diameter );printf( “%f\n”, diameter ); The name of the function is “printf”. Inside the parentheses are: print specification, where we are going to display: a floating point value (“%f”) We want to have the next thing started on a new line (“\n”). We want to display the contents of the variablediameter. printf( ) has many other capabilities.Example: Declarations and Assignments #include <stdio.h> int main( void ) { int inches, feet, fathoms ; inchesfeetfathoms427garbage504fathomsgarbagefeetgarbageinchesfathoms = 7 ; feet = 6 * fathoms ;inches = 12 * feet ;Example: Declarations and Assignments printf (“Its depth at sea: \n”) ; printf (“ %d fathoms \n”, fathoms) ; printf (“ %d feet \n”, feet) ; printf (“ %d inches \n”, inches) ; return 0 ; }Enhancing Our Example What if the depth were really 5.75fathoms? Our program, as it is, couldn’thandle it. Unlike integers, floating point numberscan contain decimal portions. So, let’suse floating point, rather than integer. Let’s also ask the user to enter thenumber of fathoms, rather than“hard-coding” it in by using the scanf() function.Enhanced Program#include <stdio.h>int main ( void ){ 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) ;


View Full Document

UMBC CMSC 104 - Variables in C

Download Variables in C
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Variables in C and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Variables in C 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?