Difference Between Syntax and Semantics - GeeksforGeeks

29 Dec, 2021
Syntax:
It refers to the rules and regulations for writing any statement in a programming language like C/C++.
It does not have to do anything with the meaning of the statement.
A statement is syntactically valid if it follows all the rules.
It is related to the grammar and structure of the language.
Semantics:
It refers to the meaning associated with the statement in a programming language.
It is all about the meaning of the statement which interprets the program easily.
Errors are handled at runtime.
Program 1: Below is the code to demonstrate the semantic error:
C++
Java
Python3
C#
Javascript
// C++ program to demonstrate semantic error #include <iostream>using namespace std; // Driver Codeint main(){    // Return statement before cout    return 0;     // Print the value    cout << "GFG!";}
// Java program to demonstrate semantic errorimport java.util.*;class GFG{ // Driver Codepublic static void main(String[] args){       // exit() statement before cout   System.exit(0);     // Print the value    System.out.print("GFG!");}} // This code is contributed by aashish1995
# Python program to demonstrate semantic errorimport sys # Driver Codeif __name__ == '__main__':     # exit() statement before cout    sys.exit(0);     # Print the value    print("GFG!");     # This code is contributed by gauravrajput1
// C# program to demonstrate semantic errorusing System; class GFG{ // Driver Codepublic static void Main(String[] args){       // exit() statement before cout    Environment.Exit(0);     // Print the value    Console.Write("GFG!");}} // This code is contributed by gauravrajput1
<script>// javascript program to demonstrate semantic error     // Driver Code         // exit() statement before cout    function fun()    {        return;         // Print the value        document.write("GFG!");    }        fun(); // This code is contributed by gauravrajput1</script>
 
 
Explanation:
The output will be blank because the above program is semantically incorrect.
This program has no syntax error as it is following every programming rule but still, it will not print anything on the screen because the return statement is written before the cout statement which causes the program to terminate before printing anything on the screen. This type of situation is considered a semantic error.
Program 2: Below is the correct code i.e, without any syntax and semantic error. 
C++
Java
Python3
C#
Javascript
// C++ program to demonstrate basic operation// without any syntax and semantic error #include <iostream>using namespace std; // Driver Codeint main(){     // To print gfg    cout << "GFG!";     return 0;}
// Java program to demonstrate basic operation// without any syntax and semantic errorclass GFG{ // Driver Codepublic static void main(String[] args){         // To print gfg    System.out.print("GFG!");}} // This code is contributed by aashish1995
# Python3 program to demonstrate basic operation# without any syntax and semantic error # To print gfgprint("GFG!") # This code is contributed by divyeshrabadiya07.
// C# program to demonstrate basic operation// without any syntax and semantic errorusing System; public class GFG{   // Driver Code  public static void Main(String[] args)  {     // To print gfg    Console.Write("GFG!");  }} // This code contributed by Rajput-Ji
<script>// Javascript program to demonstrate basic operation// without any syntax and semantic error // To print gfgdocument.write("GFG!"); // This code is contributed by patel2127</script>
GFG!
 
Syntax
Semantics
aashish1995
GauravRajput1
Rajput-Ji
divyeshrabadiya07
patel2127
khushboogoyal499
ankita_saini
sagartomar9927
C++
C++ Programs
Difference Between
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Friend class and function in C++
Sorting a vector in C++
std::string class in C++
Header files in C/C++ and its uses
Program to print ASCII Value of a character
C++ Program for QuickSort
How to return multiple values from a function in C or C++?
Shallow Copy and Deep Copy in C++