Foundations of programming

Lecture Two - Variables, memory, datatypes

WorkPlan

  1. Introduction, types of programming languages, pseudocode
  2. Variables, datatypes
  3. Pointers and collections
  4. Simple instructions
  5. Data structures
  6. Functions and procedures
  7. Input / Output
  8. Debug
  9. Operators, Iterators
  10. Polymorphism
  11. Functional / object oriented programming
  12. Advanced topics, threads
  13. Software development process, agile, tdd
  14. Final Test during last classes: about 30 open/close quentions - mostly closed.

Program

A "program" is a set of instructions stored in a computer's memory for it to execute.

Usually during execution program has to store additional information, user input, custom settings, or calculated values etc.

Because this information can vary, the term variable is used to describe the element of a program which stores this information.

Variables

In mathematics variables represent unknown or inexplicitly defined fields of an equation.

For instance, in the equation y = m * x + b, each alphabetic letter is a variable that represents something unknown.

In computer science, a variable is nothing more and nothing less than a named location in memory where data can be stored.

Every variable has a name, a type, a size and a value.

Defining variables

Before you can use a variable, you must tell the compiler about it by declaring it (i.e. its name and "type").

To declare a variable you use the syntax

<variable type> <name of variable>;

for example:

int myVariable;

Variables in C

Rules for naming a variable in C

Variable name must not start with a digit.

Variable name can consist of alphabets, digits and special symbol underscore _.

Blank or spaces are not allowed in variable name.

Keywords are not allowed as variable name.

There is no rule on how long a variable can be. However, only the first 31 characters of a variable are checked by the compiler. So, the first 31 letters of two variables in a program should be different.

C is a strongly typed language. What this means it that, the type of a variable cannot be changed.

The C++ language is a "case sensitive" language. That means that an identifier written in capital letters is not equivalent to another one with the same name but written in small letters.

For example, the RESULT variable is not the same as the result variable or the Result variable.

Exercise

Which of these are valid variable names?

a) seven
b) 2nd
c) TOP_speed
d) fast1
e) quick#

Compiled vs Interpreted

Interpreted languages allows any data type to be stores in variable while in compiled languages only data of the correct type may (or at least should) be stored in that location.

Some programming languages assign a value to the variable when you create it but usually you need to initialize the variable.

The use of an uninitialized variable is a common source of programming errors .

Assigning values

When you assign a variable, you use the = symbol. The name of the variable goes on the left and the value you want to store in the variable goes on the right.

int a = 1;
double b = 1.0;
char c = 'z';

Make sure you don't confuse the assignment operator = with the equality operator ==. The individual = symbol assigns value while the == symbol checks if two things are equal.

Example

The line of code

area = pi * r * r;

is an instruction to the computer to fetch the values from two memory locations, which do have absolute "ID numbers", but for our purposes are called pi and r, multiply those values together and then store the result in the memory location called area.

Variable Scope

A variable's scope determines where in a program a variable is available for use.

A variable's scope is defined by where the variable is initialised or created.

In C++ variable scope is defined by a block.

A block is a piece of code following a method invocation, delimited by curly braces {}.

Example variable scope

Wrong

#include <stdio.h>
int main()
{
    /* wrong!  The variable declaration must appear first */
    printf( "Declare %d next", x);
    int x;

    return 0;
}

Wrong

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

    while(true) {
        int x;
    }
    printf( "Declare %d next", x);

    return 0;
}
exampleCPP.cpp: In function ‘int main()’:
exampleCPP.cpp:9:36: error: ‘x’ was not declared in this scope
         printf( "Declare %d next", x);

Global vs Local

A variable can be either of global or local scope.

A global variable is a variable declared in the main body of the source code, outside all functions, while a local variable is one declared within the body of a function or a block.

image

Global and local scope are also maintained in interpreted languages like Python.

Memory

In dynamic typed languages (such as PHP, Javascript, or Python), the programmer cannot tell where a variable should be stored; the interpreter makes that decision.

In statically typed languages (such as C and C++), the programmer can tell where the compiler should store each variable.

The compiler (or the Operating System) can put variables in one of three places within the program's memory: static memory, stack, or heap.

Stack, heap, static

Global variables are stored in a special area in the program's memory called the data segment. This memory is in use for as long as the program is running.

The stack is used to store variables that are only used inside of a function. Stack memory is temporary. Once a function finishes running, all of the memory for its variables is freed. This cycle happens each time the function is called.

Heap is the segment where dynamic memory allocation usually takes place.

image

Stack, heap, static

Variables allocated on the stack are stored directly to the memory and access to this memory is very fast, and it's allocation is structuralized and dealt with when the program is compiled.

image

Variables allocated on the heap have their memory allocated at run time and accessing this memory is a bit slower, but the heap size is only limited by the size of virtual memory .

image

Example

Data segment

#include <stdio.h>

int aa[100000000000];

int main()
{   
    printf ("Hi");              
    return 0;
}

Output

Segmentation fault 

Stack

#include <stdio.h>  

int main()
{   
    int aa[100000000000];
    printf ("Hi");              
    return 0;
}

Output

Segmentation fault (core dumped)     

Heap

#include <stdio.h>

int main()
{

    int *aa = new int[10000000000];

    printf ("Hi");

    return 0;
}
terminate called after throwing an instance of 'std::bad_alloc' 
what():  std::bad_alloc
Aborted (core dumped)

Data types

Data type or simply type is a classification of data which tells the compiler or interpreter how the programmer intends to use the data.

Common data types include:

integers
booleans
characters
floating-point numbers
alphanumeric strings

Data types in C

There are two data types in C : primitives and derived types

Primitives are fundamental data types in C namely integer(int), floating(float), character(char) and void.

Derived data types are like array, function, structure, union and pointer. These will be discussed next week.

image

Machine data type

All data in computers based on digital electronics is represented as bits (alternatives 0 and 1) on the lowest level.

The smallest addressable unit of data is usually a group of bits called a byte (usually an octet, which is 8 bits).

Boolean type

The Boolean type represents the values true and false.

Many programming languages do not have an explicit Boolean type, instead interpreting (for instance) 0 as false and other values as true.

Numeric types

integer (whole numbers) may be sub-typed according to their ability to contain negative values (e.g. unsigned in C and C++). May also have a small number of predefined subtypes (such as short and long in C/C++);

floating point represent fractional values (rational numbers, mathematically). They usually have predefined limits on both their maximum values and their precision.

bignum or arbitrary precision numeric types lack predefined limits. They are not primitive types, and are not used frequently due to efficiency reasons.

int - Integer data types

Integers are whole numbers that can have both positive and negative values but no decimal values. In C, C++ int is a 32 bit number, which means that its higher value is 2147483647.

Example: 0, -5, 10

#include <stdio.h>
int main()
{
    int n = 2147483647;

    printf ("%d\n", n);

    n++;

    printf ("%d\n", n);

    return 0;
}
2147483647
-2147483648

Integer type that takes 64 bits is a long long.

#include <stdio.h>
int main()
{
    long long n = 2147483647;

    printf ("%lld\n", n);

    n++;

    printf ("%lld\n", n);

    return 0;
}

Floating point

In C you declare floating point number with either float or double

Float values can be represented in exponential form as well. For example:

float normalizationFactor = 22.442e2;

Difference between float and double:

The size of float (single precision float data type) is 4 bytes.

The size of double (double precision float data type) is 8 bytes.

Floating point variables has a precision of 6 digits whereas the precision of double is 14 digits.

Real numbers limits

Let's print 3.6 with an accuracy of 20 places of precision:

printf (" %.20f \n", 3.6);  

The result is

3.60000000000000008882 

It is like that because it is impossible to encode in binary system every real number on limited number of bits.

String and text types

Alphanumeric character. A letter of the alphabet, digit, blank space, punctuation mark, etc.

Alphanumeric strings, a sequence of characters. They are typically used to represent words and text.

Char

In C, char values are stored in 1 byte, and are encoded as numbers using the ASCII encoding.

You should never use the ASCII numeric value directly, but you should know that the characters 'A' to 'Z' are contiguous, 'a' to 'z' are contiguous, and '0' to '9' are contiguous.

Using the relationships between the upper case alphabetic, the lower case alphabetic, and the numeric characters we can write expressions like this:

'a' + 2          // evaluates to the character value 'c' (becuase 'a' is
                   // (encoded as a number we can add 2 to it to get 'c')
'C' > 'A'     // evaluates to true, which is a non-zero value in C
                 // (0 is false)

for(ch = 'a'; ch < 'z'; ch++) { ... 

Example

#include <stdio.h>
int main()
{
    char a = 'a';
    int b = 2;

    printf ("%c\n", a);

    a+=b;

    printf ("%c\n", a);

    a=66;

    printf ("%c\n", a);

    int y = a;

    printf ("%d\n", y);

    return 0;
}  

Output

a
c
B
66

Qualifiers

Size qualifiers alters the size of a basic type. There are two size qualifiers, long and short. For example:

long double i;

Integers and floating point variables can hold both negative and positive values. However, if a variable needs to hold positive value only, unsigned data types are used.

unsigned int positiveInteger;

Constants qualifiers

A constant is a value or an identifier whose value cannot be altered in a program.

const double PI = 3.14  

Datatypes in memory

ij

Escape sequence

For special characters which cannot be typed or has special meaning in C programming, for example: newline (enter), tab, question mark etc. we use escape sequence:

\b Backspace

\f Form feed

\n Newline

\r Return

\t Horizontal tab

\v Vertical tab

\\ Backslash

\' Single quotation mark

\" Double quotation mark

\? Question mark

\0 Null character

Enumeration constants

Keyword enum is used to define enumeration types. For example:

enum color {yellow, green, black, white};

Here, color is a variable and yellow, green, black and white are the enumeration constants having value 0, 1, 2 and 3 respectively.

Example: Enumeration Type

#include <stdio.h>

enum week { sunday, monday, tuesday, wednesday, thursday, friday, saturday };

int main()
{
    enum week today;
    today = wednesday;
    printf("Day %d",today+1);
    return 0;
}

Output

Day 4

Enum as flags

We can use binary representation to use enum as flag elements.

#include <stdio.h>

enum designFlags {
    BOLD = 1,
    ITALICS = 2,
    UNDERLINE = 4
};

int main() {
    int myDesign = BOLD | UNDERLINE;     
        //    00000001
        //  | 00000100
        //  ___________
        //    00000101
    printf("%d", myDesign);

    return 0;
}

Example

int a = 5;
int b = a;
a = 7;
printf("%d\n",b)

What is the value of b? What would you like it to be?

Variables in Python

In Python the type of a variable can change during the execution of the script:

i = 42             # data type is implicitly set to integer
i = 42 + 0.11      # data type is changed to float
i = "forty"        # and now it will be a string  

Python automatically takes care of the physical representation for the different data types, i.e. an integer values will be stored in a different memory location than a float or a string.

Memory representation

In Python variable can change a place in the memory it is assign to :

>>> x = 3
>>> y = x
>>> y = 2

First y points to the same place as x then it changes its pointer.

image

C++ 11

Auto deduction of the type by the initializer

In the C++11 standard, the auto keyword is no longer a storage class specifier, but acts as a type specifier that directs the compiler to deduce the type of a declared variable from its initialization expression.

int foo = 0;
auto bar = foo;

auto number1 = 123;

Exercise

Which of the following is not a correct variable type?

A. float
B. real
C. int
D. double

References

https://www.programiz.com/c-programming/c-variables-constants

https://en.wikibooks.org/wiki/Introduction_to_Programming_Languages/Types_of_Storage

https://en.wikiversity.org/wiki/Introduction_to_Programming/Variables

http://www.cplusplus.com/doc/tutorial/variables/

https://www.programiz.com/c-programming/c-enumeration

https://www.programiz.com/c-programming/c-data-types

https://www.python-course.eu/variables.php

http://www.geeksforgeeks.org/memory-layout-of-c-program/

http://www.cplusplus.com/doc/oldtutorial/variables/

https://www.ntu.edu.sg/home/ehchua/programming/cpp/c0_Introduction.html

https://www.cs.swarthmore.edu/~newhall/unixhelp/C_chars.html

https://www.cprogramming.com/tutorial/c/lesson1.html