Difference Between Syntax and Semantics Last Updated : 23 Jul, 2025 Suggest changes Share Like Article Like Report 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++ // 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!"; } 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 <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> Output Explanation: The output will be blank because the above program is semantically incorrect syntax.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 errors. C++ // 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; } 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 <script> // Javascript program to demonstrate basic operation // without any syntax and semantic error // To print gfg document.write("GFG!"); // This code is contributed by patel2127 </script> OutputGFG!Tabular Difference between Syntax and Semantic Error:BasisSyntax Semantics MeaningIt refers to the rules of any statement in the programming language.It refers to the meaning associated with any statement in the programming languageErrorIt 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 linguisticsThe 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.SensitivityThe syntax is case sensitive in most programming languages.Most of the semantics are case-insensitive. C CoderSaty Follow Article Tags : Misc C++ Programs Difference Between C++ Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like