0% found this document useful (0 votes)
4 views

CS 50

The document contains lecture notes on the C programming language, covering fundamental concepts such as functions, variables, data types, and control structures like loops and conditionals. It explains the importance of header files, format codes, and custom functions, as well as the process of compiling and linking code. Additionally, it touches on cryptography and the role of the compiler in converting code into executable programs.

Uploaded by

johndennisjoseph
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

CS 50

The document contains lecture notes on the C programming language, covering fundamental concepts such as functions, variables, data types, and control structures like loops and conditionals. It explains the importance of header files, format codes, and custom functions, as well as the process of compiling and linking code. Additionally, it touches on cryptography and the role of the compiler in converting code into executable programs.

Uploaded by

johndennisjoseph
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

CS 50

C programming language
Lecture notes

The basic notion for programming

Arguments functions side effects

Escape sequences

Eg \n ( indicates a new in java script)

Header file ( represents what is called a library )

#include <stdio.h> (teaches compiler to printf)

Is a separate file in the local hard drive

Library is a code that someone else wrote

(manual.cs50.io )

At a time when programming we can have multiple header files so that the code can recoginse multiple
types of conditions.

basic notion for interactive programming

Arguments functions Return value

In C (provided that we have <cs50.h> header file)

 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

%c – char ( only single character )

%f - float (real numbers, decimals)

%i - integer

%li

%s – string which is a number of characters in a line

Conditionals

If else conditionals OR if conditional = boolean expression


Variables

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

Printf(“X is not greater


Printf(“Yes x Is greater
that y”)
than y”)

stop
Loops

Loops tell the computer to do a task n number of times

There are two types of loops in c

For loop

Here for is a function

Infinite While loop

Here while is a function


Making custom functions

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.

Scope the context on which the variables exist

Textual commands for common interactions on GUI

Cd- change directory

Cp

Ls- list

Mkdir

Mv- rename

Rm- remove

Rmdir- remove directory

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

Eg: conversion of integer to float for the process of division

Section 1

Variable- A variable is the name for a value that can change over time

Variable type value

Assignment operator

Data types and Variables

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

There are other types of qualifiers such as short, long or const

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

Only present in the cs50 library

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

HOW TO CREATE A VARIABLE

A variable needs 2 things

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

Eg make hello = clang hello.c

Clang when used creates a ./a.out function

What happens when you run clang?

Preprocessing

#including <stdio.h> - is called a preprocessing directive

Preprocessing converts all of those - #include - lines to whatever underlying prototypes

Compiling

We use it as a catch all phrase to complie all the code

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

You might also like