Welcome to Linux Forums!

With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux HostsFree MagazinesJobs
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > GNU Linux Zone > Linux Programming & Scripting
Reload this Page Segmentation Fault in example C program
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

Linux Programming & Scripting C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Reply
 
Thread Tools Display Modes
Old 12-02-2003   #1 (permalink)
Linux Engineer
 
Join Date: Sep 2003
Location: Knoxhell, TN
Posts: 1,078
Send a message via MSN to lordnothing
Segmentation Fault in example C program

okay.. i bought teh book "Teach Yourself C In 21 Days" today and have found it to be a good book to learn from.. so, here i am plodding along, typing the example programs and all of a sudden one seg faults when it comes to the 'if' statements while it's running... any of you know why such a simple program would seg fault?? here is the code so you can test it if you want.. (btw, i'm running it on a K7 (Athlon) 500 w/128mb RAM, console)
Code:
/* Demonstrates the use of if statements. */
                                                                                
#include <stdio.h>
                                                                                
int x, y;
                                                                                
int main()
{
        /* input the two values to be tested */
                                                                                
        printf("\nInput an integer value for x: ");
        scanf("%d", &x);
        printf("\nInput an integer value for y: ");
        scanf("%d", y);
                                                                                
        /* test values and print result */
                                                                                
        if (x==y)
                printf("x is equal to y\n");
                                                                                
        if (x>y)
                printf("x is greater than y\n");

        if (x<y)
                printf("x is less than y\n");
                                                                                
        return 0;
}
__________________
Their code will be beautiful, even if their desks are buried in 3 feet of crap. - esr
lordnothing is offline   Reply With Quote
Old 12-02-2003   #2 (permalink)
Linux Guru
 
Join Date: Apr 2003
Location: London, UK
Posts: 3,284
Re: Segmentation Fault in example C program

Code:
             
        printf("\nInput an integer value for x: ");
        scanf("%d", &x);
        printf("\nInput an integer value for y: ");
        scanf("%d", y);
Your missing the "&" from variable y in the 2nd scanf.

always compile your code with "-Wall" , which shows these type of errors during compile time and & "-g" which includes debugging information in the binary for use in ddd / gdb.
Code:
gcc source.c -Wall -g -o executablefile
Jason
jasonlambert is offline   Reply With Quote
Old 12-02-2003   #3 (permalink)
Linux Engineer
 
Join Date: Sep 2003
Location: Knoxhell, TN
Posts: 1,078
Send a message via MSN to lordnothing
thanks.. don't knowmuch about the compiler options and such.. also shows what can happen when typing code at 3:30am...
__________________
Their code will be beautiful, even if their desks are buried in 3 feet of crap. - esr
lordnothing is offline   Reply With Quote
Old 12-04-2003   #4 (permalink)
Just Joined!
 
Join Date: Jul 2003
Location: BCN
Posts: 17
It's recommendede to
Code:
fflush(stdin);
after scanf
__________________
__/\\____/\\__
Joan Garnet is offline   Reply With Quote
Old 12-04-2003   #5 (permalink)
Linux Engineer
 
Join Date: Sep 2003
Location: Knoxhell, TN
Posts: 1,078
Send a message via MSN to lordnothing
why's that? /me is complete C noob... hence the book and ridiculous example program that does nothing useful.
__________________
Their code will be beautiful, even if their desks are buried in 3 feet of crap. - esr
lordnothing is offline   Reply With Quote
Old 12-04-2003   #6 (permalink)
Linux Guru
 
sarumont's Avatar
 
Join Date: Apr 2003
Location: /dev/urandom
Posts: 3,662
fflush() is a function that flushes out a stream like stdin or the file stream if you're reading a file. When scanf() takes the integer, there could be stuff hanging around in stdin. It will stay in the stream until the next function calls it. So the other scanf() would use what was left, not the new input. fflush( stdin ); fixes that.
sarumont is offline   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
 

Free Magazines
Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe
Systems Management News, the newspaper for IT systems administration and data center managers!
Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe
The Enterprise Newsweekly
eWeek is the essential technology information source for builders of e-business.
subscribe
Oracle Magazine
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe
Total Telecom
Total Telecom is "The Economist of the communications industry".
subscribe
More free magazines »



All times are GMT. The time now is 02:26 AM.




© 2000 - 2008 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.2.0