C – Basic Syntax and Semantics

Course Outline  | Previous LessonNext Lesson

Basic Syntax and Semantics

The basic, computer 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, and the following sections will explain the syntax and semantics for each parts of the code.

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. Thus, the two semi-colons signify the end of both statements.

Styling

The main function can be written in different ways without affecting the performance of the program. The program below uses a different style of the main function compared to the program above.

#include <stdio.h>

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

The opening curly brace ({) is now under the line, “int main()”. The program above works the same way as the previous program. Remember that moving that curly brace does not affect the performance of the program. In addition, that curly brace’s new location does not increase the size required to store that program in the computer’s memory.

The program below uses a different style for the main function.

#include <stdio.h>

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

This style may look strange, but the program still works the same way as the previous programs. The computer can still tell that there are two statements in the main function due to the semi-colon.

On the other note, beginners often make a false assumption that this styling of the code would make the program run faster and takes up fewer space compared to the previous programs above. That is a false assumption because the computer basically translates the code to machine language (zeros and ones) to execute the program. The computer ignores spaces and newlines (pressing enter on the keyboard creates a newline) while translating. What this computer is doing is called, compiling. The software that runs this mechanism in the computer is called a compiler. However, the compiler’s actual process is more complicated than just translating the code to machine language. That detailed process is beyond the scope of this course. This is why styling does not affect the program’s performance. However, be sure that the code is easily readable by any C programmers.

Conclusion

The syntax and semantics are used to create the programs above. The programs need 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

Always keep in mind that styling of the code is merely cosmetic since the compiler removes unnecessary spaces and newlines in the code.

Course Outline  | Previous LessonNext Lesson

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz