NO

Author Topic: I have this error see...  (Read 4182 times)

deyan8

  • Guest
I have this error see...
« on: July 25, 2012, 06:20:22 AM »
I have typed the following program in C:

#include <stdio.h>

main(){
   double pi = 3.14159926636;
   printf("The value of pi is approx %f \n", pi);
   printf("The value of pi in integer %d \n", pi);
   printf("The value of pi in character %c \n", pi);
   printf("The value of pi in string %s \n", pi);
   printf("The vlaue of pi in memory adress %p", pi);
   getchar();
}



Everytime I hit compile everything appears to be fine and no errors are displayed (warnings yes but no errors) however once I hit execute the following error is displayed, "An unhandled win32 exception occured in NameofFile.exe[196]. Just-In-Time debugging this exception failed with the following error: No installed debugger has Just-In-Time debugging enabled. In Visual Studio, Just-In-Time debugging cab be enabled from Tools/Options/Debugging/Just-In-Time. Check the documentation index for 'Just-in-time debugging. errors' for more information." I should also point out that everything up to the character is printed but not the string nor the memory adress.  Any way I went to Tools/Options/Debug and checked the Just-in-time debugging option. When I run the program again, however, it gives me the following message, "Exception: Access Violation" and then opens up some sort of debugger window that is apparently stopped and to be honest I do not really know what it does. Either way the rest of  the program does not get displayed even with the Just-in-time debugging option turned on. So any help is appreciated here. Just to know since I am a beginner in programming in general the more detailed explanation or solution to the problem the better. Thank you!

CommonTater

  • Guest
Re: I have this error see...
« Reply #1 on: July 25, 2012, 06:45:35 AM »
I have typed the following program in C:

There's a few problems....
Code: [Select]
#include <stdio.h>

main(){    <---- should be    int main (void)
   double pi = 3.14159926636;
   printf("The value of pi is approx %f \n", pi);
   printf("The value of pi in integer %d \n", pi);
   printf("The value of pi in character %c \n", pi); 
   printf("The value of pi in string %s \n", pi);      <--- can't represent double as string without conversion
   printf("The vlaue of pi in memory adress %p", pi);  <-- need to convert pi to an address -- &pi
   getchar();   <--- unnecessary inside the IDE
 
  <---- need to return a value to the os ... usually   return 0;
}   

so you end up with...
 
Code: [Select]
#include <stdio.h>
 
int main (void)
  { double pi = 3.14159926636;
 
    printf("The value of pi is approx %f \n", pi);
    printf("The value of pi in integer %d \n", pi);
    printf("The value of pi in character %c \n", pi);
    printf("The vlaue of pi in memory adress %p", &pi);
    return 0; }
Quote
Just to know since I am a beginner in programming in general the more detailed explanation or solution to the problem the better. Thank you!

Ok... first you need to go into pelles C Tools->Options->Debug and uncheck "Just in time debugging".
 
Next you need to get into Pelles C helpfile and look at the correct way to create projects... so that you get the compiler and linker working properly on your code.
 
You should not ignore warnings... they are a very strong hint your program is going to misbehave when you run it.
 
Finally, spend some time with your F1 key... Place the text cursor on any highlighted word in your source code and press F1 on your keyboard.  This will give you a full explaination of the keyword or library functions and their correct usages.  (Pelles C has just about the best help file I've ever seen...)
 
Hope that helps...
 

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: I have this error see...
« Reply #2 on: July 25, 2012, 01:43:03 PM »
Following previous answer I would add first of all that Just in time debugging is not a compilation option. It enables the debugger to run automatically each time a program generates an exception (when a program 'crashes').
The name of a variable, like 'pi', is just a placeholder to locate the value in memory. So you cannot print as a string whichever variable. A string variable give the address of the sequence of chars in memory, printing 'pi' as a string use its value as a memory address, that is inconsistent. That's why you get memory violation error, you are trying to read a memory that doesn't exist.
The other format are consistent, but if you know what you are doing.
The compiler provides automatic conversion between compatible formats, so a float can be an integer.
Anyway while converting a float to a char, that is an 8 bits wide integer, is not so legal, converting to a char array (string)  or an address is absolutely wrong.
The compiler let you do it just because under some special circumstances it is usefull, but this is a very very advanced topic.
Probably you have to read something on programming principles, data type and data access.
Go on more you learn more you'll find it nice  ;D
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

CommonTater

  • Guest
Re: I have this error see...
« Reply #3 on: July 25, 2012, 02:12:25 PM »
Since Frankie didn't mention it and I forgot...

To convert the double pi (or any other numerical value) to a string you need to do a conversion.
I generally use something like sprintf() or swprintf() for such a task. You should look them up in the help file for full details.

For example:
Code: [Select]
#include <stdio.h>
 
int main (void)
  { char szpi[20];  // space for string of pi
 
   double pi = 3.14159926636;
 
   // print to string
   sprintf(szpi,"%f",pi);
 
   // print to screen
   printf("%s\n",szpi);
 
   return 0; }

C isn't a high level language that does these conversions behind the scenes. If you want something done you need to tell it to do it.  For a beginner this looks like a lack of capability but as you become more experienced you will discover this "nuts and bolts" level programming is far more flexible and powerful than many higher level languages.
 
-- Hi Frankie! :D
 

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: I have this error see...
« Reply #4 on: July 25, 2012, 04:01:40 PM »
Hi Tater  ;D
Considering the errors I think that deyan8 is just a beginner.
Probably he need to read some more about variables, conversions, formats......
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: I have this error see...
« Reply #5 on: July 25, 2012, 08:11:04 PM »
I have typed the following program in C:
<snip>
Just to know since I am a beginner in programming in general the more detailed explanation or solution to the problem the better. Thank you!
Well, in order to give you a more detailed explanation, may I ask as to "why" did you create this program? What was the rational behind this test?

Ralf

CommonTater

  • Guest
Re: I have this error see...
« Reply #6 on: July 28, 2012, 06:17:28 PM »
Well, in order to give you a more detailed explanation, may I ask as to "why" did you create this program? What was the rational behind this test?

The OP appears to have disappeared... But he did say in his original post that he was a beginner, so I'm guessing he was just experimenting with formatting strings for printf() ... maybe as part of a lesson from his books.  This I think is a good thing.