Lecture 7 – Feb 14th, 2023
Setup
- Log in to clyde.
- Create a directory and
cd
into it. - Copy
~rhoyle/pub/cs241/c-intro/hello.c
to your directory.
Task
- Open
hello.c
in your editor and read it. Try to guess what it will print out. Compile the code and run it by entering the following commands in the terminal.$ clang -Wall -std=c11 -o hello hello.c $ ./hello
Did it print what you expected?
- Run the
hello
program again, this time passing it some names as arguments (try to guess what will happen before you run it). You don’t need to recompile the program, just run it again.$ ./hello Adam Bob Cynthia
-
Modify
hello.c
such that the “Hello” messages are printed in the reverse order.Since you have modified
hello.c
, you need to recompile the program. So once again, run$ clang -Wall -std=c11 -o hello hello.c $ ./hello Adam Bob Cynthia
If you get an error or warning message, read it closely and then try to fix the error/warning.
If your modified program printed out the line
Hello (null)!
then you have likely tried to print
argv[argc]
which has the special valueNULL
. This is an off-by-one error. You should fix this. - Lastly, we can use the
char *getenv(char const *environment_variable);
function to get the value of environment variables (as a string). In particular,
getenv("USER")
returns the user name of the user running the program.In order to use
getenv
, the function needs to be declared before we can use it inmain.c
. Fortunately,getenv
is part of the C standard library and is declared in thestdlib.h
header.Modify
hello.c
as follows. First, add the line#include <stdlib.h>
to the top of the file.
Next, modify the first
printf()
line so that rather than printingHello world!
, it printsHello
followed by the user’s user name. Look closely at the otherprintf()
line for inspiration. Rather than printing outargv[idx]
, you’ll want to print outgetenv("USER")
.Compile and run the program. (After editing a file, it’s easiest to press up in the terminal until you get to the
clang
line and then you can just press enter rather than typing out the wholeclang -Wall -std=c11 ...
line each time.)