C programming is a powerful language with a rich history and broad range of applications. It offers efficiency, versatility, and wide usage in various domains. By the end of this chapter, you'll grasp the basic structure of a C program and how to compile and run it effectively.
Example:
#include <stdio.h> int main() { printf("Hello, world!\n"); return 0; }
Explanation:
- The
#include <stdio.h>
line includes the standard input/output library, enabling the use ofprintf()
for console output. - The
int main() { ... }
function serves as the starting point of every C program and returns an integer value to indicate successful execution. printf("Hello, world!\n");
prints the string "Hello, world!" along with a newline character to the console.- The
return 0;
statement signifies the successful execution of the program.
To compile and run the program:
- Save the code in a file with the
.c
extension (e.g.,hello.c
). - Open a terminal or command prompt.
- Navigate to the directory containing the file.
- Use the command
gcc hello.c -o hello
to compile the program. - Execute the program by typing
./hello
(on Linux/macOS) orhello
(on Windows) and pressing Enter.
You should see the output "Hello, world!" displayed on the console.
In the next chapter, we'll dive into variables and data types in C, exploring how to declare variables, assign values, and perform operations on them.