Writing C Program: A Step-by-step Guide

Writing a C program involves three steps: add the preprocessor directives (including header files), write the body inside main(), then compile and run the program.

Moreover, understanding the distinction between lower-case and upper-case letters is not just a step in C programming; it’s a crucial aspect that can prevent potential errors and improve the quality of your code. For example, in C programming, main and Main are different names, and printf() is not the same as Printf.

To better understand, visit the “Computer Fundamentals series“.

Structure of a C program: #include directive and main function.
#include <stdio.h> // preprocessor directive: It starts with # symbol and includes the header file stdio.h
int main() { // main function: the program execution starts here
printf("My First C Program.\n"); // statement: prints output to the screen
return 0; // The operating system interprets 0 as successful program termination.
}

The pre-processor section, which includes the step #include <stdio.h>, is crucial for the program.

These are the header files that already contain some predefined functions that are known to the compiler. In other words, the programmer can use these functions without writing the code from scratch.

For instance, The most common header file in C is stdio.h (standard input/output), which declares functions such as printf(), which is not just a theoretical concept; it’s a practical and essential tool for a programmer. It lets the programmer print output to the screen without writing that code, making it a crucial part of writing efficient C programs.

Before compiling the program, the programmer gives instructions through the pre-processor directive to include specific files. This directive is not just a step; it’s a crucial and fundamental part of writing a C program. It allows the programmer to use functions and features from these files without writing the code.

The preprocessor runs first, before the compiler translates the programmer’s code. It handles lines that begin with #, such as #include. Hence, it is the first step in writing the C program.

The main() in C programming is not just any function; it’s the starting point of a program’s execution. Understanding this function is like understanding the key to the program’s execution, making it a crucial concept in C programming.

The defining of main() can be done only once. If given twice, there will be errors.

When declaring the main() in a C program, the programmer can use two methods: int main() and void main().

Declare the main function as int main(). It returns an integer to the operating system, and return 0; means the program finished successfully. Some compilers accept void main(), but it is not standard C, so avoid it.

The format for writing ‘int main’ in C program is as follows:

int main(){} it is followed by parentheses and curly braces.

Whatever code the user provides inside the curly braces {} will get executed.

While writing C program, all the statements end with a semicolon.

Importantly, when declaring variables in main(), it is essential to understand data types in C.

Statements in a program describe the function that the routine performs.

So, to specify precisely what function a particular routine performs, the programmer encloses all program statements of the routine within the curly braces.

#include <stdio.h>
int main() {
// Print output to the terminal with a newline character
printf("Programming is fun.\n");
return 0;
}

The example has two statements: the printf call and return 0;.

Check another example: the Hello World program in C.

The string of characters, ‘Programming is fun. ‘, is the argument or parameter to be passed or handed to the printf() routine. In C programming, arguments are values or expressions that are passed to a function when it’s called. They provide the function with the data it needs to perform its task.

In C programming, the printf() routine allows the program’s arguments to be printed or displayed at the terminal.

Within the string, the two characters \n (a backslash followed by n), known as the newline character. When used in the printf() function, it Instructs the C system to go to a new line after printing the string. This is important for formatting the output of your program.

All program statements must be terminated with a semicolon while writing the C program.

A comment begins with /* and ends with */, and the compiler ignores everything between them. Since the C99 standard, // starts a single-line comment.

The first argument, commonly called the format string, describes the system by which the remaining arguments are to be displayed.

The body of the C program ends with a closing curly braces}.

After writing the body, save the file as program.c, then compile and run it.

Leave a Comment