printf ultra-fast question

  • Thread starter =?ISO-8859-1?Q?Martin_J=F8rgensen?=
  • Start date
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Hi,

Perhaps a stupid question but I wonder if this isn't possible using C:

printf("\nOr push <enter> for default (which is theta=%ld)%s", theta, ": ");


It would be nicer to have it on one line... (I know this works):

printf("\nOr push <enter> for default (which is theta=%ld)", theta);
printf(": ");*/


Compiler: MS Visual studio 2005. The compiler doesn't complain actually
(well, at least not about the particular line). But when I run the
program VS2005 opens a window named "Visual Studio Just-In-Time
Debugger" and then it wants me to choose between two possible debuggers
(1: New instance of VS 2005, 2: My project). I then tell it that I don't
want to debug the program just stops. If I instead tell it that I want
to debug it, it says: "Unhandled exception at 0x10227.... (msvcr80d.dll)
Access violation reading...

What is wrong?


Med venlig hilsen / Best regards
Martin Jørgensen
 
A

August Karlstrom

Martin said:
Perhaps a stupid question but I wonder if this isn't possible using C:

printf("\nOr push <enter> for default (which is theta=%ld)%s", theta, ":
");


It would be nicer to have it on one line... (I know this works):

printf("\nOr push <enter> for default (which is theta=%ld)", theta);
printf(": ");*/


Compiler: MS Visual studio 2005. The compiler doesn't complain actually
(well, at least not about the particular line). But when I run the
program VS2005 opens a window named "Visual Studio Just-In-Time
Debugger" and then it wants me to choose between two possible debuggers
(1: New instance of VS 2005, 2: My project). I then tell it that I don't
want to debug the program just stops. If I instead tell it that I want
to debug it, it says: "Unhandled exception at 0x10227.... (msvcr80d.dll)
Access violation reading...

What is wrong?

If I remember correctly you can just write the strings one after the
other like

"first string" "second string"

The compiler will turn it into a single string constant.


August
 
T

tmp123

Martin said:
Hi,

Perhaps a stupid question but I wonder if this isn't possible using C:

printf("\nOr push <enter> for default (which is theta=%ld)%s", theta, ": ");


It would be nicer to have it on one line... (I know this works):

printf("\nOr push <enter> for default (which is theta=%ld)", theta);
printf(": ");*/

Check type of "theta"?
 
K

Keith Thompson

Martin Jørgensen said:
Perhaps a stupid question but I wonder if this isn't possible using C:

printf("\nOr push <enter> for default (which is theta=%ld)%s", theta, ": ");


It would be nicer to have it on one line... (I know this works):

printf("\nOr push <enter> for default (which is theta=%ld)", theta);
printf(": ");*/


Compiler: MS Visual studio 2005. The compiler doesn't complain
actually (well, at least not about the particular line). But when I
run the program VS2005 opens a window named "Visual Studio
Just-In-Time Debugger" and then it wants me to choose between two
possible debuggers (1: New instance of VS 2005, 2: My project). I then
tell it that I don't want to debug the program just stops. If I
instead tell it that I want to debug it, it says: "Unhandled exception
at 0x10227.... (msvcr80d.dll) Access violation reading...

What is wrong?

What's wrong is that you're not giving us enough information. Post a
small complete compilable program that exhibits the problem.

Your first printf() statement should be equivalent to:

printf("\nOr push <enter> for default (which is theta=%ld): ", theta);

That should be ok *if* theta is of type (signed) long (though the
output might not appear immediately unless the printf() is follwwed by
fflush(stdout)).

Why are you putting the ": " in a separate argument? Since "%s" and
": " are both 2 characters long, it doesn't even save you any space in
your source.
 
B

Ben Pfaff

Martin Jxrgensen said:
Perhaps a stupid question but I wonder if this isn't possible using C:

printf("\nOr push <enter> for default (which is theta=%ld)%s", theta, ": ");

That's a strange invocation of printf(). I would write it like
this:
printf("\nOr push <enter> for default (which is theta=%ld): ", theta);

Also, as another poster commented, check that `theta' is really a
long int.
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Ben said:
That's a strange invocation of printf(). I would write it like
this:
printf("\nOr push <enter> for default (which is theta=%ld): ", theta);

Also, as another poster commented, check that `theta' is really a
long int.

Aah, stupid mistake. theta was double, so the following works:

printf("\nOr push <enter> for default (which is theta=%g)%s", theta, ": ");

That explains the strange behaviour from vs2005... Thanks everyone.


Med venlig hilsen / Best regards
Martin Jørgensen
 
K

Keith Thompson

Martin Jørgensen said:
Ben Pfaff wrote: [...]
Also, as another poster commented, check that `theta' is really a
long int.

Aah, stupid mistake. theta was double, so the following works:

printf("\nOr push <enter> for default (which is theta=%g)%s", theta, ": ");

That explains the strange behaviour from vs2005... Thanks everyone.

Great -- but it still doesn't explain why you pass the ": " as a
separate argument, rather than the simpler:

printf("\nOr push <enter> for default (which is theta=%g): ", theta);

My guess is that it's a random change you made while trying to track
down the problem. Now would be a good time to back it out.
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Keith Thompson wrote:
-snip-
Great -- but it still doesn't explain why you pass the ": " as a
separate argument, rather than the simpler:

printf("\nOr push <enter> for default (which is theta=%g): ", theta);

My guess is that it's a random change you made while trying to track
down the problem. Now would be a good time to back it out.

Ah, I see... Actually I think I did it that way because that is how I
did some matlab programs for some months ago... Don't know if matlab
understands printf("\nOr push <enter> for default (which is theta=%g):
", theta);, but perhaps you're right...

Yeah, you're right. I should change the line... I'll do that tomorrow.
Gotta get some sleep now. It must be a bad habit or something, but the
important thing is that both methods work :)


Med venlig hilsen / Best regards
Martin Jørgensen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,900
Latest member
Nell636132

Latest Threads

Top