The structure or format of data is called the Syntax, which is defined by various combinations of pre-defined symbols in a programming language.
What is Syntax?
A Syntax in a programming language is the set of rules that defines a particular language, by defining the correct arrangements of the symbols which are pre-defined in a particular language eg: “;” , “->” , and the keywords like “int” , “float” etc.
Example
We have to declare a variable x and initialize it with an integer value of 4.
Statement 1 : 4 int; = x
Statement 2 : int x = 4;
Statement 1 is not correct in terms of syntax, but Statement 2 is correct. This is because, in the C++ language (like in any other programming language), we have a pre-defined set of rules, and if these rules are not followed, the compiler will generate errors. So according to C++, if we want to declare a variable and initialize it with some value, we must follow the syntax according to Statement 2.
What is Semantics?
While we know that syntax is the pre-defined set of rules which defines a particular language, semantics refers to the logic of the code which we have written according to the problem we are solving. Given below are some examples.
Example
Given two numbers x and y, we have to calculate their product.
Statement 1: int product = x+y;
Statement 2: int product = x-y;
Statement 3: int product = x*y;
All the Statements written above are correct in terms of syntax, but the logic of Statement 1 and Statement 2 is incorrect because we have to find the product of x and y, but in Statement 1 we have calculated the sum and in Statement 2 we have calculated the difference, so the only correct statement according to the problem is Statement 3.
Syntax Error
Syntax error as the name suggests is the error in the syntax of a statement of a program written by a programmer. In simple words we can say, Syntax errors are the mistakes in the source code, caused by incorrect spelling or incorrect symbols which are not defined in the programming language, and sometimes syntax errors can be as small as a single character mistake.
Example 1
#include<bits/stdc++.h>
using namespace std;
int main() {
cout << "Hello World!"
}
If we write the above code in C++, it will show the following syntax error
Line 5: Char 27: error: expected ';' after expression
cout << "Hello World!"
1 error generated.
This syntax error is clearly saying that we need to put a ‘;’ (semicolon) after the statement cout << “Hello World!” to make this code correct.
So now after making changes, this code will run fine.
#include<bits/stdc++.h>
using namespace std;
int main() {
cout << "Hello World!";
}
Output:
Hello World!**
Below is another example
Example 2
#include<bits/stdc++.h>
using namespace std;
int main() {
int x=2;
int y=4;
cout<<(x+y;
}
Output:
Line 8: Char 15: error: expected ')'
cout<<(x+y;
^
Line 8: Char 11: note: to match this '('
cout<<(x+y;
^
1 error generated.**
This syntax error is clearly saying that we need to put a ‘)’ (closed parenthesis) to match this ‘(‘ (open parenthesis).
So now after making changes, this code will run fine.
#include<bits/stdc++.h>
using namespace std;
int main() {
int x=2;
int y=4;
cout<<(x+y);
}
Output:
6
Difference between Syntax and Semantics
Syntax | Sementics |
---|---|
It refers to the set of rules and regulations that helps to write a statement in any programming language | It refers to the meaning associated with the statement written in a programming language |
Syntax errors are found by the compiler after the program has been completely executed. | Semantic errors can be found during the runtime because if our answer is wrong, there must be some logical mistake |
Syntax errors can be found easily because the compiler immediately finds the syntax errors and the line in which the error has occurred | Semantic errors are sometimes very difficult to find, because sometimes in a large code, it is difficult to find a small logical mistake |
Example: missing semicolon, missing parenthesis, wrong spelling of keywords | Example: We want to calculate x+y but by mistake we put ‘-‘ (minus) sign instead of ‘+’ (plus) sign, so our answer will be wrong |
Conclusion
In this quick tutorial, we learned the following
- We answered the question What is the Structure or Format of Data called?
- We learned what syntax and semantics is
- We learned what syntax error is with examples
- Finally, we saw the difference between syntax and semantics with examples