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.
- /* A quick program to explore char type.
- ** Joel Rees, January 19, 2020.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main()
- {
- char guard1[16] = "012345678901234";
- int canary1 = -1; /* On modern compilers, these move. */
- char bullseye = '\0';
- int canary2 = -1;
- char guard2[16] = "012345678901234";
- printf( "1st canary starts with %d,\n", canary1 );
- printf( "2nd with %d.\n", canary2 );
- printf( "Bullseye is %d, looks like '%c'.\n",
- bullseye, bullseye );
- printf( "1st guard is \"%s\"\n", guard1 );
- printf( "2nd guard is \"%s\"\n", guard2 );
- bullseye = 42;
- printf( "Now bullseye is %d, looks like '%c'.\n",
- bullseye, bullseye );
- printf( "1st canary is still %d,\n", canary1 );
- printf( "2nd is still %d.\n", canary2 );
- printf( "1st guard is still \"%s\"\n", guard1 );
- printf( "2nd guard is still \"%s\"\n", guard2 );
- strcpy( &bullseye, "Character?" );
- printf( "After strcpy, bullseye is %d, looks like '%c'.\n",
- bullseye, bullseye );
- printf( "1st canary is now %d,\n", canary1 );
- printf( "2nd is now %d.\n", canary2 );
- printf( "1st guard is now \"%s\"\n", guard1 );
- printf( "2nd guard is now \"%s\"\n", guard2 );
- printf( "Where did we write that string \"%s\"?\n",
- &bullseye );
- printf( "Clues:\n" );
- printf( "\t&guard1 \t= 0x%lx\n", (unsigned long) &guard1 );
- printf( "\t&canary1\t= 0x%lx\n", (unsigned long) &canary1 );
- printf( "\t&bullseye\t= 0x%lx\n", (unsigned long) &bullseye );
- printf( "\t&canary2\t= 0x%lx\n", (unsigned long) &canary2 );
- printf( "\t&guard2 \t= 0x%lx\n", (unsigned long) &guard2 );
- return EXIT_SUCCESS;
- }