0

I am trying to add new identifiers to printf() function to enable printing "true" and "false" .. Eg:printf("%boolean",test()); is this possible in c ?

#include<stdio.h>
typedef int boolean;
#define true 1
#define false 0

boolean test(int x){
return x%2==0?1:0;
                }

int main(){
printf("%s",test(5));
system("pause");
return 0;
}
Spmmr
  • 85
  • 2
  • 7
  • 3
    For home-grown conversion specifiers especially see this answer: http://stackoverflow.com/a/22114373/694576 – alk Apr 25 '15 at 08:41
  • i want to edit (over-ride) printf() function with adding new identifiers to its basic identifiers ("%s", "%d","%test") ! i do not want to use ternary operator , i already know it . – Spmmr Apr 25 '15 at 08:47
  • 2
    Please see the link specified (as "*GNU C library provides an API for adding custom specifiers*") in the answer I linked above. – alk Apr 25 '15 at 08:49

1 Answers1

3

Change

printf("%s",test(5));

to

printf("%s",(test(5))?"true":"false");

which tells %s to print "true" if test(5) returns a non-zero integer and "false" if test(5) returns 0.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83