Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > printf ultra-fast question

Reply
Thread Tools

printf ultra-fast question

 
 
=?ISO-8859-1?Q?Martin_J=F8rgensen?=
Guest
Posts: n/a
 
      02-14-2006
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

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
 
Reply With Quote
 
 
 
 
August Karlstrom
Guest
Posts: n/a
 
      02-14-2006
Martin Jørgensen wrote:
> 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

--
I am the "ILOVEGNU" signature virus. Just copy me to your
signature. This email was infected under the terms of the GNU
General Public License.
 
Reply With Quote
 
 
 
 
tmp123
Guest
Posts: n/a
 
      02-14-2006

Martin Jørgensen wrote:
> 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"?

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-14-2006
Martin Jørgensen <> writes:
> 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.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      02-14-2006
Martin Jxrgensen <> writes:

> 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.
--
"I ran it on my DeathStation 9000 and demons flew out of my nose." --Kaz
 
Reply With Quote
 
=?ISO-8859-1?Q?Martin_J=F8rgensen?=
Guest
Posts: n/a
 
      02-14-2006
Ben Pfaff wrote:
> Martin Jxrgensen <> writes:
>
>
>>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.


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

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-14-2006
Martin Jørgensen <> writes:
> 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.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
=?ISO-8859-1?Q?Martin_J=F8rgensen?=
Guest
Posts: n/a
 
      02-15-2006
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

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
printf affects following printf/s azza C Programming 0 10-17-2010 09:43 AM
Extracting printf(...) from (void) printf(....) guru Perl Misc 8 02-03-2009 10:37 PM
(void) printf vs printf whatluo C Programming 29 09-08-2005 05:42 PM
bus error with printf line included, error without printf line? ben C Programming 4 06-26-2004 04:42 PM
Question about evaluating the arguments of printf (maybe OT?) Edith Gross C++ 2 11-02-2003 12:16 PM



Advertisments