printf ultra-fast question

Discussion in 'C Programming' started by =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 14, 2006.

  1. 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
     
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 14, 2006
    #1
    1. Advertising

  2. 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.
     
    August Karlstrom, Feb 14, 2006
    #2
    1. Advertising

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

    tmp123 Guest

    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"?
     
    tmp123, Feb 14, 2006
    #3
  4. 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) <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.
     
    Keith Thompson, Feb 14, 2006
    #4
  5. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    Ben Pfaff Guest

    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
     
    Ben Pfaff, Feb 14, 2006
    #5
  6. 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
     
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 14, 2006
    #6
  7. 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) <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.
     
    Keith Thompson, Feb 14, 2006
    #7
  8. 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
     
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 15, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Julia Briggs

    * Ultra Newbie HTML Question *

    Julia Briggs, Nov 29, 2003, in forum: HTML
    Replies:
    2
    Views:
    372
    Spartanicus
    Nov 29, 2003
  2. We need more power captain
    Replies:
    3
    Views:
    6,709
    Muthu
    Feb 18, 2004
  3. John Salerno

    ultra newbie question (don't laugh)

    John Salerno, Sep 26, 2006, in forum: Python
    Replies:
    7
    Views:
    273
    Peter Otten
    Sep 26, 2006
  4. James

    Ultra-basic C++ question

    James, Oct 12, 2005, in forum: C++
    Replies:
    12
    Views:
    536
    Puppet_Sock
    Oct 14, 2005
  5. mark

    ultra-fast loop unrolling with g++ -O3

    mark, Jun 12, 2008, in forum: C Programming
    Replies:
    2
    Views:
    745
    santosh
    Jun 12, 2008
Loading...

Share This Page