(char)i - Dipartimento di Ingegneria informatica, automatica e ...
(char)i - Dipartimento di Ingegneria informatica, automatica e ...
(char)i - Dipartimento di Ingegneria informatica, automatica e ...
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Char<br />
Corso <strong>di</strong> Fondamenti <strong>di</strong> Informatica<br />
<strong>Ingegneria</strong> delle Comunicazioni BCOR<br />
<strong>Ingegneria</strong> Elettronica BELR<br />
Parte 2<br />
Domenico Daniele Bloisi
Docenti<br />
Parte I – prof. Silvio Salza<br />
salza@<strong>di</strong>s.uniroma1.it<br />
http://www.<strong>di</strong>s.uniroma1.it/~salza/fondamenti.htm<br />
Parte II – ing. Domenico Daniele Bloisi<br />
bloisi@<strong>di</strong>s.uniroma1.it<br />
http://www.<strong>di</strong>s.uniroma1.it/~bloisi/<strong>di</strong>dattica/fon<strong>di</strong>nf1011.html<br />
Nota: %7E corrisponde alla tilde ~<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 2
Informazioni Generali<br />
ing. Domenico Daniele Bloisi, PhD<br />
<strong>Dipartimento</strong> <strong>di</strong> Informatica e Sistemistica<br />
Via Ariosto 25<br />
(a<strong>di</strong>acente Piazza Dante,<br />
A fermate Manzoni, Vittorio Emanuele,<br />
Tram 3 fermata via Labicana)<br />
mailto:bloisi@<strong>di</strong>s.uniroma1.it<br />
http://www.<strong>di</strong>s.uniroma1.it/~bloisi<br />
Char<br />
Parte 2<br />
2010/2011 Pagina 3
Ricevimento<br />
Mercoledì 15.00 – 17.00<br />
DIS via Ariosto 25<br />
Aula docenti a<strong>di</strong>acente aula A4<br />
Si consiglia <strong>di</strong> inviare una email per conferma e<br />
<strong>di</strong> controllare la bacheca degli avvisi<br />
http://www.<strong>di</strong>s.uniroma1.it/~bloisi/<strong>di</strong>dattica/fon<strong>di</strong>nf1011.html#Avvisi<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 4
Sommario – Parte 2<br />
• Variabili<br />
• Assegnazione<br />
• Tipi <strong>di</strong> dato interi<br />
• Lettura e scrittura <strong>di</strong> interi<br />
• Tipi <strong>di</strong> dato reali<br />
• Precisione nelle rappresentazioni<br />
• Conversioni <strong>di</strong> tipo<br />
• Tipo <strong>di</strong> dato primitivo caratteri: <strong>char</strong><br />
• Insieme dei valori rappresentabili e la clausola<br />
unsigned<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 5
Precisione nella rappresentazione<br />
Non tutti i numeri compresi tra −3.4028235 · 10 +38 e +3.4028235 · 10 +38<br />
sono rappresentabili come float (considerazioni simili valgono<br />
anche per i double).<br />
I numeri rappresentabili come float sono tanto più vicini tra loro<br />
quanto più sono vicini allo zero. Inoltre, essi sono tanto più lontani<br />
fra loro quanto più ci si avvicina al massimo valore rappresentabile<br />
(in valore assoluto)<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 6
Esempio 1<br />
float x = 1222333444.0f;<br />
printf("x = %f\n", x);<br />
x += 1.0;<br />
printf("x + 1 = %f\n", x);<br />
visualizza<br />
x = 1222333440.000000<br />
x + 1 = 1222333440.000000<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 7
Esempio 2<br />
int j = 1222333444;<br />
printf("j = %d\n", j);<br />
j += 1;<br />
printf("j + 1 = %d\n", j);<br />
visualizza<br />
j = 1222333444<br />
j + 1 = 1222333445<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 8
Precisione nelle misure<br />
La precisione del risultato <strong>di</strong> una operazione<br />
<strong>di</strong>pende dalla precisione con cui si conoscono<br />
i dati.<br />
9.2 * 5.3 = 48.76<br />
(la seconda cifra decimale non è significativa)<br />
9.25 * 5.35 = 49.48<br />
(qui lo è)<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 9
Conversione <strong>di</strong> tipi<br />
E’ possibile trasformare un tipo <strong>di</strong> dato in un<br />
altro attraverso una conversione<br />
Una conversione può essere:<br />
• implicita (detta anche promozione) quando<br />
viene effettuata dal compilatore per assicurare<br />
che gli operan<strong>di</strong> siano dello stesso tipo.<br />
• esplicita quando viene effettuata dal<br />
programmatore tramite l’operatore unario cast<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 10
Gerarchia <strong>di</strong> promozione<br />
Tipo superiore<br />
promozione<br />
Tipo inferiore<br />
Char<br />
Parte 2<br />
long double<br />
double<br />
float<br />
unsigned long<br />
long<br />
unsigned int<br />
int<br />
short<br />
<strong>char</strong><br />
2010/2011<br />
Per<strong>di</strong>ta <strong>di</strong><br />
precisione<br />
Pagina 11
Conversione <strong>di</strong> tipo implicita -<br />
operatori binari (1/2)<br />
Nel caso <strong>di</strong> operatori binari utilizzati con due operan<strong>di</strong> <strong>di</strong><br />
tipo <strong>di</strong>verso: uno dei due tipi (inferiore) viene promosso<br />
all'altro (superiore) ed il risultato è del tipo superiore<br />
ESEMPIO <strong>di</strong> regola (informale)<br />
oper1 * oper2: se oper1 è double e oper2 è float,<br />
oper2 promosso a double e risultato è double<br />
a = c + b;<br />
Char<br />
Parte 2<br />
float<br />
double<br />
double<br />
2010/2011<br />
Pagina 12
Conversione <strong>di</strong> tipo implicita -<br />
operatori binari (2/2)<br />
Attenzione<br />
Se a e b sono float e c double<br />
a = b + c;<br />
assegnerà il valore double risultato della somma ad una<br />
variabile float con potenziale per<strong>di</strong>ta <strong>di</strong> informazione<br />
Infatti, in un assegnamento il tipo della variabile<br />
destinazione definisce il tipo finale<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 13
Conversione <strong>di</strong> tipo implicita -<br />
chiamata <strong>di</strong> funzione (1/2)<br />
Gli argomenti passati ad una funzione <strong>di</strong>chiarata<br />
con prototipo vengono convertiti secondo quanto<br />
specificato nella <strong>di</strong>chiarazione con potenziale<br />
per<strong>di</strong>ta <strong>di</strong> informazione<br />
ESEMPIO<br />
se gli argomenti formali sono float e int, una<br />
chiamata con argomenti attuali int e float<br />
provoca una potenziale per<strong>di</strong>ta <strong>di</strong> informazione<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 14
Conversione <strong>di</strong> tipo implicita -<br />
chiamata <strong>di</strong> funzione (2/2)<br />
ESEMPIO<br />
. . .<br />
int f(float, int);<br />
int main() {<br />
int a; float b;<br />
f(a,b);<br />
. . .<br />
}<br />
int f(float p1, int p2) {<br />
. . .<br />
}<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 15
Conversione <strong>di</strong> tipo implicita<br />
return (1/2)<br />
In return espressione; il tipo assegnato al<br />
valore <strong>di</strong> espressione è quello specificato nella<br />
<strong>di</strong>chiarazione della funzione (potenziale per<strong>di</strong>ta <strong>di</strong><br />
informazione)<br />
ESEMPIO<br />
Siano b <strong>di</strong> tipo float e c int, il tipo <strong>di</strong> ritorno int<br />
return b * c ;<br />
convertirà il risultato float del prodotto in un int<br />
con potenziale per<strong>di</strong>ta <strong>di</strong> informazione<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 16
Conversione <strong>di</strong> tipo implicita<br />
return (2/2)<br />
ESEMPIO<br />
int f() {<br />
float b;<br />
int c;<br />
. . .<br />
return b * c;<br />
}<br />
Char<br />
Parte 2<br />
ATTENZIONE<br />
potenziale per<strong>di</strong>ta <strong>di</strong><br />
informazione<br />
2010/2011<br />
Pagina 17
Conversione <strong>di</strong> tipo esplicita (cast)<br />
Per convertire esplicitamente dati da un tipo ad un altro si<br />
deve effettuare un’operazione <strong>di</strong> cast.<br />
Sintassi:<br />
(tipo) espressione;<br />
• tipo è il nome <strong>di</strong> un tipo<br />
• espressione è una espressione il cui tipo verrà forzato a<br />
tipo<br />
Semantica:<br />
Converte il tipo <strong>di</strong> una espressione ad un altro tipo in<br />
modo da rendere possibili operazioni che coinvolgono<br />
tipi altrimenti incompatibili.<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 18
Esempio: cast<br />
int a = (int) 3.75; // cast a int<br />
// dell’espressione 3.75<br />
// (<strong>di</strong> tipo double)<br />
printf("%d", a); // stampa 3<br />
Effettuando un cast, il risultato potrebbe essere affetto da<br />
per<strong>di</strong>ta <strong>di</strong> precisione. Nell’esempio precedente 3.75 viene<br />
troncato a 3.<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 19
Assegnazioni fra tipi <strong>di</strong> dato primitivi<br />
numerici <strong>di</strong>versi<br />
Un valore <strong>di</strong> un tipo <strong>di</strong> dato non va assegnato ad una<br />
variabile <strong>di</strong> un tipo <strong>di</strong> dato con minor <strong>di</strong>mensione,<br />
altrimenti si rischia per<strong>di</strong>ta <strong>di</strong> precisione dell'informazione.<br />
Esempio<br />
int a; long b; a = b;<br />
un valore long non va assegnato ad una variabile int<br />
int a; float b; a = b;<br />
un valore float non va assegnato ad una variabile int<br />
Nota: il compilatore gcc non segnala alcun<br />
errore<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 20
Esempio<br />
#include <br />
int main () {<br />
long b = 90000L;<br />
short a = (short) 3.75; // cast a short<br />
// dell’espressione 3.75<br />
// (<strong>di</strong> tipo double)<br />
printf("%hd\n", a);<br />
a = b; //assegno long a short<br />
printf("%hd\n", a);<br />
printf("%ld\n", b);<br />
return 0;<br />
}<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 21
Risultato<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 22
Tipo <strong>char</strong><br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 23
Dominio ASCII<br />
Una variabile <strong>di</strong> tipo <strong>char</strong> può contenere un carattere<br />
alfanumerico o un simbolo.<br />
Lo standard ASCII stabilisce una corrispondenza tra<br />
numeri e simboli alfabetici, numerici o simboli speciali in<br />
varie lingue.<br />
Ad esempio, il carattere 'A' corrisponde al co<strong>di</strong>ce<br />
numerico 65, il carattere 'B' al co<strong>di</strong>ce numerico 66, ecc.<br />
Per maggiori dettagli sullo standard ASCII,<br />
si veda ad esempio il sito web: www.asciitable.com<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 24
Tavola ASCII<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 25
Esempio: <strong>char</strong><br />
Una variabile <strong>di</strong> tipo <strong>char</strong> può contenere un<br />
carattere alfanumerico o un simbolo.<br />
I letterali del tipo <strong>char</strong> possono essere denotati<br />
tramite gli apici''.<br />
Esempio:<br />
Non si tratta del numero zero,<br />
ma del carattere zero (48 ASCII)<br />
<strong>char</strong> c = 'A'; <strong>char</strong> c = '0';<br />
<strong>char</strong> c = 67; //corrisponde alla C in ASCII<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 26
Operazioni sui <strong>char</strong> (1/3)<br />
Conversione da <strong>char</strong> a int, che corrisponde al calcolare il<br />
co<strong>di</strong>ce ASCII <strong>di</strong> un carattere:<br />
<strong>char</strong> c = 'A';<br />
int i = (int)c; // i contiene il co<strong>di</strong>ce ASCII<br />
// del carattere 'A'<br />
printf("%d\n", i); // stampa 65<br />
Char<br />
Parte 2<br />
Domanda: Cosa stampa<br />
printf("%c\n", i);<br />
?<br />
2010/2011<br />
Pagina 27
Operazioni sui <strong>char</strong> (2/3)<br />
Conversione da int a <strong>char</strong>, che corrisponde a creare un<br />
carattere a partire dal suo co<strong>di</strong>ce ASCII:<br />
int i = 65; // co<strong>di</strong>ce ASCII del carattere 'A'<br />
<strong>char</strong> c = (<strong>char</strong>)i;<br />
printf("%c\n", c); // stampa 'A'<br />
Char<br />
Parte 2<br />
Domanda: Cosa stampa<br />
int i = 1065;<br />
<strong>char</strong> c = (<strong>char</strong>)i;<br />
printf("%c\n", c);<br />
?<br />
2010/2011<br />
Pagina 28
Operazioni sui <strong>char</strong> (3/3)<br />
Conversioni carattere/intero<br />
Esempio 1<br />
conversione da carattere a intero corrispondente<br />
<strong>char</strong> c = '5';<br />
int i = (int)c - (int)'0'; // i = 53 - 48<br />
printf("%d\n", i); // stampa 5<br />
Esempio 2<br />
conversione da intero a carattere corrispondente<br />
int i = 5;<br />
<strong>char</strong> c = (<strong>char</strong>)(i + (int)'0'); // c = 5 + 48<br />
printf("%c\n", c); // stampa 5 (53 ASCII)<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 29
Domande Vero / Falso<br />
<strong>char</strong> c = 48; /*1*/<br />
int i = 2; /*2*/<br />
printf("%c\n", c); /*3*/<br />
printf("%d\n", i); /*4*/<br />
printf("%d\n", c); /*5*/<br />
printf("%c %d\n", c, c + '2'); /*6*/<br />
printf("%d\n", '2' + c); /*7*/<br />
Sapendo che il co<strong>di</strong>ce ASCII <strong>di</strong> zero è 48 e che il co<strong>di</strong>ce ASCII <strong>di</strong><br />
due è 50, rispondere con vero o falso alle seguenti affermazioni<br />
1. La riga 3 stampa 0<br />
2. La riga 4 stampa 50<br />
3. La riga 5 stampa 48<br />
4. La riga 6 stampa 48 98<br />
5. La riga 7 stampa 2<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 30
Esercizio<br />
Esercizio 2.7<br />
Si scriva un programma C in grado <strong>di</strong> visualizzare<br />
tutte le cifre del co<strong>di</strong>ce ASCII corrispondenti alle<br />
lettere del proprio nome.<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 31
Soluzione: Erika<br />
#include <br />
int main()<br />
{<br />
<strong>char</strong> c = 'E';<br />
printf("%c corrisponde a %d\n", c, c);<br />
c = 'r';<br />
printf("%c corrisponde a %d\n", c, c);<br />
c = 'i';<br />
printf("%c corrisponde a %d\n", c, c);<br />
}<br />
c = 'k';<br />
printf("%c corrisponde a %d\n", c, c);<br />
c = 'a';<br />
printf("%c corrisponde a %d\n", c, c);<br />
return 0;<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 32
Il tipo <strong>char</strong> usato come tipo numerico<br />
Poiché i valori <strong>char</strong> vengono convertiti<br />
<strong>automatica</strong>mente in valori interi essi sono<br />
spesso usati per rappresentare interi ad 8 bit<br />
(da -128 a +127).<br />
Con le dovute attenzioni, è possibile utilizzare<br />
<strong>char</strong> come tipo intero ad 8 bit.<br />
Es: elaborazione delle immagini<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 33
La clausola unsigned (1/2)<br />
I tipi numerici interi <strong>char</strong>, short, int, long<br />
possono essere <strong>di</strong>chiarati con la clausola<br />
unsigned.<br />
Sintassi<br />
unsigned ;<br />
Semantica<br />
I dati contenuti nelle relative variabili saranno<br />
interpretati come valori senza segno (ovvero<br />
valori non negativi).<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 34
La clausola unsigned (2/2)<br />
Sintassi ammessa<br />
[ signed | unsigned ] int<br />
[ signed | unsigned ] short [ int ]<br />
[ signed | unsigned ] long [ int ]<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 35
Tabella <strong>di</strong> confronto signed/unsigned<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 36
Esempio<br />
#include <br />
int main() {<br />
int x; unsigned int u;<br />
x = 2147483647; u = x;<br />
printf("%d %u\n", x, u);<br />
x = 2147483647+1; u = x;<br />
printf("%d %u\n", x, u);<br />
return 0;<br />
}<br />
stampa<br />
2147483647 2147483647<br />
-2147483648 2147483648<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 37
Definizione <strong>di</strong> nuovi tipi<br />
In C è possibile definire nuovi tipi me<strong>di</strong>ante l’istruzione<br />
typedef.<br />
Esempio:<br />
typedef int TipoA;<br />
typedef int TipoB;<br />
TipoA a = ...;<br />
TipoB b = ...;<br />
Tuttavia i tipi definiti sono sempre compatibili con il tipo<br />
base, quin<strong>di</strong> nell’esempio precedente l’istruzione<br />
int x=a+b; è valida e ritorna un risultato <strong>di</strong> tipo int.<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 38
Esercizi<br />
Esercizio 2.8<br />
Scrivere un programma che legga un importo in Euro x e<br />
restituisca il corrispondente importo in Dollari.<br />
Esercizio 2.9<br />
Scrivere un programma che legga da tastiera due numeri<br />
interi e stampi su schermo:<br />
• la loro me<strong>di</strong>a aritmetica (la loro somma <strong>di</strong>viso 2)<br />
• la loro me<strong>di</strong>a geometrica (la ra<strong>di</strong>ce quadrata del loro<br />
prodotto)<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 39
Operatori <strong>di</strong> uguaglianza e relazionali<br />
(1/2)<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 40
Operatori <strong>di</strong> uguaglianza e relazionali<br />
(2/2)<br />
Un’espressione relazionale ha valore intero:<br />
0 se falsa<br />
1 se vera<br />
In C falso (false) equivale a zero<br />
vero (true) equivale a qualsiasi valore <strong>di</strong>verso<br />
da zero<br />
GENERALMENTE:<br />
0 false<br />
1 true<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 41
Priorità<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 42
Valore delle espressioni<br />
int j=0, m=1, n=-1; float x=2.5, y=0.0;<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 43
Valore delle espressioni: risposte<br />
int j=0, m=1, n=-1; float x=2.5, y=0.0;<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 44
Esercizio<br />
Esercizio 2.10<br />
Cosa stampano le varie istruzioni printf?<br />
#include <br />
int main (void)<br />
{<br />
int j = 0, m = 1, n = -1, tot;<br />
float x = 2.5, y = 0.0, ris;<br />
ris = x + (y >= n);<br />
printf("\nRisultato:\t%f\n", ris);<br />
tot = ((j + 1) == m) != (y * 2);<br />
printf("\nTotale:\t%d\n", tot);<br />
printf("Totale in float:\t%f\n", tot);<br />
ris = ((j + 1) == m) != (y * 2);<br />
}<br />
printf("\nRisultato intero:\t%d\n", ris);<br />
printf("Risultato float:\t%f\n", ris);<br />
tot = ((-x) + j) == ((y > n) >= m);<br />
printf("\nTotale:\t%d\n", tot);<br />
printf("Totale in float:\t%f\n", tot);<br />
return 0;<br />
Char<br />
Parte 2<br />
2010/2011<br />
Pagina 45