Fedora Linux Support Community & Resources Center
  #1  
Old 13th June 2008, 12:03 AM
RoastP3nguin Offline
Registered User
 
Join Date: Apr 2008
Posts: 5
segmentation fault

Hi I've just started learning C using K&R, and for exercise 1-15 I have to rewrite a program, using my own function. I've written the program the function and the test for it, it compiles fine using gcc, but when I run it all I get is 'Segmentation fault'.
I've tried to figure this out, but don't really have a clue as I have just started, but here is the code I have written if this explains anything :P

Code:
/* convert to Fahrenheit from Celsius function */
#include <stdio.h>

/* cfconvert function */
float cfconvert(float celsius)
{
  float fahr;

  fahr = (celsius * 1.8) + 32.0;
  return fahr;
}

/* test conversion function */
main()
{
  float c, f;

  printf("Enter 'float' temperature in Celsius to convert: ");
  scanf("%f", c);

  f = cfconvert(c);
  printf("\nCelsius: %f.1\tFahrenheit: %f.1\n", c, f);
  return 0;
}
Thanks for your help in advance. Roastie.

Last edited by Dan; 13th June 2008 at 12:05 AM. Reason: Edited to comply with guidelines.
Reply With Quote
  #2  
Old 13th June 2008, 12:27 AM
skoona's Avatar
skoona Offline
Registered User
 
Join Date: Feb 2004
Location: Indiana, USA
Age: 57
Posts: 428
Code:
/* convert to Fahrenheit from Celsius function */
#include <stdio.h>

/* cfconvert function */
float cfconvert(float celsius)
{
  float fahr = 0.0;

  fahr = (celsius * 1.8) + 32.0;
  return fahr;
}

/* test conversion function */
int main()
{
  float c = 0.0, f = 0.0;

  printf("Enter 'float' temperature in Celsius to convert: ");
  scanf("%f", &c);

  f = cfconvert(c);
  printf("\nCelsius: %3.1f\tFahrenheit: %3.1f\n", c, f);
  return 0;
}
1. scanf() wants the address of its binary variable. => &c
2. the printf() formatting '%f.1" printed like: "Celsius: 47.000000.1", I think you wanted '%3.1f' for "Celsius: 47.0"
3. consider using this compiler line to show all errors.
gcc -Wall -O2 -g -o program program.c -- straight C
gcc -Wall -O2 -g -lm -o program program.c -- include math.h library
Reply With Quote
  #3  
Old 13th June 2008, 10:31 AM
RoastP3nguin Offline
Registered User
 
Join Date: Apr 2008
Posts: 5
Hey, cheers, it all works now. Thanks for the help.
Reply With Quote
  #4  
Old 17th June 2008, 04:11 AM
cable_txg's Avatar
cable_txg Offline
Registered User
 
Join Date: Jan 2008
Posts: 302
Just to add one more point to Skoona's:

use -ggdb to specifically include the gdb debugger, when errors occur run:

gddb <binary executable filename>
type: run <parameters for program if any>
at error point type: bt
The above should tell you which line number and what happened.

__________________
"Genius is one percent inspiration and 99 percent perspiration" --- Thomas Alva Edison.

Laptop: Intel i7, Geforce GT 425M, Blu-Ray :D
Laptop: Amd64 X2, Geforce Go 7600 Intel Core 2 Duo, Geforce 8600M GT
Desktop: :mad: Thanks Windows Vista! R.I.P. Sept. 2007 :mad:
Reply With Quote
Reply

Tags
fault, segmentation

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation fault. GodL1ke Using Fedora 7 23rd October 2007 05:04 AM
Segmentation fault! mediator Programming & Packaging 6 29th July 2007 12:55 PM
NFS on FC3: Segmentation fault pnix Servers & Networking 0 18th August 2005 10:12 AM
ls Segmentation fault hksky Using Fedora 1 28th July 2005 03:09 PM
apt segmentation fault arathalion Using Fedora 1 21st November 2004 12:34 AM


Current GMT-time: 20:44 (Wednesday, 19-06-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat