Computers |
|
| | #1 (permalink) |
| True Techie Join Date: Dec 2004
Posts: 145
| 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); |
| | |
| | #2 (permalink) |
| Monster Techie Join Date: May 2004 Location: /usr/root/mn/us
Posts: 1,098
| 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.
__________________ ![]() Its a frigging Laptop, not a Labtop!!!! |
| | |
| | #4 (permalink) |
| Monster Techie Join Date: May 2004 Location: /usr/root/mn/us
Posts: 1,098
| 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.
__________________ ![]() Its a frigging Laptop, not a Labtop!!!! |
| | |