Basic Syntax of C Program

Basic Syntax of C Program

C- basic syntax

Table of contents

No heading

No headings in the article.

C is a popular general-purpose programming language used by developers worldwide to create a wide range of applications. As with any programming language, it has a specific syntax that developers must adhere to when writing code. In this article, we'll explore the basic syntax of C and the various tokens that make up a C program.

A token in a C program is a sequence of characters representing a unit of meaning. C tokens are categorized into the following:

  1. Keywords

  2. Identifiers

  3. Constants

  4. String literals

  5. Symbols

Keywords

These are reserved words in C that have specific meanings and identifiers. Some of the commonly used keywords in C include:

auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while

Identifiers

These are user-defined names given to variables, functions, arrays, structures, etc. The identifier can be composed of lower-case, upper-case, underscore, or digits but the initial character should either be an underscore or an alphabet.

Rules for constructing identifiers:

  • The initial character of an identifier should be either an alphabet or an underscore, and it can then be followed by any character, digit, or underscore.

  • It must not start with a numerical digit.

  • Both uppercase and lowercase letters are distinct identifiers. As a result, identifiers are case-sensitive.

  • In an identifier, no commas or blank spaces are allowed.

  • Identifiers cannot be expressed through keywords.

  • The identifiers should not be longer than 31 characters.

  • Identifiers should be written in a manner that is meaningful, brief, and simple to understand.

Here is an example of declaring an integer variable using an identifier.

int age;

Constants

These are values that cannot be changed during the execution of a C program.

Constants in C programs can be declared in the following ways:

  • Using the const keyword

  • using the #define pre-operator

ConstantExamples
Integer constant1, 2,10,15, 30, 50, 100, etc.
Floating-point constant1.4, 33.5, 99.1, etc.
Octal constant011, 022,088 etc.
Hexadecimal constant0x1a, 0x4b, 0x6b, etc.
Character constant'a', 'b', 'c',
String constant"juma", "law", "c++"

String Literals

These are sequences of characters enclosed in double quotes and are used to represent strings of characters in C.

char str[] = "Hello World";

Symbols

These are special characters used in C programs for various purposes. Examples of symbols in C include parentheses, commas, semicolons, and braces. They are used to define the structure of a C program and to separate different elements of the code.

+ - * / % = == != > < >= <= && || ! & | ^ ~ << >>
/** 
Here is an example of how symbols used to perform arithmetics operations in C
**/ 
int a = 10; int b = 20; int c = a + b;

Now that we've explored the various tokens that make up a C program, let's take a look at some basic syntax rules that developers must follow when writing C code:

  • Every C program must have a main() function, which serves as the entry point for the program.

  • Comments in C can be added using the "//" and "/* */" characters to help explain the code to other developers.

  • Square brackets [ ]: The single and multidimensional subscripts are represented by the opening and closing brackets.

  • Simple brackets ( ): They are used in function declaration and calling. Printf(), for example, is a pre-defined function.

  • Curly braces { }: They are used to open and close the code. It is employed in the loop's opening and closing.

  • The comma (,) is used to separate multiple statements, such as separating function parameters in a function call or separating variables when printing the value of multiple variables with a single printf statement.

  • Hash/pre-processor (#): It's a pre-processor directive. It simply means we're using the header file.

  • Asterisk (*): This symbol is used to represent pointers and also as a multiplication operator.

  • Tilde ( ~): It is used to liberate memory as a destructor.

  • The period (.) is used to refer to a member of a structure or a union.

  • Indentation is not required in C, but it can make the code easier to read and understand.

In conclusion, the basic syntax of C consists of various tokens, including keywords, identifiers, constants, strings, literals, and symbols. Understanding these tokens is essential for writing C programs that are concise and efficient.