1 / 28

CSCI206 - Computer Organization & Programming

Learn the basics of C programming, including grammar, variables, basic input/output, and coding conventions. Explore C function parameters, types and ranges, string manipulation, and loops. Discover how to make decisions and perform conversions in C.

huntc
Download Presentation

CSCI206 - Computer Organization & Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCI206 - Computer Organization & Programming C grammar, variables, and basic IO Revised by Alexander Fuchsberger and Xiannong Meng in spring 2019 based on the notes by other instructors. zyBook: 2.3, 2.4, 2.7, 2.10, 2.12,2.13

  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. Based on Linux kernel coding style Coding conventions http://www.eg.bucknell.edu/~cs206/sp19/lab/c-coding-standards/ 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

  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 to 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!

  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 uses a format string the % character is a special character whose value will be provided by a variable in the variable list. "Printf" by I, Surachit. Licensed under CC BY-SA 3.0 via Wikimedia Commons - http://commons.wikimedia.org/wiki/File:Printf.svg#mediaviewer/File:Printf.svg

  11. %[flags][width][.precision]type 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 flags + = 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 width a number the minimum number of characters to output defaults to padding on the right with spaces

  15. %[flags][width][.precision]type .precision 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.14159265359 ); // _3.14 5-position output (including the decimal), 2 to the right of the decimal, right-justified printf( “%05.2f”, 3.14159265359 ); // 03.14 same as above but padded on the left with 0’s printf( “%-5.2f”, 3.14159265359 ); // 3.14_ same as first, but left-justified printf( “%+5.2f”, 3.14159265359 ); // +3.14 prints five positions, with a leading ‘+’ printf( “%+5.2f”, -3.14159265359 ); // -3.14 prints five positions, with a leading ‘-’

  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. 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 Go to PollEv.com/xiannongmeng758 How many MiB is 2048 KiB? 1.0 2.0 2.048 1024 2048

  28. Conversion Go to PollEv.com/xiannongmeng758 What is 5 KiB in KB? 1.024 4.8828125 5.0 5.120 5.256

More Related