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 A quick, simple, complete C program
Include file, similar to Python’s import #include <stdio.h> int main(int argc, char * argv[]) { printf(“Hello world!\n”); return 0; } Function parameters, Similar to Python’s C function name, similar to Python’s def name C statements

3 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

4 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. The following two pieces of code do the same. 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;}

5 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

6 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 #?

7 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?

8 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 in C are mutable! t e s \0

9 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

10 #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 whose value will be provided by a variable in the variable list.

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

12 Result of executing cbasics

13 %[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

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

15 %[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)

16 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!)

17 Result of executing cbasics-pos

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

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

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

21 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)

22 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

23 Loops

24 Decisions

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

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

27 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

28 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


Download ppt "CSCI206 - Computer Organization & Programming"

Similar presentations


Ads by Google