Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

What's wrong in this code? It says "segmentation fault (core dumped)"

0 votes
asked Jun 23, 2020 by NG CHUN KIAT JACK (120 points)
#include <stdio.h>
#include <stdlib.h>
#pragma warning (disable:4996)
#define YEARNOW 2020

int
main ()
{
  char name;
  int bornYear, score1, score2, score3, ageNow;
  printf ("Enter Name:");
  gets (&name);
  printf ("Enter the year you were born:");
  scanf ("%d", &bornYear);
  printf ("Enter your score for 3 papers \nPaper 1:");
  scanf ("%d", &score1);
  printf ("Paper 2:");
  scanf ("%d", &score2);
  printf ("Paper 3:");
  scanf ("%d", &score3);
  ageNow = bornYear - YEARNOW;
  printf ("Name: %s \n", name);
  printf ("Age: %d", ageNow);
  printf ("Paper 1 score: %d", score1);
  printf ("Paper 2 score: %d", score2);
  printf ("Paper 3 score: %d", score3);
  system ("pause");
}

2 Answers

0 votes
answered Jun 23, 2020 by LiOS (6,420 points)
The main issue that causes a segmentation fault is line 22 - printf ("Name: %s \n", name);

This is because you are printing a string, while name is 1 character. If I enter my name, only the first letter entered is actually written to the name variable. If you want to take a full name, you may either make an array of an unitialised size (not available in some languages), make an array of a certain size (but what if user name is not 10 characters long?) or a pointer to a string (that allocates a new byte for each character entered, not forgetting to add termination character \0).

Also, the gets() function you are using is deprecated and dangerous. Alternatives are fgets or scanf (using either %s or %[^\n] ) but this may depend on what method you use to get the user's name.

Further, line 21 has a logic error which produces a negative age, e.g. if i enter 2000, age will be -20 not 20, so need to swap variables around. Alternatively, instead of having to manually update the defining of YEARNOW when going into 2021, you can get the current year, change to numeric value instead of string using atoi().

A bit more of an updated version of code:

#include <stdio.h>
#include <stdlib.h>

#pragma warning (disable:4996)

int main(){
    
    char name;
    int bornYear, score1, score2, score3, ageNow;
    
    printf("Enter Name: ");
    gets(&name);
    
    printf("Enter the year you were born: ");
    scanf("%d", &bornYear);
    
    printf("Enter your score for 3 papers \nPaper 1: ");
    scanf("%d", &score1);
    
    printf("Paper 2: ");
    scanf("%d", &score2);
    
    printf("Paper 3: ");
    scanf("%d", &score3);
    
    ageNow = atoi(__DATE__+7) - bornYear;
    
    printf("\nName: %c \n", name); //currently set to %c to prevent segmentation fault
    printf("Age: %d \n", ageNow);
    printf("Paper 1 score: %d \n", score1);
    printf("Paper 2 score: %d \n", score2);
    printf("Paper 3 score: %d \n", score3);
    
  return 0;
}
0 votes
answered Jun 24, 2020 by Peter Minarik (84,720 points)
edited Jun 25, 2020 by Peter Minarik

LiOS's answer is correct. I'm just here to add a sample implementation part for the name itself:

#include <stdio.h>

int main()
{
    const char maxNameLength = 30; // Find a suitable length for your needs
    char name[maxNameLength];
    printf("Enter your name: ");
    fgets(name, maxNameLength, stdin);
    printf("Your name is: %s", name);
    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...