Variables in C Topics Naming Variables p Declaring
![Variables in C Topics Naming Variables p Declaring Variables p Using Variables p The Variables in C Topics Naming Variables p Declaring Variables p Using Variables p The](https://slidetodoc.com/presentation_image_h2/b27015e50427a13d261446622d7283de/image-1.jpg)
Variables in C Topics Naming Variables p Declaring Variables p Using Variables p The Assignment Statement p Reading p Sections 2. 3 - 2. 4
![What Are Variables in C? p Variables in C have the same meaning as What Are Variables in C? p Variables in C have the same meaning as](http://slidetodoc.com/presentation_image_h2/b27015e50427a13d261446622d7283de/image-2.jpg)
What Are Variables in C? p Variables in C have the same meaning as variables in algebra. That is, they represent some unknown, or variable, value. x=a+b z + 2 = 3(y - 5) p Remember that variables in algebra are represented by a single alphabetic character.
![Naming Variables in C may be given representations containing multiple characters. But there are Naming Variables in C may be given representations containing multiple characters. But there are](http://slidetodoc.com/presentation_image_h2/b27015e50427a13d261446622d7283de/image-3.jpg)
Naming Variables in C may be given representations containing multiple characters. But there are rules for these representations. p Variable names (identifiers) in C p n n May only consist of letters, digits, and underscores May be as long as you like, but only the first 31 characters are significant May not begin with a digit May not be a C reserved word (keyword)
![Reserved Words (Keywords) in C p p p p auto case const default double Reserved Words (Keywords) in C p p p p auto case const default double](http://slidetodoc.com/presentation_image_h2/b27015e50427a13d261446622d7283de/image-4.jpg)
Reserved Words (Keywords) in C p p p p 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
![Naming Conventions p C programmers generally agree on the following conventions for naming variables. Naming Conventions p C programmers generally agree on the following conventions for naming variables.](http://slidetodoc.com/presentation_image_h2/b27015e50427a13d261446622d7283de/image-5.jpg)
Naming Conventions p C programmers generally agree on the following conventions for naming variables. n Begin variable names with lowercase letters n Use meaningful identifiers n n n Separate “words” within identifiers with underscores or mixed upper and lower case. Examples: surface. Area surface_area Be consistent!
![Naming Conventions (con’t) Use all uppercase for symbolic constants (used in #define preprocessor directives). Naming Conventions (con’t) Use all uppercase for symbolic constants (used in #define preprocessor directives).](http://slidetodoc.com/presentation_image_h2/b27015e50427a13d261446622d7283de/image-6.jpg)
Naming Conventions (con’t) Use all uppercase for symbolic constants (used in #define preprocessor directives). p Note: symbolic constants are not variables, but make the program easier to read. p Examples: p #define PI 3. 14159 #define AGE 52
![Case Sensitivity p C is case sensitive n n It matters whether an identifier, Case Sensitivity p C is case sensitive n n It matters whether an identifier,](http://slidetodoc.com/presentation_image_h2/b27015e50427a13d261446622d7283de/image-7.jpg)
Case Sensitivity p C is case sensitive n n It matters whether an identifier, such as a variable name, is uppercase or lowercase. Example: area AREA Ar. Ea are all seen as different variables by the compiler.
![Which Are Legal Identifiers? AREA 3 D Last-Chance x_yt 3 num$ lucky*** area_under_the_curve num Which Are Legal Identifiers? AREA 3 D Last-Chance x_yt 3 num$ lucky*** area_under_the_curve num](http://slidetodoc.com/presentation_image_h2/b27015e50427a13d261446622d7283de/image-8.jpg)
Which Are Legal Identifiers? AREA 3 D Last-Chance x_yt 3 num$ lucky*** area_under_the_curve num 45 #values pi %done
![Declaring Variables Before using a variable, you must give the compiler some information about Declaring Variables Before using a variable, you must give the compiler some information about](http://slidetodoc.com/presentation_image_h2/b27015e50427a13d261446622d7283de/image-9.jpg)
Declaring Variables Before using a variable, you must give the compiler some information about the variable; i. e. , you must declare it. p The declaration statement includes the data type of the variable. p Examples of variable declarations: int meatballs ; float area ; p
![Declaring Variables (con’t) p When we declare a variable n n n p Space Declaring Variables (con’t) p When we declare a variable n n n p Space](http://slidetodoc.com/presentation_image_h2/b27015e50427a13d261446622d7283de/image-10.jpg)
Declaring Variables (con’t) p When we declare a variable n n n p Space is set aside in memory to hold a value of the specified data type That space is associated with the variable name That space is associated with a unique address Visualization of the declaration int meatballs ; meatballs garbage FE 07 int
![More About Variables C has three basic predefined data types: p Integers (whole numbers) More About Variables C has three basic predefined data types: p Integers (whole numbers)](http://slidetodoc.com/presentation_image_h2/b27015e50427a13d261446622d7283de/image-11.jpg)
More About Variables C has three basic predefined data types: p Integers (whole numbers) n p Floating point (real numbers) n p float, double Characters n p int, long int, short int, unsigned int char At this point, you need only be concerned with the data types that are bolded.
![Notes About Variables You must not use a variable until you somehow give it Notes About Variables You must not use a variable until you somehow give it](http://slidetodoc.com/presentation_image_h2/b27015e50427a13d261446622d7283de/image-12.jpg)
Notes About Variables You must not use a variable until you somehow give it a value. p You can not assume that the variable will have a value before you give it one. p n Some compilers do, others do not! This is the source of many errors that are difficult to find.
![Using Variables: Initialization p Variables may be be given initial values, or initialized, when Using Variables: Initialization p Variables may be be given initial values, or initialized, when](http://slidetodoc.com/presentation_image_h2/b27015e50427a13d261446622d7283de/image-13.jpg)
Using Variables: Initialization p Variables may be be given initial values, or initialized, when declared. Examples: length 7 int length = 7 ; diameter float diameter = 5. 9 ; char initial = ‘A’ ; 5. 9 initial ‘A’
![Using Variables: Initialization p Do not “hide” the initialization n put initialized variables on Using Variables: Initialization p Do not “hide” the initialization n put initialized variables on](http://slidetodoc.com/presentation_image_h2/b27015e50427a13d261446622d7283de/image-14.jpg)
Using Variables: Initialization p Do not “hide” the initialization n 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 ;
![Using Variables: Assignment p p Variables may have values assigned to them through the Using Variables: Assignment p p Variables may have values assigned to them through the](http://slidetodoc.com/presentation_image_h2/b27015e50427a13d261446622d7283de/image-15.jpg)
Using Variables: Assignment p p Variables may have values assigned to them through the use of an assignment statement. Such a statement uses the assignment operator = This operator does not denote equality. It assigns the value of the right-hand side of the statement (the expression) to the variable on the left-hand side. Examples: diameter = 5. 9 ; area = length * width ; Note that only single variables may appear on the left -hand side of the assignment operator.
![Functions p It is necessary for us to use some functions to write our Functions p It is necessary for us to use some functions to write our](http://slidetodoc.com/presentation_image_h2/b27015e50427a13d261446622d7283de/image-16.jpg)
Functions p It is necessary for us to use some functions to write our first programs, but we are not going to explain functions in great detail at this time. p. Functions are parts of programs that perform a p certain task and we have to give them some information so the function can do the task. We will show you how to use the functions as we go through the course and later on will show you how to create your own.
![Displaying Variables hold values that we occasionally want to show the person using the Displaying Variables hold values that we occasionally want to show the person using the](http://slidetodoc.com/presentation_image_h2/b27015e50427a13d261446622d7283de/image-17.jpg)
Displaying Variables hold values that we occasionally want to show the person using the program. p We have a function called printf( ) that will allow us to do that. p The function printf needs two pieces of information to display things. p n n p How to display it What to display printf( “%fn”, diameter );
![printf( “%fn”, diameter ); The name of the function is “printf”. p Inside the printf( “%fn”, diameter ); The name of the function is “printf”. p Inside the](http://slidetodoc.com/presentation_image_h2/b27015e50427a13d261446622d7283de/image-18.jpg)
printf( “%fn”, diameter ); The name of the function is “printf”. p Inside the parentheses are: p n print specification, where we are going to display: a floating point value (“%f”) p We want to have the next thing started on a new line (“n”). p n p We want to display the contents of the variable diameter. printf( ) has many other capabilities.
![Example: Declarations and Assignments #include <stdio. h> int main( void ) { int inches, Example: Declarations and Assignments #include <stdio. h> int main( void ) { int inches,](http://slidetodoc.com/presentation_image_h2/b27015e50427a13d261446622d7283de/image-19.jpg)
Example: Declarations and Assignments #include <stdio. h> int main( void ) { int inches, feet, fathoms ; inches garbage feet garbage fathoms garbage feet = 6 * fathoms ; fathoms 7 feet 42 inches = 12 * feet ; inches 504 fathoms = 7 ;
![Example: Declarations and Assignments printf (“Its depth at sea: n”) ; printf (“ %d Example: Declarations and Assignments printf (“Its depth at sea: n”) ; printf (“ %d](http://slidetodoc.com/presentation_image_h2/b27015e50427a13d261446622d7283de/image-20.jpg)
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. 75 fathoms? Our program, Enhancing Our Example What if the depth were really 5. 75 fathoms? Our program,](http://slidetodoc.com/presentation_image_h2/b27015e50427a13d261446622d7283de/image-21.jpg)
Enhancing Our Example What if the depth were really 5. 75 fathoms? Our program, as it is, couldn’t handle it. p Unlike integers, floating point numbers can contain decimal portions. So, let’s use floating point, rather than integer. p Let’s also ask the user to enter the number of fathoms, rather than “hard -coding” it in by using the scanf( ) function. p
![Enhanced Program #include <stdio. h> int main ( void ) { float inches, feet, Enhanced Program #include <stdio. h> int main ( void ) { float inches, feet,](http://slidetodoc.com/presentation_image_h2/b27015e50427a13d261446622d7283de/image-22.jpg)
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) ; return 0 ; }
![scanf (“%f”, &fathoms) ; p The scanf( ) function also needs two items: n scanf (“%f”, &fathoms) ; p The scanf( ) function also needs two items: n](http://slidetodoc.com/presentation_image_h2/b27015e50427a13d261446622d7283de/image-23.jpg)
scanf (“%f”, &fathoms) ; p The scanf( ) function also needs two items: n n p The input specification “%f”. (Never put a “n” into the input specification. ) The address of where to store the information. (We can input more than one item at a time if we wish, as long as we specify it correctly. ) Notice the “&” in front of the variable name. It says to use the address of the variable to hold the information that the user enters.
![Note About Input and Output Whenever we wish to display values or get values Note About Input and Output Whenever we wish to display values or get values](http://slidetodoc.com/presentation_image_h2/b27015e50427a13d261446622d7283de/image-24.jpg)
Note About Input and Output Whenever we wish to display values or get values from the user, we have a format problem. p We can only input characters, not values. p We can only display characters, not values. p The computer stores values in numeric variables. p printf( ) and scan( ) will automatically convert things for us correctly. p
![Final “Clean” Program #include <stdio. h> #define FEET_PER_FATHOM 6 #define INCHES_PER_FOOT 12 int main( Final “Clean” Program #include <stdio. h> #define FEET_PER_FATHOM 6 #define INCHES_PER_FOOT 12 int main(](http://slidetodoc.com/presentation_image_h2/b27015e50427a13d261446622d7283de/image-25.jpg)
Final “Clean” Program #include <stdio. h> #define FEET_PER_FATHOM 6 #define INCHES_PER_FOOT 12 int main( void ) { float inches ; float feet ; float fathoms ; /* number of inches deep */ /* number of feet deep */ /* number of fathoms deep */ /* Get the depth in fathoms from the user */ printf (“Enter the depth in fathoms : ”) ; scanf (“%f”, &fathoms) ;
![Final “Clean” Program /* Convert the depth to inches */ feet = FEET_PER_FATHOM * Final “Clean” Program /* Convert the depth to inches */ feet = FEET_PER_FATHOM *](http://slidetodoc.com/presentation_image_h2/b27015e50427a13d261446622d7283de/image-26.jpg)
Final “Clean” Program /* Convert the depth to inches */ feet = FEET_PER_FATHOM * fathoms ; inches = INCHES_PER_FOOT * feet ; /* 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 ; }
![Good Programming Practices Place each variable declaration on its own line with a descriptive Good Programming Practices Place each variable declaration on its own line with a descriptive](http://slidetodoc.com/presentation_image_h2/b27015e50427a13d261446622d7283de/image-27.jpg)
Good Programming Practices Place each variable declaration on its own line with a descriptive comment. p Place a comment before each logical “chunk” of code describing what it does. p Do not place a comment on the same line as code (with the exception of variable declarations). p Use spaces around all arithmetic and assignment operators. p Use blank lines to enhance readability. p
![Good Programming Practices Place a blank line between the last variable declaration and the Good Programming Practices Place a blank line between the last variable declaration and the](http://slidetodoc.com/presentation_image_h2/b27015e50427a13d261446622d7283de/image-28.jpg)
Good Programming Practices Place a blank line between the last variable declaration and the first executable statement of the program. p Indent the body of the program 3 to 5 spaces -be consistent! p Comments should explain why you are doing something, not what you are doing it. a = a + 1 /* add one to a */ /* WRONG */ /* count new student */ /* RIGHT*/ p
![Another Sample Program #include <stdio. h> #define PI 3. 14159 int main ( void Another Sample Program #include <stdio. h> #define PI 3. 14159 int main ( void](http://slidetodoc.com/presentation_image_h2/b27015e50427a13d261446622d7283de/image-29.jpg)
Another Sample Program #include <stdio. h> #define PI 3. 14159 int main ( void ) { float radius = 3. 0; float area; area = PI * radius; printf( “The area is %f. n”, area ); return 0 ; }
- Slides: 29