Skip to content

Instantly share code, notes, and snippets.

@rszewczyk
Last active October 17, 2017 19:51
Show Gist options
  • Save rszewczyk/3c01cc8ee4b998601f3d49c71b7d2ca5 to your computer and use it in GitHub Desktop.
Save rszewczyk/3c01cc8ee4b998601f3d49c71b7d2ca5 to your computer and use it in GitHub Desktop.
UMBC CMSC 104 - Fall 2017 - Class 6 - 9/21/2017

Class Notes - 9/21/2017

Variables

A variable in C is a name given to a location in the computer's memory where a programmer can store and/or access data.

A variable that stores a particular piece of data can also be used as a kind of alias or placeholder for the value of that data, similar to how a variable behaves in a mathematical formula.

Declaring Variables

Before you can use a variable, you must declare it. A variable declaration establishes the name of the variable and its type. It's a way of telling the compiler something like "Reserve a space in memory which I will henceforth refer to as X, capable of storing an integer".

The syntax for a variable declaration looks like:

int numberOfLegos;

In the above example int is the data type and numberOfLegos is the variable name.

Variable Names

Variable names in C must conform to the following rules:

  • May only consist of letters, digits, and underscores
  • May not begin with a number
  • Cannot be in the list of C keywords

Also, for the purposes of this class please stick to the following conventions:

  • Begin variable names with lowercase letters
  • Make an effort to use contexually meaningful names (i.e. if you're counting Legos, numberOfLegos is an excellent name)
  • Use an upper case letter to demarcate the start of each word in your variable name beyond the first

Variable Data Types

There are three categories of simple data types in C.

  1. Integers: int, long, short, uint
  2. Decimal numbers: double, float
  3. Characters: char

Within the numerical categories the various data types are differentiated by the size or precision of data that can be stored in the associated memory location - bigger or more precise numbers require more memory. In this class we will stick to using int when we need an integer and double when we need a decimal number.

The declaration of the 3 data types we will use in the class look like:

// integer
int numberOfLegos;

// decimal
double averageTemperature;

// character
char middleInitial;

Variable Assignment

Variable assignment means to give the variable a value. To assign a value to a variable, use the assignment operator = in between the variable name on the left and the value you wish to assign on the right. For example:

// integer assignment
numberOfLegos = 14;

// decimal assignment (both int and double can be negative)
averageTemperature = -10.5;

// characters assignment: note the enclosing '
middleInitial = 'V';

Variable Initialization

A variable is initialized as soon as it is assigned a value for the first time.

int numberOfLegos;

numberOfLegos = 14; // variable is now initialized

You can combine declaration and initialization into a single statement:

int numberOfLegos = 14; // this is equivalent to the previous example

Variable Scope

In a program a scope is all of the variables that are visible at a particular location in the code. For a variable to be in scope at a location in code, it must be declared before the point where it is referenced and it must appear within the same block (sections of code between an opening { and a closing }) or in a containing block. A variable cannot be used if it is not in scope.

For example:

int main()
{
  numberOfLegos = 2; // this is not ok - numberOfLegos was not declared yet
  
  int numberOfLegos;
  
  numberOfLegos = 10; // this is ok
  
  {
    double surfaceArea = 6.0;
    
    numberOfLegos = 19; // this is ok - blocks inherit scope from any enclosing scope
  }
  
  numberOfLegos = 3; // this is ok
  
  surfaceArea = 3.2;  // this is not - surfaceArea is not visible oustide it's enclosing block.
  
  return 0;
}

Class Objectives

At the end of today's class you will be able to define:

  • Variable
  • Variable Declaration
  • Variable Assignment
  • Variable Initialization
  • Variable Scope

After completing the assignment, you will be able to:

  • Identify the three categories of simple data types in a C program
  • Use variables of type int, char and double in a program
  • Declare variables in a C program
  • Initialize and assign values to variables in a C program
  • Create a valid C variable name
  • Identify what variables are in scope at a particular location in a program
  • Use basic mathematical operators in a C program
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment