SlideShare ist ein Scribd-Unternehmen logo
1 von 12
Downloaden Sie, um offline zu lesen
Programming in C
Prof. Gustavo Alonso
Computer Science Department
ETH Zürich
alonso@inf.ethz.ch
http://www.inf.ethz.ch/department/IS/iks/
©Gustavo Alonso, ETH Zürich. Programming in C 2
Programming in C
! A brief history of C
! C as a programming language
! C Programming
"main function
"constants, variables, data types
"operators, control structures
"functions
"data structures
"pointer arithmetic
"structures
"dynamic memory allocation
©Gustavo Alonso, ETH Zürich. Programming in C 3
A brief history of C
! Programming languages are used to
specify, design, and build software
systems.
! Programming languages evolve with the
systems they are used to construct. C is a
good example of how this process takes
place.
! UNIX was developed at around 1969. It
first version was programmed in assembler
and run on a DEC PDP-7.
! The second version was ported to a PDP-11
in 1971 and it was a great success:
"16 KB for the system
"8 KB for user programs
"disk of 521 KB
"limit of 64 KB per file
! While writing a FORTRAN compiler for
UNIX, a new programming language was
developed: B
! B was interpreted (like Java) and,
therefore, slow. To solve the performance
problems of B, a new language was
created: C
"allowed generation of machine code
(compilation)
"declaration of data types
"definition of data structures
! In 1973 UNIX was rewritten in C
something that was never done before
"C is much easier to handle than
assembler but
"first C version of UNIX was 20 to 40
% larger and slower than assembler
version
©Gustavo Alonso, ETH Zürich. Programming in C 4
C as a programming language
! C has been standardized (ANSI C) and
spawned new languages (C++,
Stroustrup, 1986) that improve C
! The basic characteristics of C are:
"small in size
"loose typing (lots of freedom, error
prone)
"structured (extensive use of
functions)
"designed for systems programming
(i.e., low level programming of the
type needed to implement an operating
system)
"C is higher level than assembler but
still close to the hardware and allows
direct manipulation of many system
aspects: pointers, memory allocation,
bitwise manipulation ...
! As we will see when we study assembler, C
is not very far from the assembler language
but it provides higher level language
constructs (functions, data structures)
that facilitate programming without loosing
too much performance
! Being a low level language, C gives a lot of
freedom to the programmer:
"it has the advantage that good
programmers can implement very
efficient programs in a compact
manner
"it has the disadvantage that most of us
are not good programmers and the
freedom C grants is usually translated
in error prone, messy code
©Gustavo Alonso, ETH Zürich. Programming in C 5
This is C
#include <stdio.h>
main(t,_,a)
char *a;
{return!0<t?t<3?main(-79,-13,a+main(-87,1-_,
main(-86, 0, a+1 )+a)):1,t<_?main(t+1, _, a ):3,main ( -94, -27+t, a
)&&t == 2 ?_<13 ?main ( 2, _+1, "%s %d %dn" ):9:16:t<0?t<-72?main(_,
t,"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+
,/+#n+,/#;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l q#'+d'K#!/
+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# ){n
l]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#
n'wk nw' iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c 
;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;
#'rdq#w! nr'/ ') }+}{rl#'{n' ')# }'+}##(!!/")
:t<-50?_==*a ?putchar(a[31]):main(-65,_,a+1):main((*a == '/')+t,_,a
+1 ):0<t?main ( 2, 2 , "%s"):*a=='/'||main(0,main(-61,*a, "!ek;dc 
i@bK'(q)-[w]*%n+r3#l,{}:nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);}
Winner of the international
Obfuscated C Code Contest
http://reality.sgi.com/csp/iocc
©Gustavo Alonso, ETH Zürich. Programming in C 6
The C compilation model
! The Preprocessor accepts source code as
input and
"removes comments
"extends the code according to the
preprocessor directives included in the
source code (lines starting with #)
! The Compiler takes the output of the
preprocessor and produces assembly code
! The Assembler takes the assembly code
and produces machine code (or object
code)
! The Linker takes the object code, joins it
with other pieces of object code and
libraries and produces code that can be
executed
preprocessor
compiler
assembler
linker
source code
libraries
assembly code
object
code
executable code
©Gustavo Alonso, ETH Zürich. Programming in C 7
Structure of a C program
! A C program contains the following
elements:
"Preprocessor Commands
"Type definitions
" Function prototypes -- declarations of
function types and arguments.
"Variables
"Functions
! All programs must contain a single main()
function. All function, including main,
have the following format:
type function_name (parameters) {
local variables
C Statements
}
#include <stdio.h>
#define TIMES 10 /* upper bound */
double myfunction(float);
/* Function prototype - Declaration */
main() {
double wert;
double pi = 3.14;
printf(“Multiply by 10n”);
wert = myfunction(pi);
printf(“%d * %f = %fn”,
TIMES, pi, wert);
}
double myfunction(double zahl){
int i;
double count = 0;
count = TIMES * zahl;
return count;
}
©Gustavo Alonso, ETH Zürich. Programming in C 8
Data types
! C has the following basic data types
! The sizes of the data types are not standardized (depend on the implementation)
! The type void is used to indicate functions that return no value or null pointers
! With #define, one can introduce symbolic constants
#define LIMIT 100
C Type Size (in bytes) Lower bound Upper bound Use
char 1 - - characters
unsigned char 1 0 255 small numbers
short int 2 -32768 +32767 integers
unsigned short int 2 0 65536 positive int
(long) int 4 large int
float 4 real numbers
double 8 large reals
void 0 - - no return value
31
2− 1231
−+
38
102.3 ±
⋅− 38
102.3 ±
⋅+
308
107.1 ±
⋅− 308
107.1 ±
⋅+
©Gustavo Alonso, ETH Zürich. Programming in C 9
Variables and constants
CONSTANTS
! A constant specifies a value that cannot be
modified by the program
! Special constants for use with strings:
n new line
t tabulator
r carriage return
b backspace
" escape double quote
0 end string
! Symbols defined through the preprocessor:
#define ESC '033’ /* ASCII
escape */
VARIABLES
! A variable specifies an area of memory that
contains a value of a given type that can be
modified by the program
short x;
long y;
unsigned a,b;
long double lb;
unsigned short z;
! sizeof() is a function that returns the size
of a given variable in bytes (the size
depends on the type of the variable
! Variables should be initialized before they
are used (e.g., in the declaration)
otherwise the variables contain a random
value
©Gustavo Alonso, ETH Zürich. Programming in C 10
Type conversions (casts)
! In C, the type of a value can change during the run time of a program, this is known as type
conversion or type cast
! The change can be explicit (the programmer does it) …
var_new_type = (new_type) var_old_type
int a;
float x = (float) a;
! or implicit (the compiler takes care of it in order to perform operations among variables of
different types):
"char and short can be converted to int
"float can be converted to double
"in an expression, if an argument is double, all arguments are cast to double
"in an expression, if an argument is long, all arguments are cast to long
"in an expression, if an argument is unsigned, all arguments are cast to unsigned
int a = 3;
float b = 5.0;
float c = a + b; /* a is transformed into double */
©Gustavo Alonso, ETH Zürich. Programming in C 11
Scope
! The scope determines where within a
program a particular entity is known and
can be accessed
! The scope of a variable is the part of a
program where the variable can be
manipulated. The scope can be global or
local
! Global variables are typically declared
before the main function. They can be
accessed from anywhere in the program.
Try to avoid global variables (a matter of
programming style)
! Local variables are valid and visible within
a particular block (a block is a set of C
statements enclosed within brackets {
…}). Once the control flow is outside
the block, the variable no longer exists
! Local variables are created (space in
memory is allocated for them) when the
control flow in the program reaches the
block where they are declared (e.g., a
function) and are destroyed (deallocated
from memory) when the control flow leaves
the block
! The creation and destruction of variables
can be controlled:
"extern: the variable is defined in a
different module
"static: for local variables: makes them
last the entire program, for global
variables: restricts the scope to the
current module
"register: try to use a CPU register for
the variable
"auto: default for local variables
©Gustavo Alonso, ETH Zürich. Programming in C 12
Scope (example 1)
int global_variable;
int main () {
int local_variable;
global_variable = 1;
local_variable = 2;
{
int very_local_variable;
very_local_variable = 3;
}
}
©Gustavo Alonso, ETH Zürich. Programming in C 13
Expressions and priority
! +, -, *, /, % are the basic arithmetic
operators
! Addition
x = 3 + 4;
! Subtraction
x = 10 - 3;
! Multiplikation
x = 3 * 4;
! Division
x = 73 / 8;
/* x=9, if int x */
x = 73.0 / 8.0;
/*x=9.125,if float x */
! Modulo
x = 73 % 8;
/* x=1, the reminder of the
division */
! Multiplication operators (*, /, %) have a
higher priority than the additive operators
(+, -). When evaluating an expression,
operators with a higher priority are
evaluated first:
x = 2 + 3 / 2 + 3;
/* x = 2 + 1 + 3 */
x = (2 + 3) / (2 + 3);
/* x = (5 / 5) = 1 */
x = 4*(1/2); /* x = 0 */
x = 4*1/2; /* x = 2 */
©Gustavo Alonso, ETH Zürich. Programming in C 14
Short-hand operators
! C allows for a short hand notation that introduces side effects. This is done through the
prefix- or postfix operators ++, --
! If ++ or -- are used as prefixes, the variable is modified before it is used in the evaluation
of an expression:
a = 3;
b = ++a + 3; /* b = 4 + 3 = 7 and a = 4 side effect */
! If ++ or -- are used as postfixes, the variable is first used to evaluate the expression and
then modified.
a = 3;
b = a++ + 3; /* b = 3 + 3 = 6 and a = 4 */
! Almost all operators can be combined with =
a += b; /* a = a + b */
a *= b; /* a = a * b */
©Gustavo Alonso, ETH Zürich. Programming in C 15
Bit-Operators
! The Bit-Operators & (AND), ^(Exclusive-OR), and | (Inclusive-OR) manipulate bits
according to standard two valued logic
! With & one can set bits to 0.
! With ^one can reverse the valu of bits (0 becomes 1 and 1 becomes 0)
! With | one can set bits to 1.
Bit1 Bit2 Bit1 & Bit2 Bit1^Bit2 Bit1|Bit2
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 0 1
©Gustavo Alonso, ETH Zürich. Programming in C 16
Shift Operators
! << and >> are used to manipulate the position of the bits in a byte or a word
! With a >> b the bits in the variable a are displaced b positions to the right (the new bits
are filled with 0).
! With a << b the bits in the variable a are displaced b positions to the left (the new bits
are filled with 0).
a a >> 3
a << 3a
1 0 1 1 1 0 1 0
1 1 0 1 0 0 0 0
0 0 0 1 0 1 1 1
1 0 1 1 1 0 1 0
MSB LSB
b
a 2/ =
b
a 2* =
©Gustavo Alonso, ETH Zürich. Programming in C 17
Comparison and logical operators
! The comparison operators return a 1 or a 0 depending on the result of the comparison. The
comparison operators in C are
"< (smaller than)
"> (greater than)
"<= (smaller or equal than)
">= (greater or equal than)
"== (equal than)
"!= (not equal than)
! && and || are the logical AND and OR operators
( a != b) && (c > d)
(a < b) || (c > d)
©Gustavo Alonso, ETH Zürich. Programming in C 18
if statement
! The if – then – else statement can be
used with or without the else. The two
forms are:
if (expression)
statement1
if (expression)
statement1
else
statement2
! In both cases, when the expression is true,
then statement1 is executed. If the
expression is false, then, in the first case,
statement1 is skipped (not executed), and,
in the second case, statement2 after the
else is executed.
true
Statement 1 Statement 2
false
expression
if (a >= 3) a = a - 3;
if (a == 3) a = a * 3;
else a = a * 5;
if (a >= 3) { a = a - 3;
if ( a == 3) a = a * 3;}
else a = a * 5;
©Gustavo Alonso, ETH Zürich. Programming in C 19
switch statement (1)
! The switch statement is used to
conditionally perform statements based on
an integer expression (selector)
switch (selector) {
case int-value1 : statement1; break;
case int-value2 : statement2; break;
case int-value3 : statement3; break;
default: statement4;
}
! The exact behavior of the switch statement
is controlled by the break and default
commands
"break continues execution after the
switch statement
"default is executed if no other match
is found
false
trueint–value 1
= selector
selector
Statement 1
int–value 2
= selector
true
Statement 2
false
int–value 2
= selector
true
Statement 3
false
Statement 4
Switch using break
©Gustavo Alonso, ETH Zürich. Programming in C 20
Switch statement (2)
switch (selector) {
case int-value1 : statement1;
case int-value2 : statement2;
case int-value3 : statement3;
default: statement4;
}
/* fall through */
false
trueint–value 1
= selector
selector
Statement 1
int–value 2
= selector
true
Statement 2
false
int–value 2
= selector
true
Statement 3
false
Statement 4
Switch without break
©Gustavo Alonso, ETH Zürich. Programming in C 21
Switch (example)
char a = ‘A’;
switch (a) {
case ‘A’: x *= x; break;
case ‘B’: x /= x; break;
default: x += 5;
}
! Once the case ‘A’ is found, x *= x is
executed and then we continue after the
switch statement. in all cases, only one
case will be executed (either ‘A’ or ‘B’ or
‘default’)
! Note that a is of type char. It does not
matter, char is treated as an integer using
type conversion
a = ‘A’;
switch (a) {
case ‘A’: x *= x;
case ‘B’: x /= x;
default: x += 5;
}
! Once case ‘A’ is found, x *= x is
executed. However, there is no break. This
means we continue executing statements
while ignoring the cases (the check is not
performed anymore). Thus, the following
statements will also be executed
x /= x;
x += 5;
©Gustavo Alonso, ETH Zürich. Programming in C 22
for statement
! The for statement provides a compact way
to iterate over a range of values.
for (initialization; termination; increment) {
statement
}
! All elements in the for loop are optional:
for (; ; );
! break can be used to interrupt the loop
without waiting for the termination
condition to evaluate to true
! continue can be used to skip execution of
the body of the loop and re-evaluate the
termination condition
statement
termination
initialization
true
increment
false
for (int x =0; x < 100; x = x + 3){
if (x == 27) continue;
else printf(“%d”,x);
}
©Gustavo Alonso, ETH Zürich. Programming in C 23
while statement
! The while statement is used to continually
execute a block of statements while a
condition remains true.
while (expression) {
statement
}
! As before, break and continue can be used
to terminate the loop or to finish an
iteration and go back to the evaluation of
the expression
! for and while are equivalent (can you do
it? write a for loop using the while
statement and vice versa)
expression statement
true
false
main() {
char t;
while((t = getchar()) != ‘!’) {
if (t >= ‘A' && t<= ‘Z')
printf("%cn", (char)(t+’a'-’A'));
else
printf( "%cn", (char)t);
}
}
©Gustavo Alonso, ETH Zürich. Programming in C 24
Do-while statement
! The do-while is similar to the while
statement except that the loop is always
executed once and the condition is checked
at the end of each iteration.
do {
statement
} while (expression)
! break and continue have the same effect as
before
expression
statement
true
false
©Gustavo Alonso, ETH Zürich. Programming in C 25
Odd ends
exit ()
! exit() terminates the execution of the
program passing an integer error code. e.g.
0 -> no error, 1 -> not found, 99 ->
crash
! exit() is a very primitive way to terminate
a program and one that leaves only very
limited chance to deal with failures. More
modern programming languages use
exceptions and exception propagation
mechanisms to indicate the occurrence of
an error without having to terminate the
program
if (!(buf = AllocMem (BufSize);)) {
printf(“kein Speicher vorhanden");
exit(NO_MEM);
}
Goto
! C was written as a language that is one
step above assembly language. This can be
seen, for instance, on the existence of a
goto statement
! Do not use goto when programming. There
are very good reasons to avoid it:
"Style: avoid spaghetti code
"Use of stack frames
©Gustavo Alonso, ETH Zürich. Programming in C 26
Arrays
! An array is a finite set of variables of the
same basic type
! Instead of giving each variable a name, we
use enumeration and group all of them in
an array
! The enumeration of elements within an
array always starts with 0. If the array has
N elements, the last element is in position
N-1
! The C compiler does not check the array
boundaries
"this is a very typical error that it is
very difficult to find (usually happens
inside loops that traverse the array)
"always check the array boundaries
before accessing a variable in an array
#include <stdio.h>
float data[5]; /* data to average and total */
float total; /* the total of the data items */
float average; /* average of the items */
main() {
data[0] = 34.0;
data[1] = 27.0;
data[2] = 45.0;
data[3] = 82.0;
data[4] = 22.0;
total = data[0] + data[1] + data[2] + data[3] +
data[4];
average = total / 5.0;
printf("Total %f Average %fn", total, average);
return (0);
}
©Gustavo Alonso, ETH Zürich. Programming in C 27
Multi-dimensional arrays
int a[3][3]
1 2 3 4 5 6 7 8 9
a[0][0] a[0][1] a[0][2] a[1][0]a[1][1] a[1][2]a[2][0] a[2][1] a[2][2]
int a = 1;
for (i=0; i < 3; i++)
for (j=0; j < 3; j++)
matrix[i][j] = a++;
1 2 3 4 5 6 7 8 9
int a = 1;
for (i=0; i < 3; i++)
for (j=0; j < 3; j++)
matrix[j][i] = a++;
1 4 7 2 5 8 3 6 9
MEMORY
©Gustavo Alonso, ETH Zürich. Programming in C 28
Array traversals (examples)
int array[5][5];
for (int i=0; i < 5; i++)
for (int j=0; j < 5; j++)
array[i][j] = 1;
int array[5][5];
for (int i=0; i < 5; i++)
for (int j=0; j < i; j++)
array[i][j] = 1;
0 1 2 3 4
0
1
2
3
4
i
j
inner loop traversalouterlooptraversal
0 1 2 3 4
0
1
2
3
4
i
j
inner loop traversal
outerlooptraversal
©Gustavo Alonso, ETH Zürich. Programming in C 29
More on arrays
! Arrays can be initialized when they are
defined:
/* a[0] = 3, a[1] = 7, a[2] = 9 */
int a[3] = {3, 7, 9};
/* liste[0]=0.0, …, liste[99]=0.0 */
float list[100] = {};
int a[3][3] = {
{ 1, 2, 3}
{ 4, 5, 6}
{ 7, 8, 9}
};
! Strings are arrays of characters terminated
with the null character 0
char str[6] = {‘h’,’a’,’l’,’l’,’o’,’0’}
char str[6] = "hello";
! For string manipulation, however, use the
string.h library
! In C, arrays are just a syntactic
convenience to make programming easier.
Arrays are, for the compiler, the same as
pointers (the array name is a pointer to
the beginning of the array)
1 2 3 4 5 6 7 8 9
©Gustavo Alonso, ETH Zürich. Programming in C 30
Pointers
! A variable has a name, an address, a type,
and a value:
"the name identifies the variable to the
programmer
"the address specifies where in main
memory the variable is located (i.e.,
the beginning of the memory region
reserved for this variable )
"the type specifies how to interpret the
data stored in main memory and how
long the variable is
"the value is the actual data stored in
the variable after if has been
interpreted according to a given type
! Pointers are language constructs that allow
programmers to directly manipulate the
address of variables
! Pointers are used with the * and &
operators:
int* px; /*px = pointer to an integer*/
int x; /*x is an integer */
px = &x; /* px gets the address of x */
/* or px points to x */
x = *px; /* x gets the contents of */
/* whatever x points to */
©Gustavo Alonso, ETH Zürich. Programming in C 31
Pointers (I)
int nummer = 3;
int* nummer_ptr = NULL;
nummer_ptr
int*
0
nummer
int
3
nummer_ptr = &nummer;
nummer_ptr
int*
nummer
int
3
nummer = 5;
nummer_ptr
int*
nummer
int
5
©Gustavo Alonso, ETH Zürich. Programming in C 32
Pointers (II)
*nummer = 8; /* CAREFUL, 8 is not a pointer but an integer */
int x = *nummer_ptr;
nummer_ptr
int*
nummer
int
7
nummer_ptr
int*
nummer
int
7
*nummer_ptr = 7;
x
int
7
©Gustavo Alonso, ETH Zürich. Programming in C 33
Pointers (III)
int* y_ptr = nummer_ptr;
nummer_ptr
int*
nummer
int
7
x
int
7
y_ptr
int*
*y_ptr = 6;
nummer_ptr
int*
nummer
int
6
x
int
7
y_ptr
int*
©Gustavo Alonso, ETH Zürich. Programming in C 34
Pointers (IV)
y_ptr = &x;
nummer_ptr
int*
nummer
int
6
x
int
7
y_ptr
int*
©Gustavo Alonso, ETH Zürich. Programming in C 35
Pointers (V)
int* *p_ptr_ptr;
p_ptr_ptr = &nummer_ptr;
nummer_ptr
int*
nummer
int
6
x
int
7
y_ptr
int*
p_ptr_ptr
int**
©Gustavo Alonso, ETH Zürich. Programming in C 36
Pointers (VI)
*(*p_ptr_ptr) = 5;
nummer_ptr
int*
nummer
int
5
x
int
7
y_ptr
int*
p_ptr_ptr
int**
©Gustavo Alonso, ETH Zürich. Programming in C 37
Pointers, arrays and strings
! An array is in reality a pointer:
int a[10], y;
int* px;
px = a; /* px points to a[0] */
px++; /* px points to a[1] */
px=&a[4]; /*px points to a[4] */
y = *(px+3) /*y gets the value*/
/* in a[3] */
! The pointer arithmetic in C guarantees that
if a pointer is incremented or decremented,
the pointer will vary according to its type.
For instance, if px points to an array,
px++ will always yield the next element
independently of what is the type stored in
the array
! Strings can be manipulated through
pointers:
char* message;
message = “This is a string”;
! message is a pointer that now points to the
first character in the string “This is a
string”
! Again, use the string.h for string
manipulation rather than doing it directly
(you will avoid many errors)
©Gustavo Alonso, ETH Zürich. Programming in C 38
Troubles with pointers
What is printed by the following code?
#include <stdio.h>
void f(int *aa, int *bb) {
*bb = 8;
aa[1] = bb[2];
aa = bb;
}
main() {
int a[5] = { 1, 2, 3, 4, 5 }, *b;
b = a + 2;
f(a,b);
printf("%d %d %d %d %dn",
a[0], a[1], a[2], a[3], a[4]);
}
What is printed by the following code?
#include <stdio.h>
void g(int *aa, int *bb) {
bb[2] = aa[-2];
*aa++ = 17;
*++aa = 10;
}
main() {
int blap[7] = { 1, 2, 3, 4, 5, 6, 7 };
int *c = blap + 3;
g(c,blap);
printf("%d %d %d %d %d %d %dn",
blap[0], blap[1], blap[2], blap[3],
blap[4], blap[5], blap[6]);
}
©Gustavo Alonso, ETH Zürich. Programming in C 39
Structures
! Structures allow programmers to define
complex data types. A structure is a new
(user defined) data type:
struct Id_card {
char name[100]; /* Name */
char adresse[100]; /*Address */
short int geburtsjahr; /*Geburtsjahr*/
int telefon; /* Telefonnummer */
short int semester; /* Semester */
} ethz, uniz;
struct Id_card erasmus;
! Structures of the same type can be copied
with the operator = but they should not
be compared with ==
! Access to the elements of a structure is as
follows:
ethz.name = “Gustavo”;
ethz.telefon = 1234567;
! Pointers can also refer to structures, in
which case elements are accessed through
the ->, or * operators:
struct Id_card *pid;
pid = &ethz_student;
pid->name = “Gustavo”;
pid->telefon = 1234567;
(*pid).name = “Gustavo”;
(*pid).telefon = 1234567;
! In ANSI C, structures can be passed as
arguments (by value or by reference) and
can also be the return type of a function
(this is not true in earlier versions of C)
©Gustavo Alonso, ETH Zürich. Programming in C 40
Example structures
int main () {
struct Typ_kiste {
char inhalt[50]; /* was ist in der Kiste */
int anzahl; /* wieviel davon */
float preis; /* was kostet eine Einheit */
};
float wert;
const int MAX_KISTEN = 10;
struct Typ_kiste liste_kisten[MAX_KISTEN];
/* Initialisierung … */
/* Gesammter Wert */
for (int i = 0; i < MAX_KISTEN; i++)
wert += liste_kisten[i].anzahl * liste_kisten[i].preis;
…
}
©Gustavo Alonso, ETH Zürich. Programming in C 41
struct
...
inhalt inhalt inhalt
anzahl anzahl anzahl
preis preis preis
liste_kisten[0] liste_kisten[1] liste_kisten[2]
liste_kisten[0].inhalt liste_kisten[1].inhalt liste_kisten[2].inhalt
liste_kisten[0].anzahl liste_kisten[1].anzahl liste_kisten[2].anzahl
liste_kisten[0].preis liste_kisten[1].preis liste_kisten[2].preis
char[50] char[50] char[50]
int intint
float float float
©Gustavo Alonso, ETH Zürich. Programming in C 42
Functions
! C is a modular language where the main
unit of composition is the function
! A function has the following elements:
"a return type: specifies the type of the
value returned by the function when it
terminates
"a function name: identifies the
function for the programmer
"arguments of defined types:
parameters to pass to the function,
which can be
• by value: the function gets the a
copy of the value of the
parameters but cannot modify the
actual parameters
• by reference: the function gets the
address (reference) of the
parameters and can modify them
! General syntax:
returntype function_name(def of parameters) {
localvariables
functioncode
}
! An example:
float findaverage(float a, float b) {
float average;
average=(a+b)/2;
return(average);
}
! In ANSI C functions must be declared as prototypes
before they are defined:
float findaverage(float a, float b)
©Gustavo Alonso, ETH Zürich. Programming in C 43
Examples
/* SWAP.C exchange values */
#include <stdio.h>
void swap(float *x, float *y); /* prototype */
main() {
float x, y;
printf("Please input 1st value: ");
scanf("%f", &x);
printf("Please input 2nd value: ");
scanf("%f", &y);
printf("Values BEFORE 'swap' %f, %fn", x, y);
swap(&x, &y); /* address of x, y */
printf("Values AFTER 'swap' %f, %fn", x, y);
return 0;
}
/* exchange values within function */
void swap(float *x, float *y) {
float t;
t = *x; /* *x is value pointed to by x */
*x = *y;
*y = t;
printf("Values WITHIN 'swap' %f, %fn", *x, *y);
}
* FACTORIAL *
* fact(n) = n*(n-1)*....2*1 *
#include <stdio.h>
fact(n) {
int n;
if (n == 0) return(1);
return(n * fact(n-1));
}
main() {
int n, m;
printf("Enter a number: ");
scanf("%d", &n);
m = fact(n);
printf(”Factorial of %d is %d.n", n, m);
exit(0);
}
©Gustavo Alonso, ETH Zürich. Programming in C 44
main() is also a function
/* program to print arguments from command line */
#include <stdio.h>
main(int argc, char **argv) {
int i;
printf("argc = %dnn",argc);
for (i=0;i<argc;++i)
printf("argv[%d]: %sn",i, argv[i]);
}
! argc stands for argument count and it
contains how many arguments have been
passed in the command line when the
program is invoked
! argv is the argument vector (array) and it
contains all the arguments passed through
the command line
! argc is always at least 1 since argv[0] is
the name of the program
* append one file to the another */
#include <stdio.h>
main(int argc, char **argv) {
int c;
FILE *from, *to;
if (argc != 3) { /* Check the arguments. */
fprintf(stderr, "Usage: %s from-file to-filen", *argv);
exit(1);
}
if ((from = fopen(argv[1], "r")) == NULL) {
perror(argv[1]); /* Open the from-file */
exit(1);
}
if ((to = fopen(argv[2], "a")) == NULL) {
perror(argv[2]); /* Open the to-file */
exit(1);
}
/* Read one file and append to the other until EOF */
while ((c = getc(from)) != EOF)
putc(c, to);
/*close the files */
fclose(from);
fclose(to);
exit(0);
}
©Gustavo Alonso, ETH Zürich. Programming in C 45
Dynamic memory allocation
! The definition of types and variables help
the compiler to understand the program we
have written
! The declaration of variables leads to the
allocation of memory for those variables.
In general, this happens automatically and
without intervention of the programmer
! C allows the programmer to allocate and
deallocate memory dynamically
! The functions used for memory allocation
are in stdlib.h
! Typical function calls are
"malloc
"free
typedef struct node {
int x,z;
struct node *next;
} NODE;
NODE *nptr;
if ((nptr =((NODE)*) malloc(sizeof(NODE)))
== NULL) {
printf("No memory - bye bye"); exit(99);
}
! malloc returns a pointer to the allocated
memory. The pointer is generic (void *)
and it is a good practice to cast the pointer
to the appropriate pointer type to avoid
errors.
! Allocated memory must be returned to the
system:
free(nptr);
©Gustavo Alonso, ETH Zürich. Programming in C 46
Example dynamic array
/* This program simply reads integers into a dynamic
array until eof. The array is expanded as needed */
#include <stdio.h>
#include <stdlib.h>
#define INIT_SIZE 8 /* Initial array size. */
main() {
int num; /* Number of integers */
int *arr; /* Array of integers. */
int arrsize; /* The size of the array of integers. */
int m; /* Index. */
int in; /* Input number. */
/* Allocate the initial space. */
arrsize = INIT_SIZE;
arr = (int*) malloc(arrsize*sizeof(int));
/* Read in the numbers. */
num = 0;
while(scanf("%d", &in) == 1) {
/* See if there's room. */
if(num >= arrsize) {
/* There's not. Get more. */
arrsize *= 2;
arr = (int*) realloc(arr, arrsize*sizeof(int));
if(arr == NULL){
fprintf(stderr, "Allocation failed %d.n");
exit(18);
}
}
/* Store the number. */
arr[num++] = in;
}
/* Print out the numbers. */
for(m = 0; m < num; ++m)
printf("%dn", arr[m]);
}
©Gustavo Alonso, ETH Zürich. Programming in C 47
Example string library
#include <stdio.h>
#include <string.h>
void main() {
char name1[12], name2[12], mixed[25];
char title[20];
strcpy(name1, "Rosalinda");
strcpy(name2, "Zeke");
strcpy(title, "This is the title.");
printf(" %snn", title);
printf("Name 1 is %sn", name1);
printf("Name 2 is %sn", name2);
if(strcmp(name1, name2) > 0)
/* returns 1 if name1 > name2 */
strcpy(mixed, name1);
else
strcpy(mixed, name2);
printf("The biggest name alphabetically is %sn", mixed);
strcpy(mixed, name1);
strcat(mixed, " ");
strcat(mixed, name2);
printf("Both names are %sn", mixed);
}
This is the title.
Name1 is Rosalinda
Name2 is Zeke
The biggest name alphabetically is Zeke
Both names are Rosalinda Zeke
©Gustavo Alonso, ETH Zürich. Programming in C 48
References
Some material for these foils and some of the examples have been taken from the following on-
line books on C (there are many more):
! C Programming, Steve Holmes: http://www.strath.ac.uk/IT/Docs/Ccourse/
! C language tutorial: http://www.graylab.ac.uk/doc/tutorials/C/index.htm
! Programming in C , A. D. Marshall: http://www.cs.cf.ac.uk/Dave/C/CE.html
For how C was developed, read the tutorial written by Brian W. Kernighan in 1974:
! Programming in C: A Tutorial, B. W. Kernighan:
http://www.lysator.liu.se/c/bwk-tutor.html

Weitere ähnliche Inhalte

Was ist angesagt?

Programming in C Basics
Programming in C BasicsProgramming in C Basics
Programming in C BasicsBharat Kalia
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C ProgrammingAniket Patne
 
Brief introduction to the c programming language
Brief introduction to the c programming languageBrief introduction to the c programming language
Brief introduction to the c programming languageKumar Gaurav
 
OpenGurukul : Language : C Programming
OpenGurukul : Language : C ProgrammingOpenGurukul : Language : C Programming
OpenGurukul : Language : C ProgrammingOpen Gurukul
 
Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Functionimtiazalijoono
 
Advanced C Language for Engineering
Advanced C Language for EngineeringAdvanced C Language for Engineering
Advanced C Language for EngineeringVincenzo De Florio
 
What is token c programming
What is token c programmingWhat is token c programming
What is token c programmingRumman Ansari
 
User_Defined_Functions_ppt_slideshare.
User_Defined_Functions_ppt_slideshare.User_Defined_Functions_ppt_slideshare.
User_Defined_Functions_ppt_slideshare.NabeelaNousheen
 
Introduction to c
Introduction to cIntroduction to c
Introduction to camol_chavan
 
C programming basics
C  programming basicsC  programming basics
C programming basicsargusacademy
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programmingRokonuzzaman Rony
 
Basic c programming and explanation PPT1
Basic c programming and explanation PPT1Basic c programming and explanation PPT1
Basic c programming and explanation PPT1Rumman Ansari
 

Was ist angesagt? (20)

Programming in C Basics
Programming in C BasicsProgramming in C Basics
Programming in C Basics
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
Brief introduction to the c programming language
Brief introduction to the c programming languageBrief introduction to the c programming language
Brief introduction to the c programming language
 
OpenGurukul : Language : C Programming
OpenGurukul : Language : C ProgrammingOpenGurukul : Language : C Programming
OpenGurukul : Language : C Programming
 
Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Function
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
C Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.comC Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.com
 
Advanced C Language for Engineering
Advanced C Language for EngineeringAdvanced C Language for Engineering
Advanced C Language for Engineering
 
Learning the C Language
Learning the C LanguageLearning the C Language
Learning the C Language
 
What is token c programming
What is token c programmingWhat is token c programming
What is token c programming
 
User_Defined_Functions_ppt_slideshare.
User_Defined_Functions_ppt_slideshare.User_Defined_Functions_ppt_slideshare.
User_Defined_Functions_ppt_slideshare.
 
C basics
C   basicsC   basics
C basics
 
Introduction to c
Introduction to cIntroduction to c
Introduction to c
 
C language introduction
C language introduction C language introduction
C language introduction
 
Intro to C++ - language
Intro to C++ - languageIntro to C++ - language
Intro to C++ - language
 
C language
C languageC language
C language
 
C programming basics
C  programming basicsC  programming basics
C programming basics
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
Basic c programming and explanation PPT1
Basic c programming and explanation PPT1Basic c programming and explanation PPT1
Basic c programming and explanation PPT1
 

Andere mochten auch

Tutorial basic of c ++lesson 1 eng ver
Tutorial basic of c ++lesson 1 eng verTutorial basic of c ++lesson 1 eng ver
Tutorial basic of c ++lesson 1 eng verQrembiezs Intruder
 
Tutorial basic of c++ lesson 1 ind ver
Tutorial basic of c++ lesson 1 ind verTutorial basic of c++ lesson 1 ind ver
Tutorial basic of c++ lesson 1 ind verQrembiezs Intruder
 
Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++Deepak Singh
 
+2 Computer Science - Volume II Notes
+2 Computer Science - Volume II Notes+2 Computer Science - Volume II Notes
+2 Computer Science - Volume II NotesAndrew Raj
 
Intro. to prog. c++
Intro. to prog. c++Intro. to prog. c++
Intro. to prog. c++KurdGul
 

Andere mochten auch (12)

Tutorial basic of c ++lesson 1 eng ver
Tutorial basic of c ++lesson 1 eng verTutorial basic of c ++lesson 1 eng ver
Tutorial basic of c ++lesson 1 eng ver
 
Tutorial basic of c++ lesson 1 ind ver
Tutorial basic of c++ lesson 1 ind verTutorial basic of c++ lesson 1 ind ver
Tutorial basic of c++ lesson 1 ind ver
 
C tutorials
C tutorialsC tutorials
C tutorials
 
Ace tutorial c
Ace tutorial cAce tutorial c
Ace tutorial c
 
C vs c++
C vs c++C vs c++
C vs c++
 
C vs c++
C vs c++C vs c++
C vs c++
 
Differences between c and c++
Differences between c and c++Differences between c and c++
Differences between c and c++
 
Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++
 
+2 Computer Science - Volume II Notes
+2 Computer Science - Volume II Notes+2 Computer Science - Volume II Notes
+2 Computer Science - Volume II Notes
 
Intro. to prog. c++
Intro. to prog. c++Intro. to prog. c++
Intro. to prog. c++
 
C tutorial
C tutorialC tutorial
C tutorial
 
Ppt of c++ vs c#
Ppt of c++ vs c#Ppt of c++ vs c#
Ppt of c++ vs c#
 

Ähnlich wie C Tutorials

C prog ppt
C prog pptC prog ppt
C prog pptxinoe
 
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptxIIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptxrajkumar490591
 
Reduce course notes class xii
Reduce course notes class xiiReduce course notes class xii
Reduce course notes class xiiSyed Zaid Irshad
 
1. introduction to computer
1. introduction to computer1. introduction to computer
1. introduction to computerShankar Gangaju
 
Introduction to C Unit 1
Introduction to C Unit 1Introduction to C Unit 1
Introduction to C Unit 1SURBHI SAROHA
 
The C++ Programming Language
The C++ Programming LanguageThe C++ Programming Language
The C++ Programming LanguageProf Ansari
 
C programming course material
C programming course materialC programming course material
C programming course materialRanjitha Murthy
 
C notes diploma-ee-3rd-sem
C notes diploma-ee-3rd-semC notes diploma-ee-3rd-sem
C notes diploma-ee-3rd-semKavita Dagar
 
Unit 1 c - all topics
Unit 1   c - all topicsUnit 1   c - all topics
Unit 1 c - all topicsveningstonk
 
C LANGUAGE UNIT-1 PREPARED BY MVB REDDY
C LANGUAGE UNIT-1 PREPARED BY MVB REDDYC LANGUAGE UNIT-1 PREPARED BY MVB REDDY
C LANGUAGE UNIT-1 PREPARED BY MVB REDDYRajeshkumar Reddy
 
Learn c language Important topics ( Easy & Logical, & smart way of learning)
Learn c language Important topics ( Easy & Logical, & smart way of learning)Learn c language Important topics ( Easy & Logical, & smart way of learning)
Learn c language Important topics ( Easy & Logical, & smart way of learning)Rohit Singh
 
Overview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya JyothiOverview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya JyothiSowmya Jyothi
 

Ähnlich wie C Tutorials (20)

C prog ppt
C prog pptC prog ppt
C prog ppt
 
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptxIIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
 
C programming day#1
C programming day#1C programming day#1
C programming day#1
 
Reduce course notes class xii
Reduce course notes class xiiReduce course notes class xii
Reduce course notes class xii
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
 
1. introduction to computer
1. introduction to computer1. introduction to computer
1. introduction to computer
 
Basic c
Basic cBasic c
Basic c
 
Introduction to C Unit 1
Introduction to C Unit 1Introduction to C Unit 1
Introduction to C Unit 1
 
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
 
The C++ Programming Language
The C++ Programming LanguageThe C++ Programming Language
The C++ Programming Language
 
Introduction Of C++
Introduction Of C++Introduction Of C++
Introduction Of C++
 
c.ppt
c.pptc.ppt
c.ppt
 
C programming course material
C programming course materialC programming course material
C programming course material
 
C notes diploma-ee-3rd-sem
C notes diploma-ee-3rd-semC notes diploma-ee-3rd-sem
C notes diploma-ee-3rd-sem
 
Unit 1 c - all topics
Unit 1   c - all topicsUnit 1   c - all topics
Unit 1 c - all topics
 
C LANGUAGE UNIT-1 PREPARED BY MVB REDDY
C LANGUAGE UNIT-1 PREPARED BY MVB REDDYC LANGUAGE UNIT-1 PREPARED BY MVB REDDY
C LANGUAGE UNIT-1 PREPARED BY MVB REDDY
 
C language tutorial
C language tutorialC language tutorial
C language tutorial
 
Learn c language Important topics ( Easy & Logical, & smart way of learning)
Learn c language Important topics ( Easy & Logical, & smart way of learning)Learn c language Important topics ( Easy & Logical, & smart way of learning)
Learn c language Important topics ( Easy & Logical, & smart way of learning)
 
Overview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya JyothiOverview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya Jyothi
 
Bcsl 031 solve assignment
Bcsl 031 solve assignmentBcsl 031 solve assignment
Bcsl 031 solve assignment
 

Mehr von Sudharsan S (20)

Xml1111
Xml1111Xml1111
Xml1111
 
Xml11
Xml11Xml11
Xml11
 
Xml plymouth
Xml plymouthXml plymouth
Xml plymouth
 
Xml Presentation-3
Xml Presentation-3Xml Presentation-3
Xml Presentation-3
 
Xml Presentation-1
Xml Presentation-1Xml Presentation-1
Xml Presentation-1
 
XML Presentation-2
XML Presentation-2XML Presentation-2
XML Presentation-2
 
Xml
XmlXml
Xml
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
 
Unix
UnixUnix
Unix
 
Introduction to Unix
Introduction to UnixIntroduction to Unix
Introduction to Unix
 
Unix
UnixUnix
Unix
 
C Lecture
C LectureC Lecture
C Lecture
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
 
C Introduction
C IntroductionC Introduction
C Introduction
 
College1
College1College1
College1
 
C Programming
C ProgrammingC Programming
C Programming
 
Preface
PrefacePreface
Preface
 
Toc Sg
Toc SgToc Sg
Toc Sg
 
Les08
Les08Les08
Les08
 
Les06
Les06Les06
Les06
 

Kürzlich hochgeladen

CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...Nguyen Thanh Tu Collection
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
DiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfDiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfChristalin Nelson
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...Nguyen Thanh Tu Collection
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptxDhatriParmar
 
The Emergence of Legislative Behavior in the Colombian Congress
The Emergence of Legislative Behavior in the Colombian CongressThe Emergence of Legislative Behavior in the Colombian Congress
The Emergence of Legislative Behavior in the Colombian CongressMaria Paula Aroca
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
DBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdfDBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdfChristalin Nelson
 
Unit :1 Basics of Professional Intelligence
Unit :1 Basics of Professional IntelligenceUnit :1 Basics of Professional Intelligence
Unit :1 Basics of Professional IntelligenceDr Vijay Vishwakarma
 
An Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERPAn Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERPCeline George
 
BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 8 - CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC ...
BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 8 - CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC ...BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 8 - CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC ...
BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 8 - CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC ...Nguyen Thanh Tu Collection
 
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFE
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFEPART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFE
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFEMISSRITIMABIOLOGYEXP
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...Nguyen Thanh Tu Collection
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfChristalin Nelson
 
The role of Geography in climate education: science and active citizenship
The role of Geography in climate education: science and active citizenshipThe role of Geography in climate education: science and active citizenship
The role of Geography in climate education: science and active citizenshipKarl Donert
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesVijayaLaxmi84
 

Kürzlich hochgeladen (20)

CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
Introduction to Research ,Need for research, Need for design of Experiments, ...
Introduction to Research ,Need for research, Need for design of Experiments, ...Introduction to Research ,Need for research, Need for design of Experiments, ...
Introduction to Research ,Need for research, Need for design of Experiments, ...
 
DiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfDiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdf
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
 
The Emergence of Legislative Behavior in the Colombian Congress
The Emergence of Legislative Behavior in the Colombian CongressThe Emergence of Legislative Behavior in the Colombian Congress
The Emergence of Legislative Behavior in the Colombian Congress
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
DBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdfDBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdf
 
Unit :1 Basics of Professional Intelligence
Unit :1 Basics of Professional IntelligenceUnit :1 Basics of Professional Intelligence
Unit :1 Basics of Professional Intelligence
 
CARNAVAL COM MAGIA E EUFORIA _
CARNAVAL COM MAGIA E EUFORIA            _CARNAVAL COM MAGIA E EUFORIA            _
CARNAVAL COM MAGIA E EUFORIA _
 
An Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERPAn Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERP
 
BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 8 - CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC ...
BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 8 - CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC ...BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 8 - CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC ...
BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 8 - CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC ...
 
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFE
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFEPART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFE
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFE
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
 
Spearman's correlation,Formula,Advantages,
Spearman's correlation,Formula,Advantages,Spearman's correlation,Formula,Advantages,
Spearman's correlation,Formula,Advantages,
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdf
 
The role of Geography in climate education: science and active citizenship
The role of Geography in climate education: science and active citizenshipThe role of Geography in climate education: science and active citizenship
The role of Geography in climate education: science and active citizenship
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their uses
 

C Tutorials

  • 1. Programming in C Prof. Gustavo Alonso Computer Science Department ETH Zürich alonso@inf.ethz.ch http://www.inf.ethz.ch/department/IS/iks/ ©Gustavo Alonso, ETH Zürich. Programming in C 2 Programming in C ! A brief history of C ! C as a programming language ! C Programming "main function "constants, variables, data types "operators, control structures "functions "data structures "pointer arithmetic "structures "dynamic memory allocation ©Gustavo Alonso, ETH Zürich. Programming in C 3 A brief history of C ! Programming languages are used to specify, design, and build software systems. ! Programming languages evolve with the systems they are used to construct. C is a good example of how this process takes place. ! UNIX was developed at around 1969. It first version was programmed in assembler and run on a DEC PDP-7. ! The second version was ported to a PDP-11 in 1971 and it was a great success: "16 KB for the system "8 KB for user programs "disk of 521 KB "limit of 64 KB per file ! While writing a FORTRAN compiler for UNIX, a new programming language was developed: B ! B was interpreted (like Java) and, therefore, slow. To solve the performance problems of B, a new language was created: C "allowed generation of machine code (compilation) "declaration of data types "definition of data structures ! In 1973 UNIX was rewritten in C something that was never done before "C is much easier to handle than assembler but "first C version of UNIX was 20 to 40 % larger and slower than assembler version ©Gustavo Alonso, ETH Zürich. Programming in C 4 C as a programming language ! C has been standardized (ANSI C) and spawned new languages (C++, Stroustrup, 1986) that improve C ! The basic characteristics of C are: "small in size "loose typing (lots of freedom, error prone) "structured (extensive use of functions) "designed for systems programming (i.e., low level programming of the type needed to implement an operating system) "C is higher level than assembler but still close to the hardware and allows direct manipulation of many system aspects: pointers, memory allocation, bitwise manipulation ... ! As we will see when we study assembler, C is not very far from the assembler language but it provides higher level language constructs (functions, data structures) that facilitate programming without loosing too much performance ! Being a low level language, C gives a lot of freedom to the programmer: "it has the advantage that good programmers can implement very efficient programs in a compact manner "it has the disadvantage that most of us are not good programmers and the freedom C grants is usually translated in error prone, messy code
  • 2. ©Gustavo Alonso, ETH Zürich. Programming in C 5 This is C #include <stdio.h> main(t,_,a) char *a; {return!0<t?t<3?main(-79,-13,a+main(-87,1-_, main(-86, 0, a+1 )+a)):1,t<_?main(t+1, _, a ):3,main ( -94, -27+t, a )&&t == 2 ?_<13 ?main ( 2, _+1, "%s %d %dn" ):9:16:t<0?t<-72?main(_, t,"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+ ,/+#n+,/#;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l q#'+d'K#!/ +k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# ){n l]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q# n'wk nw' iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c ;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+; #'rdq#w! nr'/ ') }+}{rl#'{n' ')# }'+}##(!!/") :t<-50?_==*a ?putchar(a[31]):main(-65,_,a+1):main((*a == '/')+t,_,a +1 ):0<t?main ( 2, 2 , "%s"):*a=='/'||main(0,main(-61,*a, "!ek;dc i@bK'(q)-[w]*%n+r3#l,{}:nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);} Winner of the international Obfuscated C Code Contest http://reality.sgi.com/csp/iocc ©Gustavo Alonso, ETH Zürich. Programming in C 6 The C compilation model ! The Preprocessor accepts source code as input and "removes comments "extends the code according to the preprocessor directives included in the source code (lines starting with #) ! The Compiler takes the output of the preprocessor and produces assembly code ! The Assembler takes the assembly code and produces machine code (or object code) ! The Linker takes the object code, joins it with other pieces of object code and libraries and produces code that can be executed preprocessor compiler assembler linker source code libraries assembly code object code executable code ©Gustavo Alonso, ETH Zürich. Programming in C 7 Structure of a C program ! A C program contains the following elements: "Preprocessor Commands "Type definitions " Function prototypes -- declarations of function types and arguments. "Variables "Functions ! All programs must contain a single main() function. All function, including main, have the following format: type function_name (parameters) { local variables C Statements } #include <stdio.h> #define TIMES 10 /* upper bound */ double myfunction(float); /* Function prototype - Declaration */ main() { double wert; double pi = 3.14; printf(“Multiply by 10n”); wert = myfunction(pi); printf(“%d * %f = %fn”, TIMES, pi, wert); } double myfunction(double zahl){ int i; double count = 0; count = TIMES * zahl; return count; } ©Gustavo Alonso, ETH Zürich. Programming in C 8 Data types ! C has the following basic data types ! The sizes of the data types are not standardized (depend on the implementation) ! The type void is used to indicate functions that return no value or null pointers ! With #define, one can introduce symbolic constants #define LIMIT 100 C Type Size (in bytes) Lower bound Upper bound Use char 1 - - characters unsigned char 1 0 255 small numbers short int 2 -32768 +32767 integers unsigned short int 2 0 65536 positive int (long) int 4 large int float 4 real numbers double 8 large reals void 0 - - no return value 31 2− 1231 −+ 38 102.3 ± ⋅− 38 102.3 ± ⋅+ 308 107.1 ± ⋅− 308 107.1 ± ⋅+
  • 3. ©Gustavo Alonso, ETH Zürich. Programming in C 9 Variables and constants CONSTANTS ! A constant specifies a value that cannot be modified by the program ! Special constants for use with strings: n new line t tabulator r carriage return b backspace " escape double quote 0 end string ! Symbols defined through the preprocessor: #define ESC '033’ /* ASCII escape */ VARIABLES ! A variable specifies an area of memory that contains a value of a given type that can be modified by the program short x; long y; unsigned a,b; long double lb; unsigned short z; ! sizeof() is a function that returns the size of a given variable in bytes (the size depends on the type of the variable ! Variables should be initialized before they are used (e.g., in the declaration) otherwise the variables contain a random value ©Gustavo Alonso, ETH Zürich. Programming in C 10 Type conversions (casts) ! In C, the type of a value can change during the run time of a program, this is known as type conversion or type cast ! The change can be explicit (the programmer does it) … var_new_type = (new_type) var_old_type int a; float x = (float) a; ! or implicit (the compiler takes care of it in order to perform operations among variables of different types): "char and short can be converted to int "float can be converted to double "in an expression, if an argument is double, all arguments are cast to double "in an expression, if an argument is long, all arguments are cast to long "in an expression, if an argument is unsigned, all arguments are cast to unsigned int a = 3; float b = 5.0; float c = a + b; /* a is transformed into double */ ©Gustavo Alonso, ETH Zürich. Programming in C 11 Scope ! The scope determines where within a program a particular entity is known and can be accessed ! The scope of a variable is the part of a program where the variable can be manipulated. The scope can be global or local ! Global variables are typically declared before the main function. They can be accessed from anywhere in the program. Try to avoid global variables (a matter of programming style) ! Local variables are valid and visible within a particular block (a block is a set of C statements enclosed within brackets { …}). Once the control flow is outside the block, the variable no longer exists ! Local variables are created (space in memory is allocated for them) when the control flow in the program reaches the block where they are declared (e.g., a function) and are destroyed (deallocated from memory) when the control flow leaves the block ! The creation and destruction of variables can be controlled: "extern: the variable is defined in a different module "static: for local variables: makes them last the entire program, for global variables: restricts the scope to the current module "register: try to use a CPU register for the variable "auto: default for local variables ©Gustavo Alonso, ETH Zürich. Programming in C 12 Scope (example 1) int global_variable; int main () { int local_variable; global_variable = 1; local_variable = 2; { int very_local_variable; very_local_variable = 3; } }
  • 4. ©Gustavo Alonso, ETH Zürich. Programming in C 13 Expressions and priority ! +, -, *, /, % are the basic arithmetic operators ! Addition x = 3 + 4; ! Subtraction x = 10 - 3; ! Multiplikation x = 3 * 4; ! Division x = 73 / 8; /* x=9, if int x */ x = 73.0 / 8.0; /*x=9.125,if float x */ ! Modulo x = 73 % 8; /* x=1, the reminder of the division */ ! Multiplication operators (*, /, %) have a higher priority than the additive operators (+, -). When evaluating an expression, operators with a higher priority are evaluated first: x = 2 + 3 / 2 + 3; /* x = 2 + 1 + 3 */ x = (2 + 3) / (2 + 3); /* x = (5 / 5) = 1 */ x = 4*(1/2); /* x = 0 */ x = 4*1/2; /* x = 2 */ ©Gustavo Alonso, ETH Zürich. Programming in C 14 Short-hand operators ! C allows for a short hand notation that introduces side effects. This is done through the prefix- or postfix operators ++, -- ! If ++ or -- are used as prefixes, the variable is modified before it is used in the evaluation of an expression: a = 3; b = ++a + 3; /* b = 4 + 3 = 7 and a = 4 side effect */ ! If ++ or -- are used as postfixes, the variable is first used to evaluate the expression and then modified. a = 3; b = a++ + 3; /* b = 3 + 3 = 6 and a = 4 */ ! Almost all operators can be combined with = a += b; /* a = a + b */ a *= b; /* a = a * b */ ©Gustavo Alonso, ETH Zürich. Programming in C 15 Bit-Operators ! The Bit-Operators & (AND), ^(Exclusive-OR), and | (Inclusive-OR) manipulate bits according to standard two valued logic ! With & one can set bits to 0. ! With ^one can reverse the valu of bits (0 becomes 1 and 1 becomes 0) ! With | one can set bits to 1. Bit1 Bit2 Bit1 & Bit2 Bit1^Bit2 Bit1|Bit2 0 0 0 0 0 0 1 0 1 1 1 0 0 1 1 1 1 1 0 1 ©Gustavo Alonso, ETH Zürich. Programming in C 16 Shift Operators ! << and >> are used to manipulate the position of the bits in a byte or a word ! With a >> b the bits in the variable a are displaced b positions to the right (the new bits are filled with 0). ! With a << b the bits in the variable a are displaced b positions to the left (the new bits are filled with 0). a a >> 3 a << 3a 1 0 1 1 1 0 1 0 1 1 0 1 0 0 0 0 0 0 0 1 0 1 1 1 1 0 1 1 1 0 1 0 MSB LSB b a 2/ = b a 2* =
  • 5. ©Gustavo Alonso, ETH Zürich. Programming in C 17 Comparison and logical operators ! The comparison operators return a 1 or a 0 depending on the result of the comparison. The comparison operators in C are "< (smaller than) "> (greater than) "<= (smaller or equal than) ">= (greater or equal than) "== (equal than) "!= (not equal than) ! && and || are the logical AND and OR operators ( a != b) && (c > d) (a < b) || (c > d) ©Gustavo Alonso, ETH Zürich. Programming in C 18 if statement ! The if – then – else statement can be used with or without the else. The two forms are: if (expression) statement1 if (expression) statement1 else statement2 ! In both cases, when the expression is true, then statement1 is executed. If the expression is false, then, in the first case, statement1 is skipped (not executed), and, in the second case, statement2 after the else is executed. true Statement 1 Statement 2 false expression if (a >= 3) a = a - 3; if (a == 3) a = a * 3; else a = a * 5; if (a >= 3) { a = a - 3; if ( a == 3) a = a * 3;} else a = a * 5; ©Gustavo Alonso, ETH Zürich. Programming in C 19 switch statement (1) ! The switch statement is used to conditionally perform statements based on an integer expression (selector) switch (selector) { case int-value1 : statement1; break; case int-value2 : statement2; break; case int-value3 : statement3; break; default: statement4; } ! The exact behavior of the switch statement is controlled by the break and default commands "break continues execution after the switch statement "default is executed if no other match is found false trueint–value 1 = selector selector Statement 1 int–value 2 = selector true Statement 2 false int–value 2 = selector true Statement 3 false Statement 4 Switch using break ©Gustavo Alonso, ETH Zürich. Programming in C 20 Switch statement (2) switch (selector) { case int-value1 : statement1; case int-value2 : statement2; case int-value3 : statement3; default: statement4; } /* fall through */ false trueint–value 1 = selector selector Statement 1 int–value 2 = selector true Statement 2 false int–value 2 = selector true Statement 3 false Statement 4 Switch without break
  • 6. ©Gustavo Alonso, ETH Zürich. Programming in C 21 Switch (example) char a = ‘A’; switch (a) { case ‘A’: x *= x; break; case ‘B’: x /= x; break; default: x += 5; } ! Once the case ‘A’ is found, x *= x is executed and then we continue after the switch statement. in all cases, only one case will be executed (either ‘A’ or ‘B’ or ‘default’) ! Note that a is of type char. It does not matter, char is treated as an integer using type conversion a = ‘A’; switch (a) { case ‘A’: x *= x; case ‘B’: x /= x; default: x += 5; } ! Once case ‘A’ is found, x *= x is executed. However, there is no break. This means we continue executing statements while ignoring the cases (the check is not performed anymore). Thus, the following statements will also be executed x /= x; x += 5; ©Gustavo Alonso, ETH Zürich. Programming in C 22 for statement ! The for statement provides a compact way to iterate over a range of values. for (initialization; termination; increment) { statement } ! All elements in the for loop are optional: for (; ; ); ! break can be used to interrupt the loop without waiting for the termination condition to evaluate to true ! continue can be used to skip execution of the body of the loop and re-evaluate the termination condition statement termination initialization true increment false for (int x =0; x < 100; x = x + 3){ if (x == 27) continue; else printf(“%d”,x); } ©Gustavo Alonso, ETH Zürich. Programming in C 23 while statement ! The while statement is used to continually execute a block of statements while a condition remains true. while (expression) { statement } ! As before, break and continue can be used to terminate the loop or to finish an iteration and go back to the evaluation of the expression ! for and while are equivalent (can you do it? write a for loop using the while statement and vice versa) expression statement true false main() { char t; while((t = getchar()) != ‘!’) { if (t >= ‘A' && t<= ‘Z') printf("%cn", (char)(t+’a'-’A')); else printf( "%cn", (char)t); } } ©Gustavo Alonso, ETH Zürich. Programming in C 24 Do-while statement ! The do-while is similar to the while statement except that the loop is always executed once and the condition is checked at the end of each iteration. do { statement } while (expression) ! break and continue have the same effect as before expression statement true false
  • 7. ©Gustavo Alonso, ETH Zürich. Programming in C 25 Odd ends exit () ! exit() terminates the execution of the program passing an integer error code. e.g. 0 -> no error, 1 -> not found, 99 -> crash ! exit() is a very primitive way to terminate a program and one that leaves only very limited chance to deal with failures. More modern programming languages use exceptions and exception propagation mechanisms to indicate the occurrence of an error without having to terminate the program if (!(buf = AllocMem (BufSize);)) { printf(“kein Speicher vorhanden"); exit(NO_MEM); } Goto ! C was written as a language that is one step above assembly language. This can be seen, for instance, on the existence of a goto statement ! Do not use goto when programming. There are very good reasons to avoid it: "Style: avoid spaghetti code "Use of stack frames ©Gustavo Alonso, ETH Zürich. Programming in C 26 Arrays ! An array is a finite set of variables of the same basic type ! Instead of giving each variable a name, we use enumeration and group all of them in an array ! The enumeration of elements within an array always starts with 0. If the array has N elements, the last element is in position N-1 ! The C compiler does not check the array boundaries "this is a very typical error that it is very difficult to find (usually happens inside loops that traverse the array) "always check the array boundaries before accessing a variable in an array #include <stdio.h> float data[5]; /* data to average and total */ float total; /* the total of the data items */ float average; /* average of the items */ main() { data[0] = 34.0; data[1] = 27.0; data[2] = 45.0; data[3] = 82.0; data[4] = 22.0; total = data[0] + data[1] + data[2] + data[3] + data[4]; average = total / 5.0; printf("Total %f Average %fn", total, average); return (0); } ©Gustavo Alonso, ETH Zürich. Programming in C 27 Multi-dimensional arrays int a[3][3] 1 2 3 4 5 6 7 8 9 a[0][0] a[0][1] a[0][2] a[1][0]a[1][1] a[1][2]a[2][0] a[2][1] a[2][2] int a = 1; for (i=0; i < 3; i++) for (j=0; j < 3; j++) matrix[i][j] = a++; 1 2 3 4 5 6 7 8 9 int a = 1; for (i=0; i < 3; i++) for (j=0; j < 3; j++) matrix[j][i] = a++; 1 4 7 2 5 8 3 6 9 MEMORY ©Gustavo Alonso, ETH Zürich. Programming in C 28 Array traversals (examples) int array[5][5]; for (int i=0; i < 5; i++) for (int j=0; j < 5; j++) array[i][j] = 1; int array[5][5]; for (int i=0; i < 5; i++) for (int j=0; j < i; j++) array[i][j] = 1; 0 1 2 3 4 0 1 2 3 4 i j inner loop traversalouterlooptraversal 0 1 2 3 4 0 1 2 3 4 i j inner loop traversal outerlooptraversal
  • 8. ©Gustavo Alonso, ETH Zürich. Programming in C 29 More on arrays ! Arrays can be initialized when they are defined: /* a[0] = 3, a[1] = 7, a[2] = 9 */ int a[3] = {3, 7, 9}; /* liste[0]=0.0, …, liste[99]=0.0 */ float list[100] = {}; int a[3][3] = { { 1, 2, 3} { 4, 5, 6} { 7, 8, 9} }; ! Strings are arrays of characters terminated with the null character 0 char str[6] = {‘h’,’a’,’l’,’l’,’o’,’0’} char str[6] = "hello"; ! For string manipulation, however, use the string.h library ! In C, arrays are just a syntactic convenience to make programming easier. Arrays are, for the compiler, the same as pointers (the array name is a pointer to the beginning of the array) 1 2 3 4 5 6 7 8 9 ©Gustavo Alonso, ETH Zürich. Programming in C 30 Pointers ! A variable has a name, an address, a type, and a value: "the name identifies the variable to the programmer "the address specifies where in main memory the variable is located (i.e., the beginning of the memory region reserved for this variable ) "the type specifies how to interpret the data stored in main memory and how long the variable is "the value is the actual data stored in the variable after if has been interpreted according to a given type ! Pointers are language constructs that allow programmers to directly manipulate the address of variables ! Pointers are used with the * and & operators: int* px; /*px = pointer to an integer*/ int x; /*x is an integer */ px = &x; /* px gets the address of x */ /* or px points to x */ x = *px; /* x gets the contents of */ /* whatever x points to */ ©Gustavo Alonso, ETH Zürich. Programming in C 31 Pointers (I) int nummer = 3; int* nummer_ptr = NULL; nummer_ptr int* 0 nummer int 3 nummer_ptr = &nummer; nummer_ptr int* nummer int 3 nummer = 5; nummer_ptr int* nummer int 5 ©Gustavo Alonso, ETH Zürich. Programming in C 32 Pointers (II) *nummer = 8; /* CAREFUL, 8 is not a pointer but an integer */ int x = *nummer_ptr; nummer_ptr int* nummer int 7 nummer_ptr int* nummer int 7 *nummer_ptr = 7; x int 7
  • 9. ©Gustavo Alonso, ETH Zürich. Programming in C 33 Pointers (III) int* y_ptr = nummer_ptr; nummer_ptr int* nummer int 7 x int 7 y_ptr int* *y_ptr = 6; nummer_ptr int* nummer int 6 x int 7 y_ptr int* ©Gustavo Alonso, ETH Zürich. Programming in C 34 Pointers (IV) y_ptr = &x; nummer_ptr int* nummer int 6 x int 7 y_ptr int* ©Gustavo Alonso, ETH Zürich. Programming in C 35 Pointers (V) int* *p_ptr_ptr; p_ptr_ptr = &nummer_ptr; nummer_ptr int* nummer int 6 x int 7 y_ptr int* p_ptr_ptr int** ©Gustavo Alonso, ETH Zürich. Programming in C 36 Pointers (VI) *(*p_ptr_ptr) = 5; nummer_ptr int* nummer int 5 x int 7 y_ptr int* p_ptr_ptr int**
  • 10. ©Gustavo Alonso, ETH Zürich. Programming in C 37 Pointers, arrays and strings ! An array is in reality a pointer: int a[10], y; int* px; px = a; /* px points to a[0] */ px++; /* px points to a[1] */ px=&a[4]; /*px points to a[4] */ y = *(px+3) /*y gets the value*/ /* in a[3] */ ! The pointer arithmetic in C guarantees that if a pointer is incremented or decremented, the pointer will vary according to its type. For instance, if px points to an array, px++ will always yield the next element independently of what is the type stored in the array ! Strings can be manipulated through pointers: char* message; message = “This is a string”; ! message is a pointer that now points to the first character in the string “This is a string” ! Again, use the string.h for string manipulation rather than doing it directly (you will avoid many errors) ©Gustavo Alonso, ETH Zürich. Programming in C 38 Troubles with pointers What is printed by the following code? #include <stdio.h> void f(int *aa, int *bb) { *bb = 8; aa[1] = bb[2]; aa = bb; } main() { int a[5] = { 1, 2, 3, 4, 5 }, *b; b = a + 2; f(a,b); printf("%d %d %d %d %dn", a[0], a[1], a[2], a[3], a[4]); } What is printed by the following code? #include <stdio.h> void g(int *aa, int *bb) { bb[2] = aa[-2]; *aa++ = 17; *++aa = 10; } main() { int blap[7] = { 1, 2, 3, 4, 5, 6, 7 }; int *c = blap + 3; g(c,blap); printf("%d %d %d %d %d %d %dn", blap[0], blap[1], blap[2], blap[3], blap[4], blap[5], blap[6]); } ©Gustavo Alonso, ETH Zürich. Programming in C 39 Structures ! Structures allow programmers to define complex data types. A structure is a new (user defined) data type: struct Id_card { char name[100]; /* Name */ char adresse[100]; /*Address */ short int geburtsjahr; /*Geburtsjahr*/ int telefon; /* Telefonnummer */ short int semester; /* Semester */ } ethz, uniz; struct Id_card erasmus; ! Structures of the same type can be copied with the operator = but they should not be compared with == ! Access to the elements of a structure is as follows: ethz.name = “Gustavo”; ethz.telefon = 1234567; ! Pointers can also refer to structures, in which case elements are accessed through the ->, or * operators: struct Id_card *pid; pid = &ethz_student; pid->name = “Gustavo”; pid->telefon = 1234567; (*pid).name = “Gustavo”; (*pid).telefon = 1234567; ! In ANSI C, structures can be passed as arguments (by value or by reference) and can also be the return type of a function (this is not true in earlier versions of C) ©Gustavo Alonso, ETH Zürich. Programming in C 40 Example structures int main () { struct Typ_kiste { char inhalt[50]; /* was ist in der Kiste */ int anzahl; /* wieviel davon */ float preis; /* was kostet eine Einheit */ }; float wert; const int MAX_KISTEN = 10; struct Typ_kiste liste_kisten[MAX_KISTEN]; /* Initialisierung … */ /* Gesammter Wert */ for (int i = 0; i < MAX_KISTEN; i++) wert += liste_kisten[i].anzahl * liste_kisten[i].preis; … }
  • 11. ©Gustavo Alonso, ETH Zürich. Programming in C 41 struct ... inhalt inhalt inhalt anzahl anzahl anzahl preis preis preis liste_kisten[0] liste_kisten[1] liste_kisten[2] liste_kisten[0].inhalt liste_kisten[1].inhalt liste_kisten[2].inhalt liste_kisten[0].anzahl liste_kisten[1].anzahl liste_kisten[2].anzahl liste_kisten[0].preis liste_kisten[1].preis liste_kisten[2].preis char[50] char[50] char[50] int intint float float float ©Gustavo Alonso, ETH Zürich. Programming in C 42 Functions ! C is a modular language where the main unit of composition is the function ! A function has the following elements: "a return type: specifies the type of the value returned by the function when it terminates "a function name: identifies the function for the programmer "arguments of defined types: parameters to pass to the function, which can be • by value: the function gets the a copy of the value of the parameters but cannot modify the actual parameters • by reference: the function gets the address (reference) of the parameters and can modify them ! General syntax: returntype function_name(def of parameters) { localvariables functioncode } ! An example: float findaverage(float a, float b) { float average; average=(a+b)/2; return(average); } ! In ANSI C functions must be declared as prototypes before they are defined: float findaverage(float a, float b) ©Gustavo Alonso, ETH Zürich. Programming in C 43 Examples /* SWAP.C exchange values */ #include <stdio.h> void swap(float *x, float *y); /* prototype */ main() { float x, y; printf("Please input 1st value: "); scanf("%f", &x); printf("Please input 2nd value: "); scanf("%f", &y); printf("Values BEFORE 'swap' %f, %fn", x, y); swap(&x, &y); /* address of x, y */ printf("Values AFTER 'swap' %f, %fn", x, y); return 0; } /* exchange values within function */ void swap(float *x, float *y) { float t; t = *x; /* *x is value pointed to by x */ *x = *y; *y = t; printf("Values WITHIN 'swap' %f, %fn", *x, *y); } * FACTORIAL * * fact(n) = n*(n-1)*....2*1 * #include <stdio.h> fact(n) { int n; if (n == 0) return(1); return(n * fact(n-1)); } main() { int n, m; printf("Enter a number: "); scanf("%d", &n); m = fact(n); printf(”Factorial of %d is %d.n", n, m); exit(0); } ©Gustavo Alonso, ETH Zürich. Programming in C 44 main() is also a function /* program to print arguments from command line */ #include <stdio.h> main(int argc, char **argv) { int i; printf("argc = %dnn",argc); for (i=0;i<argc;++i) printf("argv[%d]: %sn",i, argv[i]); } ! argc stands for argument count and it contains how many arguments have been passed in the command line when the program is invoked ! argv is the argument vector (array) and it contains all the arguments passed through the command line ! argc is always at least 1 since argv[0] is the name of the program * append one file to the another */ #include <stdio.h> main(int argc, char **argv) { int c; FILE *from, *to; if (argc != 3) { /* Check the arguments. */ fprintf(stderr, "Usage: %s from-file to-filen", *argv); exit(1); } if ((from = fopen(argv[1], "r")) == NULL) { perror(argv[1]); /* Open the from-file */ exit(1); } if ((to = fopen(argv[2], "a")) == NULL) { perror(argv[2]); /* Open the to-file */ exit(1); } /* Read one file and append to the other until EOF */ while ((c = getc(from)) != EOF) putc(c, to); /*close the files */ fclose(from); fclose(to); exit(0); }
  • 12. ©Gustavo Alonso, ETH Zürich. Programming in C 45 Dynamic memory allocation ! The definition of types and variables help the compiler to understand the program we have written ! The declaration of variables leads to the allocation of memory for those variables. In general, this happens automatically and without intervention of the programmer ! C allows the programmer to allocate and deallocate memory dynamically ! The functions used for memory allocation are in stdlib.h ! Typical function calls are "malloc "free typedef struct node { int x,z; struct node *next; } NODE; NODE *nptr; if ((nptr =((NODE)*) malloc(sizeof(NODE))) == NULL) { printf("No memory - bye bye"); exit(99); } ! malloc returns a pointer to the allocated memory. The pointer is generic (void *) and it is a good practice to cast the pointer to the appropriate pointer type to avoid errors. ! Allocated memory must be returned to the system: free(nptr); ©Gustavo Alonso, ETH Zürich. Programming in C 46 Example dynamic array /* This program simply reads integers into a dynamic array until eof. The array is expanded as needed */ #include <stdio.h> #include <stdlib.h> #define INIT_SIZE 8 /* Initial array size. */ main() { int num; /* Number of integers */ int *arr; /* Array of integers. */ int arrsize; /* The size of the array of integers. */ int m; /* Index. */ int in; /* Input number. */ /* Allocate the initial space. */ arrsize = INIT_SIZE; arr = (int*) malloc(arrsize*sizeof(int)); /* Read in the numbers. */ num = 0; while(scanf("%d", &in) == 1) { /* See if there's room. */ if(num >= arrsize) { /* There's not. Get more. */ arrsize *= 2; arr = (int*) realloc(arr, arrsize*sizeof(int)); if(arr == NULL){ fprintf(stderr, "Allocation failed %d.n"); exit(18); } } /* Store the number. */ arr[num++] = in; } /* Print out the numbers. */ for(m = 0; m < num; ++m) printf("%dn", arr[m]); } ©Gustavo Alonso, ETH Zürich. Programming in C 47 Example string library #include <stdio.h> #include <string.h> void main() { char name1[12], name2[12], mixed[25]; char title[20]; strcpy(name1, "Rosalinda"); strcpy(name2, "Zeke"); strcpy(title, "This is the title."); printf(" %snn", title); printf("Name 1 is %sn", name1); printf("Name 2 is %sn", name2); if(strcmp(name1, name2) > 0) /* returns 1 if name1 > name2 */ strcpy(mixed, name1); else strcpy(mixed, name2); printf("The biggest name alphabetically is %sn", mixed); strcpy(mixed, name1); strcat(mixed, " "); strcat(mixed, name2); printf("Both names are %sn", mixed); } This is the title. Name1 is Rosalinda Name2 is Zeke The biggest name alphabetically is Zeke Both names are Rosalinda Zeke ©Gustavo Alonso, ETH Zürich. Programming in C 48 References Some material for these foils and some of the examples have been taken from the following on- line books on C (there are many more): ! C Programming, Steve Holmes: http://www.strath.ac.uk/IT/Docs/Ccourse/ ! C language tutorial: http://www.graylab.ac.uk/doc/tutorials/C/index.htm ! Programming in C , A. D. Marshall: http://www.cs.cf.ac.uk/Dave/C/CE.html For how C was developed, read the tutorial written by Brian W. Kernighan in 1974: ! Programming in C: A Tutorial, B. W. Kernighan: http://www.lysator.liu.se/c/bwk-tutor.html