All programming starts with a simple program to output text to the screen, which uses fundamental parts of the language, including the #include preprocessor directive, the main() function, statements, and the return statement. Reviewing this first, basic program provides a foundation upon which all other programs, and the C programmer's future endeavors, are built, and the instant feedback helps motivate you to move to the next step.
- [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.
- Writing your first C program
- Working the C development cycle
- Adding comments to code for clarity
- C language data types
- Declaring variables
- Making new data types
- Typecasting variables
- Using math, assignment, and logical operators
- Creating for and while loops
- Nesting loops
- Creating functions
- Returning values from functions
- Passing arguments to functions
- Creating recursive functions
Skill Level Intermediate
C: Data Structures, Pointers, and File Systemswith Dan Gookin2h 57m Intermediate
1. The Fundamentals of C
2. Variables and Values
3. Operators and Math
4. Decisions and Loops
Next steps1m 38s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.