// Code to illustrate strings and structs in C #include #include #include struct Point { double x, y; }; typedef struct{ int x, y; } Coordinate; int main() { // structs struct Point a; a.x = a.y = 6.0221408e+23; Coordinate b; b.y = -1; // using the heap struct Point *c = malloc(sizeof(struct Point)); c->x = -30e-300; // strings char name[9]; strcpy(name, "a name"); // assignment for strings printf("Enter %s (up to 8 letters): ", name); scanf("%s", name); // try entering more! printf("Hello, %s\n", name); // truncate to 2 letters name[2] = '\0'; // randomly add the suffix "sen" strcat(name, "sen"); printf("After mangling, we now declare you to be %s\n", name); // downcase first letter if ( name[0] >= 'A' && name[0] <= 'Z' ) name[0] = name[0] - 'A' + 'a'; if ( strcmp(name, "ibsen") == 0 ) printf("Hello, famous playwright\n"); return 0; }