A bit of code to explore what a character is, and get a foothold on what a string is. Intended to be compilable on rather limited systems with pre-ANSI C compilers.

格式
C
提交日期
2021-01-19 20:13
Publication Period
Unlimited
  1. /* A quick program to explore char type.
  2. ** Joel Rees, January 19, 2020.
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. int main()
  8. {
  9. char guard1[16] = "012345678901234";
  10. int canary1 = -1; /* On modern compilers, these move. */
  11. char bullseye = '\0';
  12. int canary2 = -1;
  13. char guard2[16] = "012345678901234";
  14. printf( "1st canary starts with %d,\n", canary1 );
  15. printf( "2nd with %d.\n", canary2 );
  16. printf( "Bullseye is %d, looks like '%c'.\n",
  17. bullseye, bullseye );
  18. printf( "1st guard is \"%s\"\n", guard1 );
  19. printf( "2nd guard is \"%s\"\n", guard2 );
  20. bullseye = 42;
  21. printf( "Now bullseye is %d, looks like '%c'.\n",
  22. bullseye, bullseye );
  23. printf( "1st canary is still %d,\n", canary1 );
  24. printf( "2nd is still %d.\n", canary2 );
  25. printf( "1st guard is still \"%s\"\n", guard1 );
  26. printf( "2nd guard is still \"%s\"\n", guard2 );
  27. strcpy( &bullseye, "Character?" );
  28. printf( "After strcpy, bullseye is %d, looks like '%c'.\n",
  29. bullseye, bullseye );
  30. printf( "1st canary is now %d,\n", canary1 );
  31. printf( "2nd is now %d.\n", canary2 );
  32. printf( "1st guard is now \"%s\"\n", guard1 );
  33. printf( "2nd guard is now \"%s\"\n", guard2 );
  34. printf( "Where did we write that string \"%s\"?\n",
  35. &bullseye );
  36. printf( "Clues:\n" );
  37. printf( "\t&guard1 \t= 0x%lx\n", (unsigned long) &guard1 );
  38. printf( "\t&canary1\t= 0x%lx\n", (unsigned long) &canary1 );
  39. printf( "\t&bullseye\t= 0x%lx\n", (unsigned long) &bullseye );
  40. printf( "\t&canary2\t= 0x%lx\n", (unsigned long) &canary2 );
  41. printf( "\t&guard2 \t= 0x%lx\n", (unsigned long) &guard2 );
  42. return EXIT_SUCCESS;
  43. }
下载 可打印视图

网址

Embed with JavaScript

Embed with iframe

原始文本