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

Posted on 2001-06-20
Last Modified: 2010-04-02
#include <stdio.h>
4:  main()
5:  {
6:     char  c, *ptr_c;
7:     int   x, *ptr_x;
8:     float y, *ptr_y;
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;
Question by:awjackin35
  • 3
  • 3
  • 3
  • +2

Expert Comment

ID: 6212515
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.

Expert Comment

ID: 6212518
Sorry, there's more.  The %5.2f means show 5 characters with 2 decimal places, and the %p means address.

Accepted Solution

mnashadka earned 30 total points
ID: 6212523
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.
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.


Author Comment

ID: 6212565
So where can I find th format specifiers? Are they in the header file?
LVL 30

Expert Comment

ID: 6212579

Expert Comment

ID: 6213141
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.

Expert Comment

ID: 6213394
It called a modular. it is used in this case as a pointer reference. it formats the value to be out putted.
LVL 30

Expert Comment

ID: 6213712
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.

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.
LVL 30

Expert Comment

ID: 6213718
It is agaisnt EE policy to post an answer using a previous experts comments.
In the future, post answer as comment.

Author Comment

ID: 6214835
The answer was acceptable but I have to follow the rules.

Author Comment

ID: 6216600
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.


Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
TTimer no longer functioning 7 91
Grammars for C C++ and java 1 122
Socket Programming (Unix) 8 128
C++ question 3 65
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

773 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