Plab administration TAs Yoseph Barash Liad Blumrosen Michael

Plab - administration • TA’s: – Yoseph Barash – Liad Blumrosen – Michael Okun • Contact ONLY plab@cs • website: www. cs. huji. ac. il/~plab • newsgroups: moderated unmoderated

Plab - Tirgul 1 • requirements: – ~6 exercises, 2 quizzes • Late submission: – 1, 2, 3 days late - 4, 8, 16 points penalty – 4+ days - grade is 0! • Other comments: – lots of self-learning needed! – Personal assignments – honesty

Plab - Tirgul 1 • The C language: – Types • primitive types • user defined types – Expressions and operators – Basic Compilation • We learn C with c++ compiler

C and Java • Basic Types are similar to JAVA - int, double, char, …. • Basic flow-control is similar if (condition) { } for (start; condition; increment) while (condition) {} … • No Classes, No methods, No Packages…. • No exceptions - programs usually simply crash!

Integral types int , short , long • sizes: machine dependent! – unlike JAVA sizeof(short) sizeof(int) sizeof(long) • For example: (linux on pentiums) short: 16 bit, int: 32 bit, long: 32 bit • All can be signed/unsigned – Default: signed – unsigned short: [0, 216 -1] signed short: [-215, 215 -1]

Integral types - characters char • char is the basic type in C – sizeof(char)=1 by definition – size: usually 1 byte (8 bits) • Can also be signed/unsigned Examples: char c = ‘a’; char c = 65;

Boolean types • Boolean type – Doesn’t exist in C ! • Use int instead – zero = false – non-zero = true • Exists in c++ bool is. Good = true; (1) while (1) { } (infinite loop) (2) if (-1974) { } (true statement) (3) i = ( 3==4); (i equals zero)

Undetermined type sizes • Sizes of basic types are not determined – Machine dependent – Unlike Java • Advantage: – Hardware support for arithmetic operations • Disadvantage: – Problems with porting code from one machine to another

Casting and Type Conversion • Casting possible between all primitive types. • Casting up - usually automatic: – float => double, short => int => long, etc. (1) (2) (3) (4) int i; short s; long l; i=s; // no problem l=i; // no problem s=l; // might lose info, // warning not guaranteed

User Defined Types - struct • In C, we can define new types (in addition to the primitive types - int, char, etc. ) • Types that composite other types are structures – (The origin of classes!) • Example: struct Complex { double real; double imag; }; (1) (2) (3) struct Complex c; c. real = 1; c. imag = 0;

User Defined Type - enum • Enumerated types - a set of named constants. enum Seasons { e_winter, // e_winter = 0 by default e_spring, // e_spring = 1 e_summer, // e_summer = 2 e_autumn // e_autumn= 3 }; (1) enum Seasons curr. Season; (2) Seasons curr. Seasons; // (c++ syntax) (3) curr. Season = e_autumn; (4) int e_summer; // Error, redefinition (5) int prev. Season = e_summer; // legal

User Defined Type - enum • enums – why? – More readable code – Code less error prone – Accessible for debugger – Enables use of the numerical values • Bad programming usually!

typedef • Defines a synonym for the specified type declaration typedef <existing_type_name> <new_type_name> typedef unsigned long size_t; unsigned long num; //identical! size_t num; //identical! typedef struct My. Clock{ int minutes; int hours; } Clock;

? typedef- why • Uses for typedef: – Readibilty • shorter names or dedicated names – Portability typedef int 32 typedef short int 16 – Generic code (not always recommended) e. g. changing numbers in a data structure from char to int easily

Expressions as Values (1) int i=0; (2) if (i=5) { (3) printf(“hellon”); (4) } • • • Will we enter the if statement? We probably meant “i==5” on line 2 Completely legal!

Expressions as Values • && - “and” logic, & - “and” bitwise • || - “or” logic, | - “or” bitwise – You should learn bitwise operator by yourselves (1) int i=0; (2) if ( i==1 && x=is. Valid() ) { (3) …. might not be (4) } evaluated!!!

Basic Compilation • Consider we have 2 files to compile: driver. c, stack. c • g++ driver. c stack. c Creates an executable file called a. out (default name) • g++ driver. c stack. c -o driver Creates an executable file called driver • Running the program: Just write the executable name in the command line.

Basic Compilation errors: • Command: g++ test. File. c (1) g++: test. File. c: No such file or directory problem: wrong name of file, or compiling from the wrong directory (2) test. File. c: In function ‘int main()’ test. File. c : 12: syntax error before ‘; ’ problem: e. g. doubl d; (in line 12) Emacs tip: alt-g takes you to a specified line

Basic Compilation Print all warnings: • g++ -Wall test. File. c example: if (i=3) { //bug, we meant I==3 … } “warning: suggest parentheses around assignment used as truth value” if ( (I==3) ) { //no warning

Learn by yourself • • • unions (user defined type) Binary operators ( e. g. & | ) Variables scope
- Slides: 20