Presentation is loading. Please wait.

Presentation is loading. Please wait.

Plab - administration TA’s:TA’s: –Yoseph Barash –Liad Blumrosen –Michael Okun Contact ONLY ONLY website:

Similar presentations


Presentation on theme: "Plab - administration TA’s:TA’s: –Yoseph Barash –Liad Blumrosen –Michael Okun Contact ONLY ONLY website:"— Presentation transcript:

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

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

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

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

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

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

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

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

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

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

11 User Defined Type - enum Enumerated types - a set of named constants.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 currSeason; (2)Seasons currSeasons; (2)Seasons currSeasons; // (c++ syntax) (3)currSeason = e_autumn; (4) int e_summer; (5)int prevSeason = e_summer; (3)currSeason = e_autumn; (4) int e_summer; // Error, redefinition (5) int prevSeason = e_summer; // legal

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

13 typedef Defines a synonym for the specified type declarationDefines a synonym for the specified type declaration typedef typedef typedef unsigned long size_t; unsigned long num;//identical! size_t num;//identical! typedef struct MyClock{ int minutes; int hours; } Clock;

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

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

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

17 Basic Compilation Consider we have 2 files to compile: driver.c, stack.cConsider 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 Creates an executable file called a.out (default name) g++ driver.c stack.c -o driver Creates an executable file called driverg++ driver.c stack.c -o driver Creates an executable file called driver Running the program: Just write the executable name in the command line.Running the program: Just write the executable name in the command line.

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

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

20 Learn by yourself unions (user defined type)unions (user defined type) Binary operators ( e.g. & | )Binary operators ( e.g. & | ) Variables scopeVariables scope


Download ppt "Plab - administration TA’s:TA’s: –Yoseph Barash –Liad Blumrosen –Michael Okun Contact ONLY ONLY website:"

Similar presentations


Ads by Google