Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

printf functions 1

Status
Not open for further replies.

airdevil

Programmer
Oct 31, 1999
10
US
I am used to c++ output, can anyone tell me how to
output a variable with the printf commands.

I got cprintf("Hello") to work.

char SQUARE = int(219);

cprintf(SQUARE); // doesnt not work,

What is the format for outputting variables??

Thanx

Korn
airdevil23@hotmail.com

"procrastination is the key to happiness"
 
printf and it's cousins can be confusing the first time you use them. Look up format specifiers under printf.
To print an integer variable do something like this:

int x = 3;
printf("x is equal to %d\n", x);

Using %x instead of %d will give you the hexadecimal value of x.
Below is a list of other types of output:

Type Char Expected Input Format of output

Numerics

d Integer Signed decimal integer
i Integer Signed decimal integer
o Integer Unsigned octal integer
u Integer Unsigned decimal integer
x Integer Unsigned hexadecimal int (with a, b, c, d, e, f)
X Integer Unsigned hexadecimal int (with A, B, C, D, E, F)
f Floating point Signed value of the form [-]dddd.dddd.
e Floating point Signed value of the form [-]d.dddd or e[+/-]ddd
g Floating point Signed value in either e or f form, based on given value and precision. Trailing zeros and the decimal point are printed if necessary.

E Floating point Same as e; with E for exponent.
G Floating point Same as g; with E for exponent if e format used

Characters

c Character Single character
s String pointer Prints characters until a null-terminator is pressed or precision is reached
% None Prints the % character

Pointers

n Pointer to int Stores (in the location pointed to by the input argument) a count of the chars written so far.
p Pointer Prints the input argument as a pointer; format depends on which memory model was used. It will be either XXXX:YYYY or YYYY (offset only).




Kim_Christensen@telus.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top