Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Structure of the C-Program | Introduction
C Basics
course content

Course Content

C Basics

C Basics

1. Introduction
2. Data
3. Operators
4. Control Statements
5. Functions
6. Pointers

bookStructure 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

c

Main

copy
12345678
#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:

c

Main

copy
123456789101112131415161718192021
#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.

You can create as many `main` functions as you want in the C program.

You can create as many main functions as you want in the C program.

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 1
some-alt