Thread: Beginner Q: Compile complete, segmentation fault when running. GCC 4.7.2. GDB output.

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    5

    Beginner Q: Compile complete, segmentation fault when running. GCC 4.7.2.

    hi everyone.
    I am trying to learn C programming. I have a few books, time and patience. I have run into a problem that I do not understand.

    I am running GNU/Linux and compiling with GCC version 4.7.2. I wrote a piece of code from the book, which should be correct. I compile it withoutany issues, but when I run the program I am getting a "segmentation fault".

    I am hoping someone can help me spot the problem. I tried using gdb (GNU debugger) to get more info on the segmentation fault, but I don't really understand what it is telling me. The code should be correct, but I am getting this error. If I did something wrong in the code I am not sure, but I do not think so. Here is the whole code:

    Code:
    /*testing some arithmetic expressions from page 45*/
    #include <stdio.h>
    
    int main (void)
    
    {
            float radius = 0.0f;            // radius of the table
            float diameter = 0.0f;          // diameter of the table
            float circumference = 0.0f;     // circumferance of the table
            float area = 0.0f;              // area of the circle
            float Pi = 3.14159265;          // value of Pi
    
    
            printf("\nInput the diameter ot the table:");
    
            scanf("%f, &diameter");
            radius = diameter/2.0f;
            circumference = 2.0f*Pi*radius;
            area = Pi*radius*radius;
    
            printf("\nThe circumference is %.2f", circumference);
            printf("\nThe area is %.2f\n", area);
    
    
            return 0;
    }
    I compile it fine with "gcc file", I run the a.out file in terminal and get the question about the diameter input. I put in 6 and press enter. Then I get the segmentation fault. I also tried to input 6.0, but get the same error.

    GDB tells me the following when using "run a.out".

    Code:
    Program received signal SIGSEGV, Segmentation fault.
    0x00007ffff7a84a9c in _IO_vfscanf_internal (s=<optimized out>, format=<optimized out>, argptr=argptr@entry=0x7fffffffd928, errp=errp@entry=0x0) at vfscanf.c:2326
    2326                    *ARG (float *) = negative ? -d : d

    Ps. The example is from page 45-46 in the book "Beginning C" by Ivor Horton, fourth edition.


    Any ideas would be appreciated and helpful.
    Last edited by zeebra; 10-17-2013 at 06:02 PM.

  2. #2
    Registered User
    Join Date
    Oct 2013
    Posts
    5
    I see now with the great code interpreter here that the float with 0.0 value does not register as variables.

    Still have the same question though..

    Also, perhaps I should start using Emacs instead of terminal and Nano.

  3. #3
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    In line 16, you have
    Code:
    scanf("%f, &diameter");
    It should be
    Code:
    scanf("%f", &diameter);
    I don't know if there are any other errors (I didn't look). Also your compiler should have warned about this, try adding the -Wall or -Wextra switches to the gcc command line

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The thing you are supposed to notice with the syntax highlighting is that &diameter in your scanf is blue....

    I admit to a soft spot for nano, so you may want to search "syntax highlighting in nano".

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    I don't know what you mean by "the float with 0.0 value does not register as variables". Which float with that value (or all of them -- there are 4)?

    By "code interpreter", I assume you mean syntax highlighter? Well, that should give you a hint as to your problem. You would get another hint by compiling with maximum warning level. In gcc, use the -Wall flag (to enable all warnings):
    Code:
    $ make foo
    gcc -Wall -ggdb3 -std=c99 -O0 -o foo foo.c -lm -lpthread -lrt
    foo.c: In function ‘main’:
    foo.c:16:9: warning: format ‘%f’ expects a matching ‘float *’ argument [-Wformat]
    EDIT: I got that warning when compiling without -Wall flag too. Did you see that and ignore it? Never ignore warnings.

    As to the editor question, there is nothing wrong with using terminal and Nano. Any text editor is fine, as long as it makes it easy for you to write/edit code. If you're only programming on your home system, you can use whatever you feel like. I've never really used Nano, so I don't know what kind of features it has, but both Vim and Emacs are very powerful, though they take a while to become good at. If you have to use other systems (work, university, etc) Vim is generally more widely available, though nowadays Emacs is almost everywhere too. I can't speak for how widespread Nano is.

  6. #6
    Registered User
    Join Date
    Oct 2013
    Posts
    5
    Thanks SirPrattlepod! That is the problem..
    I naivly thought GCC would warn me about such things, but now I learned something more, thanks to you. And I feel a little bit stupid for not seeing that.

    Using gcc -wall file.c would have been helpful. I wouldn't have know exatly the problem, but it would have given me a line number.
    Code:
    fifth1.c:16:2: warning: format ‘%f’ expects a matching ‘float *’ argument [-Wformat]
    gcc -Wextra gave me nothing for this code with the error included, when I tried it now.


    Ok, so now I am thinking I should always use "gcc -Wall file.c" when compiling, but I am probably wrong?


    Again, thanks a lot! It has been a valueable lesson.

  7. #7
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    There's no reason not to always use -Wall, and do not ignore warnings! If you like, turn the warnings into errors -Wall -Werror

  8. #8
    Registered User
    Join Date
    Oct 2013
    Posts
    5
    Quote Originally Posted by tabstop View Post
    The thing you are supposed to notice with the syntax highlighting is that &diameter in your scanf is blue....

    I admit to a soft spot for nano, so you may want to search "syntax highlighting in nano".
    That sounds like a good idea, thanks!

    Since I am new, I only noticed that "radius", "circumference" and "area" was black.

  9. #9
    Registered User
    Join Date
    Oct 2013
    Posts
    5
    Quote Originally Posted by anduril462 View Post
    I don't know what you mean by "the float with 0.0 value does not register as variables". Which float with that value (or all of them -- there are 4)?
    No, I just naivly thought that was the issue since "radius", "circumference" and "area" was black. But ofcourse that was since they could not fetch any value from "diameter" input.

    Quote Originally Posted by anduril462 View Post
    By "code interpreter", I assume you mean syntax highlighter? Well, that should give you a hint as to your problem. You would get another hint by compiling with maximum warning level. In gcc, use the -Wall flag (to enable all warnings):
    Code:
    $ make foo
    gcc -Wall -ggdb3 -std=c99 -O0 -o foo foo.c -lm -lpthread -lrt
    foo.c: In function ‘main’:
    foo.c:16:9: warning: format ‘%f’ expects a matching ‘float *’ argument [-Wformat]
    EDIT: I got that warning when compiling without -Wall flag too. Did you see that and ignore it? Never ignore warnings.

    As to the editor question, there is nothing wrong with using terminal and Nano. Any text editor is fine, as long as it makes it easy for you to write/edit code. If you're only programming on your home system, you can use whatever you feel like. I've never really used Nano, so I don't know what kind of features it has, but both Vim and Emacs are very powerful, though they take a while to become good at. If you have to use other systems (work, university, etc) Vim is generally more widely available, though nowadays Emacs is almost everywhere too. I can't speak for how widespread Nano is.
    Syntax highlighter. Good, I will add that in my dictionary and remove what I said. Thanks!

    Also, I got no error message when I ran "gcc file.c". But I did not use "make" as you did above, although I don't really understand yet why you did that.


    I would never ignore a warning, for sure.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Quote Originally Posted by zeebra View Post
    Also, I got no error message when I ran "gcc file.c". But I did not use "make" as you did above, although I don't really understand yet why you did that.
    Make is a program that is used to help build larger projects, though it can be used for small programs like yours. I wrote some simple make rules that make compiling single-source-file programs like yours a bit easier. I regularly use gcc with the same basic options (-Wall, -ggdb3, etc). All make is doing in this case is executing the gcc command you see right below it, with the -Wall flag (and several other flags). It saves me typing.

    So in this case, make does not do anything special for error checking that gcc doesn't already do (make can't check for errors in C files itself, but it could call other programs to check for errors or perform other tasks).

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    A lot of editors don't highlight variable names (and so consequently they're the "normal" black color, which still let's them stand out since everything else does have some color associated to it).

    The "extra" warnings don't include the "all" warnings (now that I've written that sentence, you can see that the names are not perhaps perfectly chosen); in general, "all" is what you would want.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with Segmentation fault (core dumped) output
    By DanJRA in forum C Programming
    Replies: 8
    Last Post: 10-23-2013, 03:15 AM
  2. Beginner programmer with segmentation fault.. HELP PLEASE!
    By ericcsmygod in forum C Programming
    Replies: 7
    Last Post: 04-23-2011, 12:53 AM
  3. Complete Beginner.
    By nubby in forum C Programming
    Replies: 14
    Last Post: 10-02-2007, 07:00 AM
  4. Complete Beginner [Need Help]
    By Vintik in forum C++ Programming
    Replies: 10
    Last Post: 08-15-2005, 05:08 PM
  5. Complete Beginner, HELP!!
    By britneyspy in forum C++ Programming
    Replies: 19
    Last Post: 06-12-2003, 11:01 AM