This is the first day of my participation in the August More Text challenge

This blog Outlines the simple steps of writing your first C program, and you’ll learn what the basic components are to make it work.

Learn to write your first C program

1. C program coding fragment

Here is a snippet of C code that prints “Hello Computer” on the console. You can write this code using any text editor. However, we recommend and use Codeblocks to create, compile, and execute any C program.

#include<stdio.h>

int main(a)
{
    printf("Hello Computer");
    getchar();
    return(0);
}
Copy the code

2. C program analysis

We will now see what each line in the code above means.

2.1. # include < stdio. H >

In C’s mind, there are predefined instructions that help users execute commands faster. One example is the “printf” directive, which we’ll see shortly. There are specific libraries in C that contain information about these instructions. Therefore, “#include<stdio.h>” tells the computer to include information about these predefined directives so that you can use them in your program. “Stdio” stands for “standard INPUT output”.

Note: You can observe what happens when you don’t write this line. It displays an error for undefined function “printf”. This is because the compiler doesn’t know what it means unless you provide the header “#include

“, which has its definition.

2.2. Int main ()

Each C program has a set of instructions to execute. We usually write them inside the main() function. It is the entry point routine from which all programs begin execution. From the main function, we call all the other functions to perform a set of tasks. Therefore, a C program must have a “main” function.

In C programming, main always returns a value of type integer. The “int” before main indicates the type of return value.

AC programs can also have user-defined functions, which we will cover in a later tutorial. In C programming, however, “main” must be used.

Note: You may have noticed that we give parentheses () after each function. This is because a program can pass it a set of values or parameters when called. They also send back data using a “return” statement. If there is nothing to return, then we can specify 0 as the return value or not use it at all.

2.3. Print out ()

It is a predefined function in C that tells the computer to display text as is. It helps the compiler distinguish between commands for printing and actual text. The text in parentheses should be displayed as it is on the screen

Note: In the printf() function, the actual text to be displayed should be enclosed in double quotes “_”. Similar to printf(” Hello Computer “).

2.4 getchar ()

It is a C input function that makes the program wait for the user to press a key.

2.5. Return (0)

It represents the return code of the main() function. A non-zero value indicates an error, while a zero indicates successful program execution.

In our example, we return a zero value that is evaluated as success.

2.6. Curly braces {}

Braces allow us to create multiple units of code in a C program.

Here they define the boundaries of functions such as main() and include instructions that can be executed in C programs.

A function can have more than one nested code block, which is described in braces. Each such part can access variables from the parent block, but any of its variables are not visible from the outside.

We will provide a detailed description of variable scope in the following lessons.

3. Execute your first C program

We have now succeeded in understanding the meaning of each line. Let’s enter this code on the newly set up IDE and run it.

When you enter this code and run it, you should see something like this.

Now click on the green triangle at the top, and in a few seconds you should see a screen like this.

Congratulations, you have successfully written, compiled, and run a C program.