Lecture Two - Variables, memory, datatypes
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.
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.
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 in C you use the syntax
<variable type> <name of variable>;
for example:
int myVariable;
Rules for naming a variable in C
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.
Which of these are valid variable names?
a) seven
b) 2nd
c) TOP_speed
d) fast1
e) quick#
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 .
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.
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.
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 {}.
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);
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.
Global and local scope are also maintained in interpreted languages like Python.
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.
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.
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.
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 .
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 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
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.
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).
Since C99, language C introduce a 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.
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;
}
2147483647
2147483648
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.
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.
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.
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++) { ...
The ASCII character set has been adopted as the standard in information exchange.
The first 32 characters and the last one are control codes, the others are printable characters. The following table shows the ASCII character set.
#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
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;
A constant is a value or an identifier whose value cannot be altered in a program.
const double PI = 3.14
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
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.
#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
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;
}
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;
Which of the following is not a correct variable type in C?
A. float
B. real
C. int
D. double
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?
This is going to be next week topic.
In Python (interpreted languages) 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.
Unlike in C for interpreted languages variables can change a place in the memory they are assign to :
>>> x = 3
>>> y = x
>>> y = 2
First y points to the same place as x then it changes its pointer.
Which means that variable is not a place in the memory but a pointer to a place in the memory (you will hear more about pointers next week).
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