printf, wprintf

Aus RAD Studio
Wechseln zu: Navigation, Suche

Nach oben zu stdio.h


Header-Datei

stdio.h

Kategorie

Konsolen-Ein-/Ausgaberoutinen

Prototyp

int printf(const char *format[, argument, ...]);
int wprintf(const wchar_t *format[, argument, ...]);

Beschreibung

Schreibt eine formatierte Ausgabe in den Standardausgabe-Stream stdout.

Die Funktion printf:

  • Akzeptiert eine Reihe von Argumenten
  • Ordnet jedes Argument den Formatbezeichnern in dem Format-String zu, auf den *format zeigt
  • Gibt die formatierten Daten auf dem Bildschirm, einem Stream, stdout oder in einen String aus

Für format müssen ausreichend viele Argumente vorhanden sein. Ist dies nicht der Fall, sind die Ergebnisse nicht vorhersehbar und wahrscheinlich zerstörerisch. Sind mehr Argumente gegeben, als für format erforderlich, werden sie ignoriert.

Anmerkung: Für Win32- und Win64-GUI-Anwendungen muss stdout umgeleitet werden.

Rückgabewert

Bei erfolgreicher Ausführung gibt printf gibt die Anzahl der ausgegebenen Bytes zurück.

Bei einem Fehler wird EOF zurückgegeben.

Weitere Informationen zu printf

Beispiel

#include <stdio.h>
#include <tchar.h>

int _tmain(int argc, _TCHAR* argv[]) {
	int number = -8542;
	int countchars;
	float realnumber = 375.22352;
	char letter = 'a';
	wchar_t wletter = L'b';
	char* arrayofchars = "This is a sample text";
	wchar_t* warrayofchars = L"This is a wide sample text";


	/* Printing integers */
	printf(" Integer: %d\n Unsigned: %u\n Hexadecimal: %x\n Octal: %o\n\n ",
		number, number, number, number);

	/* Printing floating point numbers */
	printf("Float: %f\n With precision specifier: %.2f\n Scientific: %e\n\n",
		realnumber, realnumber, realnumber, realnumber);

	/* Printing characters with printf and wprintf */
	printf(" printf:\n %c %hc %C %lc\n", letter, letter, wletter, wletter);
	wprintf(L" wprintf:\n %C %c %c %lc\n\n", letter, letter, wletter, wletter);

	/* Printing strings */
	printf(" String (printf):\n %s \n %35s \n %10.6s \n\n ", arrayofchars, arrayofchars, arrayofchars);
	wprintf(L"Wide string (wprintf):\n %s \n %44s \n %11.12s \n \n", warrayofchars, warrayofchars, warrayofchars);

	/* Printing pointers */
	printf(" Address of %d as pointer: %p\n", number, &number);

	/* Counts the printed characters until the %n format specifier is encountered*/
        printf( "\n Counting the printed characters until the format specifier is encountered:\n" );
        printf( "15382%n123456789\n", &countchars );
        printf( "Number of characters: %d\n\n", countchars );
        getchar();
        return 0;
}

Ausgabe

Integer: -8542
Unsigned: 4294958754
Hexadecimal: ffffdea2
Octal: 37777757242

Float: 375.223511
With precision specifier: 375.22
Scientific: 3.752235e+03

printf:
a a b b
wprintf:
a a b b

String (printf):
This is a sample text
              This is a sample text
    This i

Wide string (wprintf):
This is a wide sample text
                  This is a wide sample text
This is a wi

Address of -8542 as pointer: 0018FF50

Counting the printed characters until the format specifier is encountered:
15382123456789
Number of characters: 5

Portabilität

POSIX Win32 ANSI C ANSI C++ Win64

printf

+

+

+

+

+

_wprintf

+

+

Themen

Siehe auch