From the course: C Essential Training

Coding your first program - C Tutorial

From the course: C Essential Training

Start my 1-month free trial

Coding your first program

- [Instructor] The traditional method of writing C code is at the command prompt in Unix. I'm starting up an editor vim, in this case, with a file name 01_01-hello.c. And now you type the code. Start with the include preprocessor directive. This tells the compiler to fetch C code stored in a file, stdio.h, which is the standard input-output header file. All C programs, start running with a main function, which returns an integer, a whole number value, back to the operating system when the program is done. The name of the function is main and its parentheses are empty, because I won't be using its arguments. All functions have their contents enclosed in curly brackets. And within each function lies statements. These statements are traditionally indented one tab stop, which the editor has done for me automatically. Here's the puts. Our put string function, which has a string argument in its parentheses, "Hello, world!", closing paren and all statements in C end with a semi-colon. The next and final statement is the return keyword passing an integer value to the operating system. Traditionally, this value is zero for okay, semi-colon and then a closing curly bracket for the end of the main function. Save and quit. To compile and link at the command prompt, I use the Clang program. This program can be obtained from the operating systems package manager if you don't have it. Follow clang with the source code file name. Press enter. No warnings or errors appear, which is good. The default output program is named a.out. To run this program at the prompt, type ./ and the program name a.out. There you go. Things are much easier in the Code::Blocks IDE, I'm going to open the exercise file, 01_01-goodbye.c. The Code::Blocks editor color codes parts of the C language, which can clue you into some typos and bugs. Here you see the same elements of C as shown earlier. The include preprocessor directive, the main function, statements in the main function. Each statement is indented one tab stop and in fact, the compiler ignores all spaces, tabs, and blank lines in your source code. What's called white space. Still these indents, this white space, makes the code easier to read. Compiling and linking is carried out by the build command. Click, the build button. The build log tab informs you that the program has successfully compiled. To execute it, click the run button. The program's output appears in a command prompt window in Windows or in a terminal window in the Unix operating system. You see the program's output in two parts. The first shows the text that was output, goodbye, cruel world. And the second shows the value returned to the operating system, which was a zero here. Press any key to close the window in Windows and in Unix, you must type the exit command to close the terminal window. This is the essence of a simple C program.

Contents