Jump to content
xisto Community
jose_a1

Crashing Command Interpretrer Whats wrong with this thing?

Recommended Posts

I'm new to C and i tried to make a C program that accepts an 5-digit integer and prints the middle digit. So I compile the program and no errors are found. The source code is:#include <stdio.h>long a,b;main(){ printf("Enter a 5-digit number");}

Share this post


Link to post
Share on other sites

I am sorry I accidentaly pressed the submit button. Ok as I was saying...I'm new to C and i tried to make a C program that accepts an 5-digit integer and prints the middle digit. So I compile the program and no errors are found. The source code is:#include <stdio.h>long a,b;main(){ printf("Enter a 5-digit number"); scanf("%d", a); b=(a%10000)%1000)/100; printf("%d",:ph34r:;}I feel the source code might be wrong right now as I type but I got it right in class. hehe. But whenever I run it on the command interpreter on Windows XP, the command interpreter crashes and nothing happens. I type the same code in Linux and after compiling and pressing the run button it works fine. Is something wrong with my programming, is there something I have to know or is it just that I should not be using Windows XP's command interpreter?

Share this post


Link to post
Share on other sites

Ok, corrected the typos here:

#include <stdio.h>long a,b;main(){          printf("Enter a 5-digit number");          [B]scanf("%d", a);[/B]          b=((a%10000)%1000)/100; // Missing '(' inserted          printf("%dl",b); // Made 'b' lowercase. }
The mistake in the code is in the scanf line. This needs to be changed to:
scanf("%d",&a);

This is down to the way pointers are handled, here's a quick summary:

1) All variables are designated an area of memory.

2) This area of memory's address can be refered to using a unique number the size of the local architecture's memory addressing.

3) When referring to a variable by name (with no modifiers), the value at it's address is pulled out behind the scenes and passed to whatever situation it is being used in.

4) Functions can be passed variables in two ways. The first is pass by value - a copy is taken of the variable, new space allocated for it and that then passed into the function. With this method the original variable is safe from unintended modification. The second is pass by reference. With this the location of the variable in memory is passed in so that the function can modify it. The reference of a variable is found by prefixing a '&' in front of it's name.

So, what was happening here was that scanf requires a pass-by-reference so that it can place the keyboard input into the desired variable. You were passing in a copy of that variable instead. As the variable was a number, the function thought that the number stored in that variable was an address in memory for it to store things, so it went to that random location instead and attempted to write to it. Windows tries not to allow that sort of thing (one application randomly writing to bits of memory that other applications are using) and shrieked.

Hope that explains it all.


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.