Skip to content
Related Articles

Related Articles

Difference Between Syntax and Semantics
  • Last Updated : 19 Feb, 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to demonstrate semantic error
 
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    // Return statement before cout
    return 0;
 
    // Print the value
    cout << "GFG!";
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate semantic error
import java.util.*;
class GFG
{
 
// Driver Code
public 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate semantic error
import sys
 
# Driver Code
if __name__ == '__main__':
 
    # exit() statement before cout
    sys.exit(0);
 
    # Prthe value
    print("GFG!");
     
# This code contributed by gauravrajput1

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate semantic error
using System;
 
class GFG
{
 
// Driver Code
public static void Main(String[] args)
{
   
    // exit() statement before cout
    Environment.Exit(0);
 
    // Print the value
    Console.Write("GFG!");
}
}
 
// This code is contributed by gauravrajput1

chevron_right


Output: 

 

 

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to demonstrate basic operation
// without any syntax and semantic error
 
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
 
    // To print gfg
    cout << "GFG!";
 
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate basic operation
// without any syntax and semantic error
class GFG{
 
// Driver Code
public static void main(String[] args)
{
     
    // To print gfg
    System.out.print("GFG!");
}
}
 
// This code is contributed by aashish1995

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to demonstrate basic operation
# without any syntax and semantic error
 
# To print gfg
print("GFG!")
 
# This code is contributed by divyeshrabadiya07.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate basic operation
// without any syntax and semantic error
using System;
 
public class GFG
{
 
  // Driver Code
  public static void Main(String[] args)
  {
 
    // To print gfg
    Console.Write("GFG!");
  }
}
 
// This code contributed by Rajput-Ji

chevron_right


Output: 

GFG!

 

Tabular Difference between Syntax and Semantic Error:

Basis

Syntax

Semantics

Meaning It refers to the rules of any statement in the programming language. It refers to the meaning associated with any statement in the programming language
Error It referred to as syntax error. It is generally encountered at the compile time. It occurs when a statement that is not valid according to the grammar of the programming language. Some examples are: missing semicolons in C++, using undeclared variables in Java, etc. It referred to as semantic error. It is generally encountered at run time. It occurs when a statement is syntactically valid but does not do what the programmer intended. This type of error is tough to catch.

 

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :