How to Work with C Syntax?

c syntaxSyntax is the set of rules governing writing a program. C, of course, has its own rules specifying how character sequences will be grouped in forming tokens – the smallest individual unit that can be found in a C program. Together, these tokens form code statements that dictate what the program should do.

Overview of the C Language

The C Programming language was developed by Dennis Ritchie at Bell Laboratories in 1973. It has a high level of abstraction and machine independent nature. It was developed with a UNIX operating system, one of the most popular network operating systems in use throughout the world.

New to C? Learn more with a class at Udemy.com.

C is actually an evolution of three different languages ALGOL, BCPL and B, all by Dennis Ritchie.  He borrowed many of the concepts of the said languages, as well as introduced many of his own – such as data types, structs and pointers. The American National Standard Institute (ANSI) formalized the language in 1988. In 1990, the International Standard Organization (ISO) approved a version of C, which was referred to as C89.

C Syntax

The structure and rules of the C Programming language is designed to allow programs that have a close relationship with objects while providing high-level data abstraction. It makes use of the “maximal munch principle,” which means that when creating a construct, the maximal amount of available input must be consumed.

Want to learn C syntax? Take a programming course at Udemy.com.

C tokens, the smallest individual unit, are keyword, identifier, constant variable or any symbol that is part of the C language vocabulary. A C program can be composed of collections of different tokens.

Examples of tokens:

  • int
  • {}
  • //comments
  • ;

Basic Rules of C Syntax

  • C is case-sensitive. The declaration int x is different from int X. All instructions must be written in lower case letters.
  • All statements must end with the terminating character – semicolon ‘;’
  • White space denotes blanks and tabs.
  • White space should be used in between keywords and identifiers.

Character Set

The characters that compose the C language syntax are grouped as follows:

  • Letters – This includes all alphabets (a-z, A-Z).
  • Digits – Numbers from 0 to 9.
  • Special Characters – The special characters in C include: semicolon (;), colon (:), period (.), underscore (_), ampersand (&) and more.
  • White Space

C Identifiers

In C programming, identifiers are names that are used to refer to variables, functions, constants and user-defined data. There exists a set of rules that should be followed when creating identifiers:

  • Identifiers should only have alphanumeric characters (a-z, A-Z, 0-9). The only non-alphanumeric character that can be used is the underscore (_).
  • The first character can only be an alphabet or an underscore (_). Numbers are not allowed to start a variable name (int 8var).
  • Identifiers are sensitive to case. Therefore int var is different from int Var or int VAR;
  • No special characters aside from the underscore are allowed to be used in an identifier.

Operators

C has a wide number of built-in operators, which can be classified into different types: Arithmetic, Relational, Logical, Bitwise, Assignment, Conditional and Special.

Getting started with C syntax? Take a course at Udemy.com.

Arithmetic – These are the operators used to process arithmetic equations:

  • +          Adds the operands
  • –           Subtracts the operands
  • *          Multiplies the operands
  • /           Divides the operands
  • %         Returns remainder after dividing the operands
  • ++        Increases the value by 1
  • —          Decreases the value by 1

 

Examples:

int x = y + 11;

int y = x++;

float q = (x/9) + 14;

int x = y++;

Relational – These are used when comparing operands to their left with the operands to their right:

  • ==        Checks if the operands have equal values
  • !=         Checks if the operands do NOT have equal values
  • <          Checks if the operand to its left is LESS than the one on the right
  • >          Checks if the operand to its left is GREATER than the one on the right
  • <=        Checks if the operand to its left is LESS than or EQUAL to the one right
  • >=        Checks if the operand to its right is GREATER than or EQUAL to the one left

Examples:

i == 6

i <= 11

checker != value

Logical – The join statements to its left with statements to its right and compares its Boolean values.

  • &&       logical AND
  • ||         logical OR
  • !           logical NOT

Examples:

(x && b) where x = true and b = true, therefore x&&b is also true.

(x || y) where x = true and y = false, therefore x||y is true.

(!x) where x = true, therefore x is false.

Assignment Operators – These are used to appoint values to variables.

  • =          This assigns the value to its right to the left side operand.
  • +=        Adds the right operand to the one on the left and then assigns the result to the left.
  • -=         Subtracts the operand to the right of it from the left operand and assigns the result to the left.
  • *=        Multiplies the left operand to the one on the right and assigns the answer to the left operand.
  • /=         Divides the left operand with the one on the right and assigns the answer to the left operand.
  • %=       Computes for the modulus and assigns the result to the left operand

Control Structures

The C programming language is a free-form language, which means that bracketing and bracing styles vary from programmer to programmer and is not considered by the compiler.

Statements are lines of code that dictate actions that the computer should perform. Arithmetic statements tell the computer to calculate the given operands using the given operators.

Expressions are finite combinations of explicit values, constants, variables, operators and functions that are translated by the C compiler to extract meaning.

Selection Statements – These statements are used when you need to determine what code to execute depending on the outcome of an expression. There are 2 types, the if and the switch.

Syntax:

//if-else if(<expression>)    <sample statement>else        <sample statement> //switch switch(<expression>){          case<case1>:               <sample statement>               break;          case<case2>:                      <sample statement>                      break;         default:                      <sample statements>               break;}

Iteration Statements – These statements allow code to be looped or executed repeatedly according to the conditions provided. They evaluate expressions and determine whether to restart running the block of code or end the loop.

Syntax:

do<sample statement>while(<expression>); while(<expression>)    <sample statement>    for(<expression>;<expression>;<expression>)    <sample statement>

Functions – These are the whole blocks of code that are called to perform specific tasks. The return type is the data type of the value you wish to return after all the statements are executed (if none, use void). The function name is a description of what your function is for. The parameter list is the list of values or references you would like to be used inside the function (coming from somewhere else). The return statement identifies what value you wish to send back to the client code.

Syntax:

<return_type> sampleFunctionName(<parameter_list>){    <sample statements>              return<expression of type same to return_type>;}

The C Programming language is a very flexible language, which you can use to perform even middle-level data manipulation. For as long as you follow the right syntax and rules as well as make use of the allowed character sets and operators.