ECS 36B Lecture 2: Transitioning to C

88 views4 pages
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
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
Unlock document

This preview shows page 1 of the document.
Unlock all 4 pages and 3 million more documents.

Already have an account? Log in

Get OneClass Notes+

Unlimited access to class notes and textbook notes.

YearlyBest Value
75% OFF
$8 USD/m
$30 USD/m
You will be charged $96 USD upfront and auto renewed at the end of each cycle. You may cancel anytime under Payment Settings. For more information, see our Terms and Privacy.
Payments are encrypted using 256-bit SSL. Powered by Stripe.