Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • Last Modified:

What is this "%" doing in this code and what is it called and used for in C++?

#include <stdio.h>
3:
4:  main()
5:  {
6:     char  c, *ptr_c;
7:     int   x, *ptr_x;
8:     float y, *ptr_y;
9:
10:    c = 'A';
11:    x = 7;
12:    y = 123.45;
13:    printf("c: address=%p, content=%c\n", &c, c);
14:    printf("x: address=%p, content=%d\n", &x, x);
15:    printf("y: address=%p, content=%5.2f\n", &y, y);
16:    ptr_c = &c;
0
awjackin35
Asked:
awjackin35
  • 3
  • 3
  • 3
  • +2
1 Solution
 
mnashadkaCommented:
The % tells the shell how to interpret the item in the variable argument list:
%c means character
%d means digit (integer)
%f means floating point.
0
 
mnashadkaCommented:
Sorry, there's more.  The %5.2f means show 5 characters with 2 decimal places, and the %p means address.
0
 
mnashadkaCommented:
Sorry, I keep doing this too fast.  They are format specifiers, and tell the compiler how to display characters.  C++ has shyed away from this a bit, preferring streams (this is from C), but there still are many times when this is more convenient.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
awjackin35Author Commented:
So where can I find th format specifiers? Are they in the header file?
0
 
AxterCommented:
0
 
hakobCommented:
It is standatd format specification for the pronting out the variables of various types in various form. This used in printf(), sprintf()  C-functions.

In the first line, f.e.

printf("c: address=%p, content=%c\n", &c, c);

instead of %p will be printed the address of c variable,
instead of %c - its value.
So, %p means the address formar type, %c - the cahracter format type.
%d means the decimal integer number format,
%f is the floating point number format. %5.2f in your
code means that it will print the floating variable y, resting only first 2 digits after the point and the total width (number of digits, including the point) of your number is 5.

For more information see the microsoft site below.

http://msdn.microsoft.com/library/wcedoc/wcecrt/crt2_89.htm
0
 
MoCommented:
It called a modular. it is used in this case as a pointer reference. it formats the value to be out putted.
0
 
AxterCommented:
Hi (hakob), welcome to EE.
All of the experts here, for the most part have learn from other experts as to the proper etiquette for posting answer.

An answer should not be posted as an answer, if other experts have previously posted possible answers as comments, and/or have already made contributions to the question.

There are many experts who never post answers as answer.  Instead, they post their answers as comments.
If you read the following link, you'll see why this is the preferred method for many of our valued experts, including myself.

http://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp


Hi (awjackin35):
Feel free to click the [Reject Answer] button near (Answer-poster's) response, even if it seems like a good answer.
Doing so will increase your chance of obtaining additional input from other experts.  Later, you can click the [Select Comment as Answer] button on any response.
0
 
AxterCommented:
hakob,
It is agaisnt EE policy to post an answer using a previous experts comments.
In the future, post answer as comment.
0
 
awjackin35Author Commented:
The answer was acceptable but I have to follow the rules.
0
 
awjackin35Author Commented:
HI mnashadka
    Your answer was ver informative to me and thanks!!!!!!!

Mr. Hakob
     Your answer was very good as well but I made the same mistake on the Vb forum and so since they are very critical of the protocal on the forums I have to follow the rules.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now