How to add the % character to a string

Posted on 2005-04-26
Last Modified: 2012-05-05
fprintf (pFile, "\nEfficiency:   %.1f %c",mynumber,"%")
fprintf (pFile, "\nEfficiency:   %.1f \%",mynumber)
fprintf (pFile, "\nEfficiency:   %.1f %",mynumber)

... none of these work.

output wanted:

Efficiency:     82.2%
Question by:galneweinhaw
    LVL 30

    Expert Comment

    Hi galneweinhaw,
    > >fprintf (pFile, "\nEfficiency:   %.1f %",mynumber)

    Use two very every one.
    fprintf (pFile, "\nEfficiency:   %.1f %%",mynumber)

    David Maisonave :-)
    LVL 30

    Accepted Solution

    In your string when you need to represent a %, you replace the single character with two of them.

    LVL 55

    Assisted Solution

    by:Jaime Olivares
    1rst case would work but with a char argument:
    fprintf (pFile, "\nEfficiency:   %.1f %c",mynumber,'%');   // don't use double quotes

    2nd and 3rd case won't work, unique alternative (and most used) is what Axter exposed.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
    This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now