Jean-Jacques BOURDIN
It obeys to orders starting with #. Examples:
Les directives qui commencent par # et elles seules sont évalluées par
le précompilateur. Il les exécute. Parmi ces directives nous verrons
essentiellement les :
#include <stdio.h> #define GT(a, b) a < b ? b : a #define CARRE(a) a * a main () { int i, s; i = 10; s = 12; printf("Le MAX de %3d et %3D est %d\n",i,s,GT(i,s)); printf("Le MAX de %3d et %3D est %d\n",i,s, GT(i++,s++)); printf("Le MAX de %3d et %3D est %d\n",i,s, GT(i+1,s+1)); printf("Le carre de %3d est %d\n",i + 1,CARRE(i + 1)); printf("Le carre de %3d est %d\n",i,CARRE(i++)); printf("Le carre de %3d est %d\n",i,CARRE(--i)); } |
main () { int i, s; i = 10; s = 12; printf("Le MAX de %3d et %3D est %d\n",i,s,i < s ? s : i); printf("Le MAX de %3d et %3D est %d\n",i,s, i++ < s++ ? s++ : i++); printf("Le MAX de %3d et %3D est %d\n",i,s, i+1 < s+1 ? s+1 : i+1); printf("Le carre de %3d est %d\n",i + 1,i + 1 * i + 1); printf("Le carre de %3d est %d\n",i,i++ * i++); printf("Le carre de %3d est %d\n",i,--i * --i); } |
dhcp2.ai.univ-paris8.fr: a.out Le MAX de 10 et 12 est 12 Le MAX de 10 et 12 est 13 Le MAX de 11 et 14 est 15 Le carre de 12 est 23 Le carre de 11 est 132 Le carre de 13 est 132 |
#define MAX(s,t) s < t ? t : s main () { int i, j, k, m, n; i = 2; j = 5; k = 6; n = MAX(i,j); m = MAX(n, k); printf("MAX(%3d,%3d,%3d) == %3d\n",i,j, k, m); printf("MAX(%3d,%3d,%3d) == %3d\n",i,j, k, MAX(MAX(i,j),k)); } |
jj@mu $ a.out MAX( 2, 5, 6) == 6 MAX( 2, 5, 6) == 5 jj@mu $ |
jj@mu $ gcc -E ex.c # 1 "ex.c" # 1 " |
Décimal Binaire 3 11 6 110 12 1100 24 11000 48 110000 96 1100000 192 11000000 4 100 8 1000 16 10000 13 1101 26 11010Remarquons que, en décimal, 1230 et dix fois plus grand que 123. Donc, en binaire, 101010 et deux fois plus grand que 10101. L'opérateur de décalage à gauche
<<permet donc de décaler un nombre binaire de n cases donc de le multiplier par 2 puissance n.
void puiss2 (int pmax) { int i, p; p = 1; for (i=0; i < pmax; i++) { printf("%d\t",p); p = p << 1; } printf("\n"); } |
01011010 & 00011100 ------------ 00011000 01011010 & 00000001 ------------ 00000000S'écrit avec &.
int imper (int i) { return i & 0x01; } |
int nonul (int i) { while (i) { if (i | 0x01) return 1; i = i << 1; } return 0; } /* completement inutile, non */ |
int differ (int i, int j) { if (i ^ j) return 1; return 0; } |
Essayez donc ceci :
int inver (int i) { return 1 + ~i; } int main () { int n; for (n = -10; n < 11; n++) printf("%d == - %d\n", n, inver(n)); } |
#include <stdio.h> #include <stdlib.h> #include <assert.h> struct vect { int nbele; int * v; } ; typedef struct vect vect_t; |
void afficv(vect_t v) { int i; for (i = 0; i < v.nbele; i++) printf("%d ",v.v[i]); printf("\n"); } |
vect_t lire (FILE * fd, int nbele) { vect_t v; int i, nbelefic, tmp; v.nbele = nbele; fscanf(fd, "%d ", &nbelefic); if (nbelefic < nbele) { nbele = nbelefic; } v.v = (int *) malloc (nbele* sizeof(int)); assert(v.v); for(i=0; i < nbele; i++) { fscanf(fd, "%d", &tmp); v.v[i] = tmp; } return v; } |
void ecrire (FILE * fd, vect_t w) { int i; fprintf(fd,"%9d ",w.nbele); printf(" on met %d\n",w.nbele); for (i = 0; i < w.nbele; i++) fprintf(fd,"%9d ",w.v[i]); fprintf(fd,"\n"); } |
vect_t creervect(nb) { int i, a, b, c; vect_t w; w.nbele = nb; w.v = (int *) malloc (nb * sizeof(int)); assert(w.v); a = 1; b = 1; w.v[0]=1; w.v[1]=1; for (i = 2; i < nb; i++) { c = a + b; w.v[i] = c; b = a; a = c; } return w; } int main (int argc, char * * argv) { int nb, lireou; FILE * fd; vect_t w; if (argc != 3) { printf(" Mode : a.out nb nmfic\rn nb est un entier et nmfic un nom de fichier\n"); exit (0); } nb = atoi (argv[1]); printf("argv[1] valait %d\n", nb); printf("Pour lire (1) ou ecrire (2) ?\n"); scanf("%d",&lireou); if (lireou == 1) { fd = fopen(argv[2],"r+");/* open the file to read it*/ assert(fd); w = lire(fd,nb); afficv(w); printf("Ou suis-je ? %ld\n",ftell(fd)); fseek(fd, 0L, 0); printf("Ou suis-je ? %ld\n",ftell(fd)); fseek(fd, -10L, SEEK_END); printf("Ou suis-je ? %ld\n",ftell(fd)); fclose(fd); exit (0); } printf("On va ecrire \n"); fd = fopen(argv[2],"w");/* open the file to write it*/ assert(fd); w = creervect(nb); afficv(w); ecrire(fd,w); fclose(fd); exit (0); } /* r r+ w w+ a a+ Où debut debut début début fin fin non err err creation ftell(fd) -> le numéro de la case du fichier (0 c'est le début, 1 le second octet...) fseed(fd,ecart,point) permet d'avancer de "ecart" octets à partir du "point" SEEK_SET 0 SEEK_CUR 1 SEEK_END 2 */ |
#include <stdio.h> #include <stdlib.h> struct vectd { int nb; double * v; } ; typedef struct vectd vectd_t; |
void afficher (vectd_t v){ int i; printf("%d ",v.nb); for (i = 0; i < v.nb; i++) printf("%lf ",v.v[i]); printf("\n"); } |
void ecrire (vectd_t v, char * nomfichier) { int i; FILE * fd; fd = fopen (nomfichier, "w"); if (! fd) { /* good old ways */ printf("Impossible d ouvrir le fichier %s !\n\n", nomfichier); exit (1); } fprintf(fd,"%d ",v.nb); for (i = 0; i < v.nb; i++) fprintf(fd,"%lf ",v.v[i]); fclose(fd); } |
vectd_t creer (int nb) { vectd_t v; int i; double x, eps; v.nb = nb; v.v = (double *) malloc (nb * sizeof(double)); assert(v.v); eps = - 0.07; for (i = 0, x=1.3; i < v.nb; i++) { v.v[i] = x; x += eps; if (x < 1.0) eps = 0.145 + eps; if (x > 1.7) eps = - 0.145 + eps; } return v; } |
vectd_t lire (char * nomfichier) { vectd_t v; int i, nb; FILE * fd; double x; fd = fopen (nomfichier, "r"); if (! fd) { printf("Impossible d ouvrir le fichier %s !\n\n", nomfichier); exit (1); } fscanf(fd,"%d ",&nb); v.nb = nb; v.v = (double *) malloc (nb * sizeof(double)); if (! v.v) { printf("Impossible d allouer la memoire de taille %d !\n\n", nb); exit (1); } for (i = 0; i < v.nb; i++) { fscanf(fd,"%lf ",&x); v.v[i] = x; } fclose(fd); return v; } |
int main (int argc, char * * argv) { int nb, choix; vectd_t w; char * nf = "donnees.txt"; choix = 0; if (argc == 3) { choix = atoi (argv[1]); nb = atoi (argv[2]); printf("creation du fichier avec %d valeurs\n", nb); w = creer(nb); afficher(w); ecrire(w,nf); return 0; } if (argc == 2) { choix = atoi (argv[1]); } if (choix < 0 || choix > 1) choix = 0; switch(choix) { case 0: printf("creation du fichier\n"); nb = 21; w = creer(nb); afficher(w); ecrire(w,nf); break; case 1: printf("lecture du fichier\n"); w = lire(nf); afficher(w); break; } } |