Foundations
Hello C
Start with a tiny C program that stores a name and prints a greeting.
main
`main` is the function where a C program starts running.
printf
`printf` writes formatted text to standard output.
Hello C
hello.c
Replay: real traced execution (multi-file project)
#include <stdio.h>
int main(void) {
const char *name = "C";
printf("Hello, %s!\n", name);
return 0;
}
#include <stdio.h>
int main(void) {
const char *name = "Ada";
printf("Hello, %s!\n", name);
return 0;
}
#include <stdio.h>
int main(void) {
const char *name = "Dennis";
printf("Hello, %s!\n", name);
return 0;
}
name ← C
3int main(void) {4 const char *name→ C = "C"; //@name="Ada", "Dennis"5 printf("Hello, %s!\n", nameC);6 return 0;7}outputHello, C!
name ← Ada
3int main(void) {4 const char *name→ Ada = "Ada";5 printf("Hello, %s!\n", nameAda);6 return 0;7}outputHello, Ada!
name ← Dennis
3int main(void) {4 const char *name→ Dennis = "Dennis";5 printf("Hello, %s!\n", nameDennis);6 return 0;7}outputHello, Dennis!
Follow the Greeting
namestarts asC.printfplacesnameinside the greeting text.- The message becomes
Hello, C!. - The program prints
Hello, C!. | name | stdout | | --- | --- | | C |Hello, C!| | Ada |Hello, Ada!| | Dennis |Hello, Dennis!|
Exercise: hello.c
Reproduce Hello, C!, then use the pinned names Ada and Dennis to predict each greeting.