Error "segmentation fault core dumped" in C

I'm kind of a novice yet but I still haven't been able to solve such a problem, so I came to resort here. I use linux and I'm having a problem in the code I did (it's pretty simple).

The code reads a number and turns it into a letter (at least this was supposed to happen) but is presenting the following error at the end: segmentation fault core dumped.

Follows the Code:

#include <stdio.h>

#include <stdlib.h>

#include <locale.h>

char nome [20];

int num;

void main(){

setlocale(LC_ALL, ("Portuguese"));

    printf(" \t\tLETRA DO ALFABETO\n ");
    printf(" \n\nOlá, digite aqui seu nome: ");
    fgets (nome, sizeof (nome), stdin);
    fflush(stdin);

        system("clear");


    printf(" \n\nOlá %s,digite aqui o número para saber qua letra ele representa: ",nome);
    scanf("%d", &num);

        system("clear");

    printf("\n\n %s, o número %d representa a letra %s\n\n",nome,num,(char)num);



return 0;

}

If you have any suggestions for improvement of the code, I accept as well.

 0
Author: G. Bittencourt, 2020-07-31

1 answers

Changing %s to % c should solve the problem since you are trying to print a single character and not a string, as already mentioned by the staff. Make sure only to enter a valid value from the ASCII table (remembering that characters from 0 to 31 cannot be printed).

As improvement suggestions I would change void main to int main (), since you is returning 0 at the end of the program (or else remove return 0), remove the calls to the system("clear") function if no are necessary, because they are platform dependent (this code would not work on windows, for example, that uses the cls command), use fgets to read the integer, so you wouldn't need more of the fflush () function (but for this you would have to convert a string to using the ATOI function, for example), define a macro with the size of the string (making it easier to change its size later), put the name and num variables inside the main function and remove the line break in the end the string returned by fgets, so the program does not skip a line after writing the name.

Follows the code with the suggested improvements:

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h> // para usar a função strlen()

#define TAMANHO_STR 20

int main()
{
    char nome[TAMANHO_STR+1];
    char num_str[TAMANHO_STR+1];
    int num;

    setlocale(LC_ALL, ("Portuguese"));

    printf("\t\tLETRA DO ALFABETO\n ");
    printf("\n\nOlá, digite aqui seu nome: ");
    fgets(nome, TAMANHO_STR+1, stdin);

    int l = strlen(nome); // descobre o comprimento da string
    if (l > 0 && nome[l-1] == '\n') // se a string não é vazia e o último caractere é \n
        nome[l-1] = 0; // define último caractere como \0

    printf(" \n\nOlá %s, digite aqui o número para saber qua letra ele representa: ",nome);

    fgets(num_str, TAMANHO_STR+1, stdin);
    num = atoi(num_str);

    printf("\n\n%s, o número %d representa a letra %c\n\n", nome, num, (char) num);

    return 0;
}

Note that the last character of every string in C is \0, so I set the string size to SIZE_STR+1 and replaced the penultimate character with \0.

Edit: as well remembered by a comment here, I forgot to explain that the function fgets saves in the string the line break character \n, if it fits. That is, if you enter as input abc the string will receive abc\n\0 (if the string has 5 or more characters in this example). To remove this, the program finds the penultimate character and changes it to \0, so the string can be printed without the line break.

 0
Author: Enzo Gaban, 2020-08-03 00:59:37