Join Dan Gookin for an in-depth discussion in this video Understanding a C program, part of Learning C (2014).
- C is a language. Like all languages, it has its parts of speech, structure, and syntax. All this stuff is what makes a source code file so delightfully cryptic. But once you understand what's going on, and why things are presented the way they are, it begins to make sense. In this movie, I introduce you to the C programming language. You'll understand the structure, what the main function does, and get to know C language keywords, functions, operators, variables, and values.
In Code::Blocks, click the New button and choose Empty File. You're now going to type source code. It's only one line, so type main, an empty set of parentheses, a space, a set of empty curly brackets, or braces, and press the Enter key to end the line. Save the source code file. Click the Save button. If you need to, choose your C programming folder, type the file name, dummy.c.
The source code file is created and saved. Click the Build button. The code does compile. What you see is the absolute minimum C program, known as 'the dummy.' All C source code must have the main function. This is where the program execution starts. And the contents of the main function are enclosed in curly brackets. Here, everything is empty, which is okay, although you might see a compiler warning, which isn't critical.
A program was created. Click the Run button to run the dummy program. And there's no output. That's predictable because the code does nothing. Like any language, the C programming language has several parts: keywords, functions, operators, values and variables, and structure. The keywords are the language part of the C language. They accomplish very basic tasks.
The good news is that, unlike English, which has 10s of 1,000 of words, there are only 44 words in the C language. Don't freak out. In practice, you may only use about half of these keywords. The real workhorses of the C language are functions. What the keywords do is really basic. To do more in C, you rely upon a function. The functions are held in libraries. The linker's job is to combine the library with your program's object code, knitting the two together to make a program.
To use a function in a program, you must incorporate a header file, which defines the function. You'll see how that's done later in this movie. Operators are symbols used to manipulate data in the program. These include the traditional math operators as well as a host of other special symbols. Values and variables are similar. Values include characters and numeric values. The numeric values are divided between integers, or whole numbers, and floating-point values, which contain a decimal part or fraction, or which can be very, very large values or very, very small values.
A variable, on the other hand, is a container for a value. Its contents can change or vary, which is why they're called variables. The values that go into variables are the same types of values you use directly in a program. All these pieces of the language must be used in a specific manner or order. That's the C language structure. The C language uses preprossesor directives to help control program flow. The main function is the first function executed in all C programs.
It's required, as you saw earlier in the dummy example. Curly brackets are used to enclose a functions' contents. Statements are like sentences in the C language. The can include C language keywords, functions, math, logical comparisons, and so on. Finally, comments are notes to yourself or other programmers, reminders, general information, or other items that are part of the source code but not compiled. Now that you know a bit of the background, let me help you put it to use by completing the dummy program.
Return to the dummy source code in the IDE's editor. The main function is defined as an integer function. That means it returns an integer value to the operating system. Therefore, some editing is necessary. In your editor, type the C language keyword int before the word main. Ensure that a space separates both. Now I'm going to clean up the curly brackets, which I prefer to put each on a line by itself. Now, I'm going to add a statement to the main function. See how the editor automatically indents the line? That's the traditional way C code is formatted.
Type the word return and a number. I'll let you pick any number, although it must be an integer. I'll type three. You can type the number in parentheses, as I've done here, or you can just specify the value. If so, you need to put a space between return and the value. Type a semicolon to end the statement. Save. Build. As long as your code looks like mine, no errors or warning appear. Run. On the Mac or Linux, you may see no output other than the build log, and it says the program terminated with status zero.
On the PC, the terminal window shows you the return value. I'm going to fix the font to make it a little more readable. Here you see, the process returned three, which is the integer value I specified in the return statement. To add output to the dummy program, you need to use an output function. C language keywords don't output anything. They're just basic vocabulary, words like return and int. As an example of an output function, you can use puts, which sends text to the standard output device.
In this case, it's the terminal window. Program code in the C language is read top-down because the return statement ends the main function. I'll add the puts function at line three by inserting a new line into the editor. In the puts function's parentheses, you place a string of text, which consists of characters snuggled between double quotes. I'll type "I am no longer a dummy." Save the source code. Build.
Whoops -- I forgot the semicolon. That happens, so get used to it. Add the semicolon to the end of the puts function at line three. Save. Build. And you may see a warning. Even if you don't, you need to know that the puts function requires a definition before it can be used. Otherwise, the compiler becomes confused. The definition is held in the standard I/O header file. You must include that header file in your source code by using the include preprocessor directive.
Insert a line at the top of the source code. Type #, include, <, stdio.h, >, and add another line for readability. This preprocessor directive includes the definition for the puts function. Save. This time, click the Build and Run icon, which is two steps in one. No warnings, no errors, good.
And there's the output. If this is your first C program, then congratulations.
- Understanding a C program
- Adding comments
- Using escape characters
- Working with values and placeholders
- Introducing variables
- Making a decision with if
- Adding functions
- Manipulating strings
- Building arrays
- Nesting structures