Quick learning: C Fundamentals in ONE Hour

(By Dr. Leo Hu)


You can create a C program in only one hour, even though you have no knowledge of programming.

Let's begin!

1.Define a C program

A C program consists of a function called main() that looks like

int main() {

    return 0;

}

where 

return 0;

 and means that function main() has successfully completed. The word return is a reserved word (or called keyword) in programming language C.  

Attention: A reserved word, or a keyword, cannot be used to name the programmer's term. 

Note: The complete set of reserved keywords can be reviewed at https://www.c-programming-simple-steps.com/c-keywords.html.)

Attention: a statement in C shall end with a semicolon ';'. 

The keyword return is followed with an integer that has been defined by the program caller. Here the program caller is the operating system. Microsoft Windows operating systems has defined

0 - success

1 - Informational

2 - Warning

3 - Error

Therefore, statement 

return 0;

means the program has completed with success. Other operating systems have the same effect. 

Tip: To increase program source code readability, we may add spaces between items and indentations before statements. We may use spacebar to add space, and tab key to add indentations in a C program source code. It doesn't matter how many spaces or how many indentations are added in a C program source code, but we should keep in mind that we apply spaces and indentations to beautify our code and improve readability of our programs. In this simple program, we have added a space between int and main(), and an indentation before return statement. 

2.Add comments

If you want to explain something to your program reader (or remind yourself), you may add comments in your source code. 

Attention: A comment is not to be used by the computer but by your reader and yourself for understanding the code. The C language compiler, which translate a C program source code into machine code, simply omits comments in a program source code. 

For example, we may add comments in the above simple program, and it looks like

// Online C compiler to run C program online

int main() {

// Write C code here

       return 0;

}

In this program, there are two comments that start with double slashes "//". However, a comment can be enclosed between "/*" and "*/", and when a comment is written on more than one line, we must use "/*" and "*/" to enclose it.  For example, the above program can be written as below without any syntax change of C program:

/* Online C compiler

to run C program online

*/

int main() {

/* Write C code here*/

       return 0;

}

3.Add a statement to do something

Let's add a statement to display "Hello World!" on the screen. The program looks like

// Online C compiler to run C program online

int main() {

// Write C code here

printf("Hello world");

        return 0;

}

Here, statement 

printf("Hello world");

display text Hello world on the screen. printf() is a function pre-defined in another program file, <stdio.h>, called a header file, in the C Standard Library, which is commonly used in C programming. It stands for "Standard Input/Output Header". This header file defines a set of functions and macros that provide input and output capabilities for C programs (RKplus, What is stdio.h and why do we use? - Cplusplus,  2023)

Before we can use any code pre-defined in another program file, such as function printf() in <stdio.h>, we must add that file so that the pre-defined code can be used in our program. So, the program should be rewritten as 

// Online C compiler to run C program online

#include <stdio.h>

int main()

{

// Write C code here

printf("Hello world");

return 0;

}

Here, #include, is called a preprocessor directive in C that tells the compiler to include a file in the source code program.

Hands-on: A C program to output a message

Click the link, https://www.programiz.com/c-programming/online-compiler/, to use the online C programming tool to watch the execution of the program we discussed above.  This tool has an editor in the left pane that host the program source code, and the program execution result shall be displayed in the Output in the right pane as shown in Figure 1 below.