C
History of C
C language was developed in the BELL labs by Dennis Ritchie. The important features of C are drawn from the language called the Basic Combined programming language (BCPL). BCPL is a predecessor to C. the language was developed primarily for developing multi user operating system called UNIX. 80% of the UNIX is developed using c.
In 1983, the American national standard institute (ANSI) formed a committee to provide the modern definition of C. This definition was called the “ANSI C”.
C is a highly flexible language. It can be used to develop any application ranging from scientific analysis to business data processing. Hence C is called a high level language. C support huge variety of data structures viz. basic data types like integer, floating point (single and double precision), character enumerative, composite data types like structures, unions and pointers.
C is also known as middle level language, as C supports lot of low level functions for manipulating memory and CPU registers.
This part concerned with the basic elements used to construct simple C statements. These elements include the C character set, identifiers and keywords. These basic elements are combined to form comprehensive program structure.
The C character set
C uses upper case letter A-Z, lower case letter a-z, the digits 0-9 and certain special characters as building blocks to form basic program elements. E.g. constants, operators, variables, expressions etc. The special characters are listed below:
+ - * / = % & # ! ? ^ “ ‘ ~ \ | ( ) [ ] { } : ; . , _ (blank space)
C uses certain combination of these characters known as escape sequences or whitespace characters. These special conditions are backspace \b, newline \n, horizontal \t, etc.
Comments
Comments are the text included in a program for explanation purpose. Comments are not executed. They are ignored by compiler. C support two types of comments.
1.Single line comments: single comments starts with // and proceeds up to end of the line.
2. Multiline comments: Any block of text enclosed between /* and */ is taken as comments.
IDENTIFIERS
Identifiers are nothing but names given to various program elements such as variables, constant, functions and arrays. Identifiers are composed of letters both upper and lower case and digits in any order except that the first character should be a letter. Underscore [“_”] is a special character that can be used in an identifier’s name. Underscore is treated as a letter. Thus an identifier can begin with an underscore.
C is a case sensitive. That is ‘max’ and ‘MAX’ is two different identifiers. Keywords cannot be used as identifiers.
EXAMPLE
The following are valid identifiers
N X23 sum_years _temp
Name interest_rate student table
The following are not valid identifiers
79: cannot begin with number
“4th”: cannot begins with special characters
System-no: cannot contain hyphen
Auto: it’s a keyword
KEYWORDS
C has a set of reserved words called keywords, which have standard predefined meanings. The usage of keywords solely for their intended purpose. It is illegal to use a keyword as a programmer defined identifier.
The standard keywords are
auto extern size of break float static case for struct char goto switch const if typeef continue int union default long unsigned do register void double return volatile else short while enum signed default
DATA TYPES
Many types of data are supported in C. each of the data types may be represented differently in the computer memories. C supports basic data types and user defined data types. The memory requirement for data types may vary from compiler to compiler.
Data types |
Description |
Memory size require |
Int |
Integer quantity |
2 bytes |
Char |
Single character |
1 bytes |
Float |
Single precision floating point |
4 bytes |
Double |
Double precision floating point |
8 bytes |
The basic data types can be augmented by using data type’s qualifiers viz. short, long, signed and unsigned. For example, an integer can be defined as short int, long int and unsigned int.
A short int require less memory than the ordinary int or it may require the same amount of memory as an ordinary int but it will exceed in ordinary int, in word length.
An unsigned int have the same memory requirement as that of ordinary int. however, in case of ordinary int the left most bit is reserved for the sign. The quantifier unsigned makes the integer to use all the bits to store value, there by altering the range of values that can be stored.
CONSTANT AND LITERALS
An elements whose value is not altered during the execution of the program is called a constant. Even constant have the data types like:
*Integer constant
* Floating point constant
* Character constant
* String constant
* Enumeration constant
Example:
Integer constant- 7 8676 766242 integer constant
Floating point constant- 12.4 -7.154 2E+5
Character constant- ‘s’ ‘t’ ‘\b’
String constant- “hello” “world” “computer\n”
Unsigned decimal-5000U
Long decimal- 123456789L
Hexadecimal number- 0x62F
Octal number- 0672
Illegal constant:
Illegal character(,)- 10,455
Illegal character(blank space)- 45 656 8
DECLARATIONS
Associating variables or group of variables to a specific type is known as declaration. All variables must be declared before (i.e., in the beginning of the program itself) they are used in the executable statements.
A declaration consists of data types followed by one or more variable names separated by comma and terminated by a semicolon.
Data-types variable_list;
Example:
Int roll_no;
Float average, rating;
Char status;
Short int a,b;
Unsigned int r,s;
Long int r,t;
Double root;
EXPRESSIONS
An expressions may consists of an single entity, such as a constant or a variable name. it may also consist of some combination of such entities interconnected by one or more operators. Most of the expressions in C involves operators.
Expressions can also represent logical condition that is either true or false. C uses 0 to represent false and 1 to represent true. Hence logical expressions are basically numerical expressions.
a+b; -an arithmetic operation
x=a; -an assignment expression
c=x+y; -an arithmetic and assignment operation
c==y; -condition expression statement checking for equality
i++; -increment expression sane as i=i+1;
STATEMENT
This is a program construct that carries out some actions. In C, there are three different classes. They are
*Expression statement
*compound statement
*Control statement
Example
1.The following are expression statement
C=7;
C=a+b;
2.compound statement consists of several individual statements enclosed with in a pair of braces{ }.
{
Pi=3.14;
Area=pi*radius*radius;
}
3.control statement
Control statement imolement special program features such as logical test, loops and branches, many control statements are composed of other statements.
While
{
Print(“the value of number=%d”,num);
Num++;
}
SYMBOLIC CONSTANTS
A symbolic constant is the name that substitutes for a sequence of characters. These characters may represent a numeric constant, a string constant or a character constant. Thus symbolic constants enable us to use names in place of numeric constant, a character constant or a string. The processor does the job of substitution. This is known as macro substitution.
Example:
#define MAX 1000
The above segment is a preprocessor directive. Here the name MAX represents the value 1000. The pre processor replaces all the MAX names in the program with the value of 1000.
OPERATORS AND EXPRESSIONS
As we have already seen, C has a rich variety of operators. This huge number of operators fall in to several categories.
Arithmetic operators- There are five arithmetic operators in C.
Operator |
Purpose |
+ |
Addition |
_ |
Subtraction |
* |
Multiplication |
/ |
Division |
% |
Reminder after integer division |
Division of one integer by another is called integer division. The operation always results in truncated quotients. On the other hand, if division operation is carried with two floating point numbers or with one floating pointing number and one integer, the result will be floating point quotient.
Example:
If X and Y are two variables with values 10 and 3 respectively then,
X+Y=13
X-Y=7
X*Y=30
X/Y=3(INTEGER DIVISION)
X%Y=1
IF X and Y are given values 12.5 and 2.0 respectively then,
X+Y=14.5
X-Y=10.5
X*Y=25.0
X/Y=6.25(FLOATING POINT DIVISION)
NOTE: There is no exponentiation operators in C. however, exponentiation can be carried out by using the library function pow().
Type conversion and casting
When an expression involves operands with different data types, the result is automatically convertedto the data type of the highest precision operands. This is known as implicit type conversion.
Example
Suppose that j is an integer whose value is 7 and f is a double precision floating point number whose value is 5.5 and c is a character type variable which represents the characters ‘w’ (ASCII value of w is 119)
J+f=12.5; (double precision)
J+c=126; integer
The value expression can be converted to a different data type if desired. This is known as explicit type conversion or casting. The syntax for casting is
(data type) expression;
Example:
Suppose that i=10 and f=3 both integers. Then i/f=3; (integer division)
If
(float) i/f =3.33; (floating point division)
In the example, we have explicitly type converted variable I to floating number.
UNARY OPERATORS
The class of operators that act upon as single operand to produce a result is known as a unary operator. Unary operator usually precede the single operands.
Example:
1.The Unary minus(-)
-555 -0x5fff -0.5
2.The increment (++) and decrement (--) operator
The increment and decrement operators cause their operands to be incremented or decremented by one respectively. There is a difference when these operators are placed before or after their operands.
Example:
Suppose I is an integer whose value is 10 then,
J = ++I;
The value of j is 11. Above expression first increments the value of I and then assigns to j. this is called pre increment operators.
J=i++;
The expression will cause the value of I to be assigned to j first and then incrementation takes place. hence value of j is equal to 10, but value of I is 11. This is called post increment operator. The same rule applies to decrement operator (--) as well.
3. The size of (operand) operator
This operators returns the size of the operands in bytes.
Example
Sizeof(int) =2;
RELATIONAL AND LOGICAL OPERATORS
There are six relation operators in C. they are
Operator |
Purpose |
< |
Less than |
> |
Greater then |
>= |
Greater than or equal to |
<= |
Less than or equal to |
== |
Equal to |
!= |
Not equal to |
These six operators are used to form a logical expression which represents conditions that are either true or false. In C, this is interpreted as zero for false and non-zero for true for true. The non-zero value can be either positive or negative.
Example:
Suppose x, y and z are integer variables with values 1, 2 and 3 respectively then
(x (x+y)>=z value=non zero (z!=3) false value=zero (y==2) true value=non zero In addition to relational operators, there are two logical operators. They are Operator Purpose && AND || Or Logical operators act upon operand that is they logical expression. The net effect is to combine the individual logical expression in to more complex condition. AND operator returns true if either one of the operand is true or both are true. If both operand are false, then the output of OR is also false. Example: Suppose I is an integer with value 7. F is floating point whose value is 5.5 then, (I > 6) && (f== 10.0) false (i==7)|| (f<3.0) true OPERATOR PRECEDENCE AND ASSOCIATIVITY Precedence and Associativity control how an expression is evaluated if more than one operator is present in the expression. Example: Suppose x=10, y = 5 and’ Z = x + y * 2; In the above expression, the value of z should be 20. That is y *2 should be evaluated first and then the result should be added with value of x. here we mean to say that * has more precedence that +. Associativity tells us in what order the expressions are evaluated if more than one some operators with same precedence occur in a statement. Example: Suppose Z = x + y -5; In this expression value of z should be 10. Here + and – have same precedence, but x+y is evaluated first, then 5 is subtracted from the result. That is, the above expression is same as Z = ((x+y)-5). The associativity is known as left to right associativity. CONDITIONAL OPERATOR Simple conditional operation can be carried out with conditional operator(?:). The general form of conditional operations is as follow. (expressions1? expression 2: expression3); When evaluating a conditional expression , expression1 is evaluated first. If expressional1 is true, then expression2 is evaluated and this becomes the value of conditional expression. However, if expression1 is false, expression3 is evaluated and this becomes the value of conditiona; expression. Example: Suppose I = 10, then Z = (i<5) ? 0 : 100; Here the value of Z should be 100. Since I < 5 is false, the value 100 is assign to Z. ASSIGNMENT OPERATOR C has different assignment operators. All of them are used to form assignment expression which assign the value of an expression to an identifier. The most commonly used assignment operator is “=”, in general form, Identifier = expression; Example: A = 10; Y = m * x + c; During assignment implicit type conversion may result and this may lead to alteration of data being assigned between different data types. C contains the following five additional assignment operators: +=, -=, *=, /= and %=. Example: A = a + b; can be written as A += b; This holds good for other operators also. LIBRARY FUNCTION Lot of library functions is bundled along with c language. The library function are categorized in to various categories such as standard input and out, mathematical function, string function, type, etc. these library functions are not part of the language though all implementation of the language include them. Anybody who wishes to use these functions should include the appropriate header file where the functions are defined. This inclusion is done by the processor directive. #include Example: #include includes standard input and output #include include console input and output #include includes string manipulation function.