Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with...
-
Upload
truongthuy -
Category
Documents
-
view
221 -
download
0
Transcript of Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with...
![Page 1: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/1.jpg)
C
#include <stdio.h>int main(){
printf ("hello class\n");return 0;
}
![Page 2: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/2.jpg)
2
Working environment
• Linux, gcc
• We’ll work with c9.io website, which works with
ubuntu
• I recommend to install ubuntu too
• Also in tirgul
![Page 3: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/3.jpg)
History
3
70’s – Development of UNIX. (Richie+Kernigham – Bell labs)
80’s – Large efficient code. (Stroustrup – Bell labs)
90’s – Language for the web. (Sun Microsystems)
Simple to convert to
machine code.VM
Fast and Efficient Secure and Safe
Welcome to C Easy to avoid bugs
Easy to Debug
C++C Java
![Page 4: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/4.jpg)
History – C:
K&R 1st
ed. ('78)ANSI C
('89)C99 ('99)
C11(’11)
This course, without the VLA!
• First "standard"
• Default int
•Enums
•Return struct
•void
• //
• VLA
• variadic
macros
•inlining
•multithreading
•Anonymous
structs
• _Generic
![Page 5: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/5.jpg)
5
C – Design Decisions
“Bare bones” – the language leaves maximal flexibility
with the programmer
Efficient code (operating systems)
Full control on memory & CPU usage
High-level
Type checking
High-level constructs
Portable
Standard language definition
Standard library
![Page 6: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/6.jpg)
C – Warning Signs
6
No run-time checks
Array boundary overruns
Illegal pointers
No memory management
Programmer has to manage memory
![Page 7: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/7.jpg)
First Program in C
7
// This line is a comment,
/* and this line also. */
// This line defines standard I/O library
#include <stdio.h>
// main – program entry point. Start form here
int main()
{ // {…} define a block
printf("Hello class!\n");
return 0;
}
7
![Page 8: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/8.jpg)
Similar to java – you should know it already.
8
The basic syntax
![Page 9: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/9.jpg)
Basic
9
• Case sensitive
• White-space not important.
• End statements with “;”
• Code block defined with “{” and “}”
• Return value from function using “return X;”
• String with “ " ”
• All lines below are legal:
int x,y;
x=5; {
x; } ;
![Page 10: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/10.jpg)
Statements - conditional
10
if (expression)
//statement or block
else if (expression)
//statement or block
else (expression)
//statement or block
switch (integer value)
later...
![Page 11: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/11.jpg)
Statements - loops
11
The usual suspects:
int x,y; //in ANSI C you cannot declare inside
the for!
for (x=0,y=0;x<10 && y<5;x++,x+=2)
//statement or block
while (condition)
//statement or block
do
//statement or block
while (condition)
![Page 12: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/12.jpg)
Statically typed – each variable has a type. Declare by:
<type> <name>.
int x;
int x,y;
Optionally initialize (otherwise undefined!)
int x=0;
Variables
12
![Page 13: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/13.jpg)
int x;
Undefined value: as in this example, can have any T(here T=int) value.
Undefined behavior: Everything may happen. Probably
won’t format your disk, but may very well give a hacker a
way to do it…
The biggest problem: often programs seems to work
fine although their behavior is undefined, and then…
Undefined – important!
13
![Page 14: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/14.jpg)
Variables
14
Where to declare?
1. Inside a block (C89 - block beginning) , will be visible only
in block.
2. Outside all blocks – global - will be visible everywhere.
int x=0; // globalint main()
{ int x=1; //local hides global
{ int x=2; //local hides outer scope //x is 2
} //x is 1 again!
}
![Page 15: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/15.jpg)
Scopes
15
• Code block defined with “{” and “}”.
• Only declarations inside current or outer scopes are visible.
• Declarations in inner scope hide declarations in outer
scopes:
• Outmost scope (global) had no brackets.
• Keep in mind that a function is also a scope.
int y=5,x=0; {
int x=y; //x is 5 {
int y; }
} // x is 0
![Page 16: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/16.jpg)
Second Program
16
#include <stdio.h>int main() {
int i; // declares i as an integer int j = 0; // declares j as an integer,
// and initializes it to 0 // for( initial ; test condition ; update step ) for( i = 0; i < 10; ++i ) {
j += i; // shorthand for j = j + iprintf("%d %d %d\n", i, j, (i*(i+1))/2);
} return 0;
}
16
![Page 17: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/17.jpg)
Running…
17
0 0 0
1 1 1
2 3 3
3 6 6
4 10 10
5 15 15
6 21 21
7 28 28
8 36 36
9 45 45
17
![Page 18: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/18.jpg)
Functions
18
C allows to define functions
Syntax:
int power( int a, int b )
{
…
return 7;
}
Return typeParameter
declaration
Return
statement
18
![Page 19: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/19.jpg)
Procedures
19
Functions that return void
void power( int a, int b )
{
…
return;
}
Return w/o value
(optional)
19
![Page 20: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/20.jpg)
Example – printing powers
20
#include <stdio.h>
int power( int base, int n )
{
int i, p;
p = 1;
for( i = 0; i < n; i++ )
{
p = p * base;
}
return p;
}
int main()
{
int i;
for( i = 0; i < 10; i++ )
{
printf("%d %d %d\n",
i,
power(2,i),
power(-3,i) );
}
return 0;
}20
![Page 21: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/21.jpg)
Functions Declaration
21
“Rule 1”: A function
“knows” only
functions which were
declared above it.
void funcA()
{
...
}
void funcB()
{
funcA();
}
void funcC()
{
funcB();
funcA();
funcB();
}
void funcA()
{
...
}
void funcB()
{
funcC();
}
void funcC()
{
funcB();
}
Error:
funcC is not
known yet.
21
![Page 22: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/22.jpg)
Functions Declaration
void funcC(int param);
// or: void funcC(int);
void funcA()
{
}
void funcB()
{
funcC(7);
}
void funcC(int param)
{
}
Amendment to “Rule 1” : Use forward declarations.
22
![Page 23: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/23.jpg)
Boolean variables – non!
23
int main()
{
int a = 5;
while(1)
{
if(!(a-3))
{
printf("** a=3 **\n");
break;
}
printf("a=%d\n",a--);
}
return 0;
}
![Page 24: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/24.jpg)
24
Building a program in C:
Preprocessor, Compilation and Linkage
![Page 25: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/25.jpg)
Building a program in C – Preprocessor
25
A text processor
Commands start with #
// copy & paste the file here
#include <stdio.h>
hello.c
Preprocessor
stdio.h
tmpXQ.i
(C code)
![Page 26: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/26.jpg)
Building a program in C – Compiling
26
Takes input C-code and produces machine code (object
file)
The object file does not contain all external references: It
leaves names, such as “printf”, “area”, etc. as undefined
references
hello.c
Preprocessor Compiler
stdio.h
tmpXQ.i
(C code)
hello.o
(object
file)
![Page 27: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/27.jpg)
Linking
27
Combines object file with external references into an
fully executable file, with no unresolved references
Main
Preprocessor,
Compiler
Main.c Main.o
Linker
libc.a
![Page 28: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/28.jpg)
Link errors
28
Gcc: /media/sf_BitEagle_Projects/cbitcoin/test/testCBAddress.c:40:undefined reference to`CBNewByteArrayFromString‘
Visual studio:Error 1 error LNK2019: unresolved external symbol _foo referenced in function _main
c:\Users\ofirpele\documents\visual studio 2012\Projects\ConsoleApplication5\ConsoleApplication5\ConsoleApplication5.obj
ConsoleApplication5
![Page 29: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/29.jpg)
29
The Preprocessor
![Page 30: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/30.jpg)
Compilation in C
30
hello.c
Preprocessor Compiler
stdio.h
tmpXQ.i
(C code)
hello.o
(object
file)
![Page 31: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/31.jpg)
Preprocessor
31
A single-pass program that:
1. Include header files
2. Expands macros
3. Control conditional compilation
4. Remove comments
Outputs – a code ready for the
compiler to work on.
![Page 32: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/32.jpg)
Preprocessor
32
In linux with gcc, we can test what
the preprocessor does:
> gcc –E hello.c
will print the C code after running the
preprocessing
![Page 33: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/33.jpg)
#include directive
33
#include "foo.h"
Include the file “foo.h”, from current directory
#include <stdio.h>
Include the file “stdio.h” from the standard
library directory (part of compiler installation)
![Page 34: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/34.jpg)
Modules & Header files
34
Complex.c
// interface of function
int area (int x1,int y1, int x2, int y2);
...
Square.h
#include "Square.h"
#include <math.h>
// implementation
int area (int x1,int y1,int x2, int y2)
{
...
}
Square.c
#include "Square.h"
int main()
{
area (2,3, 5,6);
}
Main.c
![Page 35: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/35.jpg)
Do all files get compiled every time we
build the program???
35
$ gcc –c Square.c –o Square.o
$ gcc –c Main.c –o Main.o
$ gcc Square.o Main.o –o Main
Main
Preprocessor
Compiler
Square.c Square.o
Main.c Main.o
Linker
libc.a
![Page 36: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/36.jpg)
Header files
36
Header file contain
1. Definition of data types
2. Declarations of functions & constants
3. That are shared by multiple modules.
#include directive allows several
modules to share the same set of
definitions/declarations
![Page 37: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/37.jpg)
#define directive
37
#define FOO 1
int x = FOO;
is equivalent to
int x = 1;
![Page 38: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/38.jpg)
#define with arguments
38
#define SQUARE(x) x*x
b = SQUARE (a);
is the same as
b = a*a;
![Page 39: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/39.jpg)
39
#define SQUARE (x) x*x
b = SQUARE (a+1);
c = SQUARE (a++);
Is it what we intended?
b = a+1*a+1;
//Actually: b = 2*a+1;
c = a++*a++;
//Actually: c = a*a; a+=2;
#define – cautions
![Page 40: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/40.jpg)
#define – cautions
40
#define SQUARE (x) ((x)*(x))
b = SQUARE (a+1);
c = SQUARE (a++);
Is it what we intended?
b = ((a+1)*((a+1));
//Now ok
c = ((a++)*(a++));
//Did not help: c = a*a; a+=2;
![Page 41: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/41.jpg)
#define
41
#define directive should be used with
caution!
Alternative to macros:
• Constants
enum { FOO = 1 };
or
const int FOO = 1;
• Functions – inline functions (C99,C++)
![Page 42: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/42.jpg)
#define
42
Multi-line:
All preprocessor directive effect one line (not c statement).
To insert a line-break, use “\”:
BAD:
#define x (5 +5)
// x == 10 !
GOOD:
#define x (5 + \
5)
// x == 10 !
![Page 43: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/43.jpg)
#if directive
43
Allows to have conditional compilation
#if defined(DEBUG)
// compiled only when DEBUG exists
printf("X = %d\n", X);
#endif
![Page 44: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/44.jpg)
#ifndef – for header safety
44
Complex.h:
struct Complex
{
...
MyStuff.h:
#include "Complex.h"
Main.c:
#include "MyStuff.h"
#include "Complex.h"
Error:
Complex.h:1: redefinition
of `struct Complex'
![Page 45: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/45.jpg)
#ifndef – header safety
45
Complex.h (revised):
#ifndef COMPLEX_H
#define COMPLEX_H
struct Complex
{
...
#endif
Main.c:
#include "MyStuff.h"
#include "Complex.h" // no error this time
![Page 46: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/46.jpg)
#pragma once – header safety
46
Complex.h (revised):
#pragma once
struct Complex
{
...
Main.c:
#include "MyStuff.h"
#include "Complex.h" // no error this time
![Page 47: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/47.jpg)
Preprocessor – summary
47
• Text processing program.
• Does not know c rules
• Operates before compilation, output passed to
compiler.
• Can do copy and paste / cut
• #include paste the included file here Commonly
included file (.h by convention) contains forward
declarations
• #define copy the macro body, paste it where macro
name appears - constants, simple "functions"
• #if condition not fulfilled, cut the code - Conditional
compilation – e.g. debugging code
![Page 48: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/48.jpg)
48
Debug/Test mode vs
Release mode
![Page 49: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/49.jpg)
49
Debug/Test mode vs Release mode
#include <assert.h>#define MAX_INTS 100int main() {int ints[MAX_INTS];// i should be in bounds, but is it really?i = foo(<something complicated>); // safety assertionsassert(i>=0); assert(i<MAX_INTS);ints[i] = 0;...
![Page 50: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/50.jpg)
50
#include <assert.h>#define MAX_INTS 100int main() {int ints[MAX_INTS];// i should be in bounds, but is it really?i = foo(<something complicated>); // safety assertionsassert(i>=0); assert(i<MAX_INTS);ints[i] = 0;...
Debug/Test mode vs Release mode
![Page 51: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/51.jpg)
51
#include <assert.h>#define MAX_INTS 100int main() {int ints[MAX_INTS];// i should be in bounds, but is it really?i = foo(<something complicated>); // safety assertionsassert(i>=0); assert(i<MAX_INTS);ints[i] = 0;...
#define NDEBUG
![Page 52: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/52.jpg)
52
assert
// bad, foo() will not be called// if the compiler removes the assert() // if NDEBUG is definedassert(foo() == 0);
// goodint errCode = foo();assert(errCode == 0);
![Page 53: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/53.jpg)
53
Use for:• Catching bugs
Don't use for:• checking malloc, user input,...
assert
![Page 54: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/54.jpg)
assert.h
54
#include <assert.h>
// Sqrt(x) - compute square root of x
// Assumption: x non-negative
double sqrt(double x )
{
assert( x >= 0 ); // aborts if x < 0
…
![Page 55: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/55.jpg)
assert.h
55
// procedure that actually prints error message
void __assert(char* file,int line,char* test);
#ifdef NDEBUG
#define assert(e) ((void)0)
#else
#define assert(e) \
((e) ? (void)0 : \
__assert(__FILE__, __LINE__, #e))
#endif
![Page 56: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/56.jpg)
assert.h
56
• Important coding practice
• Declare implicit assumptions
• Sanity checks in code
• Check for violations during
debugging/testing
![Page 57: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/57.jpg)
57
Compilation
![Page 58: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/58.jpg)
Compilation
58
• Translate the c code to machine code.
• Every machine architecture has a different
code. Need separate compilation.
• x86 : older but still common PCs “32-bit”
(Intel/AMD)
• x64 : newer PCs “64-bit” (Intel/AMD)
• ARM : (many versions) Phones, tablets
(ARM)
• Translated code is an “executable” – the OS
can load it to the machine memory, and the let
the CPU do whatever you wrote in the code.
![Page 59: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/59.jpg)
“Assembly”
59
int y=2; int mult(int x) {
return x*y;
mov eax,dword ptr [x]
imul eax,dword ptr [1177000h]
} int main() {
return mult(5);
push 5
call 11711A4h
}
Only part of the assembly code is shown here.
Actually each command has binary code, and the sequence of these code
is the object file.
![Page 60: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/60.jpg)
Compiling warnings
![Page 61: Working environment - ariel.ac.il · 2 Working environment • Linux, gcc • We’ll work with c9.io website, which works with ubuntu • I recommend to install ubuntu too • Also](https://reader030.fdocuments.in/reader030/viewer/2022040418/5d670fb588c993283a8b8e28/html5/thumbnails/61.jpg)
Compiling warnings
61
Add “-Wall” flag to catch things like this:
if (i=3) { //bug, we meant i==3
…
}