C – Basic Syntax and Semantics

Course Outline  | Previous LessonNext Lesson

Basic Syntax and Semantics

The basic program below will be analyzed by looking at C’s basic syntax, language’s grammar, and its semantics, meaning of the code. The analysis of semantics can determine why certain pieces of code are needed.

 

#include <stdio.h>

int main()
{
     printf("Hello World!");
     return 0;
}

By making the analysis of the code less complicated, the code above will break into parts. The following sections will explain the syntax and semantics for each parts.

Directive/Macro

#include <stdio.h>

The code above is including a library to this program where there are pre-made code available for use. The first thing a person will see above is the # sign, and that indicates that the first line of code is a directive or a macro, meaning it will modify the program before the program executes. Executing the program means running the program. In this example, we want to include something to the program. The file with the .h extension enclosed in the angle brackets (<>) is called a header file. That file is considered to be a standard library in C since it is enclosed in angle brackets. The file name stdio stands for standard input output. To reiterate, that line of code above is including a library from the stdio file to the program. The purpose of this library for this program will be known momentarily.

Main Function

int main()
{
     return 0;
}

The piece of code above is the main function, and all programs must have this type of function. This is where code from every program starts executing. If the code was written outside of that function, and the code has no relationship with that particular function, then the code will not be read. If a program has no main function, the code would produce an error.

int and return

The first thing a person will see above is int, which represents an integer.  The main function needs to have an int because it needs to return an integer to end the program. The return value signifies how the program ends. In this example, the main function returns 0, which means the program has executed successfully and exits the program. The program would execute unsuccessfully if the main function returns a nonzero value. The return statement in that case would exit the program as well but with an error message. Returning a value in the main function is a convention that everyone must follow.

Function’s name and Parentheses

The word, “main,” is the name of the function. Changing that function name would no longer give a main function in the program since it would be a different function. Thus, that code would produce an error since the main function cannot be found as stated earlier.

There are parentheses that appear to the right of the function name. The pair of the parentheses is a part of a code that can collect data from a different source. The collected data are called arguments. However, there is a word, “void” in between the parentheses to signify that arguments will not be collected from any sources. An example of this will be given under the printf section of this lesson. This will also be covered more in detail in a future lesson.

Scope

The curly braces ({ and }) with code inside is the scope of the main function in this example. Furthermore, the scope means that the code inside is the definition of the main function. The open curly brace ({) begins the scope of the function, and the close curly brace (}) ends the scope of the function.


 
 
 

printf

printf("Hello World!");

This code is calling the printf function from the main function. Function calling means that the program will “jump” from one function into another and start executing statements within the scope of that new function. This is one way you can get the code executed outside of the main function since the printf function is linked from main. Note that we did not create that function. The standard library, stdio.h, already has the printf function created, and that is why we included that particular library to the program.

In addition, there are two words that are enclosed in double quotes (” “) inside of the parentheses. The double quotes have sequence of characters enclosed that form into one item, which is called a string literal. The datum that is being sent to the function is an example of an argument. Finally, the primary purpose of printf is to output the string to the screen. That is the main semantics of the entire program. 

Semi-Colon

printf("Hello World!");
return 0;

A semi-colon signifies an end of a statement. There are two statements in our main function that prints to the screen and returns an integer. Therefore, semi-colons signify the end of both statements.

Conclusion

The syntax and semantics are used to create the program above. The program needs to include the library, stdio.h, for using the printf function. The main function is where the program starts executing the code. The parentheses to the right of function names determine if arguments will be accepted and how many will be accepted. The curly braces define the scope of a function. The semi-colon ends one statement. Finally, the overall semantics of the basic program is to output, “Hello World!” to the screen while exiting the program successfully.

Hello World!
Output
Course Outline  | Previous LessonNext Lesson

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz