fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. char *print_x(char *str)
  5. {
  6. union
  7. {
  8. int i;
  9. unsigned u;
  10. long l;
  11. long long ll;
  12. float f;
  13. double d;
  14. }data;
  15.  
  16. switch(*str)
  17. {
  18. case 'd':
  19. memcpy(&data, str + 1, sizeof(double));
  20. printf("%f", data.d);
  21. return str + 1 + sizeof(double);
  22. case 'i':
  23. memcpy(&data, str + 1, sizeof(int));
  24. printf("%d", data.i);
  25. return str + 1 + sizeof(int);
  26. /* another formats */
  27. default:
  28. printf("Not implemented");
  29. return NULL;
  30. }
  31. }
  32.  
  33. int main()
  34. {
  35. char data[100];
  36. double x = 1.234;
  37. int z = 4567;
  38.  
  39. char *str = data;
  40.  
  41. data[0] = 'd';
  42. memcpy(&data[1], &x, sizeof(double));
  43. data[1 + sizeof(double)] = 'i';
  44. memcpy(&data[2 + sizeof(double)], &z, sizeof(int));
  45.  
  46. while((str = print_x(str)))
  47. {
  48. printf("\n");
  49. }
  50.  
  51. return 0;
  52. }
  53.  
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
1.234000
4567
Not implemented