acidburnsw
16-12-2004, 10:04
Ich muss als Aufgabe printf neuprogrammieren.
Wie kann ich Warnings anzeigen wenn das format nicht stimmt?
Das kannst du nicht, dazu müßtest du den Formatstring zur Compilezeit parsen können, und das kannst du in C nicht.
Das das überhaupt klappt (bei gcc zb.) liegt daran, daß printf keine 'normale' Funktion ist (bei gcc built-in), und der Compiler daher 'alles' über die Funktion weiß, und Annahmen treffen kann.
Diese Möglichkeiten hast du nicht.
Ganz entfernt, und doch ganz anders ist sowas ähnliches mal in C++ implementiert worden:
http://boost.org/libs/format/index.html
Natürlich geht das. Aber nur mit gcc:
/* folgendes define macht, dass das __attribute__
unter nicht GNU compilern einfach mit nix ersetzt wird, damits
dort auch, halt ohne checks, compeliert. */
#ifndef __GNUC__
# define __attribute__( x ) /* nothing */
#endif
int my_printf( void * foo, const char * fmt, ... ) __attribute__(( format( printf, 2, 3 ) ));
int my_vprintf( void * foo, const char * fmt, va_list ap ) __attribute__(( format( printf, 2, 0 ) ));
int my_scanf( void * foo, const char * fmt, ... ) __attribute__(( format( scanf, 2, 3 ) ));
int my_vscanf( void * foo, const char * fmt, va_list ap ) __attribute__(( format( scanf, 2, 0 ) ));
Powered by vBulletin® Version 4.2.5 Copyright ©2025 Adduco Digital e.K. und vBulletin Solutions, Inc. Alle Rechte vorbehalten.