2
0

printf.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include <stdio.h>
  2. #include <stdlib.h> // free()
  3. #include <math.h> // INFINITY, NAN constants
  4. int main(void) {
  5. int sofar = 0;
  6. int len = printf(
  7. "percent: %%\nstring: %s\nchar: %c\nchar: %c\nint: %d\n%nuint: %u\nhex: %x\nHEX: %X\nstring: %s\n",
  8. "String",
  9. 'c',
  10. 254,
  11. -16,
  12. &sofar,
  13. 32,
  14. 0xbeef,
  15. 0xC0FFEE,
  16. "end"
  17. );
  18. printf("%%n returned %d, total len of write: %d\n", sofar, len);
  19. puts("\nPadding madness:");
  20. printf("% -5.3d %+3d\n", 1, 2);
  21. printf("%4c\n", 'c');
  22. printf("%#10.7x\n", 0xFF);
  23. printf("%#4.3o\n", 01);
  24. printf("%#x %#x\n", 0, 1);
  25. printf("%.0d %.0d\n", 0, 1);
  26. printf("(%05d) (%5d)\n", 123, 123);
  27. printf("(%05d) (%5d)\n", -123, -123);
  28. printf("(%13.0d)\n", 0);
  29. printf("%p\n", (void*) 0xABCDEF);
  30. printf("%p\n", (void*) 0);
  31. puts("\nPositional madness:");
  32. printf("%3$d %2$d %1$d\n", 2, 3, 4);
  33. printf("%.*3$d\n", 2, 0, 5);
  34. printf("|%-*6$.*5$s|%-*6$.*5$s|%*6$.*5$s|%*6$.*5$s|\n", "Fizz", "Buzz", "FizzBuzz", "TotalBuzz", 3, 8);
  35. printf("int: %*6$d double: %lf %lf %lf %lf\n", 5, 0.1, 0.2, 0.3, 0.4, 10);
  36. printf("%1$d %1$lf\n", 5, 0.1);
  37. printf("%1$d %lf\n", 5, 0.2);
  38. //printf("int: %*6$d no info on middle types\n", 5, 0.1, 0.2, 0.3, 0.4, 10);
  39. puts("\nFloat madness:");
  40. printf("%20e\n", 123.456789123);
  41. printf("%20E\n", 0.00001);
  42. printf("%20f\n", 123.456789123);
  43. printf("%20F\n", 0.00001);
  44. printf("%20e\n", -123.456789123);
  45. printf("%020e\n", -123.456789123);
  46. printf("%%.5g: %.5g\n", -123.456789123);
  47. printf("%%.5f: %.5f\n", -123.456789123);
  48. printf("%%.5e: %.5e\n", -123.456789123);
  49. printf("%%.*g: %.*g\n", 2, -123.456789123);
  50. printf("%%.*f: %.*f\n", 2, -123.456789123);
  51. printf("%%.*e: %.*e\n", 2, -123.456789123);
  52. printf("%%.*2$g: %.*2$g\n", -123.456789123, 5);
  53. printf("%%.*2$f: %.*2$f\n", -123.456789123, 5);
  54. printf("%%.*2$e: %.*2$e\n", -123.456789123, 5);
  55. printf("%g\n", 100000.0);
  56. printf("%g\n", 1000000.0);
  57. printf("%e\n", 1000000.0);
  58. printf("%G\n", 0.0001);
  59. printf("%G\n", 0.00001);
  60. printf("%E\n", 0.00001);
  61. double nonfinites[] = {INFINITY, -INFINITY, NAN, -NAN};
  62. char *float_formats[] = {"%e", "%E", "%f", "%F", "%g", "%G"};
  63. puts("\nNon-finite float madness:");
  64. for (size_t i = 0; i < sizeof(float_formats)/sizeof(char *); i++) {
  65. printf("%s:", float_formats[i]);
  66. for (size_t j = 0; j < sizeof(nonfinites)/sizeof(double); j++) {
  67. printf(" ");
  68. printf(float_formats[i], nonfinites[j]);
  69. }
  70. printf("\n");
  71. }
  72. puts("Things that have been buggy");
  73. printf("%s%0*lu\n", "+", 2, 5l); // this format string was found in GDB
  74. puts("Testing asprintf...");
  75. char *s = NULL;
  76. int res = asprintf(&s, "test string");
  77. printf("printed: %s, value: %d\n", s, res);
  78. free(s);
  79. res = asprintf(&s, "test string %d", 2);
  80. printf("printed: %s, value: %d\n", s, res);
  81. free(s);
  82. res = asprintf(&s, "test %s %d", "string", 2);
  83. printf("printed: %s, value: %d\n", s, res);
  84. free(s);
  85. }