Language Basics




In this section we will cover the basic concept of C languages. Below are topics on which we will focus here.

C Tokens:

C Tokens are the basics and smallest units of the C program. There are mainly six types of tokens.

  • Keywords
  • Identifiers
  • Constants
  • Strings
  • Operators
  • Special symbols

Keywords and Identifiers:

Keywords are the sequence of characters that have one or more fixed meanings. Meanings of the keywords cannot be changed
by any means. One more point to be noted is that all the keywords must be written in lowercase. Below is the list of keywords
supported by C.

List of keywords
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 are the names that we give to the program elements such as names of variables, arrays, functions, etc. There are certain
rules which need to be followed while declaring an identifier name, the same are listed below.

  • First character must be a an alphabet (lowercase or uppercase) or an underscore.
  • After that all succeeding must be either letter or digits.
  • No special character are allowed except underscore "_" .
  • No two successive underscore are allowed.
  • Keywords should not used as identifiers.
  • C treats uppercase and lowercase identifiers as different.



Structure of the program:

For better understanding the format of C program we can subdivide it into its basic components. Below are the same:

  • main() function
  • Curl braces {… }
  • Declaration and statements
  • User defined functions

We can write the syntax describing the basic structure of the program as below:

                
                  preprocessor directives
                  global variable declaration
                  main()
                  {
                    local variable declarations;
                    function calls;
                    statements;
                  }
                  user defined function
                
              


Preprocessor directives:
Preprocessors are the states which are first to be executed during the compilation process. This statement directs the C preprocessor
to include the header files or other dependent libraries. It always starts with "#" symbol.
E.g. - #include < stdio.h> : Including library for standard input and output function.
        #include < Demo.h> : Including the header file "Demo".
        #define Blank 0 : Defining the symbolic constant Blank = 0.

Global Variable Declaration:
Global variables are those variables whose existence is known into the main function and other user-defined functions. Their
declaration should be made outside and before the main() function.

main() function:
The main function is the compulsory function of a C program. The execution of a C program starts with the main() function. All the
other library functions or user-defined functions should be called from inside of the main function. Below are some of the rules regarding
the main function:

  • The function main() should always be written in lowercase letter.
  • It should not be terminated by a semicolon.
  • There must be only one main() function in each C program.

Curly Braces:
Every function in a C program uses a pair of curly braces to indicate the start and end of the function. Between the opening and closing
braces lies the body of the function where the main logic or calculations goes.

Declarations & statements:
Inside the body of the function, the main logic of the program is written using declarations and statements.Declaration is the part of the
program where all the variables, functions, arrays, etc. which will be further used in the program are declared and may be initialized with
their basic data types. On the other hand, Statements are the set of instructions to the computer to perform some specific task. There are
different types of statements:

  • Arithmetic statements
  • Control statements
  • Input-output statements, etc.

User defined functions:
These the Subprograms or subroutines which are defined by the user to implement some specific logic or some specific calculations. They
may be written before or after the main() function. For their execution they must be called from the main(). We look them in details in the
function chapter.

Compilation and Execution process:

Compilation is the process of translating the C program into machine-readable code by the C compiler. When we are using a simple editor
to write the code then we need to install the compiler separately. But also there are various IDEs (Integrated Development Environment)
are available in which C compiler is already embedded. These IDEs provide various features like editor, compiler, debugger, linker, loader,
testing, etc. Examples of these IDEs are Turbo C, Borland C or C++, Visual studio, etc.

Now coming to the program execution, we can divide the whole process into following steps:

  • Writing the C Program source files (of .c extension) and if necessary header files (.h extension).
  • Compilation of the program
  • Linking the dependent system library
  • Execution of the generated binaries for final output.

In case we are using the IDEs to write the c program then we simply compile and run the code from the available menu options.But if
we are working on Unix or Linux platform and using vi editor to write the code then we have to use the below command in the command
prompt or terminal:

  • "vi " command to create the source file.
    Ex. vi testProg.c
    This will create C source file with filename testProg and open the editor.
  • Then write the program in the editor, save it and quit the vi editor.
  • "cc" command to start the compilation process.
    Ex. cc testProg.c .
    This will compile and link the program and generate the executables with name a.out
  • To run the executable binaries give the command "./a.out". This will gives us the final output.