17 Jan 2019
School
Department
Course
Professor

ECS 36B - Lecture 2 - Transitioning to C
~ Wednesday, January 9th, 2019
Getting and Validating Input:
#include <stdio.h // C is a lower-level programming language so we have to use many libraries
#include <math.g> // another example of how to import from available C library
Unlike python, C variables cannot change type throughout the program:
● in Python a variable can initialized as a string and then be changed to an integer
○ Python is a higher-level programming language and takes care of things like this
for the user
● in C the type of a variable becomes LOCKED to that variable
○ This is to reduce type errors throughout the program
Types:
Int - integers (same as python)
Char - character (same as python)
Double - real numbers (NEW!) → has more precision than a float (takes up 8 bytes)
Float - real numbers (same as python)
→ will only have to use these in specific scenarios, not
crucial for our purposes [takes up 4 bytes -- less precise than doubles]
Arithmetic symbols carry over from Python:
%d -- denotes decimal value; it’s just a placeholder for decimal numbers in print statements
● printf(“He is %d years old”, [var_age]
)
○ The var_age variable replaces the %d in the statement and then prints
%.2lf : same thing as %d above except with the constraint that the input ONLY accepts numbers
with exactly 2 numbers after the decimal point = a specific input requirement
ASCII -- allows for arithmetic between numbers and characters (multiply, add, subtract, etc)
Character * Number → ASCII value will be used for the character and a number is returned
String * Number → will do some funny things … so don’t do it
● Note that in Python a string * integer will print the string “* integer” number of times
During Division between floats & doubles → result will always be a double or float
During Division between integers → result will be integers
● (same as Python)
ord() → returns the ASCII value of the ASCII character inside the parenthesis
chrd() → converts the ASCII value in the parenthesis back into an ASCII character