Menu

I'm new to this, and have a question

jt1776
2009-02-16
2012-09-26
  • jt1776

    jt1776 - 2009-02-16

    to me, it looks like this code should work, can anyone tell me why it crashes when it gets to the scanf line?

    include <stdio.h>

    int main(void)
    {
    char ch;

    printf(&quot;Please enter a character.\n&quot;);
    scanf(&quot;%c&quot;, &amp;ch);   
    printf(&quot;The code for %c is %d.\n&quot;, ch, ch);
    
    return 0;
    

    }

    i'm using dev c++ 4.9.9.2

     
    • jt1776

      jt1776 - 2009-02-20

      I thanked him for his help, it was good information. this is free help from a comunity of people who are not paid. All I was trying to say, is if it is so much trouble to help somone, then why bother if you are going to talk down to them, that isn't helping that is discouraging. Helping people is more then giving them the answer to a problem, and I would say that you have it backwards as to who feels entitled. if no one answered me I would have moved on. I would not have demanded an answer or expect that somone had to take time to answer my question. I thank the both of you for the time you have taken to help, but I don't think I will burden you with anymore requests for help.

       
      • TexasOwl

        TexasOwl - 2009-02-20

        I am also a newbie. And yes I did get frustrated with some of the answers. I attribuite it to my lack of understaning and because of your knowledge many things are obvious to you.

        That said I want to thank you for taking up your time and helping us to learn. When one receives a gift they can accept it or reject it. But do not complain about the gift!!!!. If you do not appreciate it do not accept it.

        To complain and then ask for another gift is just ungratfull and demonstrates an entitlement mentality.

        Dick Laughlin

         
    • Wayne Keen

      Wayne Keen - 2009-02-20

      "but I don't think I will burden you with anymore requests for help."

      Perfect example of your selfish attitude.

      All you see in this place is someplace to get YOUR questions answered. The notion that YOU might contribute something here as you grow never occurs to you.

      This is a community of users. Not a help line. You probably understand the words, but clearly not what they mean.

      As Zero would have said, don't let the door hit you on the butt on the way out.

      Wayne

       
    • cpns

      cpns - 2009-02-20

      The sigh was simply because this issue has been covered many times on this and many other forums. It is exactly covered by FAQ 12.18a/b on the comp.lang.c FAQ list. http://c-faq.com/index.html

      Clifford

       
    • jt1776

      jt1776 - 2009-02-16

      a little somthing to add, I noticed that if I don't input anything and just press enter, it dosn't crash. I changed the main variable to and int, and added a printf to print the value before the scanf line, and the variable has a value of 2 but I don't know why.

       
    • cpns

      cpns - 2009-02-17

      > the variable has a value of 2 but I don't know why

      Before scanf() it is uninitialised so could have any value. Only static data is implicitly initialised to zero.

      > if I don't input anything and just press enter
      If you just press <enter>, then that is not 'nothing', the newline (or line feed) character is what gets assigned to ch. If you enter characters before <enter>, they will be buffered and the first character will be assigned.

      In what manner does this 'crash' manifest itself?

      The only problem I can see is in the printf(). Being a variadic function it has arguments of both undetermined number and undetermined type. For both it relies on the format specifiers you supply, and there are no runtime checks for validity. Consequently when you tell it to expect an int but pass a char, it may perform incorrectly. You should change that line as follows:

      printf( "The code for %c is %d.\n", ch, (int)ch ) ;

      note the type-cast.

      If you are using GCC (which you are if you are using Dev-C++), then add the -wformat -werror compiler options to trap this error. Unfortunately most other compilers don't have such an option. That said, many other compilers have better debuggers than Dev-C++, which is probably the best way to resolve many crashing code problems.

      Clifford

       
    • jt1776

      jt1776 - 2009-02-17

      thanks for the info, I will look into your sugestions. I had somone compile it in visual c++ and the compiled file runs fine. it seems to just be an issue with the GCC compiler.. maybe a setting or somthing is wrong. i'll mess around with it and see if I can get it running right.

       
    • cpns

      cpns - 2009-02-17

      > I had somone compile it in visual c++ and the compiled file runs fine.

      Yes I did that too.

      > it seems to just be an issue with the GCC compiler.

      Apply Occam's razor before making such wild assertions.

      Ask yourself; "Which could be wrong, my novice code, or the world's most ubiquitous compiler used in thousands of applications around the world (including Linux)?"

      Actually there is another aspect to this. MinGW/GCC is 'special', in order to run natively on Windows without a lot of *nix emulation baggage like Cygwin, it does not use the GCC library. It uses Microsoft's!

      I think you will find that I explained the problem. The fact that it runs in one compiler and not another, should not be taken as evidence that one compiler is broken. Passing an incorrectly matched argument to printf() is an example of "undefined behaviour". Whether the program crashes or not, is entirely dependent upon the actual behaviour. The 'actual' behaviour may change between compilers, executions, builds, compiler options, machines, OS versions, time of day, or any number of factors. It is entirely non-deterinistic.

      When something gets corrupted the consequences depend on what gets corrupted, when, and how, and how and when the corrupted data is subsequently used if it did not cause am immediate failure. C and C++ perform very little runtime checking so often failures go unnoticed or can be benign until code changes make them otherwise. Code changes that may be entirely unrelated to the point of failure.

      Modern OS's trap invalid memory accesses, but most memory faults corrupt other data within the same process, which the OS cannot determine to be invalid.

      Clifford

       
    • cpns

      cpns - 2009-02-17

      Oh.. and additionally, you never did answer the question: "In what manner does this 'crash' manifest itself?". It was not idle curiosity, it is an attempt to assist, if you want assistance, you ought to respond perhaps. Does it perhaps do any of teh following: Raise an exception?, Produce incorrect output? Report an error message?

      Clifford

       
    • jt1776

      jt1776 - 2009-02-17

      when you enter input it drops out without finishing the instructions to print it out. there is no error code, no warning, it just stops. it's just a simple exercise in a book. after I couldn't get it to work, I downloaded the examples off the publishers website, and it was identicle to my code, except for the comments. I tried compiling the code I downloaded and it had the same problem.

       
    • Wayne Keen

      Wayne Keen - 2009-02-18

      Are you saying that the display window simply closes.

      If so, this is NOT a crash.

      Wayne

       
    • jt1776

      jt1776 - 2009-02-18

      I have added getchar(); at the end just before return 0;. it exits the program after getting input, before executing the last printf line. I tried going through it step by step in the debugger and it worked once, but the next time I went through it it dropped out at the same place, after getting the input. i've also tried taking other code i've found online from websites that have tutorials, and all the code i've taken from there with scanf being used to take input, exit the program after taking input. you say it's not a crash, but I don't know how else to put it, the program exits without finishing the task.

       
    • cpns

      cpns - 2009-02-19

      sigh

      Console input is line oriented, so scanf() does not return until you have pressed ENTER, so if you press say "A<enter>", that's two characters, not one. The first gets assigned to ch by scanf() the second gets consumed by the getchar() call, so it returns without waiting for new input - it has what it needs already. The program then terminates normally, and the OS closes its window, so it never crashed, it just finished!

      Don't use getchar() use system("pause"), which will always wait regardless of the state of the process's console input buffer (because it runs in a separate cmd process, and does not use stdin in any case).

      Alternatively flush the line after the scanf like this:

      while( ch != '\n' && getchar() != '\n' ) ;

      So if you just press <enter> the while loop terminates immediately, but if you entered anything at all, everything after the first character will be consumed up to and including the end of the line.

      Another solution is to always get an entire line using fgets() for example, then process the string rather than the console input directly. You can use sscanf() for that, although in the case of extracting a single character it would be overkill.

      Clifford

       
    • jt1776

      jt1776 - 2009-02-20

      thank you, that had some useful information in it. however, when you start your post with sigh it makes you look like an ass. Your attitude comes across as elitist, and that me, and others who come here for help, are not worth your time to answer. I'm sorry if my lack of expertise with the c language is that much of a drain on you, i'm just learning, I'm sure you where new at this once too.

       
      • Wayne Keen

        Wayne Keen - 2009-02-20

        And your post makes you look like a selfish fool with an overgrown sense of entitlement.

        Let me make this clear for you, you are asking complete strangers for help
        for free. You get the help of a higly trained professional, and you whine
        like a 5 year old about it not being friendly enough for you.

        Get you head out of your selfish butt. This is not a paid support line.
        No one here owes you anything. They do what they do because they are
        great people. Clifford has helped literally thousands of people with
        problems, big and small for many years. What have you done, besides
        come here with your hand out?

        Wayne

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.