C Programming - Introduction

C Programming - Introduction

Introduction of C:
Ø ‘C’ seems a strange name for a programming language. But this strange sounding language is one of the most popular computer languages today because it is a structured, high-level, machine independent language. It allows software developers to develop programs without worrying about the hardware platforms where they will be implemented.
Ø ‘C’ language was developed by Dennis Ritchie at Bell laboratories in 1972 AD.

Character set used in C:
A character denotes any alphabets, digit or special symbol used to represent information.
Letters
Digits
Special Characters
Upper case A….Z
Lower case a……z
All decimal digits
0…………..9
, . ; ? ( ) {} $ % *

Ø Use of comments:  Comment lines are not executing statement.
// Program written by ALK
                                                                       
Ø Identifiers:
Identifiers refer to the name of variables, functions. Both upper case and lower case letters are permitted, through lower case letters are commonly used.

Ø Keywords:
Keywords are the words whose meaning has already been explained to the C compiler. All keywords must be written in lower case. There are 32 keywords in C. Keywords cannot be used as variable name.

Auto
double
int
struct
Break
else
long
switch
Case
enum
register
typedef
Char
extern
return
union
Const
float
short
unsigned
Signed
continue
for
avoid
Default
goto
sizeof
volatile
Do
if
static
while
Ø Token:
The smallest individuals unit in a program is called C token. C token has six token.

Keywords
Constant
String
Operators
Special Symbol
Identifier
int, float
3.14, 10
“ALEX”
“Ramjanaki”
+ - * /
{ } ( ) [ ]
a, b, l, h

Ø Data type in C language:
The basic element of any program, which describes the nature of variables is called data type .Data types are mainly used to define the type and nature of data such that compiler detects and proceeds.

Data types used in C are as follows:

1.     Int: - int is the keyword for integer. It contains the whole numbers between -32,768 to 32,767. It requires 2 bytes memory and its type specifier is %d.
Examples: - int a=10;
2.     Float: - float is the keyword for floating numbers i.e. decimal numbers. It contains number 3.4e – 38 to 3.4e+38. It requires 4 bytes memory and its type specifier is %f. Example: - float pi=3.14;
3.     Char: - char is the keyword for character. It represents the single alphabet. It requires 1 byte memory and its type specifier is %c. Example: - char choice;
4.     String: - The collection of character is called string. It is enclosed within double quote. Its type specifier is %s.
Example: Char str= “Computer”;


Ø Variable: -
A variable is a data name that may be used to store a data value. A variable may take different values at different times during program execution. Variable names may consist of letters, digits and the underscore ( _ ) characters. A variable name can be chosen by the programmer in a meaningful way so as to reflect its functions or nature in the program.

Rules for variable naming:-
1. They must begin with a letter.
2. ANSI (American National Standard Institute) standard recognizes a length of 31 characters. However, length should not be normally more than 8 characters, since only the first 8 characters are treated as significant by many compilers.
3. Upper case and lower case are significant that is, the variable area is not the same
    as Area or AREA.
4. It should not be a keyword.
5. White space is not allowed.
Example: - Roll No is invalid but Roll_No is valid variable name.

Types of variable: -

There are 2 types of variables.
·        Numeric Variable: - The variables which stores the numeric data only is called numeric variable. It can be whole number or decimal number. Example: 10, 11, 3.14 etc.
·        String Variables: - The variable which stores the character data only is called String variable. It can be a single character or string. Examples: ‘a’, “apple” etc.

Ø Constant: -
 A constant is a value that does not change during the program execution. There are several types of constants. They are:
a)     Integer constant: - An integer constant refers a sequence of digits.
Examples: 123, 23, 0 etc.
b)    Real (Floating constant): - Floating point constant contains a decimal point. Examples: 3.14, 6.12, etc
c)     Character: - A character constants are enclosed within single quote. Examples: ‘a’, ‘b’ etc.
d)    String constant: - A string constant is a sequence of characters enclosed in double quote.   Examples: “Hi this is PLK sir”, “2075” etc.

e)     Symbolic constant: - A symbolic constant is simply an identifier used in place of a constant. In C symbolic constants are usually defined at the start of program. Examples:
# define     pi    3.14

Statement:
The command given to the computer which causes the computer to carry out some action is called statement. It is of 2 types.

a)Simple Statement:
 Statement consists of an expression followed by a semicolon is called simple statement. Eg: a=2;

b) Compound Statement:
A compound statement consists of several individual statements enclosed in a pair of braces { }. It provides capability for embedding statements within other statements.
Example:
{
l=2;
b=3;
area=l*b;
}


OPERATOR

A symbol that instructs C to perform some operation on one or more operands is called operator.
The data on which operator are performed is called Operand.
For example: p=3+4; P, 3 and 4 are operands where as = and + are operator and p=7 is the result.



The operators provided by C language are as follows:

1. Arithmetic Operator:
Operators which are used in mathematical expression are called arithmetical operators. The various arithmetic operators are:

Operators
Name
Example
+
Addition
a=2+3=5
-
Subtraction
a=5-1=4
*
Multiplication
a=3*4=12
/
Division
a=12/2=6
%
Modulus(Remainder)
a=12%2=0

2. Logical Operator
These operators are generally used in conditional expression. The three logical operators in C are as follows:
Operators
Name
Example
&&
Logical AND
(a>b)&&<a>c)
||
Logical OR
(a>b)||(a>c)
!
Logical NOT
!(a==b)

3. Assignment Operator
The assignment operator calculates the expression on the right side and gives the values of left side variables.
Example: -   a=2+3=5
C has a set of “shorthand” assignment operator of the form
V op = exp;
Where, V is variable, op is arithmetic operator and exp is an expression. 
Example:  a+ = 1   i.e. a = a+1
Here, at first the arithmetic operation is performed than only assignment operation is performed.
4. Relational Operator
It is used to compare the values between operands and gives a result whether it is true or false. It is also called comparison operator due to its comparing characteristics. There are 6 relational operators are:


Operators
Name
Example
< 
Less than
a<b
< =
Less than equal to
a<=b
> 
Greater than
a>b
>=
Greater than equal to
a>=b
= =
Equal to
a==b
! =
Not equal to
a!=b

5. Ternary Operator (? :)
C has only ternary operator of this operators result in terse (shorten) and compact mode.
Syntax:
<condition> ? <expression-1> : <expression-2>
If the condition is true, expression-1 will be executed; otherwise, expression-2 will be executed. The ? : is similar to if -else statement.
Example:
int x,a,b;
a=6;
b=10;
x= (a<b)? a:b;
printf("%d",x);
Here, the value of x is 10.

6. Comma Operator
The comma operator can be used to link the related expressions together. A comma-linked list of expressions is evaluated left to right and the value of right-most expression is the value of the combined expression. For examples: The statement
                             F= (a=2, b=3, a+b)
First assigns the value 2 to a, then assign 3 to b and finally assigns 5 to F.