linux system programming:...

105
Linux System Programming: Introduction Prof. Michele Loreti Laboratorio di Sistemi Operativi Corso di Laurea in Informatica (L31) Scuola di Scienze e Tecnologie Prof. Michele Loreti Linux System Programming: Introduction 2 / 76

Upload: others

Post on 19-Jul-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Linux System Programming: Introduction

Prof. Michele Loreti

Laboratorio di Sistemi OperativiCorso di Laurea in Informatica (L31)Scuola di Scienze e Tecnologie

Prof. Michele Loreti Linux System Programming: Introduction 2 / 76

Page 2: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

System programming...

This lectures will focus on system programming, which is the practice ofwriting system software.

System software lives at a low level, interfacing directly with the kerneland core system libraries.

Examples of system software are. . .

. . . your text editor, your compiler and your debugger, your core utilitiesand system daemons are all system software;

. . . but also the network server, the web server, and the database.

All these components primarily, if not exclusively, interact with the kerneland the C library.

Prof. Michele Loreti Linux System Programming: Introduction 3 / 76

Page 3: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

System programming...

This lectures will focus on system programming, which is the practice ofwriting system software.

System software lives at a low level, interfacing directly with the kerneland core system libraries.

Examples of system software are. . .

. . . your text editor, your compiler and your debugger, your core utilitiesand system daemons are all system software;

. . . but also the network server, the web server, and the database.

All these components primarily, if not exclusively, interact with the kerneland the C library.

Prof. Michele Loreti Linux System Programming: Introduction 3 / 76

Page 4: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

System programming...

This lectures will focus on system programming, which is the practice ofwriting system software.

System software lives at a low level, interfacing directly with the kerneland core system libraries.

Examples of system software are. . .

. . . your text editor, your compiler and your debugger, your core utilitiesand system daemons are all system software;

. . . but also the network server, the web server, and the database.

All these components primarily, if not exclusively, interact with the kerneland the C library.

Prof. Michele Loreti Linux System Programming: Introduction 3 / 76

Page 5: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

System programming...

This lectures will focus on system programming, which is the practice ofwriting system software.

System software lives at a low level, interfacing directly with the kerneland core system libraries.

Examples of system software are. . .

. . . your text editor, your compiler and your debugger, your core utilitiesand system daemons are all system software;

. . . but also the network server, the web server, and the database.

All these components primarily, if not exclusively, interact with the kerneland the C library.

Prof. Michele Loreti Linux System Programming: Introduction 3 / 76

Page 6: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

System programming...

Traditionally, all Unix programming was system-level programming.

Unix systems historically did not include many higher-level abstractions

We can compare and contrast system programming with applicationprogramming

System programming: programmer must have an acute awareness of thehardware and the operating system on which they work.

Efficiency!

Application programming: programmer also use high-level libraries thatabstract away the details of the hardware and operatingsystem.

Portability!

In this lecture we will focus on Linux System Programming!

Prof. Michele Loreti Linux System Programming: Introduction 4 / 76

Page 7: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

System programming...

Traditionally, all Unix programming was system-level programming.

Unix systems historically did not include many higher-level abstractions

We can compare and contrast system programming with applicationprogramming

System programming: programmer must have an acute awareness of thehardware and the operating system on which they work.

Efficiency!

Application programming: programmer also use high-level libraries thatabstract away the details of the hardware and operatingsystem.

Portability!

In this lecture we will focus on Linux System Programming!

Prof. Michele Loreti Linux System Programming: Introduction 4 / 76

Page 8: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

System programming...

Traditionally, all Unix programming was system-level programming.

Unix systems historically did not include many higher-level abstractions

We can compare and contrast system programming with applicationprogramming

System programming: programmer must have an acute awareness of thehardware and the operating system on which they work.

Efficiency!

Application programming: programmer also use high-level libraries thatabstract away the details of the hardware and operatingsystem.

Portability!

In this lecture we will focus on Linux System Programming!

Prof. Michele Loreti Linux System Programming: Introduction 4 / 76

Page 9: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

System programming...

Traditionally, all Unix programming was system-level programming.

Unix systems historically did not include many higher-level abstractions

We can compare and contrast system programming with applicationprogramming

System programming: programmer must have an acute awareness of thehardware and the operating system on which they work.

Efficiency!

Application programming: programmer also use high-level libraries thatabstract away the details of the hardware and operatingsystem.

Portability!

In this lecture we will focus on Linux System Programming!

Prof. Michele Loreti Linux System Programming: Introduction 4 / 76

Page 10: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

System programming...

Traditionally, all Unix programming was system-level programming.

Unix systems historically did not include many higher-level abstractions

We can compare and contrast system programming with applicationprogramming

System programming: programmer must have an acute awareness of thehardware and the operating system on which they work.

Efficiency!

Application programming: programmer also use high-level libraries thatabstract away the details of the hardware and operatingsystem.

Portability!

In this lecture we will focus on Linux System Programming!

Prof. Michele Loreti Linux System Programming: Introduction 4 / 76

Page 11: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

System programming...

Traditionally, all Unix programming was system-level programming.

Unix systems historically did not include many higher-level abstractions

We can compare and contrast system programming with applicationprogramming

System programming: programmer must have an acute awareness of thehardware and the operating system on which they work.

Efficiency!

Application programming: programmer also use high-level libraries thatabstract away the details of the hardware and operatingsystem.

Portability!

In this lecture we will focus on Linux System Programming!

Prof. Michele Loreti Linux System Programming: Introduction 4 / 76

Page 12: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

System programming...

Traditionally, all Unix programming was system-level programming.

Unix systems historically did not include many higher-level abstractions

We can compare and contrast system programming with applicationprogramming

System programming: programmer must have an acute awareness of thehardware and the operating system on which they work.

Efficiency!

Application programming: programmer also use high-level libraries thatabstract away the details of the hardware and operatingsystem. Portability!

In this lecture we will focus on Linux System Programming!

Prof. Michele Loreti Linux System Programming: Introduction 4 / 76

Page 13: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Cornerstones of Linux System Programming. . .

There are three cornerstones of system programming in Linux:

1. system calls,

2. the C library,

3. and the C compiler.

System calls (often shortened to syscalls) are function invocations madefrom user space into the kernel (the core internals of the system) in orderto request some service or resource from the operating system.Examples of system calls are:

� read()

� write ()

� get thread area ()

� set tid address ()

Prof. Michele Loreti Linux System Programming: Introduction 5 / 76

Page 14: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Cornerstones of Linux System Programming. . .

There are three cornerstones of system programming in Linux:

1. system calls,

2. the C library,

3. and the C compiler.

System calls (often shortened to syscalls) are function invocations madefrom user space into the kernel (the core internals of the system) in orderto request some service or resource from the operating system.Examples of system calls are:

� read()

� write ()

� get thread area ()

� set tid address ()

Prof. Michele Loreti Linux System Programming: Introduction 5 / 76

Page 15: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Cornerstones of Linux System Programming. . .

There are three cornerstones of system programming in Linux:

1. system calls,

2. the C library,

3. and the C compiler.

The C library (libc) is at the heart of Unix applications.

On modern Linux systems, the C library is provided by GNU libc,abbreviated glibc.

In addition to standard C library, the GNU C library provides wrappers forsystem calls, threading support, and basic application facilities.

Prof. Michele Loreti Linux System Programming: Introduction 6 / 76

Page 16: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Cornerstones of Linux System Programming. . .

There are three cornerstones of system programming in Linux:

1. system calls,

2. the C library,

3. and the C compiler.

The C library (libc) is at the heart of Unix applications.

On modern Linux systems, the C library is provided by GNU libc,abbreviated glibc.

In addition to standard C library, the GNU C library provides wrappers forsystem calls, threading support, and basic application facilities.

Prof. Michele Loreti Linux System Programming: Introduction 6 / 76

Page 17: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Cornerstones of Linux System Programming. . .

There are three cornerstones of system programming in Linux:

1. system calls,

2. the C library,

3. and the C compiler.

The C library (libc) is at the heart of Unix applications.

On modern Linux systems, the C library is provided by GNU libc,abbreviated glibc.

In addition to standard C library, the GNU C library provides wrappers forsystem calls, threading support, and basic application facilities.

Prof. Michele Loreti Linux System Programming: Introduction 6 / 76

Page 18: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Cornerstones of Linux System Programming. . .

There are three cornerstones of system programming in Linux:

1. system calls,

2. the C library,

3. and the C compiler.

The C library (libc) is at the heart of Unix applications.

On modern Linux systems, the C library is provided by GNU libc,abbreviated glibc.

In addition to standard C library, the GNU C library provides wrappers forsystem calls, threading support, and basic application facilities.

Prof. Michele Loreti Linux System Programming: Introduction 7 / 76

Page 19: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Cornerstones of Linux System Programming. . .

There are three cornerstones of system programming in Linux:

1. system calls,

2. the C library,

3. and the C compiler.

The C library (libc) is at the heart of Unix applications.

On modern Linux systems, the C library is provided by GNU libc,abbreviated glibc.

In addition to standard C library, the GNU C library provides wrappers forsystem calls, threading support, and basic application facilities.

Prof. Michele Loreti Linux System Programming: Introduction 7 / 76

Page 20: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Cornerstones of Linux System Programming. . .

There are three cornerstones of system programming in Linux:

1. system calls,

2. the C library,

3. and the C compiler.

The C library (libc) is at the heart of Unix applications.

On modern Linux systems, the C library is provided by GNU libc,abbreviated glibc.

In addition to standard C library, the GNU C library provides wrappers forsystem calls, threading support, and basic application facilities.

Prof. Michele Loreti Linux System Programming: Introduction 7 / 76

Page 21: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Cornerstones of Linux System Programming. . .

There are three cornerstones of system programming in Linux:

1. system calls,

2. the C library,

3. and the C compiler.

In Linux, the standard C compiler is provided by the GNU CompilerCollection (gcc).

� gcc supports many languages (C, C++, Java, Ada. . . );

� gcc is also the name of the C compiler.

Prof. Michele Loreti Linux System Programming: Introduction 8 / 76

Page 22: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Cornerstones of Linux System Programming. . .

There are three cornerstones of system programming in Linux:

1. system calls,

2. the C library,

3. and the C compiler.

In Linux, the standard C compiler is provided by the GNU CompilerCollection (gcc).

� gcc supports many languages (C, C++, Java, Ada. . . );

� gcc is also the name of the C compiler.

Prof. Michele Loreti Linux System Programming: Introduction 8 / 76

Page 23: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Portability. . .

At the system level, there are two separate sets of definitions anddescriptions that impact portability:

� Application Programming Interface (API)� Application Binary Interface (ABI)

Application Programming Interface (API)

An API defines the interfaces by which one piece of softwarecommunicates with another at the source level

It provides abstraction by providing a standard set of interfaces —usuallyfunctions— that one piece of software can invoke from another piece ofsoftware.

API guarantees source compatibility: the user of the API will successfullycompile against the implementation of the API.

Prof. Michele Loreti Linux System Programming: Introduction 9 / 76

Page 24: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Portability: API vs ABI. . .

At the system level, there are two separate sets of definitions anddescriptions that impact portability:

� Application Programming Interface (API)� Application Binary Interface (ABI)

Application Programming Interface (API)

An API defines the interfaces by which one piece of softwarecommunicates with another at the source level

It provides abstraction by providing a standard set of interfaces —usuallyfunctions— that one piece of software can invoke from another piece ofsoftware.

API guarantees source compatibility: the user of the API will successfullycompile against the implementation of the API.

Prof. Michele Loreti Linux System Programming: Introduction 9 / 76

Page 25: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Portability: API vs ABI. . .

At the system level, there are two separate sets of definitions anddescriptions that impact portability:

� Application Programming Interface (API)� Application Binary Interface (ABI)

Application Programming Interface (API)

An API defines the interfaces by which one piece of softwarecommunicates with another at the source level

It provides abstraction by providing a standard set of interfaces —usuallyfunctions— that one piece of software can invoke from another piece ofsoftware.

API guarantees source compatibility: the user of the API will successfullycompile against the implementation of the API.

Prof. Michele Loreti Linux System Programming: Introduction 9 / 76

Page 26: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Portability: API vs ABI. . .

At the system level, there are two separate sets of definitions anddescriptions that impact portability:

� Application Programming Interface (API)� Application Binary Interface (ABI)

Application Programming Interface (API)

An API defines the interfaces by which one piece of softwarecommunicates with another at the source level

It provides abstraction by providing a standard set of interfaces —usuallyfunctions— that one piece of software can invoke from another piece ofsoftware.

API guarantees source compatibility: the user of the API will successfullycompile against the implementation of the API.

Prof. Michele Loreti Linux System Programming: Introduction 9 / 76

Page 27: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Portability: API vs ABI. . .

At the system level, there are two separate sets of definitions anddescriptions that impact portability:

� Application Programming Interface (API)

� Application Binary Interface (ABI)

Application Binary Interface (ABI)

ABI defines the binary interface between two or more pieces of software ona particular architecture

It defines how an application interacts with itself, how an applicationinteracts with the kernel, and how an application interacts with libraries.

ABI ensures binary compatibility: a piece of object code will function onany system with the same ABI, without requiring recompilation.

Prof. Michele Loreti Linux System Programming: Introduction 10 / 76

Page 28: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

C Language Standards. . .

K&R C Dennis Ritchie and Brian Kernighan’s, The C ProgrammingLanguage (Prentice Hall), acted as the informal Cspecification;

ANSI C American National Standards Institute (ANSI) developed anofficial version of C (1989);

ISO CXX International Organization for Standardization (ISO) updatesthe C standards:

� ISO C90, based on ANSI C;� ISO C95, an updated of ISO C90;� ISO C99, introduced many new features, including inline

functions, new data types, variable-length arrays. . . ;� ISO C11, latest version of the standard equipped with a

formalized memory model, enabling the portable use ofthreads across platforms.

Prof. Michele Loreti Linux System Programming: Introduction 11 / 76

Page 29: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Language C: Introduction

Prof. Michele Loreti

Laboratorio di Sistemi OperativiCorso di Laurea in Informatica (L31)Scuola di Scienze e Tecnologie

Prof. Michele Loreti Language C: Introduction 12 / 76

Page 30: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

C keywords. . .

auto break case char

const continue default do

double else enum extern

float for goto if

inline int long register

restrict return short signed

sizeof static struct switch

typedef union unsigned void

volatile while

You have already seen many of these keywords in Java. . .. . . the meaning is almost the same in C!

Prof. Michele Loreti Language C: Introduction 13 / 76

Page 31: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

C keywords. . .

auto break case char

const continue default do

double else enum extern

float for goto if

inline int long register

restrict return short signed

sizeof static struct switch

typedef union unsigned void

volatile while

You have already seen many of these keywords in Java. . .

. . . the meaning is almost the same in C!

Prof. Michele Loreti Language C: Introduction 13 / 76

Page 32: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

C keywords. . .

auto break case char

const continue default do

double else enum extern

float for goto if

inline int long register

restrict return short signed

sizeof static struct switch

typedef union unsigned void

volatile while

You have already seen many of these keywords in Java. . .. . . the meaning is almost the same in C!

Prof. Michele Loreti Language C: Introduction 13 / 76

Page 33: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Your first C program. . .

/∗∗This i s a comment that , l i k e i n Java . . .. . . goes on m u l t i p l e l i n e s !∗∗/

// Statement to i n c l u d e a s e t o f d e f i n i t i o n s !#i n c l u d e <s t d i o . h>

#d e f i n e MESSAGE ” H e l l o World !\ n”

i n t main ( v o i d ) {p r i n t f (MESSAGE) ;

}

Prof. Michele Loreti Language C: Introduction 14 / 76

Page 34: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Compiling your program. . .

Compiling a C program is a multi-stage process. At an overview level, theprocess can be split into four separate stages:

1. preprocessing;

2. compilation;

3. assembly;

4. linking.

Prof. Michele Loreti Language C: Introduction 15 / 76

Page 35: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Compiling your program: Preprocessing. . .

The first stage of compilation is called preprocessing. In this stage, linesstarting with a # character are interpreted by the preprocessor aspreprocessor commands.

To print the result of the preprocessing stage, pass the −E option to gcc:

gcc −E h e l l o w o r l d . c

Prof. Michele Loreti Language C: Introduction 16 / 76

Page 36: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Compiling your program: Preprocessing. . .

The first stage of compilation is called preprocessing. In this stage, linesstarting with a # character are interpreted by the preprocessor aspreprocessor commands.

To print the result of the preprocessing stage, pass the −E option to gcc:

gcc −E h e l l o w o r l d . c

Prof. Michele Loreti Language C: Introduction 16 / 76

Page 37: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Compiling your program: Compilation. . .

In this stage, the preprocessed code is translated to assembly instructionsspecific to the target processor architecture. These form an intermediatehuman readable language.

To save the result of the compilation stage, pass the −S option to gcc:

gcc −S h e l l o w o r l d . c

Prof. Michele Loreti Language C: Introduction 17 / 76

Page 38: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Compiling your program: Compilation. . .

In this stage, the preprocessed code is translated to assembly instructionsspecific to the target processor architecture. These form an intermediatehuman readable language.

To save the result of the compilation stage, pass the −S option to gcc:

gcc −S h e l l o w o r l d . c

Prof. Michele Loreti Language C: Introduction 17 / 76

Page 39: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Compiling your program: Assembly. . .

During the assembly stage, an assembler is used to translate the assemblyinstructions to machine code, or object code. The output consists ofactual instructions to be run by the target processor.

To save the result of the compilation stage, pass the −c option to gcc:

gcc −c h e l l o w o r l d . c

Running the above command will create a file named hello world .o,containing the object code of the program. The contents of this file is in abinary format and can be inspected using hexdump or od:

hexdump h e l l o w o r l d . ood −c h e l l o w o r l d . o

Prof. Michele Loreti Language C: Introduction 18 / 76

Page 40: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Compiling your program: Assembly. . .

During the assembly stage, an assembler is used to translate the assemblyinstructions to machine code, or object code. The output consists ofactual instructions to be run by the target processor.

To save the result of the compilation stage, pass the −c option to gcc:

gcc −c h e l l o w o r l d . c

Running the above command will create a file named hello world .o,containing the object code of the program. The contents of this file is in abinary format and can be inspected using hexdump or od:

hexdump h e l l o w o r l d . ood −c h e l l o w o r l d . o

Prof. Michele Loreti Language C: Introduction 18 / 76

Page 41: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Compiling your program: Assembly. . .

During the assembly stage, an assembler is used to translate the assemblyinstructions to machine code, or object code. The output consists ofactual instructions to be run by the target processor.

To save the result of the compilation stage, pass the −c option to gcc:

gcc −c h e l l o w o r l d . c

Running the above command will create a file named hello world .o,containing the object code of the program. The contents of this file is in abinary format and can be inspected using hexdump or od:

hexdump h e l l o w o r l d . ood −c h e l l o w o r l d . o

Prof. Michele Loreti Language C: Introduction 18 / 76

Page 42: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Compiling your program: Linking. . .

The object code generated in the assembly stage is composed of machineinstructions that the processor understands.

However, some pieces of the program are out of order or missing.

The linker will arrange the pieces of object code so that functions in somepieces can successfully call functions in other pieces. It will also add piecescontaining the instructions for library functions used by the program.

The result of this stage is the final executable program:

gcc −o h e l l o w o r l d h e l l o w o r l d . c

Without the −o hello world option, file a.out is generated.

Prof. Michele Loreti Language C: Introduction 19 / 76

Page 43: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Compiling your program: Linking. . .

The object code generated in the assembly stage is composed of machineinstructions that the processor understands.

However, some pieces of the program are out of order or missing.

The linker will arrange the pieces of object code so that functions in somepieces can successfully call functions in other pieces. It will also add piecescontaining the instructions for library functions used by the program.

The result of this stage is the final executable program:

gcc −o h e l l o w o r l d h e l l o w o r l d . c

Without the −o hello world option, file a.out is generated.

Prof. Michele Loreti Language C: Introduction 19 / 76

Page 44: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Compiling your program: Linking. . .

The object code generated in the assembly stage is composed of machineinstructions that the processor understands.

However, some pieces of the program are out of order or missing.

The linker will arrange the pieces of object code so that functions in somepieces can successfully call functions in other pieces. It will also add piecescontaining the instructions for library functions used by the program.

The result of this stage is the final executable program:

gcc −o h e l l o w o r l d h e l l o w o r l d . c

Without the −o hello world option, file a.out is generated.

Prof. Michele Loreti Language C: Introduction 19 / 76

Page 45: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Compiling your program: Linking. . .

The object code generated in the assembly stage is composed of machineinstructions that the processor understands.

However, some pieces of the program are out of order or missing.

The linker will arrange the pieces of object code so that functions in somepieces can successfully call functions in other pieces. It will also add piecescontaining the instructions for library functions used by the program.

The result of this stage is the final executable program:

gcc −o h e l l o w o r l d h e l l o w o r l d . c

Without the −o hello world option, file a.out is generated.

Prof. Michele Loreti Language C: Introduction 19 / 76

Page 46: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Identifiers. . .

In C language identifiers are the names given to variables, constants,functions and user-define data.

These identifier are defined against a set of rules:

1. can only have alphanumeric characters (a−z , A−Z , 0−9) andunderscore ( );

2. the first character of an identifier can only contain alphabet (a−z ,A−Z) or underscore ( );

3. are case sensitive. . .

. . . for example name and Name are two different identifiers in C;

4. keywords are not allowed to be used as Identifiers.

Prof. Michele Loreti Language C: Introduction 20 / 76

Page 47: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Identifiers. . .

In C language identifiers are the names given to variables, constants,functions and user-define data.

These identifier are defined against a set of rules:

1. can only have alphanumeric characters (a−z , A−Z , 0−9) andunderscore ( );

2. the first character of an identifier can only contain alphabet (a−z ,A−Z) or underscore ( );

3. are case sensitive. . .

. . . for example name and Name are two different identifiers in C;

4. keywords are not allowed to be used as Identifiers.

Prof. Michele Loreti Language C: Introduction 20 / 76

Page 48: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Identifiers. . .

In C language identifiers are the names given to variables, constants,functions and user-define data.

These identifier are defined against a set of rules:

1. can only have alphanumeric characters (a−z , A−Z , 0−9) andunderscore ( );

2. the first character of an identifier can only contain alphabet (a−z ,A−Z) or underscore ( );

3. are case sensitive. . .

. . . for example name and Name are two different identifiers in C;

4. keywords are not allowed to be used as Identifiers.

Prof. Michele Loreti Language C: Introduction 20 / 76

Page 49: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Identifiers. . .

In C language identifiers are the names given to variables, constants,functions and user-define data.

These identifier are defined against a set of rules:

1. can only have alphanumeric characters (a−z , A−Z , 0−9) andunderscore ( );

2. the first character of an identifier can only contain alphabet (a−z ,A−Z) or underscore ( );

3. are case sensitive. . .

. . . for example name and Name are two different identifiers in C;

4. keywords are not allowed to be used as Identifiers.

Prof. Michele Loreti Language C: Introduction 20 / 76

Page 50: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Identifiers. . .

In C language identifiers are the names given to variables, constants,functions and user-define data.

These identifier are defined against a set of rules:

1. can only have alphanumeric characters (a−z , A−Z , 0−9) andunderscore ( );

2. the first character of an identifier can only contain alphabet (a−z ,A−Z) or underscore ( );

3. are case sensitive. . .

. . . for example name and Name are two different identifiers in C;

4. keywords are not allowed to be used as Identifiers.

Prof. Michele Loreti Language C: Introduction 20 / 76

Page 51: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Identifiers. . .

In C language identifiers are the names given to variables, constants,functions and user-define data.

These identifier are defined against a set of rules:

1. can only have alphanumeric characters (a−z , A−Z , 0−9) andunderscore ( );

2. the first character of an identifier can only contain alphabet (a−z ,A−Z) or underscore ( );

3. are case sensitive. . .

. . . for example name and Name are two different identifiers in C;

4. keywords are not allowed to be used as Identifiers.

Prof. Michele Loreti Language C: Introduction 20 / 76

Page 52: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Identifiers. . .

In C language identifiers are the names given to variables, constants,functions and user-define data.

These identifier are defined against a set of rules:

1. can only have alphanumeric characters (a−z , A−Z , 0−9) andunderscore ( );

2. the first character of an identifier can only contain alphabet (a−z ,A−Z) or underscore ( );

3. are case sensitive. . .

. . . for example name and Name are two different identifiers in C;

4. keywords are not allowed to be used as Identifiers.

Prof. Michele Loreti Language C: Introduction 20 / 76

Page 53: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Types. . .

C Data Types

void comprises an empty set of values; it is an incompleteobject type that cannot be completed.

char usually 8-bits (1 byte)short int at least 16-bitsint usually the natural word size

for a machine or OS (e.g., 16, 32, 64 bits)long int at least 32-bitsfloat usually 32-bitsdouble usually 64-bitslong double usually at least 64-bits

Prof. Michele Loreti Language C: Introduction 21 / 76

Page 54: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Types: Ranges. . .

#i n c l u d e <s t d i o . h>#i n c l u d e < l i m i t s . h> /∗ i n t e g e r s p e c i f i c a t i o n s ∗/#i n c l u d e < f l o a t . h> /∗ f l o a t i n g −p o i n t s p e c i f i c a t i o n s ∗/

/∗ Look at ra n g e l i m i t s o f c e r t a i n t y p e s ∗/i n t main ( v o i d ){

p r i n t f ( ” I n t e g e r r a ng e :\ t%d\ t%d\n” , INT MIN , INT MAX) ;p r i n t f ( ”Long r an g e :\ t%l d \ t%l d \n” , LONG MIN , LONG MAX) ;p r i n t f ( ” F l o a t r a n g e :\ t%e\ t%e\n” , FLT MIN , FLT MAX) ;p r i n t f ( ” Double r a n ge :\ t%e\ t%e\n” , DBL MIN , DBL MAX) ;p r i n t f ( ”Long d o u b l e ra n g e :\ t%e\ t%e\n” , LDBL MIN , LDBL MAX

) ;p r i n t f ( ” F l o a t−Double e p s i l o n :\ t%e\ t%e\n” , FLT EPSILON ,

DBL EPSILON ) ;}

Prof. Michele Loreti Language C: Introduction 22 / 76

Page 55: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Types: size in bytes. . .

The instruction sizeof can be used to obtain the size, in byte, of a C type.

##i n c l u d e <s t d i o . h>

i n t main ( v o i d )/∗ P r i n t t h e s i z e o f v a r i o u s t y p e s i n ”number−of−c h a r s ” ∗/{

p r i n t f ( ” v o i d \ t c h a r \ t s h o r t \ t i n t \ t l o n g \ t f l o a t \ t d o u b l e \n” ) ;p r i n t f ( ”%3d\ t%3d\ t%3d\ t%3d\ t%3d\ t%3d\ t%3d\n” ,\

s i z e o f ( v o i d ) , s i z e o f ( c h a r ) , s i z e o f ( s h o r t ) ,\s i z e o f ( i n t ) , s i z e o f ( l o n g ) , s i z e o f ( f l o a t ) ,\s i z e o f ( d o u b l e ) ) ;

}

Prof. Michele Loreti Language C: Introduction 23 / 76

Page 56: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Types: size in bytes. . .

The instruction sizeof can be used to obtain the size, in byte, of a C type.

##i n c l u d e <s t d i o . h>

i n t main ( v o i d )/∗ P r i n t t h e s i z e o f v a r i o u s t y p e s i n ”number−of−c h a r s ” ∗/{

p r i n t f ( ” v o i d \ t c h a r \ t s h o r t \ t i n t \ t l o n g \ t f l o a t \ t d o u b l e \n” ) ;p r i n t f ( ”%3d\ t%3d\ t%3d\ t%3d\ t%3d\ t%3d\ t%3d\n” ,\

s i z e o f ( v o i d ) , s i z e o f ( c h a r ) , s i z e o f ( s h o r t ) ,\s i z e o f ( i n t ) , s i z e o f ( l o n g ) , s i z e o f ( f l o a t ) ,\s i z e o f ( d o u b l e ) ) ;

}

Prof. Michele Loreti Language C: Introduction 23 / 76

Page 57: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Types. . .Type modifiers. . .

� signed and unsigned, can be applied to integer types (char, int , long). . .

. . . an unsigned type is always non-negative;

. . . integer types are signed by default.

� const, identifies a variable that cannot be changed

c o n s t i n t DoesNotChange = 5 ;DoesNotChange = 6 ; /∗ E r r o r : w i l l not c o m p i l e ∗/

� volatile , refers to variables whose value may change in a mannerbeyond the normal control of the program (in multithreading).

Prof. Michele Loreti Language C: Introduction 24 / 76

Page 58: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Types. . .Type modifiers. . .

� signed and unsigned, can be applied to integer types (char, int , long). . .

. . . an unsigned type is always non-negative;

. . . integer types are signed by default.

� const, identifies a variable that cannot be changed

c o n s t i n t DoesNotChange = 5 ;DoesNotChange = 6 ; /∗ E r r o r : w i l l not c o m p i l e ∗/

� volatile , refers to variables whose value may change in a mannerbeyond the normal control of the program (in multithreading).

Prof. Michele Loreti Language C: Introduction 24 / 76

Page 59: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Types. . .Type modifiers. . .

� signed and unsigned, can be applied to integer types (char, int , long). . .

. . . an unsigned type is always non-negative;

. . . integer types are signed by default.

� const, identifies a variable that cannot be changed

c o n s t i n t DoesNotChange = 5 ;DoesNotChange = 6 ; /∗ E r r o r : w i l l not c o m p i l e ∗/

� volatile , refers to variables whose value may change in a mannerbeyond the normal control of the program (in multithreading).

Prof. Michele Loreti Language C: Introduction 24 / 76

Page 60: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Types. . .Type modifiers. . .

� signed and unsigned, can be applied to integer types (char, int , long). . .

. . . an unsigned type is always non-negative;

. . . integer types are signed by default.

� const, identifies a variable that cannot be changed

c o n s t i n t DoesNotChange = 5 ;DoesNotChange = 6 ; /∗ E r r o r : w i l l not c o m p i l e ∗/

� volatile , refers to variables whose value may change in a mannerbeyond the normal control of the program (in multithreading).

Prof. Michele Loreti Language C: Introduction 24 / 76

Page 61: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Types. . .Type modifiers. . .

� signed and unsigned, can be applied to integer types (char, int , long). . .

. . . an unsigned type is always non-negative;

. . . integer types are signed by default.

� const, identifies a variable that cannot be changed

c o n s t i n t DoesNotChange = 5 ;DoesNotChange = 6 ; /∗ E r r o r : w i l l not c o m p i l e ∗/

� volatile , refers to variables whose value may change in a mannerbeyond the normal control of the program (in multithreading).

Prof. Michele Loreti Language C: Introduction 24 / 76

Page 62: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Literals. . .

Integers can be a decimal, octal, or hexadecimal constant

� prefix 0x (or 0X) indicates hexadecimal;

� prefix 0 indicates octal;

� nothing for decima

A suffix that is a combination of u (or U) and l (or L), for unsigned andlong, respectively.

85 /∗ d e c i m a l ∗/0213 /∗ o c t a l ∗/0 x4b /∗ h e x a d e c i m a l ∗/30 /∗ i n t ∗/30u /∗ u n s i g n e d i n t ∗/30 l /∗ l o n g ∗/30 u l /∗ u n s i g n e d l o n g ∗/

Prof. Michele Loreti Language C: Introduction 25 / 76

Page 63: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Literals. . .

Integers can be a decimal, octal, or hexadecimal constant

� prefix 0x (or 0X) indicates hexadecimal;

� prefix 0 indicates octal;

� nothing for decima

A suffix that is a combination of u (or U) and l (or L), for unsigned andlong, respectively.

85 /∗ d e c i m a l ∗/0213 /∗ o c t a l ∗/0 x4b /∗ h e x a d e c i m a l ∗/30 /∗ i n t ∗/30u /∗ u n s i g n e d i n t ∗/30 l /∗ l o n g ∗/30 u l /∗ u n s i g n e d l o n g ∗/

Prof. Michele Loreti Language C: Introduction 25 / 76

Page 64: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Literals. . .

Integers can be a decimal, octal, or hexadecimal constant

� prefix 0x (or 0X) indicates hexadecimal;

� prefix 0 indicates octal;

� nothing for decima

A suffix that is a combination of u (or U) and l (or L), for unsigned andlong, respectively.

85 /∗ d e c i m a l ∗/0213 /∗ o c t a l ∗/0 x4b /∗ h e x a d e c i m a l ∗/30 /∗ i n t ∗/30u /∗ u n s i g n e d i n t ∗/30 l /∗ l o n g ∗/30 u l /∗ u n s i g n e d l o n g ∗/

Prof. Michele Loreti Language C: Introduction 25 / 76

Page 65: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Literals. . .

A floating-point literal can be represented either in decimal form orexponential form. It has:

� an integer part;

� a decimal point;� a fractional part;� and an exponent part.

Decimal form: the decimal point, the exponent, or both must be included;

Exponential form: integer part, the fractional part, or both must beinclude, the signed exponent is introduced by e or E.

Suffix f is used for literal of type float , while d is used for type double.

3.14159 /∗ L e g a l ∗/314159E−5L /∗ L e g a l ∗/510E /∗ I l l e g a l : i n c o m p l e t e exponent ∗/210 f /∗ I l l e g a l : no d e c i m a l o r exponent ∗/. e55 /∗ I l l e g a l : m i s s i n g i n t e g e r o r f r a c t i o n

Prof. Michele Loreti Language C: Introduction 26 / 76

Page 66: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Literals. . .

A floating-point literal can be represented either in decimal form orexponential form. It has:

� an integer part;� a decimal point;

� a fractional part;� and an exponent part.

Decimal form: the decimal point, the exponent, or both must be included;

Exponential form: integer part, the fractional part, or both must beinclude, the signed exponent is introduced by e or E.

Suffix f is used for literal of type float , while d is used for type double.

3.14159 /∗ L e g a l ∗/314159E−5L /∗ L e g a l ∗/510E /∗ I l l e g a l : i n c o m p l e t e exponent ∗/210 f /∗ I l l e g a l : no d e c i m a l o r exponent ∗/. e55 /∗ I l l e g a l : m i s s i n g i n t e g e r o r f r a c t i o n

Prof. Michele Loreti Language C: Introduction 26 / 76

Page 67: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Literals. . .

A floating-point literal can be represented either in decimal form orexponential form. It has:

� an integer part;� a decimal point;� a fractional part;

� and an exponent part.

Decimal form: the decimal point, the exponent, or both must be included;

Exponential form: integer part, the fractional part, or both must beinclude, the signed exponent is introduced by e or E.

Suffix f is used for literal of type float , while d is used for type double.

3.14159 /∗ L e g a l ∗/314159E−5L /∗ L e g a l ∗/510E /∗ I l l e g a l : i n c o m p l e t e exponent ∗/210 f /∗ I l l e g a l : no d e c i m a l o r exponent ∗/. e55 /∗ I l l e g a l : m i s s i n g i n t e g e r o r f r a c t i o n

Prof. Michele Loreti Language C: Introduction 26 / 76

Page 68: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Literals. . .

A floating-point literal can be represented either in decimal form orexponential form. It has:

� an integer part;� a decimal point;� a fractional part;� and an exponent part.

Decimal form: the decimal point, the exponent, or both must be included;

Exponential form: integer part, the fractional part, or both must beinclude, the signed exponent is introduced by e or E.

Suffix f is used for literal of type float , while d is used for type double.

3.14159 /∗ L e g a l ∗/314159E−5L /∗ L e g a l ∗/510E /∗ I l l e g a l : i n c o m p l e t e exponent ∗/210 f /∗ I l l e g a l : no d e c i m a l o r exponent ∗/. e55 /∗ I l l e g a l : m i s s i n g i n t e g e r o r f r a c t i o n

Prof. Michele Loreti Language C: Introduction 26 / 76

Page 69: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Literals. . .

A floating-point literal can be represented either in decimal form orexponential form. It has:

� an integer part;� a decimal point;� a fractional part;� and an exponent part.

Decimal form: the decimal point, the exponent, or both must be included;

Exponential form: integer part, the fractional part, or both must beinclude, the signed exponent is introduced by e or E.

Suffix f is used for literal of type float , while d is used for type double.

3.14159 /∗ L e g a l ∗/314159E−5L /∗ L e g a l ∗/510E /∗ I l l e g a l : i n c o m p l e t e exponent ∗/210 f /∗ I l l e g a l : no d e c i m a l o r exponent ∗/. e55 /∗ I l l e g a l : m i s s i n g i n t e g e r o r f r a c t i o n

Prof. Michele Loreti Language C: Introduction 26 / 76

Page 70: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Literals. . .

A floating-point literal can be represented either in decimal form orexponential form. It has:

� an integer part;� a decimal point;� a fractional part;� and an exponent part.

Decimal form: the decimal point, the exponent, or both must be included;

Exponential form: integer part, the fractional part, or both must beinclude, the signed exponent is introduced by e or E.

Suffix f is used for literal of type float , while d is used for type double.

3.14159 /∗ L e g a l ∗/314159E−5L /∗ L e g a l ∗/510E /∗ I l l e g a l : i n c o m p l e t e exponent ∗/210 f /∗ I l l e g a l : no d e c i m a l o r exponent ∗/. e55 /∗ I l l e g a l : m i s s i n g i n t e g e r o r f r a c t i o n

Prof. Michele Loreti Language C: Introduction 26 / 76

Page 71: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Literals. . .

A floating-point literal can be represented either in decimal form orexponential form. It has:

� an integer part;� a decimal point;� a fractional part;� and an exponent part.

Decimal form: the decimal point, the exponent, or both must be included;

Exponential form: integer part, the fractional part, or both must beinclude, the signed exponent is introduced by e or E.

Suffix f is used for literal of type float , while d is used for type double.

3.14159 /∗ L e g a l ∗/314159E−5L /∗ L e g a l ∗/510E /∗ I l l e g a l : i n c o m p l e t e exponent ∗/210 f /∗ I l l e g a l : no d e c i m a l o r exponent ∗/. e55 /∗ I l l e g a l : m i s s i n g i n t e g e r o r f r a c t i o n

Prof. Michele Loreti Language C: Introduction 26 / 76

Page 72: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Literals. . .

A floating-point literal can be represented either in decimal form orexponential form. It has:

� an integer part;� a decimal point;� a fractional part;� and an exponent part.

Decimal form: the decimal point, the exponent, or both must be included;

Exponential form: integer part, the fractional part, or both must beinclude, the signed exponent is introduced by e or E.

Suffix f is used for literal of type float , while d is used for type double.

3.14159 /∗ L e g a l ∗/314159E−5L /∗ L e g a l ∗/510E /∗ I l l e g a l : i n c o m p l e t e exponent ∗/210 f /∗ I l l e g a l : no d e c i m a l o r exponent ∗/. e55 /∗ I l l e g a l : m i s s i n g i n t e g e r o r f r a c t i o n

Prof. Michele Loreti Language C: Introduction 26 / 76

Page 73: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Literals. . .

A floating-point literal can be represented either in decimal form orexponential form. It has:

� an integer part;� a decimal point;� a fractional part;� and an exponent part.

Decimal form: the decimal point, the exponent, or both must be included;

Exponential form: integer part, the fractional part, or both must beinclude, the signed exponent is introduced by e or E.

Suffix f is used for literal of type float , while d is used for type double.

3.14159 /∗ L e g a l ∗/314159E−5L /∗ L e g a l ∗/510E /∗ I l l e g a l : i n c o m p l e t e exponent ∗/210 f /∗ I l l e g a l : no d e c i m a l o r exponent ∗/. e55 /∗ I l l e g a l : m i s s i n g i n t e g e r o r f r a c t i o n

Prof. Michele Loreti Language C: Introduction 26 / 76

Page 74: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Literals. . .

A floating-point literal can be represented either in decimal form orexponential form. It has:

� an integer part;� a decimal point;� a fractional part;� and an exponent part.

Decimal form: the decimal point, the exponent, or both must be included;

Exponential form: integer part, the fractional part, or both must beinclude, the signed exponent is introduced by e or E.

Suffix f is used for literal of type float , while d is used for type double.

3.14159 /∗ L e g a l ∗/314159E−5L /∗ L e g a l ∗/510E /∗ I l l e g a l : i n c o m p l e t e exponent ∗/210 f /∗ I l l e g a l : no d e c i m a l o r exponent ∗/. e55 /∗ I l l e g a l : m i s s i n g i n t e g e r o r f r a c t i o n

Prof. Michele Loreti Language C: Introduction 26 / 76

Page 75: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Symbolic Constants. . .

Symbolic constants represent constant values, from the set of constanttypes, by a symbolic name:

#d e f i n e BLOCK SIZE 100#d e f i n e TRACK SIZE (16∗BLOCK SIZE )#d e f i n e HELLO ” H e l l o World\n”#d e f i n e EXP 2.7183

NB: #define, like #include, is a preprocessor command!

Another form of symbolic constant is an enumeration, which is a list ofconstant integer values:

enum D i r e c t i o n { NORTH, SOUTH, EAST , WEST } ;

Symbolic constants and enumerations are by convention given uppercasenames.

Prof. Michele Loreti Language C: Introduction 27 / 76

Page 76: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Symbolic Constants. . .

Symbolic constants represent constant values, from the set of constanttypes, by a symbolic name:

#d e f i n e BLOCK SIZE 100#d e f i n e TRACK SIZE (16∗BLOCK SIZE )#d e f i n e HELLO ” H e l l o World\n”#d e f i n e EXP 2.7183

NB: #define, like #include, is a preprocessor command!

Another form of symbolic constant is an enumeration, which is a list ofconstant integer values:

enum D i r e c t i o n { NORTH, SOUTH, EAST , WEST } ;

Symbolic constants and enumerations are by convention given uppercasenames.

Prof. Michele Loreti Language C: Introduction 27 / 76

Page 77: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Symbolic Constants. . .

Symbolic constants represent constant values, from the set of constanttypes, by a symbolic name:

#d e f i n e BLOCK SIZE 100#d e f i n e TRACK SIZE (16∗BLOCK SIZE )#d e f i n e HELLO ” H e l l o World\n”#d e f i n e EXP 2.7183

NB: #define, like #include, is a preprocessor command!

Another form of symbolic constant is an enumeration, which is a list ofconstant integer values:

enum D i r e c t i o n { NORTH, SOUTH, EAST , WEST } ;

Symbolic constants and enumerations are by convention given uppercasenames.

Prof. Michele Loreti Language C: Introduction 27 / 76

Page 78: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Symbolic Constants. . .

Symbolic constants represent constant values, from the set of constanttypes, by a symbolic name:

#d e f i n e BLOCK SIZE 100#d e f i n e TRACK SIZE (16∗BLOCK SIZE )#d e f i n e HELLO ” H e l l o World\n”#d e f i n e EXP 2.7183

NB: #define, like #include, is a preprocessor command!

Another form of symbolic constant is an enumeration, which is a list ofconstant integer values:

enum D i r e c t i o n { NORTH, SOUTH, EAST , WEST } ;

Symbolic constants and enumerations are by convention given uppercasenames.

Prof. Michele Loreti Language C: Introduction 27 / 76

Page 79: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Declarations. . .

All variables must be declared before they are used.

The general form of a declaration6 is

<q u a l i f i e r > <type> < i d e n t i f i e r 1 > = <v a l u e 1 >, < i d e n t i f i e r 2 > =<v a l u e 2 >, . . . ;

where the assignment to an initial value is optional.

i n t lower , upper , s t e p ; /∗ 3 u n i n i t i a l i s e d i n t s ∗/c h a r tab = ’ \ t ’ ; /∗ a c h a r i n i t i a l i s e d w i t h ’\ t ’ ∗/c h a r buf [ 1 0 ] ; /∗ an u n i n i t i a l i s e d a r r a y o f c h a r s ∗/i n t m = 2+3+4; /∗ c o n s t a n t e x p r e s s i o n : 9 ∗/i n t n = m + 5 ; /∗ i n i t i a l i s e d w i t h 9+5 = 14 ∗/f l o a t l i m i t = 9 . 3 4 f ;c o n s t d o u b l e PI = 3 . 1 4 1 6 ;

Prof. Michele Loreti Language C: Introduction 28 / 76

Page 80: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Declarations. . .

All variables must be declared before they are used.

The general form of a declaration6 is

<q u a l i f i e r > <type> < i d e n t i f i e r 1 > = <v a l u e 1 >, < i d e n t i f i e r 2 > =<v a l u e 2 >, . . . ;

where the assignment to an initial value is optional.

i n t lower , upper , s t e p ; /∗ 3 u n i n i t i a l i s e d i n t s ∗/c h a r tab = ’ \ t ’ ; /∗ a c h a r i n i t i a l i s e d w i t h ’\ t ’ ∗/c h a r buf [ 1 0 ] ; /∗ an u n i n i t i a l i s e d a r r a y o f c h a r s ∗/i n t m = 2+3+4; /∗ c o n s t a n t e x p r e s s i o n : 9 ∗/i n t n = m + 5 ; /∗ i n i t i a l i s e d w i t h 9+5 = 14 ∗/f l o a t l i m i t = 9 . 3 4 f ;c o n s t d o u b l e PI = 3 . 1 4 1 6 ;

Prof. Michele Loreti Language C: Introduction 28 / 76

Page 81: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Declarations. . .

All variables must be declared before they are used.

The general form of a declaration6 is

<q u a l i f i e r > <type> < i d e n t i f i e r 1 > = <v a l u e 1 >, < i d e n t i f i e r 2 > =<v a l u e 2 >, . . . ;

where the assignment to an initial value is optional.

i n t lower , upper , s t e p ; /∗ 3 u n i n i t i a l i s e d i n t s ∗/c h a r tab = ’ \ t ’ ; /∗ a c h a r i n i t i a l i s e d w i t h ’\ t ’ ∗/c h a r buf [ 1 0 ] ; /∗ an u n i n i t i a l i s e d a r r a y o f c h a r s ∗/i n t m = 2+3+4; /∗ c o n s t a n t e x p r e s s i o n : 9 ∗/i n t n = m + 5 ; /∗ i n i t i a l i s e d w i t h 9+5 = 14 ∗/f l o a t l i m i t = 9 . 3 4 f ;c o n s t d o u b l e PI = 3 . 1 4 1 6 ;

Prof. Michele Loreti Language C: Introduction 28 / 76

Page 82: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Arithmetic operators. . .

Operator name Syntax

Basic assignment a = b

Addition a + b

Subtraction a − b

Unary plus (integer promotion) +a

Unary minus (additive inverse) −a

Multiplication a ∗ b

Division a / b

Modulo (integer remainder) a % b

Increment Prefix ++a

Increment Postfix a++

Decrement Postfix a++

Decrement Postfix a−−

Prof. Michele Loreti Language C: Introduction 29 / 76

Page 83: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Relational and logical operators. . .

Operator name Syntax

Equal to a == b

Not equal to a != b

Greater than a > b

Less than a < b

Greater than or equal to a >= b

Less than or equal to a <= b

Operator name Syntax

Logical negation (NOT) !a

Logical AND a && b

Logical OR a || b

Prof. Michele Loreti Language C: Introduction 30 / 76

Page 84: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Bitwise operators. . .

Operator name Syntax

Bitwise NOT ˜a

Bitwise AND a & b

Bitwise OR a | b

Bitwise XOR a ˆ b

Bitwise left shift a << b

Bitwise right shift a >> b

Prof. Michele Loreti Language C: Introduction 31 / 76

Page 85: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Compound assignment operators. . .

Operator name Syntax Meaning

Addition assignment a += b a = a + b

Subtraction assignment a −= b a = a − b

Multiplication assignment a ∗= b a = a ∗ b

Division assignment a /= b a = a / b

Modulo assignment a %= b a = a % b

Bitwise AND assignment a &= b a and eq b

Bitwise OR assignment a |= b a or eq b

Bitwise XOR assignment a ˆ= b a xor eq b

Bitwise left shift assignment a <<= b a = a << b

Bitwise right shift assignment a >>= b a = a >> b

Prof. Michele Loreti Language C: Introduction 32 / 76

Page 86: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Branching and iterations. . .

We use statement to indicate a single C instruction or a block (a sequenceof statements grouped by { and }).

� If-Then-Else:

i f ( e x p r e s s i o n ) s t a t e m e n te l s e s t a t e m e n t

� Conditional expression: ( expression1 ?expresion2 : expression3 )

� Switch:

s w i t c h ( e x p r e s s i o n ) {c a s e cont−i n t−e x p r : s t a t m e n tc a s e cont−i n t−e x p r : s t a t m e n t. . .d e f a u l t : s t a t e m e n t s

}

Prof. Michele Loreti Language C: Introduction 33 / 76

Page 87: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Branching and iterations. . .

We use statement to indicate a single C instruction or a block (a sequenceof statements grouped by { and }).

� If-Then-Else:

i f ( e x p r e s s i o n ) s t a t e m e n te l s e s t a t e m e n t

� Conditional expression: ( expression1 ?expresion2 : expression3 )

� Switch:

s w i t c h ( e x p r e s s i o n ) {c a s e cont−i n t−e x p r : s t a t m e n tc a s e cont−i n t−e x p r : s t a t m e n t. . .d e f a u l t : s t a t e m e n t s

}

Prof. Michele Loreti Language C: Introduction 33 / 76

Page 88: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Branching and iterations. . .

We use statement to indicate a single C instruction or a block (a sequenceof statements grouped by { and }).

� If-Then-Else:

i f ( e x p r e s s i o n ) s t a t e m e n te l s e s t a t e m e n t

� Conditional expression: ( expression1 ?expresion2 : expression3 )

� Switch:

s w i t c h ( e x p r e s s i o n ) {c a s e cont−i n t−e x p r : s t a t m e n tc a s e cont−i n t−e x p r : s t a t m e n t. . .d e f a u l t : s t a t e m e n t s

}

Prof. Michele Loreti Language C: Introduction 33 / 76

Page 89: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Branching and iterations. . .

� While Loops:

w h i l e ( e x p r e s s i o n )s t a t e m e n t

dos t a t e m e n t

w h i l e ( e x p r e s s i o n ) ;

� For Loops:

f o r ( e x p r 1 ; e x p r 2 ; e x p r 3 )s t a t e m e n t

Prof. Michele Loreti Language C: Introduction 34 / 76

Page 90: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Branching and iterations. . .

� While Loops:

w h i l e ( e x p r e s s i o n )s t a t e m e n t

dos t a t e m e n t

w h i l e ( e x p r e s s i o n ) ;

� For Loops:

f o r ( e x p r 1 ; e x p r 2 ; e x p r 3 )s t a t e m e n t

Prof. Michele Loreti Language C: Introduction 34 / 76

Page 91: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Functions. . .

In C functions represent the basic building blocks of computations.

Function Declarations provide info (used by the compiler) about theprototype of a function:

1. function’s name;

2. return type;

3. types of parameters.

Function Definitions provide the actual body of the declared functions.

Each function must be declared before its definition!

Typically declarations are placed in myfile .h file, this is included in thecorresponding myfile .c where function definitions are placed.

Prof. Michele Loreti Language C: Introduction 35 / 76

Page 92: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Functions. . .

In C functions represent the basic building blocks of computations.

Function Declarations provide info (used by the compiler) about theprototype of a function:

1. function’s name;

2. return type;

3. types of parameters.

Function Definitions provide the actual body of the declared functions.

Each function must be declared before its definition!

Typically declarations are placed in myfile .h file, this is included in thecorresponding myfile .c where function definitions are placed.

Prof. Michele Loreti Language C: Introduction 35 / 76

Page 93: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Functions. . .

In C functions represent the basic building blocks of computations.

Function Declarations provide info (used by the compiler) about theprototype of a function:

1. function’s name;

2. return type;

3. types of parameters.

Function Definitions provide the actual body of the declared functions.

Each function must be declared before its definition!

Typically declarations are placed in myfile .h file, this is included in thecorresponding myfile .c where function definitions are placed.

Prof. Michele Loreti Language C: Introduction 35 / 76

Page 94: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Functions. . .

In C functions represent the basic building blocks of computations.

Function Declarations provide info (used by the compiler) about theprototype of a function:

1. function’s name;

2. return type;

3. types of parameters.

Function Definitions provide the actual body of the declared functions.

Each function must be declared before its definition!

Typically declarations are placed in myfile .h file, this is included in thecorresponding myfile .c where function definitions are placed.

Prof. Michele Loreti Language C: Introduction 35 / 76

Page 95: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Functions. . .

In C functions represent the basic building blocks of computations.

Function Declarations provide info (used by the compiler) about theprototype of a function:

1. function’s name;

2. return type;

3. types of parameters.

Function Definitions provide the actual body of the declared functions.

Each function must be declared before its definition!

Typically declarations are placed in myfile .h file, this is included in thecorresponding myfile .c where function definitions are placed.

Prof. Michele Loreti Language C: Introduction 35 / 76

Page 96: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Functions. . .

In C functions represent the basic building blocks of computations.

Function Declarations provide info (used by the compiler) about theprototype of a function:

1. function’s name;

2. return type;

3. types of parameters.

Function Definitions provide the actual body of the declared functions.

Each function must be declared before its definition!

Typically declarations are placed in myfile .h file, this is included in thecorresponding myfile .c where function definitions are placed.

Prof. Michele Loreti Language C: Introduction 35 / 76

Page 97: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Functions. . .

In C functions represent the basic building blocks of computations.

Function Declarations provide info (used by the compiler) about theprototype of a function:

1. function’s name;

2. return type;

3. types of parameters.

Function Definitions provide the actual body of the declared functions.

Each function must be declared before its definition!

Typically declarations are placed in myfile .h file, this is included in thecorresponding myfile .c where function definitions are placed.

Prof. Michele Loreti Language C: Introduction 35 / 76

Page 98: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Functions. . .

In C functions represent the basic building blocks of computations.

Function Declarations provide info (used by the compiler) about theprototype of a function:

1. function’s name;

2. return type;

3. types of parameters.

Function Definitions provide the actual body of the declared functions.

Each function must be declared before its definition!

Typically declarations are placed in myfile .h file, this is included in thecorresponding myfile .c where function definitions are placed.

Prof. Michele Loreti Language C: Introduction 35 / 76

Page 99: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Functions: An example. . .

#i n c l u d e <s t d i o . h>

i n t f a c t o r i a l ( i n t ) ;

i n t f a c t o r i a l ( i n t n ) {i f ( n>0) {

r e t u r n n∗ f a c t o r i a l ( n−1) ;} e l s e {

r e t u r n 1 ;}

}

i n t main ( v o i d ) {p r i n t f ( ”!%d=%d\n” , 5 , f a c t o r i a l ( 5 ) ) ;

}

Prof. Michele Loreti Language C: Introduction 36 / 76

Page 100: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Handling errors. . .

To handle errors C standard library provides the macro assert .

This macro can be used to che if a give condition is satisfied or not.

i n t f a c t o r i a l ( i n t n ) {a s s e r t ( n>=0) ; //<−− NEW!i f ( n>0) {

r e t u r n n∗ f a c t o r i a l ( n−1) ;} e l s e {

r e t u r n 1 ;}

}

Prof. Michele Loreti Language C: Introduction 37 / 76

Page 101: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Handling errors. . .

To handle errors C standard library provides the macro assert .

This macro can be used to che if a give condition is satisfied or not.

i n t f a c t o r i a l ( i n t n ) {a s s e r t ( n>=0) ; //<−− NEW!i f ( n>0) {

r e t u r n n∗ f a c t o r i a l ( n−1) ;} e l s e {

r e t u r n 1 ;}

}

Prof. Michele Loreti Language C: Introduction 37 / 76

Page 102: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Handling errors. . .

To handle errors C standard library provides the macro assert . This macrocan be used to che if a give condition is satisfied or not.

i n t f a c t o r i a l ( i n t n ) {a s s e r t ( n>=0) ; //<−− NEW!i f ( n>0) {

r e t u r n n∗ f a c t o r i a l ( n−1) ;} e l s e {

r e t u r n 1 ;}

}

To use the macro, #include <assert.h> must be used at the beginning ofyour program.

Prof. Michele Loreti Language C: Introduction 38 / 76

Page 103: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Handling errors. . .

To handle errors C standard library provides the macro assert . This macrocan be used to che if a give condition is satisfied or not.

i n t f a c t o r i a l ( i n t n ) {a s s e r t ( n>=0) ; //<−− NEW!i f ( n>0) {

r e t u r n n∗ f a c t o r i a l ( n−1) ;} e l s e {

r e t u r n 1 ;}

}

To use the macro, #include <assert.h> must be used at the beginning ofyour program.

Prof. Michele Loreti Language C: Introduction 38 / 76

Page 104: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

Handling errors. . .

To handle errors C standard library provides the macro assert . This macrocan be used to che if a give condition is satisfied or not.

i n t f a c t o r i a l ( i n t n ) {a s s e r t ( n>=0) ; //<−− NEW!i f ( n>0) {

r e t u r n n∗ f a c t o r i a l ( n−1) ;} e l s e {

r e t u r n 1 ;}

}

To use the macro, #include <assert.h> must be used at the beginning ofyour program.

Prof. Michele Loreti Language C: Introduction 38 / 76

Page 105: Linux System Programming: Introductiondidattica.cs.unicam.it/lib/exe/fetch.php?media=didattica:triennale:so:... · In addition to standard C library, the GNU C library provides wrappers

C Standard Library. . .

The standard library has a large number of functions (about 145) whichprovide many commonly- used routines and operations.

� Mathematical functions: sqrt , pow, sin , cos, tan.

� Manipulating characters. isdigit , isalpha , isspace , toupper, tolower.

� Manipulating strings. strlen , strcpy , strcmp, strcat , strstr , strtok .

� Formatted input and output. printf , scanf, sprintf , sscanf.

� File input and output. fopen, fclose , fgets , getchar, fseek.

� Error handling. assert , exit .

� Time and date functions. clock, time, difftime .

� Sort and search. qsort, bsearch.

� Low-level memory operations. memcpy, memset.

Prof. Michele Loreti Language C: Introduction 39 / 76