CS 50
CS 50
C programming language
Lecture notes
Escape sequences
(manual.cs50.io )
At a time when programming we can have multiple header files so that the code can recoginse multiple
types of conditions.
get_string is basically telling the compiler to get an input from the user
%s is almost equal to saying join
Whenever we want to write a code we have to include the corresponding library.
Format codes
%i - integer
%li
Conditionals
Whatever the variable is they should be suffixed by its corresponding type of function.
Eg:
Int age = 3
Here the format type int such characters help the computer to understand what type of function we r
referring to.
Char agree = y
Here the function is char which helps to only obtain a single character from the user, is extensively
undes in the terms and conditions agreement
Etc
Comparison
Flow chart
Case 1 ( only if )
start
x>y
true
False
Printf(“Yes x Is greater
than y”)
stop
Case 2 ( if else )
start
x>y
true
False
stop
Loops
For loop
Here the void meow(void) with the red underline is what is called a prototype it allows the computer to
understand that it tells the computer that meow() is a custom function which the computer should look
for lower down in the code
The few lines of code in the blue box is the actual code for the function.
Cp
Ls- list
Mkdir
Mv- rename
Rm- remove
Integer overflow
Truncation-
It is just a term of art that means if you take an integer and you divide it by an integer, even if you get a
fractional value the fraction gets thrown away because you’re only doing integer based math. So if there
is anything after the decimal point it get truncated ( literally discarded)
Type casting
It means when your converting one type of value into another format even if it dosen’t have a
mathematical impact. This will allow the result to be considered as a float, hence truncation will not
happen
Section 1
Variable- A variable is the name for a value that can change over time
Assignment operator
1. int
the int data type is used for variables that will store integers
int always takes up 4 bytes of memory (32 bits). This means that the range of values that they
can store is limited to 32 bits of information.
1 byte = 8 bits
Unsigned int
Unsigned int is a qualifier which can be applied to certain types ( including int ), whih effectively doubles
the positive range of variables of that type, at the cost of disallowing all the negative values
2. Char
The char data type that is used for variables that will store single characters
Chars will always take up 1 Byte of memory ( 8 bits ). This means that the range of
information that they store is necessarily limited to 8 bit worth of information
Thanks to ASCII we have developed a mapping of characters like A, B, C……etc to numeric
values in the positive side of this range.
3. Float
The float data type is used for variables that will store floating point values , also known
as real numbers.
Floating point always takes up 4 bytes (32 bits) of memory
It is a little complicated to describe the range of a float, but suffice it to say with 32 bits
of precision, some of which might be used for an integer part, we are limited to how
precise we can be.
4. Double
The double data type is used for variables that will store floating point values, also
known as real numbers
The difference is that doubles are double precision. They always take up 8 bytes (64 bits)
of memory
With an additional 32 bits of precision relative to that of floats, doubles allow us to
specify more precise real numbers
5. Void
It’s a type, not a data type
Functions can have a void return type, which means that they don’t return a value
The parameter list of a function can also be void, it simply means that the function takes
no parameters. Eg: int main(void) mainly means that the main function does not take
any parameters
Eg: printf() is a void function it returns nothing
6. Bool
The bool data type is used to store a Boolean value. More precisely, they are capable of
only storing one of 2 values: true or false
7. String
The string data type is used for variables that will store strings of characters, which
programmers usually call a string.
String include things such as words, sentences, paragraphs and the like
A type ( specifying what type of variable it is ) and a name (a identifier for the variable)
Eg:
Int number ;
Char letter ;
Using a variable
After a variable has been declared there is no need to declare to same variable again( doing so has some
unintended consequences)
If you are simultaneously declaring and setting the value of a variable (sometimes called initializing), you
can consolidate this to one single step
Week 2
Cryptography
The process done by a computer inorder to securely transmit messages to the second user from the first
user
Compiler language
Clang= make
Preprocessing
Compiling
assembly
When we use clang it produces an output called- ./a. out- here what it means is assembly output
It is basically linking the clang and all the code to assembly language which was what was used to code
computers in the earlier days of computing
Linking