Download presentation
Presentation is loading. Please wait.
1
C Short Overview Lembit Jürimägi
2
Compilation Code written in C doesn’t run directly but needs to be compiled Code itself may be cross-platform but a compiled executable is not Compilers supplied by hardware vendors, operating systems, etc Compiler may be separate or integrated to IDE Compiler workflow: Code preprocessed for macro replacement, conditional compilation, source inclusion (preprocessor) Preprocessed code compiled to object code (compiler) Object code assembled to machine language (assembler) Machine language packaged into an executable (linker)
3
C Standards Classic ANSI C (C89/C90) C99 C11
4
C Syntax Case-sensitive Limited list of reserved words
Sparse notation using single characters Nonessential functionality delegated to libraries Short code but hard to read
5
Reserved words (C89) Datatypes Type related Statements Other
char, double, enum, float, int, long, short, signed, struct, union, unsigned, void Type related auto, const, extern, register, static, typedef, volatile Statements break, case, continue, default, do, else, for, goto, if, return, switch, while Other main, sizeof
6
Variables Typed but support implicit type conversion
Defined as type followed by identifier Identifiers consist of latin letters, numbers and underscore Identifier must start with a lower-case or upper-case letter Variable can be defined anywhere in code By default the scope of the variable is limited to current block If defined outside any block then it’s a global variable (try to avoid) If identifier followed by [ ], then it’s an array
7
Built-in data types Type name Size Value range char, unsigned char
8 bits , , ASCII characters short, unsigned short, short int, unsigned short int, (int) 16 bits -32, ,767, ,535 long int, unsigned long int, (int) 32 bits -2,147,483, ,147,483,647, ,294,967,295 long long int, unsigned long long int, (long int) 64 bits -9,223,372,036,854,775, ,223,372,036,854,775, ,446,744,073,709,551,615 float 1.1 × 10− × 1038 double 2.2 x 10− x 10308 long double 80 bits 3.4 x x void ?
8
Constants Integer constants Floating point constants Characters
12, 0x1F, 055 (decimal, hecadecimal, octal) Stored as signed int unless specified otherwise (12l, 5u, 40ul) Floating point constants 3.2, 14e3 Stored as double unless specified otherwise (1.4f, 2.7l) Characters 'a' Stored as char Strings "a" Stored as character array
9
Examples int a; // define 32 bit signed integer, value undetermined long long unsigned int k = 0xFFFFFFFFFFFFFFFFul; // define 64 bit unsigned integer and set to max value float f = 3.4; // define 32 bit float, assign value 3.4 f = 9 / 5; // f gets value 1, since both operands are integers! double x, y, z; // define 3 variables at once int M[2] = {1, 5}; // define int array, set M[0] to 1, M[1] to 5 char S[] = "Hello"; // define character array big enough to hold string constant "Hello" S[0] = 'J'; // replace first character with 'J'
10
Operators Arithmetic Bitwise Assignment Boolean Comparison
+, -, *, /, %, ++, -- Bitwise ~, &, |, ^, <<, >> Assignment =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>= Boolean !, &&, || Comparison <, <=, >, >=, ==, != Reference and dereference &, *, [ ] Conditional ? :
11
Operator precedence Operator Type () Parentheses ! Logical NOT ++ --
Increment, decrement * / % Multiply, divide, remainder + - Add, subtract << >> Shift (multiply by 2, divide by 2) < > <= >= Less, greater, less-equal, greater-equal == != Equal, not equal & Bitwise AND ^ Bitwise XOR | Bitwise OR && Logical AND || Logical OR ?: Conditional =, .. Assignment
12
Statements Conditional statements Loops
if, else, switch, case, default Loops while, do, for, goto, break, continue
13
If if (expression) statement; if(a < b) x = a;
if (expression) statement; else statement; if(b < c) x = c; else
14
Switch switch(a){ case value: statement; break; default: statement; }
case 1: x = -1; break; case 2: y = 0; default: x = 1; }
15
While while(expression) statement; do statement; while(expression);
while(i < 10) i++; a = 5; while(a > 0){ printf("%d\n", a); a--; } a = -1; do{ } while(a > 0);
16
For for(initialization; expression; statement) statement;
for(i = 0; i < 10; i++) printf("%d\n", i); i = 20; for(; i; printf("%d\n", i--));
17
Functions Function definition is similar to global identifier definition but identifier must be followed by parentheses Parameters are passed by position, one way At most single return value To bypass these limitations pointers are used A program must have main function The return statement is used to return value and / or break program flow
18
Example int sum(int a, int b){ int x = a + b; return x; } void switchIfSmaller(int *a, int *b){ if(*a >= *b) return; int x = *a; *a = *b; *b = x;
19
Libraries stdio.h for input output functionality
printf, scanf, fgets, fopen, feof, fclose FILE datatype stdlib.h for conversion and memory management atoi, atof, malloc, realloc, free, qsort, rand NULL constant string.h for string operations strcmp, strcpy, strlen, memcpy, memset math.h for math cos, sin, tan, acos, asin, atan, exp, log, pow, sqrt, ceil, floor, fabs time.h for date and time functions time struct tm datatype
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.