Main Page | Modules | Class Hierarchy | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

printf - Code example
[Printf - Print formatted data to stdout]

 #include <otSerial.h>
 #include <otTimer.h>
 #include <otMalloc.h>
 #include <otMath.h>
 #include <otPrintf.h>
 
 void setup()
 {
        UartEnableSignals(UART0);
        UartSetBaud(UART0, 115200);
        _initMalloc();
        setStdout(4096, UART0);   // printf output is now directed on UART0    
 }
 
 void loop()
 {
        int     value;
        void    * ptr = &value;
        int     stackValue;
        void    * stackPtr = &stackValue;
        
        DelayMs(2000);  // Wait the terminal start
        printf("Hello world {%u}\n", sizeof(unsigned long));
        printf("Hello %s\n", "World");
        printf("String %4.4s\n", "Large");
        printf("String %*.*s\n", 4, 4, "Hello");
        printf("integer %05d %+d %d %2d %5d\n", -7, 7, -7, 1234, 1234);
        printf("Integer %+05d %-5d % 5d %05d\n", 1234, 1234, 1234, 1234);
        printf("Integer blank % d % d\n", 1, -1);
        printf("Unsigned %u %lu\n", 123, 123Lu);
        printf("Hex with prefix %#x %#x %#X %#08x\n", 0, 1, 2, 12345678);
        printf("Octal %o %lo\n", 123, 123456L);
        printf("Octal with prefix %#o %#o\n", 0, 5);
        printf("Hex %x %X %lX\n", 0x1234, 0xf0ad, 0xf2345678L);
        printf("Special char %%\n");
        printf("Size    of void * %u(%u)\n", (U32) sizeof(void *), (U32) sizeof(void *));
        printf("Sizeof char=%d short=%d int=%d long=%d void*=%u size_t=%u\n", sizeof(char), sizeof(short), sizeof(int), sizeof(long), sizeof(void *), sizeof(U32));
        // Float test
        printf("Floating %f\n", -0.6);
        printf("Floating %6.2f\n", 22.0 / 7.0);
        printf("Floating %6.2f\n", -22.0 / 7.0);
        printf("Floating %+6.1f %6.2f\n", 3.999, -3.999);
        printf("Floating %6.1f %6.0f\n", 3.999, -3.999);
        printf("Floating %5.0f\n", 3.14);
        printf("Floating %.0f\n", 0.491);
        printf("Floating %.0f\n", 0.490);
        printf("Floating %.0f\n", 0.489);
        printf("Floating %.0f\n", -0.491);
        printf("Floating %.0f\n", -0.490);
        printf("Floating %.0f\n", -0.489);
        //
        printf("*Sizeof of void * %zu\n", sizeof(void *));
        printf("*Binary number %b %#b\n", 5, 6);
        printf("*Stack  ptr %p %P\n", stackPtr, stackPtr);
        printf("*Static ptr %p %P\n", ptr, ptr);
        printf("*Text   ptr %p %P\n", xvformat, xvformat);
        printf("*boolean %B %B\n", 1, 0);
        printf("*Text pointer as sizeof %zX\n", xvformat);
        // long long
        printf("long long int %lld\n", (long long) 123);
        printf("long long int %lld\n", (long long) -123);
        printf("long long hex %#llx\n", (long long) 0x123456789abcdef);
        printf("long long hex %#llX\n", (long long) 0x123456789abcdef);
        printf("Floating > 32 bit %f\n", pow(2.0, 32.0) +1.0);
        printf("Floating < 32 bit %f\n", -pow(2.0, 32.0) -1.0);
        
        IDLE;
 }
footer
otStudio - Library Reference - (C) 2020-25 Officina Turini, All Rights Reserved
Document built with Doxygen 1.4.0