Introduction to CS. Terms I
All this information is derived from the Harvard CS50 course taught by David J. Malan. The following information is taught by Dough Lloyd.
In this ‘TERMS’ series, you’ll learn:
- Data Types
- Operators
- Conditional Statements
- Loops
- Command Lines
Let’s get started with the first one.
DATA TYPES & VARIABLES
Modern languages like PHP or Java Script don’t ask you to declare the type of the data when using it. In C, we need to specify the data type of every variable we create: string, character, etc.
int
Stores integer values: 1,2,3, -1…They always take 4 bytes of memory, or 32 bits. The range of values is limited to what it can fit within 32 bits of information. The range of values to represent goes from -2³¹ to 2³¹ -1. Half of the values are positive and half are negative.
unsigned int
it is called a qualifier. it slightly modifies the data of integer. It doubles the postive range of values an int can take on. If you have a number higher than 2B but less than 4B then use an unsign, because the value will never be negative. Now the range is from 0 to 2³² -1
There are other qualifiers as short, long, and const.
char
used for variables that store single variables. They take just 1 byte of memory, or 8 bits. Which means that they cannot store more than 8 bits of information. The range goes from -128 to 127.
float
floating point numbers are real numbers, they have decimal points. They contain within 4 bytes or 32 bits of memory. There is no clear range. Pi has an integer part 3 and a floating part has a .14… if the decimal part gets very long, I will not be able to represent the precise decimals. There is a precision problem with the floats.
double
like floats, we store real numbers. They are double precision, they fit 64 bits of data of 8 bytes. If you have a really long decimal place and a lot of precision is important, use a double. It gives you extra 32 bits to work with.
void
It is a type, but not data type. If a function has a void return type means that it doesn’t return a value .Printf is one, if prints something on the screen but does not give a value back. Printf is a void funciton. The parameter list of a function can also be void as int main(void). Main does not take any arguments. Void is a placeholder to think as ‘nothing’ no return value or parameters.
These are the 5 types in C, but CS50 provides you with more:
bool
data type to store a boolean value, true or false. For this, include #include <cs50.h>.
string
Strings are words, sentences, paragraphs. If you need to use them to store a word, add #include <cs50.h>.
Later on structures or structs and defined types or typedefs will appear.
To create a variable:
step 1: give it a type
step 2: give it a name
int number;
char letter:
int height, width;
float sqrt2, sqrt3, pi; → we are creating 3 values at once. We can do it in 3 separate lines.
Desing-wise, just create a variable when you need it.
After declaring a variable, how do you use them?
int number; → declaration
number=17; → assignment
char letter; → declaration
letter=’H’; → assignment
initialization is when you declare and set the value at the same time. int number = 17
You can access the course here: https://online-learning.harvard.edu/course/cs50-introduction-computer-science?delta=0
You can follow me on LinkedIn here: https://www.linkedin.com/in/nur-younis-aa79a9183/