Error C, implicit declaration function

1

I have the following slogan: Design a program that allows generating a text file that allows to load five names separated by period. Show the contents of the file one name below the other.

#include<stdio.h>
int main(){
int total;
char nombre[20];
char aux;
FILE *f;
f=fopen("4_1.txt","w");
if(f==NULL){
    printf("No se pudo abrir el archivo.\n");
    exit(1);
}
for(total=0;total<5;total++){
    printf("Ingrese el nombre numero %d:",total+1);
    fgets(nombre,20,stdin);
    fprintf(f,"%s.",nombre);
}
fclose(f);
f=fopen("4_1.txt","r");
if(f==NULL){
    printf("No se pudo abrir el archivo.\n");
    exit(1);
}
while(aux!=EOF){
    fgetc(f);
    if(aux=='.'){
        printf("\n");
    }
    else{
        printf("%c",aux);
    }
}
fclose(f);
printf("\n");
system("pause");
return 0;

The error that appears to me when I try to compile is: On the line where this exit (1), implicit declaration of function 'exit' [Wimplicit-fuction-declaratio] The same thing seems to me with 'system'

    
asked by EmaB 13.07.2017 в 16:27
source

2 answers

4

You have two options:

  • You include the header that contains those functions, in this case stdlib.h

  • You do things right.

  • Suppose you choose the second option. What do I mean by doing things right?

    exit() serves to end the application with a return code ... but it turns out that your program only has a main() function. When the function ends, the program ends.

    In this case, the uses of exit() can be replaced without problems by a return .

    Example:

    // Cambia esto...
    if(f==NULL){
        printf("No se pudo abrir el archivo.\n");
        exit(1);
    }
    
    // ... por esto
    if(f==NULL){
        printf("No se pudo abrir el archivo.\n");
        return 1;
    }
    

    And what about system ? With system it happens that it is a very slow mechanism and totally dependent on the operating system ... you are running console commands with which you can hardly interact.

    Knowing the limits of C to manage the console a good substitute could be:

    puts("Pulse una tecla para continuar...");
    while ((c = getchar()) != '\n' && c != EOF); // limpiamos el buffer de entrada
    getchar(); // Esperamos a que el usuario pulse una tecla.
    

    Although in most cases it could be simplified with:

    puts("Pulse una tecla para continuar...");
    getchar(); // Normalmente nos encontramos un salto de linea residual
    getchar(); // Esperamos a que el usuario pulse una tecla.
    

    And with this you no longer need to use the stdlib library

        
    answered by 13.07.2017 / 16:47
    source
    2

    To make that error disappear you simply have to include the header with the prototype of those functions.

    The prototypes of the exit() and system() functions are within the stdlib.h file. When belonging to the standard library you do not need to include any bookstore explicitly when compiling. Just add the following line at the beginning:

    #include <stdlib.h>
    
        
    answered by 13.07.2017 в 16:37