Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 04-05-2005, 07:24 PM   #1 (permalink)
 
True Techie

Join Date: Dec 2004

Posts: 145

Mohan Giri

Default C Programming

Hi Everyone,
I write a program to add 4 digit numbers. When I compile a program I didn't get any error message. But When I run the program I am getting message like

"segmentation fault". Anybody can find any logical error in my program.

For Information. I am compiling the codes using inbuilt compiler in Redhat Linux machine.

my code is

int i,no;
float sum = 0;
scanf("%d",&no);
for(i=1;i<=4;i++)
{
sum=sum+no%10;
no=no/10;
}
printf("Sum=%f",sum);
Mohan Giri is offline  
Old 04-05-2005, 11:38 PM   #2 (permalink)
 
Monster Techie

Join Date: May 2004

Location: /usr/root/mn/us

Posts: 1,121

bla!! is on a distinguished road

Default

I have no idea what would be wrong with that.
I compiled it using the gcc compiler and it worked perfeclty.

Even the fact that scanf doesn't check for wrong input, in this case it will either cast a non-int to an int, or interpret a string as its numerical value.

Maybe post the entire function to see if something else is causing it.
__________________
<br>
Its a frigging Laptop, not a Labtop!!!!
bla!! is offline  
Old 04-08-2005, 11:31 PM   #3 (permalink)
 
Newb Techie

Join Date: Apr 2005

Posts: 8

ashishjha54

Default

segmentation fault occurs when a program tries to access restricted part of memory.you can correct it easily.
ashishjha54 is offline  
Old 04-09-2005, 07:51 PM   #4 (permalink)
 
Monster Techie

Join Date: May 2004

Location: /usr/root/mn/us

Posts: 1,121

bla!! is on a distinguished road

Default

Yes, but how is this function accessing any memory other than its own?

From what we're given, it's a straight forward calculation. It doesn't recurse, and the function scanf prevents buffer overflows (for the most part).

I don't see how this function could wind up accessing any memory other than its own given to it by the OS.
__________________
<br>
Its a frigging Laptop, not a Labtop!!!!
bla!! is offline  
Old 04-10-2005, 12:13 PM   #5 (permalink)
 
Newb Techie

Join Date: Dec 2004

Posts: 46

ewomack

Default

Try a different complier and see if you get the same results.

Or, you could use pointers to see where the info is going; maybe you'll be surprised?
__________________
Ed Womack
Get Milked
ewomack is offline  
Old 04-14-2005, 09:56 PM   #6 (permalink)
 
Newb Techie

Join Date: Apr 2005

Posts: 6

f11m

Default

someone tell me what scanf does
f11m is offline  
Old 04-15-2005, 09:40 AM   #7 (permalink)
 
True Techie

Join Date: Dec 2004

Posts: 145

Mohan Giri

Default

scanf stores value which we enter through keyboard into memory in the location of no's address
Mohan Giri is offline  
 
Closed Thread

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 On
Pingbacks are On
Refbacks are On