280 likes | 296 Views
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.
E N D
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
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
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
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;}
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 #?
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?
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!
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
#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
%[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
%[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
%[flags][width][.precision]type width a number the minimum number of characters to output defaults to padding on the right with spaces
%[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)
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 ‘-’
scanf - read input using format strings int i; scanf (“%d”, i);
scanf - read input using format strings int i; scanf (“%d”, i); Parameters are passed by value, this can’t work!
scanf - read input using format strings int i; i = scanf (“%d”);
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)
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
Decisions Avoid common bugs by ensuring each clause is in parens! ( ) ( )
Decisions warning: case blocks do not start a new scope block and fall through unless you use the break keyword
Conversion Go to PollEv.com/xiannongmeng758 How many MiB is 2048 KiB? 1.0 2.0 2.048 1024 2048
Conversion Go to PollEv.com/xiannongmeng758 What is 5 KiB in KB? 1.024 4.8828125 5.0 5.120 5.256