Printf Error In Dev C++

Thanks for answering but i still get the same output, the printf doesn't show inside of dev c, do you have any other suggestion? And i will make my codes clearer sorry I'm new:( thanks again. – Enrique Ortiz May 27 '15 at 2:13. Jan 23, 2010  Okay, if I ever use the printf or cont function, the window will quickly close itself out. I know how to fix this when I am using the cont function (by putting cin.ignore right after it), but how do I fix this when using the printf function. Can anyone help me? I accidently put D-C in the topic title, it should read Dev-C.

Most examples you see of C++ use the so-called stream output for the code. Stream output uses the << operator, as shown in this example:

However, C++ inherits another form of output from its predecessor, C. This form is based upon a set of functions that are very similar both in appearance and in the way they function. Collectively these functions carry the name of their most widely used member, printf().

You can ignore this article and continue using stream output, or you can switch over to printf() output if you prefer — but you should not mix the two in the same program. These sets of functions use different classes for buffering output to reduce the number of disk accesses, thereby increasing program performance. Mixing the two will cause output to get interleaved in unpredictable ways resulting in confusing and perhaps meaningless output.

The general form of printf() output

The printf() function has the following prototype defined in the cstdio include file:

The ellipses () in a prototype declaration means any number of any type of variables.

The first argument to printf() is a string to be output. If this string contains format specifiers, which are characters preceded by a ‘%’, then printf() outputs the next argument in line using that format specifier as guidance.

This is best demonstrated with a simple example:

This would output the string

There must be at least as many arguments following the format string as there are format specifiers in the string. If there are more, they are ignored. printf() returns the number of characters printed. If an error occurs, this number will be negative.

Format specifiers

Format specifiers have the form

Each of these format specifiers is described in the following sections.

Type specifiers

The following type specifiers are available to printf()

Type SpecifierTypeExample
d or iSigned decimal integer–123
uUnsigned decimal integer456
oUnsigned octal05670
xUnsigned hexadecimal (lowercase)89abc
XUnsigned hexadecimal (uppercase)89ABC
f, FDecimal floating point123.456
eScientific notation (lowercase)1.23456e+2
EScientific notation (uppercase)1.23456E+2
gThe shorter of f or e
GThe shorter of F or E
aHexadecimal floating point (lowercase)
AHexadecimal floating point (uppercase)
cCharacterc
schar* (ASCIIZ string)example
pPointer addressbc080
%The % character%

There is at least one type specifier for each of the variable types intrinsic to C++. In the absence of any further information, C++ uses default values. For example, an integer number output with a d is preceded with a if it is negative but not preceded with anything if it is positive. In addition, such a value takes only as many spaces as are needed to output the number.

Output amplifier flags

What if the default display format for an integer specifier such as d is not what you want? For example, for some applications, it might be important that positive numbers are preceded by a + (plus sign) in the same way that negatives are preceded by a (minus sign). For that, printf() provides these output amplifier flags.

FlagOperating on TypeHas the Following Effect
allLeft justify output.
+numericPrecedes positive numbers with a +. Negative
numbers are always preceded by a -.
spacenumericInsert a blank if no sign is going to be written.
#o, x, or XPrecede number with 0, 0x,or
0X.
#a, A, e, E, f, F, g, GInclude a decimal point even if the fractional part of the
number is zero.
0numberLeft-pad the number with zeroes (useful when printing dollar
amounts).

Output width flag

Suppose that you want all of the numbers in a column to line up. In that case, it would be important that each number occupy the same number of spaces even if not all of those spaces are needed to display the value. For this and thousands of other applications, printf() allows the user to specify the width by using these width flags.

WidthMeaning
numberThe minimum number of characters to allocate for this
field.
*The width is specified in an integer argument to
printf() preceding the number to be formatted.

Precision flag

The precision flag is most often combined with the width flag when displaying floating point numbers. In this case, the precision flag tells printf() how many digits to display after the decimal point.

The precision flag has been given meaning for types other than floating point, as shown here, but these are less commonly used.

PrecisionOperating on typeHas the following effect
numberd, i, o, u, x, X
(integer types)
The minimum number of characters to output. Pad on the left
with 0’s if necessary.
numbera, A, e, E, f, F
(floating point types)
The number of digits to print after the decimal point.
numberg, G
(floating point types)
The maximum number of significant digits to be printed.
numbers
(character string)
The maximum number of characters to output.
blankallA period not followed by a number is the same as a precision of
0.
*allThe precision is specified in an integer argument to
printf() preceding the number to be formatted.

Length flags

Unlike the flags discussed above, the length flag is not so much about telling printf() how to display the number but more about telling printf() about the number itself. For example, suppose you want to output a variable using a d format, but that variable is actually a long int? No problem, just use ld,as described here.

Lengthd, iu, o, x, Xdecimalcs
noneinunsigned intdoubleintchar*
hhsigned charunsigned char
hsigned shortunsigned short
llongunsigned longwchar_twchar_t*
lllong longunsigned long long
Llong double

Reviewing the advantages and disadvantages of printf()

The printf() style of output has one significant advantage compared with stream output: the grammar is extremely terse. Once you’ve master all the special types and lengths, widths and precisions, you can output a variable in just about any way you want with a minimum number of keystrokes.

The terseness comes with a price, however:

  • The terseness makes printf() output difficult for the uninitiated to understand.

  • printf() is not type safe.

    Mac utilities

    If you say to output the next field using a %Lf, then printf() will assume that a long double is waiting there. It has no way to double check. If a simple double or (heaven forbid!) an integer is the next variable on the stack, then printf() will output garbage. Worse yet, it will continue to output garbage from that point forward since now the specifiers and the arguments are out of sync.

  • printf() is not extensible.

    The writers of printf() thought of a lot of different types of variables, but if they didn’t think of it, then you’re out of luck.

  • The C Standard Library
  • C Standard Library Resources
  • C Programming Resources
  • Selected Reading

Description

The C library function int printf(const char *format, ..) sends formatted output to stdout.

Declaration

Following is the declaration for printf() function.

Parameters

  • format − This is the string that contains the text to be written to stdout. It can optionally contain embedded format tags that are replaced by the values specified in subsequent additional arguments and formatted as requested. Format tags prototype is %[flags][width][.precision][length]specifier, which is explained below −

Sr.No.Specifier & Output
1

c

Character

2

d or i

Signed decimal integer

3

e

Scientific notation (mantissa/exponent) using e character

4

E

Scientific notation (mantissa/exponent) using E character

5

f

Decimal floating point

6

g

Uses the shorter of %e or %f

7

G

Uses the shorter of %E or %f

8

o

Signed octal

9

s

String of characters

10

u

Unsigned decimal integer

11

x

Unsigned hexadecimal integer

12

X

Unsigned hexadecimal integer (capital letters)

13

p

Pointer address

14

n

Nothing printed

15

%

Character

Sr.No.Flags & Description
1

-

Left-justify within the given field width; Right justification is the default (see width sub-specifier).

2

+

Forces to precede the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a -ve sign.

3

(space)

If no sign is going to be written, a blank space is inserted before the value.

4

#

Used with o, x or X specifiers the value is preceded with 0, 0x or 0X respectively for values different than zero. Used with e, E and f, it forces the written output to contain a decimal point even if no digits would follow. By default, if no digits follow, no decimal point is written. Used with g or G the result is the same as with e or E but trailing zeros are not removed.

5

0

Left-pads the number with zeroes (0) instead of spaces, where padding is specified (see width sub-specifier).

Sr.No.Width & Description
1

(number)

Hypersonic 3 vst download. Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.

2

*

The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

Sr.No..precision & Description
1

.number

For integer specifiers (d, i, o, u, x, X) − precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0. For e, E and f specifiers − this is the number of digits to be printed after the decimal point. For g and G specifiers − This is the maximum number of significant digits to be printed. For s − this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered. For c type − it has no effect. When no precision is specified, the default is 1. If the period is specified without an explicit value for precision, 0 is assumed.

2

.*

The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

Sr.No.Length & Description
1

h

The argument is interpreted as a short int or unsigned short int (only applies to integer specifiers: i, d, o, u, x and X).

2

l

The argument is interpreted as a long int or unsigned long int for integer specifiers (i, d, o, u, x and X), and as a wide character or wide character string for specifiers c and s.

3

L

The argument is interpreted as a long double (only applies to floating point specifiers: e, E, f, g and G).

Printf Error In Dev C File

  • additional arguments − Depending on the format string, the function may expect a sequence of additional arguments, each containing one value to be inserted instead of each %-tag specified in the format parameter (if any). There should be the same number of these arguments as the number of %-tags that expect a value.

Return Value

If successful, the total number of characters written is returned. On failure, a negative number is returned.

Example

Printf Error In Dev C Code

The following example shows the usage of printf() function.

Let us compile and run the above program to produce the following result −

C++ Error Type Printf

stdio_h.htm