?
Solved

Adding a percentage sign - Borland and MS C++

Posted on 2004-12-01
17
Medium Priority
?
604 Views
Last Modified: 2011-10-03

Hi,

In a program I compiled in Borland I added the % sign by doing the following %%

However when I compile in C++ the % sign does not appear in the output.

Any idea's what I should be adding to get this to work ?
0
Comment
Question by:andyw27
  • 5
  • 3
  • 3
  • +5
17 Comments
 
LVL 4

Expert Comment

by:skypalae
ID: 12713985
what about "\%" ??
S.
0
 
LVL 3

Expert Comment

by:udo_borkowski
ID: 12713992
      printf("Sample %%");

works for me, at least in Visual Studio 6.

Can you give your code fragment?
0
 
LVL 3

Expert Comment

by:udo_borkowski
ID: 12714012
S.: "\%" is an undefined escape sequence

(see Stroustrup C++ Ref 2.5.2 etc.)
0
Become an IT Security Management Expert

In today’s fast-paced, digitally transformed world of business, the need to protect network data and ensure cloud privacy has never been greater. With a B.S. in Network Operations and Security, you can get the credentials it takes to become an IT security management expert.

 
LVL 4

Expert Comment

by:skypalae
ID: 12714032
ok ok .. it was just a guess :)
S.
0
 

Author Comment

by:andyw27
ID: 12714069

Strange it does come after:

            printf("%f"    , var1);

If that any help ?
0
 
LVL 3

Expert Comment

by:udo_borkowski
ID: 12714093
>>Strange it does come after:
>>
>>            printf("%f"    , var1);
>>
>>If that any help ?

What means "it does come after"?
Can you show me the (Borland) code that actually has the "%%" included and that does no appear in VS?

0
 
LVL 11

Expert Comment

by:avizit
ID: 12714332
udo_borkowski is correct

http://www.faqs.org/faqs/C-faq/abridged/


12.6:      How can I print a '%' character with printf?

A:      "%%".

I presume what comp.lang.c says is ANSI C , so if any compiler doesn't support that it isn't ANSI C and you are better off staying away from it
0
 
LVL 11

Expert Comment

by:avizit
ID: 12714361
yes another way would be to use its ascii value
see the following test program

#include <stdio.h>
int main(){
printf("%c\n",37);
return 0;
}

37 is the ascii value of the '%' character in decimal
0
 
LVL 4

Expert Comment

by:skypalae
ID: 12714432
avizit,
this is even better :)

printf ("%c",'%') ;

S.
0
 
LVL 11

Expert Comment

by:avizit
ID: 12714486
hehe ;) I know I don't see the obvious so many times .. it has become routine now :)
0
 
LVL 2

Expert Comment

by:arun80_inin
ID: 12715420
try this
this is guess only
for example
int n;
if u want to print the % before the value of n
then
printf("%");
printf("%d",n);

or after the value of n
printf("%d",n);
printf("%");

if it is not work out  replace printf("%") with printf("\%")

0
 
LVL 3

Expert Comment

by:udo_borkowski
ID: 12715672
arun80_inin: we had this before.
"\%" is an undefined escape sequence

and for
     printf("%")
I would assume that the result is depending on the implementation of "printf" (i.e. undefined)

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12720474
Just to reforce previous ideas, this will work in EVERY compiler:
printf ("%f%c",somedouble, '%')
0
 
LVL 7

Expert Comment

by:aib_42
ID: 12744193
Please stick to using "%%" and run like the plague from any compiler that does not like it.
0
 
LVL 8

Accepted Solution

by:
adg080898 earned 1000 total points
ID: 12762185
Remember that the % signs are not interpreted by the compiler, the printf function is responsible for detecting and handling it. If there is a problem which causes %% to fail, it is the fault of the implementation of the printf function.

Some compilers will automatically optimize a printf to a puts when it is better. For example, the following two lines are exactly equivalent:

printf("This is a test!\n");
puts("This is a test!");

It is possible that the compiler is optimizing the printf into a puts, but failing to handle the % sequences properly.

To see if this is the case, try tricking the compiler into keeping printf:

printf("This is a test!%s\n", "");
0
 
LVL 3

Expert Comment

by:udo_borkowski
ID: 12763277
adq: Just curious: What compiler do you know that does this "puts instead of printf" trick?
0
 
LVL 8

Expert Comment

by:adg080898
ID: 12863540
gcc will optimize simple printf ending with \n into a puts. There is a compile switch to disable it. It's an intrinsic function thing.

Don't know which others do.
0

Featured Post

Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

601 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