Week 2.docx

8 Pages
Unlock Document

Electrical & Computer Engineer
ECE 15
Alon Orlitsky

1. How to Comment in Source Code a. “//” is used at the end of the line to input comments that are not registered by the OS as part of the executable file. i. printf(“Hello World!\n”); // I am a comment ii. printf(“Hello World!\n”); /*I am also a comment *With many lines*/ *useful for multiple line commands 2. Writing (Printing) a. The C printing function “printf()” prints formatted data in UNIX. This function is shared by many languages. b. Integers i. Usually formatted using %d (decimal) or %i (integer) to indicate its data type to the OS. 1. printf(“%d”, -13); a. outputs “-13” 2. printf(“my %i cents”, 2); a. outputs “my 2 cents” 3. printf(“%d”, 2*3+4); a. outputs “10” 4. printf(“%d+%d=%d”, 1, 2, 1+3); a. outputs “1+2=4” b. Although there is a plus sign in the argument, the “printf” function does not do arithmetic; it simply prints what is exactly in the quotes. The data types are represented by the arguments made after the quotes. (1, 2, 1+3). These arguments will use arithmetic operations. ii. %3d – uses at least 3 locations given in one argument (if no numbers given, it will replace with spaces) 1. printf(“#%d#%3d#%3d#”, 1, 2, 3456); a. outputs “#1# 2#3456#” 2. printf(“#%d#%3d#%-3d#”, 1, 2, 3; a. outputs “#1# 2#3 #” 3. printf(“#%5.3d#%-5.3d#”, 1, 2); a. outputs “# 001#002 #” i. The negative reverses the order (so starts with number, then spaces) ii. The “.3” indicates to precede with three 0’s before the argument, regardless of reverse order or not. 3. Variables a. Hold information, and can change overtime b. Variables must be declared i. Format is usually type, name, optional initial value 1. int num,sum; a. “int” is the data type, and “num” and “sum” are two separate variable names 2. double weight = 0.0; a. “double” is the data type, “weight” is the data name, and “0.0’” is the optional initial value. 3. char digit = ‘4’ c. Data Types i. All constants and variables have a data type, such as an integer, real number, character, or a string. ii. Integer 1. Basic integer type; integers are unitialized (contains previously stored value) 2. Integers can be assigned a value at either declaration or assignment a. Declaration: i. int a=0; ii. int a=0, b=1; 1. “=” does not mean equal, it means to store a value in the variable. b. Assignment i. int a; a=0; ii. int a, b; a=0 b=1; iii. int a, b; a=b=0 3. Printing integers is the same as printing constants a. printf(“%d”, a); i. “a” is the variable used as the argument for %d 4. Integers can be “read” (scans for human input) by using the function “scanf()”. Any value that is read by this function can be stored into a variable using the “&” value placed before the variable. a. #include int main() { int value; printf(“Type an integer: “); scanf(“%d”, &value); printf(“It is: %d\n”, value); return 0; } i. This executable scans for an integer, assigns the value to the variable “value”, then prints out the value. However, it will only accept an integer data type, as indicated by the “%d”. scanf() will stop reading as soon as it encounters a non-integer, such as a period, or comma (e.g 1.5 or 1,234. In this situation, the “1” will still be read. 4. How Much Time Left (program) a. Asks user for current time h:m, output time left i. #include int main() { int hour, minute; printf(“Enter current time (hh:mm) : “); scanf(‘%d:%d”, &hour, &minute); printf(“%d hours and %d minutes left\n”, 23-hour, 59- minute); } 5. How Many Values can “scanf” Read? a. Can return number of successfully read variables 6. Representation of Integers a. Computers represent everything in binary: 0,2 b. For nonnegative integers, they use standard binary representation c. For 3 bits: 0=000, 1=001, 2=010, 3=011 i. This binary representation starts with 0 n−1 ii. With n bits, 0 is represented 0 to “2 −1 ” numbers d. Negative integers start with 1 7. Size of integers represented a. Most compilers allocate 4 bytes (32 bits) 31 31 i. So the range of integers represented is -2 −2 ,..,01,0,1,…,2 −1 b. “sizeof(object) returns number of bytes used to store object i. Object can be a type name (int) , a variable name(x), or an expression(x+1) 8. Unsigned Integers a. Represents only positive integers, hence doubles the range. The first binary number is not used to determine wither integer is positive or negative, thus the first binary number can be used to represent more numbers. 9. Constants a. In order to represent decimals, you can either using the floating point (e.g 1.23, .7, 1 13.0) or through scientific notation (1.23* e ) b. Printing %g, %f, %e will try to use the floating point format i. printf(“%g” , 24.00); = 24 printf(“%f” , 2.4); = 2.400000 *automatically defaults to six decimal points 10. Float and Double a. Basic types,
More Less

Related notes for ECE 15

Log In


Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.