140 likes | 157 Views
This course provides practical training in computer systems using the C/C++ programming language. The course covers topics such as constants, basic types, statements, loops, switch statements, complex types, variables, expressions, pointers, and structures. It also includes examples and literature resources.
E N D
Practical Course in Computer Systems C/C++ language Jakub Yaghob
Literature and slides • Slides • http://www.ksi.mff.cuni.cz/lectures/NSWI173/index.html
Example Pascal C int f(int p) { int a[10]; for(int k=0;k<10;++k) { p += a[k]; } return p; } function f(p:integer):integer; var k:integer; a:array[1..10] of integer; begin for k:=1 to 10 do begin p := p+a[k]; end f := p; end
Constants • Integer numbers • Decimal number • 123, -18 • Hexadecimal number • 0x7a • Floating point number • -1.234e-5 • String • “foo bar” • Char • ‘a’ • Escape sequence • \n – LF • \r – CR • \t – TAB • \\ – \ • \’ – ’ • \” – ” • \xab – char 0xab
Basic types • Integer types • Base • char, int • Modifiers • short, long • signed, unsigned • Auxiliary • size_t, byte, word • Floating point types • float, double • Other types • void, bool • Implicit conversion
Statements Pascal C Block { } Assignment a = b+c; if(a!=b) S1 else S2 gotolbl;lbl: return 5; • Block • begin end • Assignment • a := b+c; • if a<>b then S1 else S2 • goto 3;3:
Statements – loops Pascal C while(a<b) S do S while(a>=b) for(i=1;i<=10;i+=2) S continue; break; while a<b do S repeat S until a<b; for i:=1 to 10 step 2 do S
Statements – switch Pascal C switch(i) {case 0: S0; break;case 1: S1; break;case 2:case 3: S23; break;default: Soth; break;} case i of 0: S0 1: S1 2,3: S23 otherwise: Sothend;
Complex types Pascal C using a=int[10]; typedefint a[10]; struct b {int x; char y;}; typedefstruct {int x; char y;} b; using pi=int*; typedefint *pi; typea=array[1..10] of integer;b=record x:integer; y:charend;pi=^int;
Variables Pascal C inti,j; a arr; double arm[5]; b br; pi ptr; int *ptrm; vari,j : integer;arr : a;arm : array [1..5] of real;br : b;ptr : pi;ptrm : ^integer;
Expression Pascal C Arithmetic +, -, *, /, % ++, -- Comparison <, <=, >, >=, ==, != Bitwise ~, &, |, ^, <<, >> Logical &&, ||, ! Pointers &, * Assignment =, +=, -=, *=, /=, %=, &=, |=, ^= • Arithmetic • +, -, *, div, mod, / • Comparison • <, <=, >, >=, =, <> • Bitwise • not, and, or, xor, shl, shr • Logical • and, or, not • Pointers • @, ^
Remnants Pascal C Comments /* */, // Constants #define C 13 constexprint C = 13; Enumerated type enum e { RED, BLUE, GREEN }; Importing a module #include <system.h> • Comments • { }, // • Constants • const C = 13; • Enumerated type • type e = (RED, BLUE, GREEN); • Importing a module • uses system;
Pointers and structures Pascal C typedef litem *liptr; struct litem { inti; liptr next; }; litem i1,i2; liptr p1=&i1,p2=&i2; int *pi1=&i1.i; i1.i = 10; p2->i = 20; *pi1 = 30; type liptr = ^litem; litem = record i : integer; next : liptr; end; var i1,i2 : litem; p1,p2: liptr; pi1: ^integer; p1:=@ i1; p2:=@i2; i1.i:=10; p2^.i:=20; pi1:=@i1.i; pi1^:=30;
Functions Pascal C int f(inti, int *p, int &r) { *p = i; r = i; return i; } void p() { } function f(i: integer; p: ^integer, var r: integer):integer; begin p^ := i; r :=i; f := i; end; procedure p; begin end;