13 June 2013

Simple C - Chapter 2, Constant and Variable



In this chapter we will continue with some basic definitions and the general structure of the C program. Humans need to communicate with each other and they use languages like English as a mean for communication. In any language, alphabets combined together to create words and sentences. In the C program, like any other ordinary language, letters, digits and special characters use for creating keyword, constant and variables. this combination of alphabet and special characters is called Character Set of the C language.
When we write a program, we need to input data for the manipulation. This data and information need to store in some locations of the memory. In the C program we can use two identifiers "Constant" and "Variable" to assign data. A variable simply is a name we give to a location in the memory for storing data or constant.

 
Constant

The constant is an entity that its value does not change during execution of a program. In the statement "Pi = 3.14", 3.14 is a constant. We have different type of constant in the C program such as Numeric constant and Non-numeric constant. But we can categorize the constant in two major types:

1. Primary constant

2. Secondary constant

The Primary constant further divided into

  • Integer constant
  • Real constant 
  • Character constant
  
And secondary constant into

  • Array
  • Pointer
  • Structure
  • Union
  • Enum

We will learn all about these constants in their corresponding chapters later.


Variable

A Variable is a name that we are given to a memory location in the computer, to store some values. Consider the expression  

X = 20

Here X is the variable name and 5 is the constant. So variable is an entity that may vary during execution of the program. An integer variable can only hold an integer constant, a real variable can hold a real variable and finally a character constant can hold only a  character constant.


Rules for declaring variable name

1. A variable name is a combination of alphabet, digits and underscores.  

2. The first character must be an alphabet.

3. Its length must not exceed 31 characters (some compilers allow up to 247 characters).

4. No commas or blanks or spaces are allowed within a variable name.

5. No special symbols other than underscore are allowed within the variable name.

 
Keywords

Keywords in c language are predefined or reserved words whose meanings are defined for c compiler. Example of keywords are char, int, if, do etc. As the keywords are token of C program then obviously we cannot use a keyword as a a variable name.


General structure of a C program

1. Documentation section

2. Preprocessor directive section

3. Main function, main()

4. Body of the program start with "{" and end by "}" and contains variables declaration and instructions

5. End of the program 

Not: We can add more sections to this general structure like global variable declaration and user defined functions. We will study about these sections in more details in the further chapters. 
 

A simple C program



Now let me explain about each section of the C program's structure to understand the concept better.


1. Documentation section

The first line of our above simple C program is the documentation section. In the documentation section, we can write comments about our program. Comments are non executable statements that we use to give an explanation about statement/s. Using comments make the program more readable for other programmers or developers. Comments are optional in the C program and the general syntax is:


Any number of comments can be used in any place in a program and you can apply comments in multiple lines like:



2. Preprocessor section

The lines 2 and 3 represent the Preprocessors of our simple C program and are pre-compiler statements. If the compiler contains the INCLUDE and LIB sub directories in the TC directory, then defining these pre-compilers in the beginning of the program are necessary. The Pre-compiler statements are separated in two parts:

  • Link
  • Definition


Link

As its name suggests, this part links the predefined functions such as printf(), scanf() and clrscr() included in the compiler library. The general form is:


In our simple C program, in the second line the statement "#include<stdio.h>" means, include the sub directory having the header file "stdio.h" and similarly for the third line "conio.h". The stdio.h signifies the standard input/output header file and conio.h is in the charge of printing the output on the system screen.


Definition

In the Definition section, we can define variables and assign values to them. The general syntax is:


For example #define Age 30


3. Main() function

The line number 4 is the Main function. In the C language, every function has a pair of parentheses "()" associated with it. The Main is a collection name given to a set of statements. These statements enclosed in a pair of bracket "{}". Every C program must contain a main() function and the compiler always starts from this function. Also each statement must end with a semicolon ";".



4. Body of the program

The statements that included in the main() function (line 5 to 12) is called the Body of the program. It always starts with a "{" and end with a "}". In this section we declare our variables and write executable statements or instructions. 


5. End of the program

We will not be able to execute our program unless we end it before the right curly brace "}". We use the getch() function (line 11) to end our program. This function returns the next character from the standard input (stdin) and gets a single character from the console.  Now lets execute our simple C program and see the result. To compile the program press Alt + F9 and to run the program press Ctrl + F9. Keep in mind also that the C program is a case sensitive program therefore "A" and "a" are not equal to each other. 




Unable to open include file 'stdio.h' error

If you use the Turbo C++ compiler, you may face the "Unable to open include file 'stdio.h' or 'conio.h'" error. This problem is due to the environment include path. To solve the issue, check your compiler root directory name and path. Try to rename the root directory name to a short name like "TC". For example I have copied the Turbo C compiler in the C: drive and rename it to TC. Now from the menu, go to the Options>Directories and under the "Include Directories", set the path to the C:\TC\INCLUDE and under the "Library Directories", set the path to the C:\TC\LIB. Make sure that the Turbo C (TC) compiler root directory on your system contains the Include and Lib directories. The paths you enter in the Options>Directories must match the path of the TC compiler on your system.





The function printf should have a prototype error

If you get the "Function printf should have a prototype" error, then from the menu, got to the Options>Compiler>C++ options. In the next window, select "C++ always". 



Now again go to the Options>Environment>Editor and make sure that the default extension is "C" not "CPP".