image
  • 3rd Nov 2022

The C Program Structure

  • Total View :2996
  • Posted By-Biswajit Swain

C has few keywords, 32 to be precise. These keywords, combined with the formal C syntax, form the C language. But many C compilers have added more keywords to use the memory organization of certain preprocessors.

Some rules for programs  written in C are as follows:

  • All keywords  are lowercased
  • C is case sensitive, do while is different from DO WHILE
  • Keywords cannot be used for any other purpose, that is, they cannot be used as a variable or function name
  • main() is always the first function called when a program execution begins (discussed later in the session)

Consider the following program code:

Various aspects of a C program are discussed with respect to the above code. This code will be referred to as sample_code, wherever applicable.

 

Function Definition

C programs are divided into units called functions. The sample_code has only one function main(). The operating system always passes control to main()  when a C program is executed. This  function name is always followed by parentheses. The parentheses may or may not contain parameters.

 

Delimiters

The function definition is followed by an open curly brace ({).This curly brace signals the beginning of the function. Similarly a closing curly brace (}) after the statements, in the function, indicate the end of the function. The opening brace ({) indicates that a code of block is about to begin and the closing brace (}) terminates the block of code. In sample_code, there are two statements between the braces.In addition to functions, the braces are also used to delimit blocks of code in other like loops and decision-making statements.

 

Statement Terminator

Consider the line int i = 0 in sample_code is a statement. A statement in C is terminated with a semicolon (;).  A carriage return, whitespace, or a tab is not understood by the C compiler. There can be more than one statement on the same line as long as each one of them is terminated with a semi-colon. A statement that does not end in a semicolon is treated as an invalid line of code in C.

 

Comment Lines

Comments are usually written to explain the task of a particular command, function or a whole program. The compiler ignores them. In C, comments begin with /* and are terminated with */, in case the comments contain multiple lines. Care should be taken that the terminating delimiter (*/) isn't forgotten. Otherwise, the whole program will be treated like a comment. In sample_code “This is a sample program” is a comment line. In case the comment contains just a single line you can use // to indicate that it is a comment. For example:

The C Library

All C compilers include a standard library of functions that perform the common tasks. In some installations of C, the library exists in one large file while in others it's contained in numerous small files. While writing a program, the functions contained within the library can be used for various tasks. A function written by a programmer can be placed in the library and be used in as many programs as and when required. Some compilers allow functions to be added within the standard library, while some compilers require a separate library to be created.

  • Share:

Leave a Comment