GCC can't do that but GDB (a debugger) sure can. Compile you program using the -g switch, like this:

gcc program.c -g

Then use gdb:

$ gdb ./a.out
(gdb) run
<segfault happens here>
(gdb) backtrace
<offending code is shown here>

Here is a nice tutorial to get you started with GDB.

Where the segfault occurs is generally only a clue as to where "the mistake which causes" it is in the code. The given location is not necessarily where the problem resides.

Answer from nc3b on Stack Overflow
🌐
GNU
gcc.gnu.org › bugs › segfault.html
How to debug a GCC segmentation fault - GNU Project
-v shows how cc1 was invoked (useful for invoking cc1 manually in gdb). ... Print out the values of interesting variables, e.g., the ones in the statement which got the segmentation fault. You can use the pt and pr macros from the gdbinit.in file to display GCC data.
🌐
Rose-Hulman Institute of Technology
rose-hulman.edu › class › csse › csse132 › 2324c › debugging-segfaults.html
Debugging Segmentation Faults using GEF and GDB
Load the program in GDB and cause the segfault. Edit the same .gef.rc file. Look for the enable = True line and change it to enable = False ... Program received signal SIGSEGV, Segmentation fault.
🌐
Ucsb
discover.cs.ucsb.edu › commonerrors › tutorial › gdbtutorial.html
GDB Tutorial: Finding Segmentation Faults
This tutorial will show you how to use gdb's up function to find the cause of segmentation faults. I'll be using a c++ program I wrote as an example.
🌐
Unknownroad
unknownroad.com › rtfm › gdbtut › gdbsegfault.html
RMS's gdb Tutorial: Segmentation Fault Example
We are going to use gdb to figure out why the following program causes a segmentation fault. The program is meant to read in a line of text from the user and print it. However, we will see that in it's current state it doesn't work as expected · 1 : #include <stdio.h> 2 : #include <stdlib.h> ...
🌐
Cplusplus
cplusplus.com › articles › iwTbqMoL
Debugging using gdb: Find a segFault - C++ Articles
What I type is in bold, what's ... j@j-desktop:~/badCode$ ./a.out Segmentation fault Oh no, a segfault. I'll rebuild it with debugging symbols and then run it under the debugger. j@j-desktop:~/badCode$ g++ -g 227.cpp j@j-desktop:~/badCode$ gdb ./a.out GNU gdb (GDB) 7.1-ubuntu ...
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › how-to-find-segmentation-error-in-c-c-using-gdb
How to find Segmentation Error in C & C++ ? (Using GDB) - GeeksforGeeks
July 11, 2025 - $ ./a.out (it is Object File) If it shows Segmentation fault (core dumped) then follow following steps. Step 3:Debug it $ gdb ./a.out core Your output will look like something this: ------------------------------------------------------------------------------------ GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git Copyright (C) 2018 Free Software Foundation, Inc.
🌐
GitHub
gist.github.com › rhaps0dy › 3c5c51073c64672fb4d4
HOWTO: Catch segmentation faults on gdb (GNU debugger) · GitHub
$ gcc prog1.c $ ./a.out Input some string! blargh Here is your reversed string: hgralb $ ./a.out Input some string! thisstringisverylonghasmorethan16letters Here is your reversed string: ÐW¸msahgnolyrevsignirtssiht Segmentation fault · Oops, what happened? The program received SIGSEGV? Time to use gdb! #How to find the bug First, we compile the program with debug symbols
Find elsewhere
🌐
YouTube
youtube.com › watch
gdb segfaults tutorial - YouTube
In this video, we'll use GDB in combination with segfaults.If you'd like more information on GDB, please visit: https://www.gnu.org/software/gdb/
Published   March 22, 2015
🌐
Tutorialadda
tutorialadda.com › gdb › how-to-debug-segmentation-fault-in-c-program-using-gdb
How to Debug Segmentation Fault in C Program Using GDB
backtrace or bt --> It shows the stack frames. stack frames contain information about how one function is called to another function. frame --> To switch the particular frame (gdb) bt #0 0x0000000000400522 in main (argc=1, argv=0x7fffffffddc8) at test.c:8 # It is showing a frame 0 and will check this frame 0 with the below command (gdb) frame 0 #0 0x0000000000400522 in main (argc=1, argv=0x7fffffffddc8) at test.c:8 8 *ptr=1; (gdb) print ptr $1 = (int *) 0x0 · frame 0 provides info that line 8 (*ptr=1) is causing the issue so we checked the ptr value (address of the pointer variable) and it is a NULL pointer(0x0). In our program, We are trying to write a value at the NULL pointer. that's why getting the segmentation fault.
🌐
Pconrad
pconrad.github.io › old_pconrad_cs16 › 10W › extraLabs › el01
CS16, el01, 10W--Debugging segmentation faults with gdb (for reference only--not on final)
You can use this as a roadmap when you have your own segfaults to deal with. Good luck! ... -bash-3.2$ make sumArrayWrong cc sumArrayWrong.c -o sumArrayWrong -bash-3.2$ ./sumArrayWrong Segmentation fault -bash-3.2$ ... -bash-3.2$ gdb sumArrayWrong GNU gdb Fedora (6.8-32.fc10) Copyright (C) ...
🌐
University of Idaho
marvin.cs.uidaho.edu › Teaching › CS445 › debuggingSegFaults.html
Debugging Segfaults
(gdb) bt #0 0x0000000000400578 in strlen (s=0x0) at happy.cpp:7 #1 0x00000000004005d7 in main () at happy.cpp:20
🌐
SheCodes
shecodes.io › athena › 39354-how-can-i-debug-a-segmentation-fault-seg-fault
[Debugging] - How can I debug a segmentation fault (seg | SheCodes
Learn how to debug a segmentation fault (seg fault) error using tools like gdb and techniques like tracking the memory location.
🌐
IU Knowledge Base
kb.iu.edu › d › aqsj
Identify what's causing segmentation faults (segfaults)
October 2, 2023 - Start your debugger with the command gdb core, and then use the backtrace command to see where the program was when it crashed. This simple trick will allow you to focus on that part of the code. If using backtrace on the coreg file doesn't find the problem, you might have to run the program ...
Top answer
1 of 2
3

Here is a sample program that would definitely cause segmentation fault :

include

int main() {
    int *pVal = NULL;

    printf("ptr value is : %d", *pVal);
    return 0;
}

You need to compile in debug mode in order to add additional debug information in executable file:

gcc -g segFault.c

Then all you need is to run gdb and give that the executable file path (i.e a.out in this case). Then by just running it you can see that gdb highlights the line causing segmentation fault.

 ~/Dropbox/cprog/demos : $ gdb a.out
GNU gdb (GDB) 7.12
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin15.6.0".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.out...Reading symbols from /Users/rohankumar/Dropbox/cprog/demos/a.out.dSYM/Contents/Resources/DWARF/a.out...done.
done.
(gdb) run
Starting program: /Users/rohankumar/Dropbox/cprog/demos/a.out

Program received signal SIGSEGV, Segmentation fault.
0x0000000100000f62 in main () at segFault.c:6
6       printf("ptr value is : %d", *pVal);

You can also print the values and see stacktrace of program. You can read more about gdb here .

Happy coding!

2 of 2
0

Run gdb and run your program from gdb, then use backtrace. you 'll get stack frame, which you can walk through by fram command and use print to check variables' values. Check gdb tips\docs over internet. You can use gdb to load already existing core file, generated by crashed program, to find spot where problem occured. Loaded core file is equal to state at crash point, you just usebacktrace.

Answer on your question was here: Determine the line of C code that causes a segmentation fault?

🌐
DEV Community
dev.to › ayush12303 › how-to-debug-segmentation-fault-in-c-md9
How to Debug Segmentation Fault in C++ - DEV Community
October 27, 2023 - If a segmentation fault occurs, GDB will provide the line number and function where the segfault happened.
🌐
Quora
quora.com › How-do-I-debug-a-segmentation-fault-in-C
How to debug a segmentation fault in C - Quora
Answer (1 of 5): If a segfault shows up in my code, it’s usually related to something I just added or changed. Desk-check the recently added code. Can I spot the bone-headed mistake? Maybe add some [code ]fprintf(stderr, …)[/code] lines to trace through the recently added code.
🌐
Tech Rants
mytechrants.wordpress.com › 2009 › 05 › 22 › debugging-a-segmentation-fault-using-gdb
Debugging a segmentation fault using gdb | Tech Rants
May 22, 2009 - You’ll need the following pre-requisites to use gdb to debug a segmentation fault: 1) make sure you have compiled the executable WITH debugging symbols. i.e. the "-g" flag. eg gcc -g -o hello hello.c Without debugging symbols, gdb won’t be able to do much. 2) Linux should core-dump on segmentation fault. Set: ulimit -c unlimited (man ulimit for more info) Now just run that the excutable that is segfaulting. As soon as it segfaults, you should get an output something like "Segmentation fault (core dumped)". ls in your working directory and you will find a new core file has been created (probably with the name core.{pid})