Difference Between Syntax and Semantics (original) (raw)

Last Updated : 10 Jun, 2024

**Syntax:

**Semantics:

**Program 1:
Below is the code to demonstrate the semantic error:

C++ `

// C++ program to demonstrate semantic error

#include using namespace std;

// Driver Code int main() { // Return statement before cout return 0;

// Print the value
cout << "GFG!";

}

Java

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

Python

Python program to demonstrate semantic error

import sys

Driver Code

if name == 'main':

# exit() statement before cout
sys.exit(0);

# Print the value
print("GFG!");

This code is contributed by gauravrajput1

C#

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

JavaScript

`

**Explanation:

**Program 2:
Below is the correct code i.e, without any syntax and semantic errors.

C++ `

// C++ program to demonstrate basic operation // without any syntax and semantic error

#include using namespace std;

// Driver Code int main() {

// To print gfg
cout << "GFG!";

return 0;

}

Java

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

Python

Python3 program to demonstrate basic operation

without any syntax and semantic error

To print gfg

print("GFG!")

This code is contributed by divyeshrabadiya07.

C#

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

JavaScript

`

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 is referred to as a 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 a 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.
In linguistics The syntax is the arrangement or order of words, determined by both the writer’s style and grammar rules. There are two areas of semantics that are logical semantics and lexical semantics.
Sensitivity The syntax is case sensitive in most programming languages. Most of the semantics are case-insensitive.