CS50x Week 1 - C

Correctness - We need to do a code that work the way we need.
Design - Beyond correctness, we need that our code must be well designed. (It can never be a visual mess).
Style - Create a habit of write code in a certain way.
Command Line Interface (CLI) - Offered by Linux to transform source code in machine code.

Comands (Linux):

  1. make/code (+ name): create a program after finish the code.
  2. rm (+ name): to remove the program created
  3. y: to confirm
  4. cd: change directory
  5. type cd +directory name to create a new directory.
  6. type cd +directory name of a directory that already exist to enter in the directory. (to create a new directory inside the actual directory, just type cd again and repeat the steps)
  7. -
  8. type cd.. to get out of the directory (one step).
  9. type cd../.. to back to the default folder.
  10. cp: copy
  11. ls: list
  12. mkdir: make directory
  13. mv: move(or rename)
  14. rmdir: remove directory

Commands/Data Types (C) - Standard IO (stdio.h):

  1. String: used to save and print a string, like a phrase or a name.
  2. Bool: To create a Boolean value, if something is true or false.
  3. Char: Used to a single character.
  4. Float: To real numbers with a decimal point.
  5. Double: Used with you need more precision (more numbers after the decimal point).
  6. Int: To real numbers (intergers)
  7. Long: when you have to use a bigger int.

Commands to get a type of data (C) / to print a type of data (C) - CS50 IO (cs50.h):

  1. Get_char: / %c
  2. Get_float: / %f
  3. Get_int: / %i
  4. Get_long: / %li
  5. Get_string: / %s
  6. Get_double:

Functions: Can be of different types, for different purposes, for example: get_string( );
Also, every function (or almost all) comes together with the structure: parenthesis, doble apostrophe and semicolon to end the function.

Get_string(“”) is a function to keep a informed value and have it used afterwards.

It has to be stored in some identifier, so it is used the equal symbol to indicate where the string informed in the get_string will be stored.

Answer = get_string(“what’s your name?”);

You also have to indicate what type of value you are going to store (as you can see, computer needs high level of specification.

string answer = get_string(“what’s your name?”);

After storing the information, you want to print it, return the value to the screen. You have to inform to the computer, inside of the doble quotes that you want to have a placeholder using %s. Doing this, the computer understand that it have to put a string where %s indicated.

Printf( “hello, %s”);

Next step is to insert a coma then indicating the variable you want that the computer plug in at that %s location.

Printf( “hello, %s”, answer);

When setting a variable line counter you can define its initial value as 0, or another value you want. But to add a value you must follow some rules:

  • Counter = counter + 1; (note that the equal symbol doesn’t mean equality, it means that the final value of counter will be the result of what is informed after the equal symbol and stored in the variable counter. Other options below.
  • <Counter = +1;
  • <Counter++;
It turns that the initial value of the variable counter will be 1.

Interger Overflow
Must substitute the int for long when the returned result of a operation is bigger than 32bits and it comes as an error. Using long in place of int you expand the amount of bits supported by the program.

To comment in C the command to initiate the comment is // ..comment

Conditions (Boolean Expressions)
when you have to obey a condition to have a result. The result can come through to ways (conditions – IF or ELSE). If the condition is attended the result will return a value declared to IF. If the condition is not attended, the result will return as declared to ELSE.

When you see something inside parenthesis generally is a function, but not when using IF.

When a Boolean expression (a question in program) has 3 possibilities, x > y, x < y, x = y, you just have to ask two questions. The first question will return a true or false answer, already testing two possibilities. For example, x > y if true the program will print the answer. If false the ELSE question will be asked y > x, but as we have a third option, this ELSE must be a ELSE IF, asking ELSE IF y > x or ELSE x == y.

When using conditionals and eventually loops, there’s no semicolons involved. (not adding ; at the end of if, else, else if.) Semicolons usually finish your thought after a function.

To avoid some errors or mistakes in the code we can create a ‘const’ to the variable int mine = 2; > const int mine = 2; It will save the value of 2 into the variable mine making it impossible to be changed inside the program throughout other variable/function. (you are saying to the computer not trust in you, making it constant and neve thinking about it again).

Nice tip is to make the name of the constant capitalized, mine > MINE. A visual reminder.

Parity (even or odd) To tell a number resultant of an operation (or just informed by the user) is even or odd we must use the operator % (reminder) and if (n % 2 == 0) then it’s even, otherwise its odd. Another hint about this line of code is the double equal symbols. Using two of them you tell to the computer you mean literally equality and not equivalency (when you use a single equal sign you mean that the result of the operation after the = will fill the value.

Agree (ex: checkbox to agree/disagree) – using Or When you came to answer a simple question in a process to agree or disagree with something like using just y for yes/agree and n for no/disagree it turns that you are using a Character (or cha) because you will have in return as a value just a single character, y or n. In addition, to avoid mistakes, you can indicate to the program that you also accept a capitalized Y or N as answer.

A Loop [While (true)] and For Loop When you want a action to repeat forever or repeat X times as you defined. To repeat forever, the value set must be ‘true’. To repeat a defined amount of times, must be defined a counter and in every repetition the must be added one point at the counter.

Creating functions To create a new function, we have to declare before the name of the function what it will return, if nothing just void, then we create the function name ‘meow’. The void within the parentheses after the name of the function indicates the that function doesn’t take any arguments or inputs.
void meow(void)
So now we have another way to type our code with the same result. For that, we also declare what do we want to have inside our function, in this case:

Discount (Float)
Sometimes we need to create functions that returns a value. In this case we have as example the type of data Float.
First the function to apply the discount must be created.
float discount(float price);
It means that the function is a float type, its name discount and returns a price in it.

In C, we must specify the *return type (of data)*, the *name of the function*, and the *inputs, or arguments* to that function in that order and if none of them are applicable, you write the word void.
Example: float discount(float price).