Wednesday, January 8, 2014

Chapter 2: Basic Elements of C



Basic Elements of C

2.1 C Basics

The C character set

C uses the uppercase letter A to Z, the lowercase letters a to z, the digits 0 to 9, and certain special characters as building blocks to form basic program elements.(e.g. constants, variables, operators, expressions etc.). The special characters are listed below:
            +          -           *          /           =          %         &         #          !           ?          ^                      "           '           ~            \           |           <          >          (           )           [           ]
            {          }          :           ;           .           ,           _          (blank space)
Certain other characters such as @ and $ can be included within string and comments.
C uses certain combination of these characters such as \b, \n, \t to represent special conditions such as backspace, new line, and horizontal tab respectively. These character combinations are known as escape sequences.

C Tokens
o       In a C source program, the basic element recognized by the compiler is the token.
o       Token is the smallest individual unit in a program.
o       In any programming language, any program is made up of individual syntactic elements, called tokens. These includes: identifiers, variables, constants, punctuators, keywords, operators etc.
o       A token is a source program text that the compiler does not break down into component elements.

2.2 Escape Sequences
We saw earlier how the new line character,\n, when inserted in printf( )'s format string, takes the cursor to the beginning of the next line. The new line character is an "escape sequence", so called because the backslash symbol (\) is considered an "escape character": it causes an escape from the normal interpretation of string, so that the character is recognized as one having a special meaning.
Some escape sequences used in C are:
Escape Sequence                               Purpose
\n                                                         New Line (Line feed)
\b                                                         Backspace(Moves the cursor one position to the left of its current position)
\t                                                          Horizontal Tab
\r                                                          Carriage return( Takes the cursor to the beginning of the line in which it is currently placed)
\f                                                          Form feed
\'                                                          Single quote
\"                                                          Double quote
\\                                                          Backslash
\a                                                         Alert (Alerts the user by sounding the speaker inside the computer)
\?                                                         Question mark

e.g. printf("He said,\"Let's do it!\""); will print
      He said,"Let's do it!"

2.3 Identifiers (Variables and Constants)
Identifier is the word used for name given to variables, functions, constants etc. It can also be defined as a set of combinations of one or more letters or digits used to identify constants, variables, functions etc.
Rules for Valid Identifiers
1.ANSI standard - 31 characters are allowed. (Some implementation of C only recognizes 8 characters). It would be better to follow the rule of 8 characters.
2.Both uppercase and lowercase letters may be used and are recognized as distinct.
3.First character should always be letter.
4.Only underscore (_) is allowed among special symbols and is treated as letter.
5.Space and other symbols are not valid.
6.Proper naming conventions should be followed.

Valid Identifiers                                                Invalid Identifiers
Count                                                               4th
test23                                                               order-no
high_balance                                                     error flag
_test                                                                 hi!there

 

Keywords

Keywords are the words whose meaning has already been explained to the C compiler. The keywords cannot be used as variable names because if we do so we are trying to assign a new meaning to the keyword, which is not allowed by the compiler. The keywords are also called reserve words. Note that keywords are always lowercase. There are only 32 keywords available in C

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

 

Variables and Constants:

Constants

The term constant means that it does not change during the execution of a program. It can be classified as:
a)Primary constants (integer, real, character, string)
b)Secondary constants (array, pointer, structure, union)
Here, we will discuss only primary constants.

i. Integer constants: Integer constants are whole numbers without any fractional parts.

Rules for constructing Integer constants:
1.An integer constant must have at least one digit.
2.It must not have a decimal point.
3.It could be either positive or negative
4.If no sign precedes an integer constant is assumed to be positive.
5.No commas or blank spaces are allowed within an integer constant.
6.The allowable range is -2147483648 to +2147483647.

Valid integer constants: 1345                3468                -9746
Invalid integer constants: 11.                 45,35               $12      025      x248

There are three types of integers that are allowed in C
a)Decimal constants (base 10)
b)Octal constants (base 8)
c)Hexadecimal constants (base 16)

Decimal integer constants:
·The allowable digits in a decimal integer constant are 0,1,2,3,4,5,6,7,8,9.
·The first digit should not be 0.
·Should at least one digit.
e.g. Valid -> 1334        -9746
                   Invalid -> 11.        256      $78      025      x248

Octal integer constants:
·The allowable digits in a octal constant are 0,1,2,3,4,5,6,7
·Must have at least one digit and start with digit 0.
e.g. Valid->      0245                -0476               +04013           
       Invalid->   25 (does not start with 0)                      0387( 8 is not an octal digit)                                          04.32(Decimal point is not allowed)

Hexadecimal integer constant:
·The allowable digits in a hexadecimal constant are 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
·Must have at least one digit and start with 0x or 0X.
e.g. Valid-> 0x14AF                0X34680         -0x2673E
       Invalid-> 0345 (Must start with 0x)            0x45H3 ( H is not a hexadecimal digit)

ii. Floating point / Real Constants: A floating point constant may be written in one or two forms called fractional form or the exponent form.

Rules for constructing floating point / real constants:
1.A floating-point constant must have at least one digit to the left and right of the decimal point.
2.It must have a decimal point.
3.It could be either positive or negative
4.If no sign precedes an floating point constant is assumed to be positive.
5.No commas or blank spaces are allowed.
6.When value is too large or small, can be expressed in exponential form comprising mantissa and exponent. E or e should separate mantissa and exponent. The mantissa may have upto 7 digits and the exponent may be between -38 and +38.
e.g. Valid-> +325.34 , 426.0, 32E-89, 35e+56
            Invalid-> 1 (decimal point missing)                    1. (No digit following decimal digit)
                         -1/2 (Symbol / is illegal)                       .5 (No digit to the left of the decimal)
                          56,8.94 (Comma not allowed)            -125.9e5.5 (exponent can not be fraction)
                        0.158e+954 (exponent too large)
iii. character constants: A character constant is a single alphabet, single digit, or a single special symbol enclosed within single quotes. Remember golden rule: single character single quote.
e.g. 'A'    '5'    '+'     '?'

iv. string constants: A string constant is a sequence of alphanumeric characters enclosed in double quotation marks whose maximum length is 255 characters. For e.g. const char txt [ ] =”This is C and C++ Programming language.”;
e.g.  "green", "My name &&&!!!    ???", "$2525"

Declaration of Constants
·Value of an identifier defined as constant, cannot be altered during program execution.
·Generally, the constant is associated with an identifier and that name is used instead of actual number.
General Syntax
            const typename identifier = const;
e.g.      const float pi=3.14159;
Another way to define constants is with the #define preprocessor which has the advantage that it does not use ant storage (but who count bytes these days?).
#define Identifier Value
#define PI 3.1415

Variables


·A variable is a named location in memory that is used to hold a value that may be modified by the program.
·A variable is an identifier that is used to represent some specified type of information within the designated portion of a program. The information (data items) must be assigned to the variable at some point in the program. The data item can then be accessed later in the program simply by referencing to the variable name.
·Thus, the information represented by the variable can change during the execution  of the program. So, quantity that may vary during program execution is called variable. Each variable has a specific storage location in memory where its numerical value is stored. These locations can contain integer, real, or character constants.
·User defined variables must be declared before using them in the program.
General syntax typename var1, var2;
e.g. int a, b;
       float c,d;
       char e;

Rules for constructing variable names
1.A variable name is any combination of 1 to 8 alphabets, digits, or underscore. Some compilers allow variable names whose length could be up to 40 characters, still, it would be better to use the rule of 8 characters.
2.Both uppercase and lowercase letters may be used and are recognized as distinct.
3.First character should always be letter.
4.Only underscore ( _ ) is allowed among special symbols and is treated as letter.
5.Space and other symbols are not valid.

2.4 Scope of Variables (Where Variables are declared?)
Variables will be declared in three basic places: inside functions, in the definition of function parameters, and outside of all functions. These are local variables, global variables, and formal parameters.

Local Variables
Variables that are declared inside a function are called local variables. Local variables may be referenced only by statements that are inside the block in which the variables are declared. Remember, a block of code begins with an opening curly brace and terminates with a closing curly brace. For example consider the following two functions:
            void func1( )
            {
               int x;
               x=10;
            }
           
            void func2( )
            {
               int x;
               x=-199;
            }
The integer variable x is declared twice, once in func1( ) and once in func2( ). The x in func1( ) has no relationship to the x in func2( ). This is because each x is only known to the code within the same block as the variable declaration.

Global Variables
Unlike local variables, global variables are known throughout the program and may be used by any piece of code. Also, they will hold their value throughout the program's execution. You create global variables by declaring them outside of any function.

            #include<stdio.h>
            #include<conio.h>
            int count; /* count is global */
            void func1( );
            void func2( );
            void main( )
            {
               count=100;
               func1( );
               getch( );
            }
            void func1( )
            {
               int temp;
               temp=count;
               func2( );
               printf("count is %d",count);
            }
void func2( )
            {
              int count=50;
              printf("Value of count:%d",count);
            }
Look closely at this program. Notice that although neither main() or func1() has declared the variable count, both may use it. func2(), however, has declared a local variable called count. When func2() refers to count, it refers to only its local variable, not the global one. If a global variable and a local variable have the same name, all references to that variable name inside the code block in which the local variable is declared will refer to that local variable and have no effect on the global variable. This can be convenient, but forgetting it can cause your program to act strangely, even though it looks correct.

Formal Parameters
If a function is to use arguments, it must declare variables that will accept the values of the arguments. These variables are called the formal parameters of the function. They behave like any other local variables inside the function.
            #include<stdio.h>
            #include<conio.h>
            int square(int x);
            main()
            {
               int t;
               t=square(t);
               printf("Value of t:%d",t);
               getch();
            }
            int square(int x)
            {
               x=x*x;
               return x;
            }
2.5 Simple Data Types in C
C has a concept of 'data types’ that are used to define a variable before its use. The definition of a variable will assign storage for the variable and define the type of data that will be held in the memory location.
Broadly, Primary data types in C can be categorized as
·int
·char
·float
·double

int: Integers are whole numbers, both positive and negative. The keyword used to define integer is int. Example of declaring integer variable is
            {
               int sum;
               sum=10;
            }
float: float is used to define floating point numbers. Example of declaring float variable is
            {
              float miles;
              miles=5.6;
}
double: double is used to define BIG floating point numbers. It reserves twice the storage for the numbers. Example of declaring double variable is
            {
               double  big;
               big = 312E7;
            }
char: char is used to define single character. Example of declaring character variable is
            {
               char letter;
               letter = 'x';
            }
*Note: Single character use single quote.
Sample program illustrating each data type
#include<stdio.h>
#include<conio.h>
main()
{
   int sum;
   float money;
   char letter;
   double pi;
   sum = 10;
   money = 2.21;
   letter = 'A';
   pi = 2.01E6;
   printf("Value of sum=%d\n",sum);
   printf("Value of money=%f\n",money);
   printf("Value of letter=%c\n",letter);
   printf("Value of sum=%e\n",pi);
   getch( );
}
Memory requirement of data type may vary from one compiler to another.

Modifying the Basic Data Types:

The basic types may have various modifiers preceding them. The modifiers define the amount of storage allocated to the variable. You use a modifier to alter the meaning of the base type to fit various situations more precisely. The lists of modifiers are:
·signed
·unsigned
·short
·long

 signed, unsigned, short, long can be used with int.
 signed, unsigned can be used with char.
 long can be used with double.

·Use of signed on integers is allowed, but redundant, because the default integer declaration assumes a signed number.
·Char is unsigned by default, so signed is used to modify character.

 The amount of storage allocated is not fixed. ANSI has the following rules
short int <= int <= long int
float <= double <= long double
What this means is that a "short int" should assign less than or same amount of storage as an "int" and the "int" should be less or the same bytes than a "long int".

Data Type
No of Bytes
Numeric range
Format
Int
4
-2147483648 to +2147483647
%d
Char
1
0 to 255
%c
Float
4
-3.4e38 to +3.4e38
%f
Double
8
-1.7e308 to +1.7e308
%lf or %e
Using modifiers



Short int / signed short int
2
-32768 to +32767
%d
Unsigned short int
2
0 to 65535
%d
Unsigned int
4
0 to 4294967295
%lu
Signed int / int
4
-2147483648 to +2147483647
%d
Long int / signed long int
4
-2147483648 to +2147483647
%ld
Unsigned long int
4
0 to 4294967295
%lu
Signed char
1
-128 to +127
%c
Unsigned char / char
1
0 to 255
%c
Long double
10
-1.7e4932 to +1.7e4932
%Lf

These figures only apply to today’s generation of PCs. Mainframes and midrange machines could use different figures.
You can find out how much storage is allocated to a data type by using the size of operator.
e.g
main( )
{
  int a;
  printf("Size of Integer is %d", sizeof a);
  getch( );
}
2.6 Expressions
An expression is any valid combination of different entities for example a constant, a variable, an array element or a reference to a function.
It may also consists of some combination of such entities, interconnected by one or more operators.
Types of Expressions:
Simple Expression
The simplest C expression consists of a single item: a simple variable, a literal constant, or symbolic constant.
                        Expression                              Description
                        PI                                            A symbolic constant defined in the program
                        20                                            A literal constant
                        sum                                          A variable
Complex Expression
Complex expressions consists of simple expressions connected by operators. For example a+b is a complex expression in which two simple expressions are connected with + operator.
Logical Expressions
The expressions which represents logic conditions that are either true or false are logical expressions. The conditions true and false are represented by integer value 1 and 0 respectively.
            e.g x<=y ,x!=y etc

2.7 Statements and Comments
Statements
A statement is a part of your program that can be executed. A statement causes the computer to carry out some action. In C, there are three different classes of statements. They are:
i.    Expression Statement
ii.    Block Statement
iii.   Control Statement

i. Expression Statement
An expression statement consists of an expression followed by a semicolon. The execution of an expression statement causes the expression to be evaluated.

            Example:
                           A = 3;
                           C = A + B;
                           ++i;
                           printf ( “ Area = %f ”, area);
                           ;

The first two expression statements are assignment-type statements. Each causes the value of the expression on the right of the equal sign to be assigned to the variable on the left.
The third expression statement is an incrementing-type statement, which causes the value of i to increase by 1.
The fourth expression statement causes the printf( ) function to be evaluated. This is a standard C library function that writes information out of the computer.
The last expression statement does nothing, since it consists of only a semicolon. It is simply a mechanism for providing an empty expression statement in places where this type of statement is required. Consequently, it is called a null statement.

ii. Block Statement
Block statements are simply groups of related statements that are treated as a unit. The statements that make up a block are logically bound together. Block statements are also called compound statement. A block begins with a {and terminated by its matching}. The individual statements may themselves be expression statements, compound statements, or control statements. Thus, the compound statement provides a capability for embedding statements within other statements. Unlike an expression statement, a compound statement does not end with a semicolon.
Example:                   
 {
                                    pi = 3.141593;
                                    Circumference = 2 * pi * radius;
                                    Area = pi * radius * radius;
                                    }
iii. Control Statements
In most of the C programs we have encountered so far, the instructions that were executed in the same order in which they appeared within the program. Each instruction was executed one and only once. Program of this type are unrealistically simple, since they do not include any logical control structures. But in realistic C programs, the program needs logical condition to determine if certain conditions are true or false, they do require the repeated execution of groups of statements and, they involve the execution of individual groups of statements on a selective basis. And all these operations can be carried out using the various control statements included in C.
The different types of control statements are:
a.   Branching or Selection Statement
b.   Iteration or Looping Statement
c.   Jump Statement

Comments
Comments are the text in the program, which won't be compiled or executed. It is good to put comments because it will be helpful to debug the program in the future, which is made before. There are two types of comments:
Single line comment: It means we can give comment for a single line. It uses double forward slash (//).
e.g. //This is a test program.
Multi line comment: Multi line comment means programmer can give comment for multiple line. This type of comment is enclosed with in a  /* comment */
e.g. /* this is
            a simple
program */
Comments can appear anywhere in the program provided that either they are enclosed within a /*  */ or followed by //

End of Chapter Two


[ Note: If you have any problems regarding this then you can post the problem in comment box. we will try to solve this here. Or if you know any solution, you can also help :) ]