Thread: Getting error "Segmentation fault (core dumped)" during exectution

  1. #1
    Registered User
    Join Date
    Oct 2011

    Getting error "Segmentation fault (core dumped)" during exectution

    #include <stdio.h>
    #include <string.h>
    char prn_stars(int s);
    int main(void){
    int avg;
    scanf("%d", &avg);
    char prn_stars(int i){
            if (i >= 200)
            if (i > 200)
            if (i > 100)
    prn_stars is a function to print stars

  2. #2
    casual coder spaghetticode's Avatar
    Join Date
    Jan 2011
    Hesse, Germany
    Your function doesn't return anything.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8 | Acer Aspire V3-771G
    OS: Windows 10 Home

  3. #3
    Registered User
    Join Date
    Sep 2011
    Stockholm, Sweden
    Your main function doesn't return any value, it should since it's declared with the return type int. This isn't causing the segfault however, the problem is with your printf statement and your prn_stars function. The %s in printf expects the first argument to be a null-terminated string. The argument is in fact nothing (afaik nothing = whatever happens to be in the eax-register in x86), since your prn_stars function does not return any value. Even if it did, the return type of char is wrong, since that is just a single char, and not a null-terminated string. So, either:
    - turn prn_stars into a function that returns void, and skip the printf statement in main
    - make prn_stars return a null-terminated string with the appropriate number of stars
    Last edited by iceaway; 10-31-2011 at 12:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with 'Segmentation fault (core dumped)' error?
    By Von Fuzzball in forum C Programming
    Replies: 12
    Last Post: 05-03-2011, 02:29 PM
  2. how to fix "Segmentation fault (core dumped)"
    By kapil1089thekin in forum C++ Programming
    Replies: 5
    Last Post: 08-20-2010, 04:24 AM
  3. Segmentation fault, core dumped
    By dweenigma in forum C Programming
    Replies: 2
    Last Post: 05-21-2007, 03:50 PM
  4. Runtime error 'segmentation fault (core dumped)'
    By mikemccready in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 07:14 AM
  5. Segmentation fault (core dumped)
    By JYSN in forum C Programming
    Replies: 1
    Last Post: 02-21-2002, 03:24 AM

Tags for this Thread