• Hello World

    What is the Hello World program?:

    • A very simple program to show what makes up a program
    • The intent of the program is to print the following text to the screen:
      Hello World
      Bye World

    The first program, hello_world.c :

    Basic Rules:

    • computers are very strict about punctuations and brackets
      • every statement needs to end with a semicolon ;
      • every open parenthesis ( must be closed with a close parenthesis )
      • every open curly braces { must be closed with a close curly braces }
      • every open quote " must be matched with a close quote "
      • every open square bracket [ must be closed with a close square bracket ]
    • each line of code is sequentially executed
    • the main function is the start of our program, analogous to start of a letter
    • main(){} is how we define a function
      • function name: main
      • argument list: (), there are no arguments expected for the main function
      • function definition: {}, what the function does is defined in the curly brackets
      • we'll discuss more about functions later
    • you can call other functions, like printf
      • specify the name of the function: printf
      • specify the arguments to the function: "Hello World\n" and "Bye World\n"
    • including the standard input/output library #include <stdio.h> to gain more functionality
    • \n tells the computer to output a newline, read more about escaping characters at http://en.wikipedia.org/wiki/Escape_sequences_in_C
    • white space doesn't matter, but is necessary to allow other humans to read your code
    • you can add comments to your code to explain what you are trying to do in your code
  • Compile and Execute

    What does compiling do?

    • the compilation steps translate your C code into assembly code, object code, and then finally to machine code
    • This SO answer has a good visual diagram of what is happening during the compilation process
    • the gcc command is doing the preprocessing, compiling, assembling, and linking for you

    What does executing do?

    • running the executable file hello_world will:
      • load the machine code into memory (such as RAM)
      • run the machine code instructions, e.g. run your program

    How do you compile and execute?

    • gcc hello_world.c -o hello_world will compile hello_world.c to an executable file hello_world
    • syntax (pattern/grammar): gcc input_filename -o output_filename
    • ./hello_world will run the executable file hello_world to run you program
    • if you are not in the same directory as hello_world, then you'll need to use something like folder_name/hello_world

    Why do you compile?

    • Compiling is needed to translate your C code into code your computer understands
    • A precompiled executable file (hello_world) will run faster than running straight from C code (hello_world.c), which is not possible for the C language
    • Some languages do not need to be precompiled to run, they are usually called "interpreted" languages
    • Some integrated development environments (IDEs) will compile automatically for you
  • printf

    What is printf?

    • A function that outputs text

    More Info:

  • Assignment

    Please submit your assignment using the self-checking and submission tool detailed in the Extras>Tools Page. The are two separate components of the same program, e.g. you run both programs with the same command.

    Homework Problem 2.1, at the end of this problem you should submit to Laulima:

    • a file named UHusername_02_1.c
      • example:
      • zhaol_02_1.c

    Compile and execute the hello_world.c program in your workspace

    • Make sure your program outputs the following text:
    • Hello World
      Bye World

    Rename hello_world.c to the file you will be submitting (e.g. zhaol_02_1.c)

    • Modify the program to output the following text:
    • It was nice meeting you, world.
      Hope to see you again soon. :-\
      Don't forget to write.
  • Extra References

    This video walks through how to compile and execute your assignment: How to Compile and Execute a Program

  • Optional Assignments

    Optional assignments are not graded and does not need to be turned in. It is intended to give you more practice (if you want it) using the programming concepts discussed in lecture. Some optional assignments are intended to introduce related concepts (if you care about it) not discussed in this course to broaden your knowledge about programming in general.

    If the optional assignment is modifying a required assignment, be sure to leave your previous assignment work untouched by copying them to a new folder for the optional assignment and modify only the files in the new folder

    Hello Colorful World

    • There is a way to add color to the text output from the program. See if you can figure out how to add color to your program.

    Click here for the answer if you get stuck:

    See if this SO post helps

    Here is an example program that displays colored text

    More on Compiling