Show Menu
Cheatography

C Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Libs (#include)

<stdio.h>
<stdlib.h>
<string.h>
<math.h>
<unistd.h> - sleep()

I/O

FLUSH IN
__fpurge(stdin)
FLUSH OUT
fflush(stdout)
IN
scanf("fo­rma­to", &var)
OUT
printf("st­rin­g")

Data

(un)signed char
%c
short
%hi
unsigned short
%hu
int
%i / %d
unsigned int
%u
long
%li
float
%f
pointer addr
%p
string
%s

Format

%0.nf
n decimales
%nd
Tamaño (espacios pre)
%0nd
Tamaño (0 pre)

Char Array -> String

void qs(char *cad){
    if(cad[strlen(cad)-1]=='\n'){
        cad[strlen(cad)-1]='\0';
    }
}
 

Strings

fgets(str, size, stdin)
strlen(str)
strlcpy(dest, src, size)
strcmp(str1, str2) (M < m)
 ­ ­ ­ ­ ­ > 0 ­ ­ ­ str1 > str2
 ­ ­ ­ ­ ­ = 0 ­ ­ ­ str1 = str2
 ­ ­ ­ ­ ­ = 0 ­ ­ ­ str1 = str2
end: "­\0"

Pointers

declar­ation
int *ip;
storing address
ip = &var;
ip
bffd8b3c
*ip
var (value)

Files

FILE *pointer
pointer = fopen("files", "mode")
fread(point(O), sizeof(varType), 1, point(I))
fwrite(point(I), sizeof(varType), 1, point(O))
fseek(pointer, size, whence: )
           SEEK­_SET       Inicio
           SEEK­_CUR       Pos actual
           SEEK­_END       Final
fclose(pointer)

File Open: mode - operation - exist

r
read
N: NULL
w
write
Y: overw N: create
a
append
N: create
r+
read/write
N: NULL
w+
read/write
Y: overw N: create
a+
read/app
N: create
Mb
binary
 

Reservar Memoria

int** reservaMemoria(int filas, int columnas) {
    int ** matriz, i=0;
    matriz = (int**) malloc(filas * sizeof (int*));
    for (i = 0; i < filas; i++){
        matriz[i] = (int*) malloc(columnas * sizeof (int));
    }
    return(matriz);
}

Liberar Memoria

 void liberaMemoria(int**matriz, int filas){
    int i=0;
    for(i=0;i<filas;i++){
        free(matriz[i]);
    }
    free(matriz);
}

Reservar struct

struct struct_files *arrayFiles;

void reservarStruct(int nElementos, struct struct_files* arrayFiles){
    
     arrayFiles = (struct struct_files*) malloc(sizeof(struct struct_files) * nElementos);

}

Liberar struct

void liberarStruct(struct struct_files* arrayFiles){
    
    free(arrayFiles);
}

Open Carefully

if((salida = fopen("files.dat", "r+")) == NULL){
        salida = fopen("files.dat","w+");
    }