December 5th, 2013, 05:37 PM
APPCRASH Unknown Reason !Beginner!
Hi everyone, Im just getting into C and I wrote this extremely simple program for an exercise in my book. The program crashes a few seconds after "Age:" has been shown and I just dont get why.
This is the Code:
int Age, NextAge;
NextAge = Age + 1;
printf("Hi %s, You are %d years old, next year you will be %d", Name, NextAge);
Please help me out!
December 5th, 2013, 05:59 PM
What did you input for "Name: "? Only a single character? Or more than one character. Because all that the first scanf will do is to read in a single character, leaving the rest of the multi-character input for "Name: " in the input buffer to be read by the next scanf. But since that next scanf is expecting a digit, when it gets a letter instead then it will fail, not update Age (so that whatever garbage value it contains will remain unchanged), and proceed on to the NextAge assignment statement.
If instead you wanted to store a complete name in Name, then you need to declare it as a char array and you would need to give scanf a "%s" instead of a "%c".
So then, just what is it that you want to do?
And are you sure that the program actually crashes? Or does it display the "Hi <the first char you entered followed by an unknown amount of garbage> ..." message with the NextAge field being filled with a weird and probably very large number?
Since you have written this program to be run from the command line, I assume that that is what you did. I mean, you didn't do anything so stupid as to run it from within a GUI IDE, right? Because if you did, then the instant the program terminated the console that had been opened for it would close, probably too quickly for you to see the output of that printf function call. But who would write a program that is specifically intended to be run from the command line -- ie, from a console that has already been opened -- and then run it from a GUI application, such as an IDE ("integrated development environment" which combines editor, compiler, linker, debugger, project manager, etc; commonly misnamed "my compiler")?
If you were indeed so foolish and intend to persist in your foolhardiness, then you need to keep that console (AKA "shell", AKA "cmd prompt", AKA "terminal", AKA "DOS window") from closing, the more common way being to keep the program from terminating until you explicitly tell it to. Read this thread on this forum, program ends in miliseconds? , for various suggestions of how to do that.
And OBTW, in the future always format your code properly and use code tags to keep HTML from stripping out its indentation when you post it here.
December 5th, 2013, 06:15 PM
I gave my name as Input, and even after I changed the %c to %s this made no diffrence. I did write this to run from the Command Line.
The program does acctually crash, it seems to be that this crash happens after a said amount of time has passed after launching it. I can enter my name and age if I am fast enough but the last printf will never occur
December 5th, 2013, 07:09 PM
Did you also change Name into an array? Because if you don't, then you will indeed cause a crash now, but not before.
I couldn't see anything in your first posting that would cause a crash, just an "ignoring" of the Age input and immediate completion of the program and the closing of the console if you had launched it from a GUI.
You say that you did write it to run from the command line. Are you indeed running it from the command line? Stupid question, but I do have to ask it.
You say that the program does actually crash. What are the indications of that? What error message does the operating system display to inform you of the crash?
As you were!
I just tried to compile your program -- MinGW gcc on WinXP with all warnings turned on:
Those Line 15 warnings correspond to this line:
The warning, "format argument is not a pointer (arg 2)", refers to the argument, Name, which "%s" tells printf should be a char pointer or array, whereas Name is actually just a single char. If you were to declare Name as a char array, that would go away and the scanf to read in Name would work (assuming the change from "%c" to "%s").
printf("Hi %s, You are %d years old, next year you will be %d", Name, NextAge);
Since printf would be trying to use the value of the single character in Name as an address and since that value is less than 127 (you say it's 'I', which is a value of 73), then address 73 would be deep down in the operating system's memory, which is very strictly forbidden for any user program to go and which will cause the offending program to be terminated with extreme prejudice with a citation for either access violation or SEGFAULT.
Give printf an actual address of a string, which means that Name has to be changed to an actual valid C-style string.
The warning, "too few arguments for format", refers to the fact that the format string says you're going to give it three arguments, whereas you only give it two. You lied to the compiler. You must never lie to the compiler:
What would happen is that printf will go looking for that third argument where it expects it to be. Since that location is being used for something else, it will contain garbage, such that that last "%d" field will display a garbage value. Of course, the other %d field also will display garbage, since you never change the garbage value of Age and NewAge is just garbage plus one.
Originally Posted by Henry Spencer
You know what you need to fix. Make the corrections and post the new code. Properly indented and with code tags. If you need an indent style, you should use the Allman style (the K&R style that you appear to be gravitating towards requires strict adherence to consistent indenting, which you appear to have no interest in doing).
And actually declare main to be int instead of using an implicit default. Compilers are starting to get sick and tired of that. And be sure to return 0 at the end of main.
Of course, my question is, "Why did you ignore those warnings?" Never ignore warnings! Warnings are much more important than error messages.
Also, knowing what OS and compiler you're using would help us.
Last edited by dwise1_aol; December 5th, 2013 at 07:13 PM.
December 6th, 2013, 03:37 AM
I always run the program using CTRL + F5 so that it debugs without debugging or w/e, the teacher said something like that. & Whoa these explanations are so in detail lol, I don't understand half of what you are saying haha :D
But I changed name into an Array I made it and now it runs! Thanks for all the help, I understand how you might be frustrated that I didn't get this myself, I am an utter N00b tho.
December 6th, 2013, 09:40 AM
My approach to these kinds of problems is that C is a language and we use languages to communicate, so we use programming languages to communicate to the computer what we want it to do. Plus there's an old programming saying (which was even used in the 1980's movie, "Short Circuit"): "Computers don't do what you want them to do. They only do what you tell them to do." When we write a program, we are telling the computer what to do. If we tell the computer to do something different from what we want it to do, then we have a problem. So when we read a computer program, we need to read it for its content, for what we are telling the computer to do, and compare that with what we want it to do.
With all that in mind, I read your program for what it was telling the computer to do. I feel that you will learn more if you are told why what you wrote was wrong than if you are only told "No, that is wrong." Because I feel that you need to learn to read code for content.
BTW, what's with that Ctrl-F5? And does "debugs without debugging" mean that you tell it to compile and run all in one action? From what somebody else described recently, it sounds like that will not allow you to read the warnings that the compiler generates. As I demonstrated in solving your problem, being able to read the warnings is absolutely essential to the process. If Ctrl-F5 does not allow you to read the warnings, then it is an extremely bad idea to use it!
Are you using Microsoft Visual Studio? I've worked on some sizable projects using Visual Studio -- BTW, I'm a software engineer with 30 years professional experience. I absolutely never ever just tell an IDE to build and run. I always first build the project, then I look at the build output for errors or warnings, and only then do I run it. Since I have buttons for those actions on the tool bar, it's only two clicks of the mouse (ignoring any need to scroll through the output window, but then if I have to do that then I wouldn't want to run the program).
Never hide the warnings from yourself. Never ignore warnings.
December 6th, 2013, 03:19 PM
Thats some good advice which I will certainly think of when I'm making my exam tomorrow :) . I usually write more complicated stuff than this, its only that it has been a while since I last coded and I skipped kind of alot of lessons this semester (oops) so I had to get back on track for a second. All is going fine now :D.
And I have honestly no Idea why the teacher is making us do this, I'll be sure to ask him! But for now Im just going to stick with his rules since I don't want to take any risks to possibly screw over my exam tomorrow lol :D