Course Content
C Basics
C Basics
Structure of the C-Program
Programs written in the C language are block-structured, often referred to as "building blocks." Here's a basic program that displays the message "Hello, c<>definity!"
on the screen:
Please, reload the page if you have any problems with runnable examples
Main
#include <stdio.h> // preprocessor directive int main() // the main function { printf("Hello, c<>definity!\n"); // print text return 0; // exit }
The double forward slashes //
indicate a comment. Comments don't influence the behavior of your program. They're meant for human readers, not the computer. There are two types of comments in C:
Include Directive
The #include
is a preprocessor directive that incorporates the "stdio.h"
file into our program. This directive should be placed at the beginning, before the main program (main
) kicks off.
Stdio Header File
The "stdio.h"
file contains the printf()
function. By including it, we're simply adding the capability to display text on the screen to our program. Many C programs don't inherently have access to I/O (input/output) functions or others from the "stdio.h"
library. That's why we need to explicitly bring it in using the #include
directive.
Note
A guiding principle in C is to keep your program lean, avoiding the inclusion of unnecessary functions.
Main Function
This is the primary function where the heart of your program resides. In this example, it's tasked with displaying text on the screen. The name of this function, main
, is reserved in C, and there should only be one main
function in each program. Think of the main
function as the engine of a car; it's essential. We'll delve deeper into functions as this course progresses.
Brackets and Scope
You'll encounter plenty of curly braces { }
in C and other C-derived languages. It's a hallmark of the language.
These braces define blocks of code, much like bricks make up a wall. Here's a way to enhance our sample program:
Main
#include <stdio.h> int main() { // first block { printf("First block\n"); } // second block { printf("Second block\n"); } // third block { printf("Third block\n"); } return 0; }
Each block produces its respective output.
Thanks for your feedback!