Elements of C

Image result for elements of c language

Overview

Computer does not do anything on its own. he most important skills to learn is how intelligently one can program it. It can be used to solve multidimensional problems. The C being a magic tool for writing programs is used to solve variety of problems; a beginner just need practice and more practice of writing programs to master it.
             Here we shall introduce the basic elements of C language.these are actually the building blocks of every C program. The proper use of these basic elements helps a lo to write effective C programs.

  • Identifiers
     "Identifiers are the names used to represent variables, constants, types, functions, and labels in the program". Identifiers in C can contain any number of characters, but only the firs 31 are significant to C compiler. There are two types of identifiers in |C: 

Standard identifiers                                         User-defined identifier

Standard Identifiers:

"Like reserved words, standard identifiers have special meaning in C, but these can be redefined to use in the program for other purposes, however this practice is not recommended. If a standard identifier is redefined, C no longer remains able to use it for its original purpose". Examples of standard identifiers include print f and scan f , which are names of input/output functions defined in standard input/output library i.e., stdio.h.

User-defined Identifiers:

"In a C program, The programmer may need to access memory locations for storing data and program results. For this purpose memory cells are named that are called user-defined identifiers".

C is a case sensitive language. This means that C compiler considers uppercase and lowercase letters to be distinct characters. For example, the compiler considers SQUARE_AREA and square_Area as two different identifiers referring to different memory locations.

Keywords:

"Keywords or reserved words are the words, which have predefined meaning in C. There are 32 words defined as keywords in C". These have predefined uses and cannot be used or redefined for any other purpose in a C program. They are used by the compiler as an aid to compile the program. They are always written in lower case. A complete list of ANSI C keywords is given at the end of this chapter.

Keywords or reserved words can not be redefined in the program.

Variables:

"Variables are named memory locations (memory cells), which are used to store program's input data and its computational results during program execution".
                                         int a      and     float abc

As the name suggests, the value of a variable may change during the program execution. We are familiar with the concept of variable with reference to algebra. The variables are created in memory (RAM); therefore the data is stored in them temporarily. one should not mix the contents of a variable with its address. These are entirely different concepts. The contents of a variable can be thought of as the residents of your neighboring house, while the address of the variable can be thought of as the address of that house.

Declaring a Variable in C:

C is a strongly typed language i.e. all variables must be declared before being used. The compiler will report an error if an undeclared variable is used in a program. A variable is declared in C by specifying its type (data type) and name. The syntax takes the form:

                data type            .variable_name;

data type refers to the type of the data being stored in the variable. For example

   int            kgs;
   double     length:

A list of names of variables separated by commas is specified with the data type to declare more variables of the same data type such as:

   int            marks, total_students, no_of_rooms; 
   double     kgs, length, volume, height;

Declaring vs Defining a Variable:

Variable declaration tells the compiler the names of the variable to be used in the program and the type of the information stored in it. in a C program, the

                                       int     volume;
                                       char       ch;
variable declaration tell the compiler the name of two variables (volume and ch) used to store an integer and character data respectively.

A variable declaration does not set aside memory location for the data to be stored. it just informs the compiler the name of the variable and the type of data to be stored in it, while the definition of the variable that set aside memory location for the variable.

however in C, the variable declaration statement not only declares the variable but also defines it as in case of above two statement. It does not mean that the declaration of a variable can not be separated from its definition in C. C is such a powerful language that provides us all what we need for developing a program.

Initializing a Variable:

"Assigning a value to a variable at the time of its declaration is called initializing a variable".
In a C program, when a variable is declared, the compiler set aside some memory space for it. This allocated memory space may contain data meaningless to the program (also called garbage). The computation involving this variable may produce unexpected results. To avoid this situation, all variables should be declared before being used. In C, a variable can also be initialized at the time of its declaration e.g.,
                 int    count;    (variable declaration and definition)
                 count = 125;  (variable initialization)
                 char     ch = 75.8;

Rules for Naming Variables:

To use variables in C programs, we must know the rules to choose names for them. Here are some important rules for naming a variable in C:

  • A variable name can consist of letters, digits, and the underscore character ( _ )
  • The first character of the name must be a letter. the underscore is also a legal first character, but its use is not recommended. Therefore, 9 winner, home, and 2 kgms are invalid names in C.
  • C is a case sensitive language. Thus, the names count and COUNT refers to two different variables.
  • C keywords can't be used as a variable names e.g., we can not use int, void,signed, or while as variables names.
  • For many compilers, a C variable name can be up o 31 characters long. (It can actually be longer than that, but the first 31 characters of the name are significant)   e.g., Turbo  C++  restricts the maximum length of a variable name to (31 characters). Hence, the names    problems_solving_techniques_in_C_language      (40 characters)    would appear to be the same to the compiler. The compiler does not differentiate these two names because the first 31 characters of both are the same.
  • Blank spaces are not allowed in the name e.g., problem solving is an invalid variable name in C.
  • A variable can only be declared for one data type.
  • Reserved C words cannot be used as a variable names.
C programmers commonly use only lowercase letters in variable names, although this isn't required. using all-uppercase letters is usually reserved for the names of constants.
  

Variable Names should be Readable:

Let's consider a program that calculates loan payments could store the values of the prime interest rate in a variable named interest_rate. The variable name helps make its usage clear. we could also create a variable named x or even Ahmed; it doesn't matter to the C compiler. Many naming conventions are used for variable names. We've seen one style:  interest_rate. Using an underscore to separate words in a variable name makes it easy to interpret. Another style is to capitalize the first letter of each word. Instead of interest_rate, the variable would be named interest Rate.

Constants:

"A constant is a quantity whose value can not be changed". Unlike a variable, the value stored in a constant can't be changed during execution. The define directive can be used to define constant macros, for example: 
                               #define     PI    3.142857
defines a constant i.e. PI, whose value will remain unchanged during the program execution. C has two types of constant; numeric constant and character constant, each with its own specific uses.
                                   

Numeric Constants:  Numeric constants consist of numbers. It can be further divided into integer and floating point constants.

Integer: Integer constants represents values that are counted and do not have a fractional part e.g., +56, -678, 8, etc.

Floating Point: Floating point constants represents values that are measured e.g., -4.786, 5.0, 0.45 etc.

 Character Constants:

A character constant is a single alphabet, a single digit or a single symbol enclosed within the apostrophes e.g., 'A' is a valid character constant., 'A', 'I', '5', '=' etc. The maximum length of a character constant is 1 character.

Data Type:

In C, the data type defines a set of values and a set of operations on those values. We know that computer program manipulates various types of data. The data is given to the program as input. The data is processed according to the program instruction and output is returned. In program designing, the data and its type are defined before designing the actual program used to process the data. The type of each data value is identified at the beginning of program design. The values or data used in a program may be of different types.
             In a C program, we may need to process different type of data. Therefore, variables should be capable of storing data of different types. This is done by associating certain data types to the variables.
         To work with different types of data, C defines some standard data types and also allows us to define our own data types known as user-define data types. In C, a standard data type is one that is predefined in the language such as int, double, char, float etc. Let's look at some of the important standard data types:

Data Types for Integers  (int, short, long):

In C, the data type int is used to represent integers - the whole numbers. This means that int variable store number that have no fractional parts such as 1128, 1010, 32432 etc. Along with standard type int, the compiler also supports two more types of integers i.e. short int and long int, often abbreviated to just short and long. In addition to these types, an integer variable can be signed or unsigned. If not mentioned, all integer variables are considered to be signed. The data type signed int and int can handle both signed and unsigned whole numbers such as 245, 1010, and -242 etc, where as the data type unsigned can not handle negative numbers.

Because of the limited size of the memory cell, all integers can not be represented by int, short or long. When a variable of type is int is declared, the compiler allocates two bytes of memory to it. Therefore, only the numbers ranging from -2 through 2 - 1 (i.e. -32768 to 32767) can be represented with the int type variables. The variable of type unsigned int can handle numbers ranging from 0 through 2 -1. (i.e. 0 to 65635). The long is used to represent larger integers. It occupies four bytes of memory and can hold represent larger integers. It occupies four bytes of memory and can hold numbers ranging from -2 through 2 -1 (i.e -214748648 to 2147483647).

 Data Types for Floating Point Numbers (float, double, long double):

Floating point numbers are the numbers that have a fractional part e.g. 12.3456, 0.4564, and -8.23, 101.234 etc. The floating point numbers are represented in computer in a format that is analogous to scientific notation (floating-point format). The storage area occupied by the number is divided into two sections: the mantissa and the exponent. Mantissa is the value of the number and the exponent is the power to which it is raised.

For example, in exponential notation the number 245634 would be represented as 2.45634 * 10 power of 5, where 2.45634 is the mantissa and 5 is exponent. However in C, the representation of scientific notation is slightly different e.g. the above number will be represented as 2.45634e5. We don't express the exponent as the power of 10, rather the letter e or E is used to separate exponent from the mantissa. If the number is smaller than 1 (one), then exponent would be negative. For example, the number 0.00524 will be represented in computer as 5.24 E-3.

ANSI C specifies three floating point types that differ in their memory requirements: float, double, and long double. These data types provide higher precision than the data types used for integers. The following table shows memory requirement, precision and the range of values that can be represented by data types float, double and long double.
                              

























































Comments

Popular posts from this blog

Data Base Design Process

Data Base

Data Integrity And Normalization