Introduction to C
C Program Structure: From Source to Execution
C Program Structure helps you separate the text you write from the actions performed when a program runs. You will learn to recognize the main regions of a source file and follow that file through a build. The resulting executable is a different artifact from your editable source code.
Reading a small program
Consider this complete program:
#include "stdio.h"
int main(void)
{
printf("Console ready\n");
return 0;
}A preprocessing directive instructs the preprocessing stage rather than specifying an ordinary runtime action. Here, #include "stdio.h" makes the contents of the standard input/output header available, including the declaration of printf. The directive does not print anything, and it does not end with a semicolon.
In a hosted C environment, main is the function called at program startup. The header int main(void) specifies an integer return type and no parameters. This is one standard form of main; another form supports command-line arguments.
The braces delimit the function body. A statement is a unit of C syntax that specifies an action or control step. The printf call forms an expression statement, and return 0; is a return statement. Both end with semicolons. The closing brace ends this function body without an additional semicolon.
Source element | Role | Ending in this example |
|---|---|---|
Header inclusion directive | Makes header contents available | End of directive line |
Function body | Groups the statements belonging to main | Closing brace |
Call or return statement | Specifies a runtime action | Semicolon |
From source text to a running process
The governing distinction is between building a file and executing a program:
A conventional native build first processes directives. The source file together with the included material forms a translation unit. Compilation translates the prepared C, and assembly produces an object file containing machine code and supporting information. Linking combines the required object files and library code to produce an executable.
A compiler driver can perform several stages in one command. On a Unix-like system with a suitable cc command, cc console.c -o console builds the executable. A later command, ./console, starts it. The intermediate files need not remain visible when the driver manages them automatically.
Successful translation does not guarantee successful linking. A declaration can describe a library function sufficiently for translation while its compiled implementation is still unavailable to the linker.
Reading runtime actions accurately
When execution reaches printf, the call writes the specified characters to standard output. In a string, \n represents a newline character rather than the two printed characters backslash and n.
Example: A call containing "North\nSouth\n" writes two newline characters and completes two output lines when output begins on an empty line. Splitting that call across source lines would not change the string's output.
The statement return 0; ends this invocation of main and reports successful program termination to the surrounding environment. It does not print the number zero. Comments and indentation improve readability, but comments do not execute and indentation does not replace punctuation.
What to remember
- Directives prepare source material; statements inside main specify runtime actions.
- Braces group the function body; semicolons terminate the call and return statements shown here.
- Building creates an executable; starting it begins execution.
- Printed output and the status returned by main serve different purposes.
Example: Repairing a Weather-Station Startup Program
A weather station uses the following C program to announce its startup state. The file is named startup.c and contains one punctuation mistake. Assume a hosted C environment on a Unix-like system with a working compiler driver named cc. All output calls succeed, output begins on an empty line, and no other output is written.
#include "stdio.h"
int main(void)
{
printf("Station ready\n")
printf("Checks complete\nAwaiting reading\n");
return 0;
}a) Repair the punctuation mistake and show the corrected program.
A syntax error is a violation of the language's grammar. Here, the first call has a closing parenthesis, but its expression statement lacks the semicolon needed to terminate it. The following call already has its semicolon, and the braces correctly enclose the function body.
Add the missing semicolon immediately after the first call:
#include "stdio.h"
int main(void)
{
printf("Station ready\n");
printf("Checks complete\nAwaiting reading\n");
return 0;
}The corrected program above completes both output statements before the return statement.
b) After the repair, determine the exact displayed text and the number of complete output lines.
First count the newline characters in each string. The first call writes ________ newline character, and the second writes ________ newline characters. Each newline completes the current output line under the stated starting conditions.
The displayed text is:
Station ready
Checks complete
Awaiting readingAdding the contributions gives:
The return statement does not write another line. The final result is 3 complete output lines, with the text shown above.
c) Explain the different roles of cc startup.c -o startup, ./startup, and return 0; when the corrected program builds and runs successfully.
The command cc startup.c -o startup asks the compiler driver to build an executable named startup from the source file. With no option requesting an earlier stopping point, the driver carries the build through linking.
The command ./startup then asks the environment to start that executable from the current directory. During program startup, main is called, and execution reaches its two output statements in order.
Finally, return 0; ends main and reports successful program termination. The return value is a completion status, not printed output.
Thus, the first command builds, the second command starts execution, and the return statement reports success when execution finishes.
A lab assistant is annotating a small C startup program. Match each source element to its role in a conventional C source file.
A line beginning with # that gives an instruction during an early translation phase
Source text enclosed by /* and */ that provides notes without executing
Quoted source text that supplies character data, such as a message
A semicolon that ends an expression statement or a return statement
Block comment
Statement terminator
Preprocessing directive
String literal
A laboratory uses a conventional native C toolchain that generates assembly before producing object code. Arrange these build and execution actions in their correct order, beginning with the source file.
Loading ordering steps…