What is the Structure or Format of Data Called?

Learn via video course
FREE
View all courses
DSA Problem Solving for Interviews using Java
DSA Problem Solving for Interviews using Java
by Jitender Punia
1000
4.9
Start Learning
DSA Problem Solving for Interviews using Java
DSA Problem Solving for Interviews using Java
by Jitender Punia
1000
4.9
Start Learning
Topics Covered

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

If we write the above code in C++, it will show the following syntax error

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.

Output:

Below is another example

Example 2

Output:

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.

Output:

Difference between Syntax and Semantics

SyntaxSementics
It refers to the set of rules and regulations that helps to write a statement in any programming languageIt 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 occurredSemantic 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 keywordsExample: 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