Semantics and syntax difference

What is the difference between semantics and syntax of a programming language?

Syntax is defined by the grammar of the language. Semantics is definied by the implentation.

For example,
int a;
a = 123;

here is a=123; is both syntactically and semantically correct

but consider this

int a;
int *b;

a = b;

a = b is syntactically correct (grammar allows this, lvalue is a variable that can be written, rvalue is a variable or a constant or an expression) but semantically the statement is wrong.
Some compilers may catch this and some may not, even if they catch it is just a warning. Also some semantic errors like above can be avoided by typecasting, but a syntax error cannot be avoided.

One more example suppose you have a class called String and you declare
String s1, s2;
s1="abc";
s2=s1;

Now syntax tells whether this operation has to be permitted as per the grammar. How it is implemented internally is left to semantics.

You can either copy the value of s1 to s2, which is called value semantics or you can just copy the reference, which is called pointer semantics. In case of value semantics you can change s1 and s2 independently but in case of pointer semantics if you change s1 it changs s2 as well.

There are an awful lot of things in this country that I think would be...

It's Interesting...

Montague grammar is an approach to natural language semantics, named after American logician Richard Montague. The Montague grammar is based on formal logic, especially higher order predicate logic and lambda calculus, and makes use of the notions of intensional logic, via Kripke models. Montague pioneered this approach in the 1960s and early...

Twitter Activity

@StateOfTheU.com: I'm not nitpicking or playing semantics any further, guys. I'm NOT getting into the coachspeak debate. I'm not doing it.
Thu, 25 June 2015 05:12 PM

@IroynA Oh so now we are down to playing semantics games. #GamerGate You are so intellectual dishonest and bankrupt.
Thu, 25 June 2015 08:53 PM

@ClayTravisBGID well I mean if you're playing the game you're really playing it not pretending to play it. #semantics
Thu, 25 June 2015 03:14 PM