Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

What is the difference between syntax and semantics of programming languages (like C)?
This question seem to be basic but I do not get the satisfactory answer yet.

share|improve this question

3 Answers

up vote 6 down vote accepted

Syntax is about the structure or the grammar of the language. It answers the question: how do I construct a valid sentence? All languages, even English and other human (aka "natural") languages have grammars, that is, rules that define whether or not the sentence is properly constructed.

Here are some C language syntax rules:

  • separate statements with a semi-colon
  • enclose the conditional expression of an IF statement inside parentheses
  • group multiple statements into a single statement by enclosing in curly braces
  • data types and variables must be declared before the first executable statement

Semantics is about the meaning of the sentence. It answers the questions: is this sentence valid? If so, what does the sentence mean? For example:

x++;                  // increment
foo(xyz, --b, &qrs);  // call foo

are syntactically valid C statements. But what do they mean? Is it even valid to attempt to transform these statements into an executable sequence of instructions? These questions are at the heart of semantics.

Consider the ++ operator in the first statement. First of all, is it even valid to attempt this?

  • If x is a float data type, this statement has no meaning (according to the C language rules) and thus it is an error even though the statement is syntactically correct.
  • If x is a pointer to some data type, the meaning of the statement is to "add sizeof(some data type) to the value at address x and store the result into the location at address x".
  • If x is a scalar, the meaning of the statement is "add one to the value at address x and store the result into the location at address x".

Finally, note that some semantics cannot be determined at compile-time and must therefore must be evaluated at run-time. In the ++ operator example, if x is already at the maximum value for its data type, what happens when you try to add 1 to it? Another example: what happens if your program attempts to dereference a pointer whose value is NULL?

In summary, syntax is the concept that concerns itself only whether or not the sentence is valid for the grammar of the language . Semantics is about whether or not the sentence has a valid meaning.

share|improve this answer
 
OK. If x is at the maximum value for its data and 1 is added to it then it results in some weird output (0), isn't it semantic error? –  haccks Jul 29 at 19:09
 
Consider an odometer in a vehicle -- it has a series of interrelated wheels with the digits 0 through 9 printed on each one. The rightmost wheel rotates the fastest; when it wraps from 9 back to zero, the wheel to its immediate left advances by one. When this wheel advances from 9 to 0, the one to its left advances, and so on. –  Jeff N Jul 29 at 19:20
 
What does it mean? –  haccks Jul 29 at 19:21
 
A datatype is like the wheel of an odometer: it can only hold up to a certain value. When the maximum value is reached, the next advance causes the wheel to return to zero. Whether or not this is a semantic error depends on the language rules. In this case, you need to refer back to the C language standard. I don't know exactly what the C language standard says, but here are some of the options. Overflow is: -not an error; the result is zero. -an error; the compiler MUST generate an overflow exception. -UNDEFINED;the compiler is free to do whatever it wants. –  Jeff N Jul 29 at 19:32

Syntax refers to the structure of a language, tracing its etymology to how things are out together.
For example you mgiht require the code to be put together by declaring a type then a name and then a semicolon, to be syntactically correct.

Type token;

On the other hand, the semantics is about meaning. A compiler or interpreter could complain about syntax errors. Your co-workers will complain about semantics.

share|improve this answer

Semantics is what your code means--what you might describe in pseudo-code. Syntax is the actual structure--everything from variable names to semi-colons.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.