HISTORY, BASIC SYNTAX

Introduction

1 2

What is ? Why C?

C is a general-purpose programming It’s still popular and widely used language initially developed by Dennis – Available on almost all platforms Ritchie between 1969 and 1973 at – Lots of libraries for different tasks AT&T Bell Labs Provides a relatively low-level (or mid-level) access to the It is an imperative procedural language operating system and hardware intended for system software – System-level programming, embedded systems – Strong ties with operating system – Can lead to better performance It has influenced many other programming languages – C++, C#, ObjC, Java, JavaScript, Go, csh, ...

3 4 Standards Look and feel

First standard by ANSI in 1989, known as “ANSI C” or C89 /* Computing the factorial of an specified (by the user) number */ #include – Still the best choice when portability is important! int fact(int n); int main(void){ – ISO adopted the same standard in 1990 (C90) int current; printf("Enter some POSITIVE integer (non-positive to finish): "); Next revision in 1999, scanf("%", ¤t); – New datatypes, complex numbers, variable length arrays,... while (current > 0) { printf("Factorial of %d is %d\n", current, fact(current)); – Not fully backwards compatible with C90 printf("Enter a POSITIVE integer (non-positive to finish): "); scanf("%d", ¤t); Current standard is C11 } return 0; – Improved Unicode support, atomic operations, multi-threading, }

bounds-checked functions, ... /* This function returns the factorial of the specified integer (n) */ int fact(int n) { ...

5 6

Basic syntax Formatting

Code is case sensitive /* example function */ Free format Statements terminate with ; float foo(int bar) { int i, c = 0; – Whitespace, , layout etc. do not matter … {} enclose blocks float x; x = 0.1; There are two ways to comment: /* example function */ for (i = 0; i < bar; i++) { to the computer ! float foo(int bar) { x += i*3.14 + c; int i, c=0; c = i + 2; /* Single or float x; } /* same example function? */ Multiple lines */ return x; float x = 0.1; } foo(/*float f,*/int bar) { int i,c=0; float x; for (i=0; i

7 8 Basic data types

Basic datatypes in C are:

char character DATATYPES, VARIABLES, ASSIGNMENT AND OPERATORS int integer number long long integer number float floating point number double double precision float

Integers can be signed (default) or unsigned C has also pointer types There is also a special type void

9 10

Variables Variable assignment

Variable data types are static // variable declaration Assign a value to a variable: // examples of assignment int i; count = 10; Declare variables before float f, g; variable = value; i = j = 0; double total = 1.9; using them Both should have the same k = i*j + 1; // valid names – data type // assign at declaration C90 requires that the int i3, myINT, I_o; int i = 4; declarations are before any float o3Gf_ry9; – Implicit conversion between other statements // invalid names compatible types // typecast from int to float int 33, 9a, i-o; int i; Valid variable names in C: float o3.Gf; – Explicit conversion (typecast) float f; . Upper and lower case // data type matters syntax: i = 5 * 21; char c; f = (float) i; letters, underline and float f; (type) var numbers are allowed // watch out for operator order f = 1.234; (float) i/5 != (float) (i/5) c = f; // ERROR! // wrong data type

11 12 Arithmetics Compound assignment

Operators: addition C has a short-hand notation + // example of compound assignment – subtraction for combined arithmetic int count = 10; count += 5; // now count is 15 * multiplication operation and assignment count /= 3; // and now count is 5

/ division Given an operator and // Adding one to count % modulus values a and b, the syntax is count++; a=b and the result is // This is also valid! Operator precedence a=ab ++count; 1) % * / For special cases a+=1 and 2) + - a-=1 there are special Parentheses can be used to group operations () and change operators ++ and -- evaluation order

13 14

Arithmetics and assignment examples Other operators

Logical operators Bit-wise operators // addition, substraction // grouping with ()s i = 5 + 2; b = a * (1.3 + (25%3)); i = 5 – 2; && AND >> shift right i += 1; // i = i + 1 // watch out for precedence! || OR << shift left f -= 3.4; f = 1 + q / (1 - q); > larger than & AND i++; // i = i + 1 f = (1 + q) / (1 - q); < less than | OR i--; >= larger or equal ^ XOR // also function calls use () <= less or equal ~ complement // multiplication, division f = r * sin(theta); i = 5 * 2; f = (r + p)*sin(theta); == equal i = 5 / 2; // integer division != unequal f *= 3.0 / 4.2; ! negation

// modulus m = 25 % 3;

15 16 Basic I/O

printf prints formatted text to the screen Format of printf call is printf(