.exe has stopped working

Solved
goodby -  
[Dal] Posted messages 6122 Registration date   Status Contributor Last intervention   -
I wrote a program in C language, after its compilation it shows me the message .exe has stopped working.
I don't understand why?? I'm using Code::Blocks
If someone could answer me, thank you in advance.

2 answers

  1. TECHSYS
     
    Hello,

    Could you provide us with the source code of your program?

    Best regards,
    TECHSYS
    0
    1. goodby
       
      oui bien sûr
      #include<stdio.h>
      int main()
      {
      char c;
      int myInt1;
      int myInt2;
      printf("Enter the number of hours:");
      scanf("%d", &myInt1);
      myInt2=myInt1*3600;
      printf("there are %d seconds for %d hours", myInt2, myInt1);
      c=getchar();
      putchar(c);
      printf("you just need to multiply by 3600");
      return 0;
      0
  2. [Dal] Posted messages 6122 Registration date   Status Contributor Last intervention   1 108
     
    Hi goodbye,

    the message indicating that your program has stopped working is due to the fact that you are writing in a memory area not allocated by your program

    to capture an int, scanf needs a pointer to a variable of type int, so you must pass
    scanf("%d",&myInt1);
    , for example, for the first scanf (the same for the second) - these errors are the reason for the crash

    your printf does not have the correct syntax: you need to place the integer variables corresponding to your two %d specifiers outside the quotes, separated by commas. Here, printf expects for a %d data of type int, not pointers, so the & are inappropriate. Ultimately, you should do
    printf("there are %d s for %d h\n", myInt2, myInt1); 


    there is also a missing closing brace in what you posted, at the end of your code, but I assume that is a copy-paste error

    Dal
    0
    1. goodby
       

      Alright, thank you very much for your help, but I didn't understand when to use & and when not to use it in a pointer?

      0
    2. [Dal] Posted messages 6122 Registration date   Status Contributor Last intervention   1 108
       
      You should review your course or a C manual

      essentially:

      you do not put & in front of a variable if what you want to pass to the function is the content of the variable - in this case the function works on a copy of the value stored in memory in the space reserved for this variable and this copy is located in another memory area that the function has access to during its execution

      you put a & in front of the variable if what you pass to the function is the memory address where this content is located - in this case, the function gets a pointer to this memory address and uses it by dereferencing the pointer passed to access the data, it can then read and modify the pointed content whose address has been passed

      strings in C are special because they are:
      • pointers to char (pointing to a memory area allocated to the program where a succession of characters terminated by the null character
        '\0'
        can be stored)
      • or arrays of chars (storing the same type of data)

      for strings, you do not need to prefix them with & to indicate that you are passing a memory address, because they are already pointers or are understood as such (if the string is declared as an array of chars, passing the array will be understood as passing a pointer to the memory address of the first element of the array)

      finally, when you use standard C functions, you do not choose "at random" how to pass your values, you must do so according to what is expected by the standard operation of that function - and you determine that simply by looking at the function's documentation, as I indicated to you.
      0