cs1313 variables and constants lesson...

40
CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants Lesson 1 Outline 1. Variables & Constants Lesson 1 Outline 2. C Character Set 3. C is Case Sensitive 4. Character String Literal Constant 5. String Literal Cannot Use Multiple Lines 6. Multi-line String Literal Example 7. Newline 8. Newline Example 9. Statements 10. Statement Terminator 11. Standard Input & Standard Output 12. Data Types 13. What is a Variable? 14. What Does a Variable Have? 15. The Value of a Variable Can Vary 16. Compile Time and Runtime 17. Variable Declaration: Name & Data Type 18. Variable Declaration: Address 19. Variable Declaration: Initial Value #1 20. Variable Declaration: Initial Value #2 21. Variable Declaration: Initial Value #3 22. Declaration Section & Execution Section 23. Setting the Value of a Variable 24. Variable Assignment 25. Variable Assignment Example 26. Variable Assignment Example Program Part 1 27. Variable Assignment Example Program Part 2 28. The Same Source Code without Comments 29. Assignment is an Action, NOT an Equation #1 30. Assignment is an Action, NOT an Equation #2 31. Changing a Variable’s Contents 32. Changing a Variable’s Contents: Example Part 1 33. Changing a Variable’s Contents: Example Part 2 34. The Same Source Code without Comments 35. Variable Initialization 36. Variable Initialization Example Part 1 37. Variable Initialization Example Part 2 38. The Same Source Code without Comments 39. C Variable Names 40. Favorite Professor Rule for Variable Names

Upload: others

Post on 13-Jul-2020

39 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 1

Variables & Constants Lesson 1 Outline1. Variables & Constants Lesson 1 Outline2. C Character Set3. C is Case Sensitive4. Character String Literal Constant5. String Literal Cannot Use Multiple Lines6. Multi-line String Literal Example7. Newline8. Newline Example9. Statements10. Statement Terminator11. Standard Input & Standard Output12. Data Types13. What is a Variable?14. What Does a Variable Have?15. The Value of a Variable Can Vary16. Compile Time and Runtime17. Variable Declaration: Name & Data Type18. Variable Declaration: Address19. Variable Declaration: Initial Value #120. Variable Declaration: Initial Value #221. Variable Declaration: Initial Value #3

22. Declaration Section & Execution Section23. Setting the Value of a Variable24. Variable Assignment25. Variable Assignment Example26. Variable Assignment Example Program Part 127. Variable Assignment Example Program Part 228. The Same Source Code without Comments29. Assignment is an Action, NOT an Equation #130. Assignment is an Action, NOT an Equation #231. Changing a Variable’s Contents32. Changing a Variable’s Contents: Example Part 133. Changing a Variable’s Contents: Example Part 234. The Same Source Code without Comments35. Variable Initialization36. Variable Initialization Example Part 137. Variable Initialization Example Part 238. The Same Source Code without Comments39. C Variable Names40. Favorite Professor Rule for Variable Names

Page 2: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 2

C Character Set

These are the characters that C recognizes.Letters (upper case and lower case)

A B C D E F G H I J K L MN O P Q R S T U V W X Y Za b c d e f g h i j k l mn o p q r s t u v w x y z

Digits0 1 2 3 4 5 6 7 8 9

Special Charactersspace (also known as blank)’ " ( ) * + - / : =! & $ ; < > % ? , .ˆ # @ ˜ ‘ { } [ ] \ |

Page 3: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 3

C is Case SensitiveC is case sensitive: it distinguishes between UPPER case

(CAPITAL) and lower case (small) letters.Keywords in C — for example, the keyword int — MUST be

in lower case. For example:#include <stdio.h>

int main (){ /* main */

int height_in_cm;

height_in_cm = 160;printf("My height is %d cm.\n",

height_in_cm);} /* main */

Page 4: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 4

Character String Literal ConstantA character string literal constant is a sequence of

characters delimited by a double quote at the beginning and a double quote at the end.

A character string literal constant is also known as a character string literal or a string literal for short.

For example, in this printf statement:printf("This is a printf.\n");

the following is a string literal:"This is a printf.\n"

The output of this printf statement is:This is a printf.

followed by a newline, also known as a carriage return.

Page 5: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 5

String Literal Cannot Use Multiple LinesA character string literal constant can only use one

line; that is, both of its delimiters MUST be on the same line of source code text.

So, this is correct:printf("This string literal takes one line");printf(" and so does this string literal.\n");

And this is WRONG WRONG WRONG:printf("This string literal takes

more than one line so it’s WRONG!\n");

Some compilers will accept this but won’t be happy; other compilers will simply reject it.

Regardless, if this appears in a program in CS1313,YOU WILL BE SEVERELY PENALIZED!

Page 6: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 6

Multi-line String Literal Example% cat bad_string_literal.c#include <stdio.h>

int main (){ /* main */

printf("This string literal takesmore than one line so it's WRONG!\n");

} /* main */% gcc -o bad_string_literal bad_string_literal.cbad_string_literal.c:5:12: warning: multi-line

string literals are deprecated

Deprecated: severely frowned upon

Page 7: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 7

Newline

In C, you can place a newline, also known as a carriage return, inside a string literal using:

\n

If a newline appears inside a string literal in the source code, then when the string literal is output, the newline causes the output to move to a new line.

Page 8: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 8

Newline Example% cat newline.c#include <stdio.h>

int main (){ /* main */

printf("Howdy do!\n");printf("This string literal contains a newline in the\nmiddle ");printf("but this string literal contains a newline at the end.\n");printf("So there!\n");

} /* main */% gcc -o newline newline.c% newlineHowdy do!This string literal contains a newline in themiddle but this string literal contains a newline at the end.So there!

Note: in general, it’s better programming practice to put newlines only at the end of your string literals, not in the middle, because in the middle they can be difficult for programmers (e.g., graders) to see.

Page 9: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 9

Statements

A statement in a program is like a sentence in a natural language: it’s the smallest possible collection of words and punctuation that can stand by itself and have meaning.

For example:printf("Hello, world.\n");

This statement tells the compiler to output to the terminal screen the string literal

Hello, world.

followed by a newline.

Page 10: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 10

Statement Terminator

In C, every statement ends with a semicolon, which is known as the statement terminator.

For example:int height_in_cm;

height_in_cm = 160;printf("My height is %d cm.\n",

height_in_cm);

Notice that a statement CAN take more than one line (but recall that a string literal CAN’T take more than one line).

The way you find the end of a statement is by finding its statement terminator.

Page 11: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 11

Standard Input & Standard Output

Standard input is when a user types at the keyboard. It is sometimes shortened to stdin, pronounced “standard in.”Standard output is when the computer outputs to the terminal screen. It is sometimes shortened to stdout, pronounced “standard out.”

In C, a printf statement always outputs to standard output, and a scanf statement always inputs from standard input. More on this later.

Page 12: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 12

Data Types

A data type is (surprise!) a type of data:Numeric

int: integerfloat: floating point (also known as real)

Non-numericchar: character

#include <stdio.h>int main (){ /* main */

float standard_deviation, relative_humidity;int count, number_of_silly_people;char middle_initial, hometown[30];

} /* main */

Page 13: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 13

What is a Variable?

A variable is an association betweena name,an address,a data type.

Page 14: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 14

What Does a Variable Have?Every variable has:

a name (e.g., number_of_students), chosen by the programmer;an address (i.e., a location in memory, such as 123456), chosen by the compiler;a data type (e.g., int, float, char), chosen by the programmer;a value (which may be undefined, also known as garbage), sometimes chosen by the programmer, and sometimes determined while the program is running (at runtime). The value is also known as the contents of the variable — that is, the value is the contents of the variable’s memory location.

Page 15: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 15

The Value of a Variable Can Vary

The value of a variable can vary; that is, it can be changed at runtime. We’ll see how in a moment.

Page 16: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 16

Compile Time and Runtime

Events that occur while a program is being compiled are said to happen at compile time.Events that occur while a program is running are said to happen at runtime.

For example, the address of a variable is chosen at compile time, while its value typically is determined at runtime.

Page 17: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 17

Variable Declaration: Name & Data Typeint x;

A declaration is a statement that tells the compiler that an item of data (e.g., a variable) exists, and what some of its properties are (specifically, its name and its data type).

For example, the declaration above tells the compiler to choose a location in memory, name it x, and think of it as an integer.

Note that the declaration above doesn’t specify a value for x.

Page 18: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 18

Variable Declaration: Addressint x;

The compiler might decide that x will live at, say, address 3980 or address 98234092 or address 56436.

We don’t know and don’t care what address x lives at, because the compiler will take care of that for us.

It’s enough to know that x has an address and that the address of x will stay the same throughout a given run of the program.

Page 19: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 19

Variable Declaration: Initial Value #1

When x is first declared, we don’t know what its value is, because we haven’t put anything into its memory location yet, so we say that its value is undefined, or, informally, garbage.

We’ll see in a moment how to put values into our variables.

????????x: (address 56436)int x;

Page 20: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 20

Variable Declaration: Initial Value #2When x is first declared, we don’t know what its value

is, because we haven’t put anything into its memory location yet, so we say that its value is undefined, or, informally, garbage.

Note: some compilers for some languages automatically initialize newly declared variables to default values (e.g., all integers get initialized to zero), but not every compiler does automatic initialization.

You should NEVER NEVER NEVER assume that the compiler will initialize your variables for you.

You should ALWAYS ALWAYS ALWAYS explicitly assign values to your variables in the body of the program, as needed.

Page 21: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 21

Variable Declaration: Initial Value #3

You can think of a variable’s memory location as a box that always contains exactly one thing.

So, if you haven’t put anything into the box yet, then the contents of the box is whatever was left in itwhen the previous user got done with it.

You don’t know what that value means, so to you it’s garbage.

6262 5

5

(1) (2) (3)

Page 22: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 22

Declaration Section & Execution Section

The declaration section of a program is the section that contains all of the program’s declarations.

The declaration section always goes at the beginningof the program, just after the block open that follows the main function header:

#include <stdio.h>

int main (){ /* main */

int height_in_cm;

height_in_cm = 160;printf("My height is %d cm.\n", height_in_cm);

} /* main */

The execution section, also known as the body, comes after the declaration section.

Declaration Section

Body

Page 23: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 23

Setting the Value of a Variable

There are three ways to set the value of a variable:assignment;initialization;input.

Page 24: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 24

Variable Assignment

An assignment statement sets the contents of a specific variable to a specific value:

x = 5;

This statement tells the compiler to put the integer value 5 into the memory location named x, like so:

We say “x is assigned five” or “x gets five.”5x: (address 56436)

55

Page 25: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 25

Variable Assignment Example

????????x: (address 56436)int x;

5x: (address 56436)x = 5;

12x: (address 56436)x = 12;

Page 26: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 26

Variable Assignment Example Program Part 1% cat assign.c/************************************************* Program: assign ****** Author: Henry Neeman ([email protected]) ****** Course: CS 1313 010 Fall 2005 ****** Lab: Sec 011 Fridays 9:30am ****** Description: Declares, assigns and ****** outputs a variable. *************************************************/#include <stdio.h>

int main (){ /* main */

/********************************************* Declaration section **************************************************************** Local variables ********************** height_in_cm: my height in cm*/int height_in_cm;

Page 27: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 27

Variable Assignment Example Program Part 2/*********************************************** Execution section *********************************************** Assign the integer value 160 to height_in_cm.*/height_in_cm = 160;/** Print height_in_cm to standard output.*/printf("My height is %d cm.\n", height_in_cm);

} /* main */% gcc -o assign assign.c% assignMy height is 160 cm.

Page 28: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 28

The Same Source Code without Comments% cat assign.c#include <stdio.h>

int main (){ /* main */

int height_in_cm;

height_in_cm = 160;printf("My height is %d cm.\n", height_in_cm);

} /* main */% gcc -o assign assign.c% assignMy height is 160 cm.

Page 29: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 29

Assignment is an Action, NOT an Equation #1An assignment is an action, NOT an equation.#include <stdio.h>

int main (){ /* main */

int height_in_cm;

height_in_cm = 160;printf("My height is %d cm.\n", height_in_cm);

} /* main */

The assignment statementheight_in_cm = 160;

means “put the int value 160 into the memory location of the int variable named height_in_cm.”

Page 30: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 30

Assignment is an Action, NOT an Equation #2An assignment is an action, NOT an equation.The variable whose value is being set by the

assignment MUST appear on the left side of the equals sign.

% cat not_an_equation.c#include <stdio.h>

int main (){ /* main */

int height_in_cm;

160 = height_in_cm;printf("My height is %d cm.\n", height_in_cm);

} /* main */% gcc -o not_an_equation not_an_equation.cnot_an_equation.c: In function `main':not_an_equation.c:7: invalid lvalue in assignment

ERROR!

Page 31: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 31

Changing a Variable’s Contents

One way to change the value – the contents – of a variable is with another assignment statement.

Page 32: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 32

Changing a Variable’s Contents: Example Part 1% cat change.c/*************************************************** Program: change ****** Author: Henry Neeman ([email protected]) ****** Course: CS 1313 010 Fall 2005 ****** Lab: Sec 011 Fridays 9:30am ****** Description: Declares, assigns, changes ****** and outputs a variable. ***************************************************/#include <stdio.h>int main (){ /* main */

/********************************************** Declaration section ****************************************************************** Local variables ********************** height_in_cm: my height in cm*/int height_in_cm;

Page 33: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 33

Changing a Variable’s Contents: Example Part 2/*********************************************** Execution section *********************************************** Assign the integer value 160 to height_in_cm.*/height_in_cm = 160;/** Print height_in_cm to standard output.*/printf("My height is %d cm.\n", height_in_cm);/** Assign the integer value 200 to height_in_cm.*/height_in_cm = 200;/** Print height_in_cm to standard output.*/printf("My height is %d cm.\n", height_in_cm);

} /* main */% gcc -o change change.c% changeMy height is 160 cm.My height is 200 cm.

Page 34: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 34

The Same Source Code without Comments% cat change.c#include <stdio.h>

int main (){ /* main */

int height_in_cm;

height_in_cm = 160;printf("My height is %d cm.\n", height_in_cm);height_in_cm = 200;printf("My height is %d cm.\n", height_in_cm);

} /* main */% gcc -o change change.c% changeMy height is 160 cm.My height is 200 cm.

Page 35: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 35

Variable Initialization

To initialize a variable means to declare it and assign it a value in the same statement:

int x = 5;

This statement is the same as declaring x in the declaration section, and then assigning it 5 at the beginning of the execution section:

int x;

x = 5;

Page 36: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 36

Variable Initialization Example Part 1% cat initialize.c/************************************************* Program: initialize ****** Author: Henry Neeman ([email protected]) ****** Course: CS 1313 010 Fall 2005 ****** Lab: Sec 011 Fridays 9:30am ****** Description: Declares/initializes and ****** outputs a variable. *************************************************/#include <stdio.h>

int main (){ /* main */

/******************************************** Declaration section **************************************************************** Local variables ********************** height_in_cm: my height in cm*/int height_in_cm = 160;

Page 37: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 37

Variable Initialization Example Part 2/******************************************** Execution section ********************************************* Print height_in_cm to standard output.*/printf("My height is %d cm.\n", height_in_cm);

} /* main */% gcc -o initialize initialize.c% initializeMy height is 160 cm.

Page 38: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 38

The Same Source Code without Comments% cat initialize.c#include <stdio.h>

int main (){ /* main */

int height_in_cm = 160;

printf("My height is %d cm.\n", height_in_cm);} /* main */% gcc -o initialize initialize.c% initializeMy height is 160 cm.

Page 39: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 39

C Variable NamesC identifiers (including variable names) have the following

properties:Constructed using only these characters:

Letters (case sensitive: it matters whether it’s upper case or lower case)a b c d e f g h i j k l mn o p q r s t u v w x y zA B C D E F G H I J K L MN O P Q R S T U V W X Y Z

Digits0 1 2 3 4 5 6 7 8 9

Underscore (NOTE: NOT hyphen)_

The first character is a letter or an underscore:a123_456 is good, and so is a123_456 but not 1a23_456

Page 40: CS1313 Variables and Constants Lesson 1hneeman.oscer.ou.edu/CS1313/variables_and_constants_lesson1.pdf · CS1313: Variables & Constants Lesson #1 Fall 2005 1 Variables & Constants

CS1313: Variables & Constants Lesson #1Fall 2005 40

Favorite Professor Rule for Variable Names

A variable name should be so obvious that your favorite professor in your major, even if they know nothing about programming, could immediately tell what the variable name means.