| | 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"); } |
|
|
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", } 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?
QUOTE(jose_a1 @ Sep 19 2005, 04:45 PM) Ok, corrected the typos here: CODE #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: CODE 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.
Similar Topics
Keywords : crashing, command, interpretrer, whats, wrong, thing
Looking for crashing, command, interpretrer, whats, wrong, thing
|
|
![]() Crashing Command Interpretrer - What's wrong with this thing? |
| ADD REPLY / Got an Opinion! | Remove these ADs! | RAPID SEARCH! | Free Web Hosting | [X] |
|
Express your Opinions, Thoughts or Contribute more info. to help others. Ask your Doubts & Queries to get answers, So that "Together We can help others!" |
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP. | 500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE |
|