Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI206 - Computer Organization & Programming

Similar presentations


Presentation on theme: "CSCI206 - Computer Organization & Programming"— Presentation transcript:

1 CSCI206 - Computer Organization & Programming
C grammar, variables, and basic IO zyBook: 2.3, 2.6, 2.8, 2.11, 2.16

2 Survey results: most important
Abstraction (hardware and software) Moore’s Law ISA, API, ABI

3 Survey results: puzzled, want more
Connection between assembly and higher level languages ABI Is there a limit to Moore’s Law? Hierarchy of memory Gates and transistors, relation Embedded computer How to write better code with ISA in mind How to best use the zybook? Do we need to take notes? Are you (professor) going to repeat what the exercises and book already said?

4 Powers of two What is ? 1 2 3 4 Go to socrative.com Log in as student
Pick “MENG7820” as the classroom Powers of two What is ? 1 2 3 4

5 Powers of two What is ? 27 64 96 128 256

6 Powers of two What is ? 8 48 72 74 124

7 Coding conventions Names “names should fit” how it is used
Based on Linux kernel coding style Names “names should fit” how it is used lowercase with _ separating words, or camel case this is the same as in Python and in Java, though most C porgrammers prefer lowercase with _ length proportional to importance temporary vars can be i, j, k important vars: student_grade, course_name include units if appropriate: timeout_msec, height_ft constants: ALL_CAPS

8 Coding conventions Indentation and spacing: While indentation in Python is a critical piece of syntax, indentation and spacing in C is more for easier reading and reducing possibilities of errors. int func(int a) { if (a == 0) return 4; else return a * 3 + 4; } int func(int a) {if (a==0) return 4; else return a*3+4;}

9 C basic types and their ranges
int INT_MIN and INT_MAX insigned int 0 and UINT_MAX float - FLT_MIN to + FLT_MAX double - DBL_MIN to + DBL_MAX char Depending on the coding scheme

10 Implicit type conversion
If any value in a numeric expression is floating point, both operands are converted to floating point. else, integer operations are used. even if the result would be floating point. e.g., int x = 4 / 5; // what is x? e.g., What does it result in Python? x = 4 / 5 #? x = 4 // 5 #?

11 Explicit type conversion
Use (type) to force a type conversion. x = (int) 3.5; // results in x == 3 Note, float->int truncates (doesn’t round). (int)3.9999; //? What if we want to round up?

12 String There is no native string data type in C
There is a character data type (char) A string is a null-terminated sequence of chars char test[] = “test”; “test” occupies 5 bytes in memory (include the null terminator!) Note: unlike Python, strings are mutable! t e s \0

13 Manipulating Strings Looping through arrays of char is tedious work...
#include <string.h> Gives access to a number of useful functions (length, concatenate, copy, etc) Go read the manual man 3 string

14 #include <stdio.h>
man 3 printf printf introduction "Printf" by I, Surachit. Licensed under CC BY-SA 3.0 via Wikimedia Commons - printf uses a format string the % character is a special character that will be provided by a variable.

15 %[flags][width][.precision]type
d,i = integer u = unsigned integer f,F,e,E = double x = hexadecimal unsigned integer s = null terminated string c = character p = pointer % = print the % character

16 %[flags][width][.precision]type
+ = always include +/- for a numeric value - = left align output (default is right aligned) 0 = use 0 instead of space to pad to a given width

17 %[flags][width][.precision]type
a number the minimum number of characters to output defaults to padding on the right with spaces

18 %[flags][width][.precision]type
a number for floats the number of digits to the right of the decimal for strings the maximum number of characters to output (truncate)

19 printf - example formats
printf( “%5.2f”, ); // _3.14 5-position output (including the decimal), 2 to the right of the decimal, padded on the left with spaces printf( “%05.2f”, ); // 03.14 same as above but padded on the left with 0’s printf( “%-5.2f”, ); // 3.14_ same as first, but padded on the right with spaces (cannot right pad with 0’s!)

20 Conversion How many MiB is 2048 KiB? 1.0 2.0 2.048 1024 2048 Go to
socrative.com Log in as student Pick “MENG7820” as the classroom How many MiB is 2048 KiB? 1.0 2.0 2.048 1024 2048

21 Conversion What is 5 KiB in KB? 1.024 4.8828125 5.0 5.120 5.256 Go to
socrative.com Log in as student Pick “MENG7820” as the classroom What is 5 KiB in KB? 1.024 5.0 5.120 5.256

22 scanf - read input using format strings
int i; scanf (“%d”, i);

23 scanf - read input using format strings
int i; scanf (“%d”, i); Parameters are passed by value, this can’t work!

24 scanf - read input using format strings
int i; i = scanf (“%d”);

25 scanf - read input using format strings
int i; i = scanf (“%d”); There is a return code, it is the number of inputs read (zero means failure)

26 scanf - read input using format strings
int i; scanf (“%d”, &i); & is the address-of operator. This gives the address of the local variable i to scanf scanf reads an integer (%d) and stores it in the given memory address

27 Loops

28 Decisions

29 Avoid common bugs by ensuring each clause is in parens!
Decisions Avoid common bugs by ensuring each clause is in parens! ( ) ( )

30 Decisions warning: case blocks do not start a new scope block and fall through unless you use the break keyword


Download ppt "CSCI206 - Computer Organization & Programming"

Similar presentations


Ads by Google