Guest User

Untitled

a guest
Jan 23rd, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6.  
  7. char *speak ( char *bs, unsigned int counter ) {
  8. char *str = "Hello %d: %s";
  9.  
  10. size_t bs_l = strlen( bs );
  11. size_t str_l = strlen( str ) - 2;
  12. size_t c_l = counter == 0? 1 : log(counter) / log(10) + 1;
  13. size_t alloc = str_l + c_l + bs_l;
  14.  
  15. char *ptr = malloc( alloc );
  16. int written = snprintf( ptr, alloc, str, counter, bs );
  17.  
  18. printf( "(%zd + %zd + %zd) = %zd <> %i: ", str_l, c_l, bs_l, alloc, written );
  19.  
  20. return ptr;
  21. }
  22.  
  23. int main ( void ) {
  24.  
  25. unsigned int c;
  26.  
  27. for( c = 0; c == c; c = c + 5 ) {
  28. char *foo = speak("Have a nice day!", c);
  29. printf("%s\r", foo);
  30. free( foo );
  31. }
  32.  
  33. exit(0);
  34. }
Add Comment
Please, Sign In to add comment