![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jul 2005
Posts: 1
Rep Power: 0
![]() |
Segmentation Fault!!!
I have some background in programming and i have to take this programming class for school. Anyway I was trying to run this program and when I compile it with gcc it compiles with no errors. But then when I try to run the program it returns with "Segmentation Fault". I tried to find out where the error was, but I am having no luck...i know this is a simple program but can you guys try to help me out? This is the code..
/************************* *This program is creating* *A table of Olympic******* *Running distances******** *************************/ #include <stdio.h> int main() { double a=100, b=200, c=400, d=800; printf( "Table of Olympic Running Distances\n" "__________________________________________\n" "%6s%20s%20s%20s\n" "%6.0f%20.3f%20.3f%20.3f\n" "%6.0f%20.3f%20.3f%20.3f\n" "%6.0f%20.3f%20.3f%20.3f\n" "%6.0f%20.3f%20.3f%20.3f\n" "Meters", "Kilometers", "Yards", "Miles", a, a*0.001, a*1.094, a*0.0006215, |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 116
Rep Power: 0
![]() |
@note Please use code tags next time
![]() It looks as if your code has been cut off, would you mind reposting with the rest of the code (or if the file is huge the trouble spots) I would suggest gdb as a debugger. compile with: gcc -ggdb <file.c> And you can then invoke the debugger to watch as your program runs. @edit: btw, I think you are one short on your commas: "%6.0f%20.3f%20.3f%20.3f\n" "%6.0f%20.3f%20.3f%20.3f\n" "Meters", "Kilometers", "Yards", "Miles", I believe you should have a comma before "Meters" to begin the argument list. I have to say also, that that is one ugly way to use a printf statement. Just my opinion.
__________________
"...and though our kids are blessed their parents let them shoulder all the blame." - The Quiet Things That No One Ever Knows [BrandNew] |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Apr 2004
Location: Texas
Posts: 106
Rep Power: 5
![]() |
I think this fits your situation perfectly ... check out the source book if you get a chance ...
"One new feature introduced with ANSI C is the convention that adjacent string literals are concatenated into one string. ... However, the automatic concatenation means that a missing comma in an initialization list of string literals no longer causes a diagnostic message. A missing comma now results in a silent marriage of adjacent strings. This has dire consequences in circumstances like ..." ... yours ... source: Expert C Programming: Deep C Secrets By Peter van der Linden ... pretty interesting read so far ....
__________________
[ [ SykkN alloc ] initWithThePowerTo: destroyYouAll ]; /* Don't make me use it! */ |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
From what I see posted, and what does cause segfaults, is the that he has more format specifiers than he gives as variables. Obviously some code was truncated.
printf does not know how many arguments it has been given. It assumes there is one variable for each format specifier. So it keeps on trying to print them even when they are out of memory bounds. |
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 822
Rep Power: 4
![]() |
I think what sykkn was trying to say is that the OP has missed the comma between the end of his format specifier ("blahblah...%20f\n") and the first data parameter "Meters", this means that "Meters" is added to the end of the format specifier and all of the OP's carefully laid out parameters no longer match their %s and %f formats. This will cause a segfault when printf tries to interpret a double as a char *.
Edit: ignore this - didn't see L7Sqr's edit. |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Oct 2005
Posts: 17
Rep Power: 0
![]() |
Bumping this for my own problem.
As per the original post I'm having the same error...I have no idea why. The entire program: #include <stdio.h>
#define size 10
int main(void)
{
float sluggo[size];
int i=0,j=0;
printf("\nWelcome to Sluggo, the program that generates an array of ten elements.");
for(i=0;i<=9;i++)
{
printf("\nPlease enter your %l number:",sluggo[i+1]);
scanf("%l", &sluggo[i]);
}
for(j=0;j<=9;j++)
{
printf("\nHere are the members of the sluggo(in input order):");
printf("%l,",sluggo[j]);
}
return 0;
}Any suggestions? |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
"l" is a modifier, not a format specifier. One one use %ld or %lf or whatever. Read the freakin' manual for what you use. Maybe it won't make sense, but at least give it a shot.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#8 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 822
Rep Power: 4
![]() |
This is a different problem, so probably deserves its own thread.
It would help if you told us when the segmentation fault occurs, is it straight way or only after entering all the items. Possibly the %l as causing the problem as it is not a proper printf format specifier. You probably want %f for floats and %d for integeres. Also this line printf("\nPlease enter your %l number:",sluggo[i+1]);printf("\nPlease enter your %d number:",i+1);edit: damn your fast fingers Dawei! |
|
|
|
|
|
#9 | |
|
Newbie
Join Date: Oct 2005
Posts: 17
Rep Power: 0
![]() |
Quote:
Anyways it fixed the error, I just have to fix the saving...because it didn't save any input. |
|
|
|
|
|
|
#10 | |
|
Expert Programmer
Join Date: Jun 2005
Posts: 822
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|