C-Programming Language
Introduction
It
is a structured programming language, uses simple English word with some
mathematical expression, has a high speed and easy to understand. It is also
termed as middle level language or intermediate language between high level and
low level language. It is very powerful programming language because it is used
to prepare system software as well as application software. It has large
numbers of vocabularies and syntax to write program. It was developed by Dennis
Ritchie in 1970, USA.
Why to use C?
a. C is a powerful and flexible language so
system software and application software can be developed.
b. C is a popular language preferred by
professional as it has wide variety of c-compilers and accessories.
c. C is a portable language so can be compiled
and run on another system too.
d. Those words which has special meaning to a
language compiler (called keyword or reversed word), can be used to do any task
while making program.
e. C is a modular so subroutines can be reused
in other applications or programs.
Features of C-Language
It
is a popular language because it has large number of features for programmer to
write medium types of program. Some features of it are given below:
a. It is highly portable language.
b. It is procedural programming language because
program is divided into number of functions.
c. It inherits or invisible functionality(memory
management, CPU register manipulation, etc.)
d. It is general purpose structured programming
language.
e. It is internationally standardized
programming language.
Advantages
a. It is very easy for debugging, testing and
maintaining.
b. It is portable so can be run in any computer.
c. It is fast for executing.
d. The statement in C-language are generally
short but very powerful.
e. It has only 32 keywords so that easy to
remember.
Disadvantages
C-language
has not strong disadvantages but has some negligible disadvantages which are
given below:
a. There is no runtime checking.
b. As the program extends it is very difficult
to fix the bug.
c. It may be compile time overhead due to the
misplacing and excessive use of pointers.
d. It does not use to develop complex type of
software nowadays.
e. Object oriented programming concept is not
included in C.
Fundamental of C
C - Character sets are letters, digits, and special
characters and white spaces that form words, numbers and expressions allowed to
use in C programming language.
The following character sets are used in
C.
Letters Digits
Upper case A to Z 0 to 9
Lower case a to z
Special characters
,
|
Coma
|
.
|
Period
|
;
|
Semicolon
|
:
|
Colon
|
?
|
Question mark
|
'
|
Apostrophe
|
"
|
Quotation marks
|
!
|
Exclamation mark
|
|
|
Vertical bar
|
/
|
Slash
|
\
|
Back slash
|
~
|
Tilde
|
_
|
Underscore
|
$
|
Dollar sign
|
%
|
Percentage sign
|
&
|
Ampersand
|
^
|
Caret
|
*
|
Asterisk
|
-
|
Minus sign
|
+
|
Plus sign
|
<
|
Opening angle
|
>
|
Closing angle
|
(
|
Left parenthesis
|
)
|
Right parenthesis
|
[
|
Left bracket
|
]
|
Right bracket
|
{
|
Left brace
|
}
|
Right brace
|
#
|
Hash
|
White space
white space is any section of a document that
is unused or space around an object
- Blank line - Horizontal Tab - Carriage Return - New line
- Form feed
Comments:
Comments
in code can be useful for various purpose such as for providing visual split
between various parts of the code, gives information about the code that what
is going to do, etc.
Comments can be written in two Forms:
//single line comments
/*multi-line comments*/
Tokens:
Tokens are fundamental parts of C
program from which other parts are formed. There are six types of C tokens that
are listed below:
a) Operators b) Keywords c) Identifiers
d) Constants e) strings f) Special symbols
Keywords or reserved words
C keeps a small set of keywords for its
own use. These keywords cannot be used as identifiers in the program. It has special
meaning in the program to a language compiler that are pre defined in C
programming language.
Auto
|
Double
|
Int
|
Struct
|
Break
|
Else
|
Long
|
Switch
|
Case
|
Enum
|
Register
|
Typedef
|
Char
|
Extern
|
Return
|
Union
|
Const
|
Float
|
Short
|
Unsigned
|
Continue
|
For
|
Signed
|
Void
|
Default
|
Goto
|
Sizeof
|
Volatile
|
Do
|
If
|
Static
|
While
|
Identifiers
Identifiers
are the names given to program unit such as variable, structure, etc. They are
not defined in programming language but are used to define by the programmer.
Some basic rules to define identifiers are given below:
a. First character must be an alphabet
underscore then digits or alphabets.
b. It must consist of only letters, digits and
underscore.
c. Any standard C language keyword cannot be
used as identifier name.
d. It should not be contain a space.
e. It allows both upper case and lower case
characters.
Valid Identifier: B1, first_name, lastname, etc.
Invalid Identifier: 1A,
int, void, sum-is, first name,
etc.
Data Type in C
Data
type can be represented in variety of ways in any programming language, so that
we can store data in system. It determines what types of data should be stored.
There are two types of data type in C which are:
a. Primary data type and
b.
Secondary data type.
Primary data type
It
is the fundamental data types used to build other data types. The fundamental
data types are given below:
Variable type
|
Keyword
|
Bytes
|
Range
|
Character
|
Char
|
1
|
-128 to 127
|
Unsigned character
|
Unsigned char
|
1
|
0 to 255
|
Integer
|
Int
|
2
|
-32768 to 32767
|
Short integer
|
Short int
|
2
|
-32768 to 32767
|
Long integer
|
Long int
|
4
|
-2147483648 to ….647
|
Unsigned integer
|
Unsigned int
|
2
|
0 to 65535
|
Unsigned short integer
|
Unsigned short int
|
2
|
0 to 65535
|
Unsigned long integer
|
Unsigned long int
|
4
|
0 to 4294967295
|
Float
|
Float
|
4
|
1.2E-38 to
|
Double
|
Double
|
8
|
2.2e-308 to
|
Long double
|
Long double
|
10
|
3.4E-4932 to …
|
Secondary Data type
a.
Arrays b. Pointers
c. Structure (we will study letter)
Variable
A
variable is a location of a computer's memory used to store data value using
unique name and can be changed during program execution time. Any variable
declared in a program should confirm to the following:
a. They must always begin with a letter,
although some systems permit underscore as the first character.
b. The length of a variable must not be more
than 8 characters.
c. White space is not allowed.
d. A variable should not be a keyword.
e. It should not contain any special
characters.
Valid Variables:
Sum1,
numb, Salary, emp_name,
average1, etc.
Invalid variables:
123,
(area), 6th , %ans,
first name
Types of variable:
1. Numeric Variable: The variable which stores numeric data only called numeric variable.
It may be whole number or fractional number.
Ex. Int Salary float average
2. String variable: The variable which stores character data only is called string
variable. It may be character or string.
Ex.
Char name, char w
Constant
A
constant is the fixed values that do not change during the execution of a
program. C supports four types of constants. They are character, string,
integer and floating point constants.
Types of constant are :
a. Character constant:
Character constant is a single character which is enclosed with singe
quotation mark. Eg. 'a' , 'A', '4' , etc.
b. String constant: String constant of any number of
consecutive characters enclosed in double quotation marks whose maximum length
is 255 characters. Eg. "it's very easy language",
"2*3/5(55-2)", "3400", etc.
c. Integer constant: An integer constant refers to a
sequence of digits that will be either positive or negative. There are three
types of integers namely, decimal, octal and hexadecimal.
i. Decimal
integer constant: A decimal constant may have the combination of the digits
from 0 to 9 and –ve or +ve sign. Eg. 123,
-56, 0, etc.
ii. Octal
integer constant: An octal integer constant can consist of the combination
of the digits 0 to 7 with a leading 0. eg.
0, 01, 0675, etc.
iii. Hexadecimal
integer constant: A hexadecimal integer should begin with 0x and it
contains the digits from 0 to 9 and an alphabet A to F. eg. 0x2, 0xgf, etc.
d. Floating point constant: A floating point constant is a base 10
number that contains either a decimal point or an exponent or both. 0, 33,
1.345E+4, 4.22e78, etc.
`
Escape
sequence
The non printing characters started with
slash ( \ ) which uses special characters with output functions. These are
listed below:
Escape sequence Meaning
\a Audible alert
\b Backspace
\n New line
\t Horizontal tab
\v Vertical tab
\'
Single quote
\"
Double quote
\? Question mark
Statement
Statement causes the computer to carry
out some action as per given instructions. It is terminated by semicolon ( ; ).
The types of statement are given below:
a. Expression statement
It
consists of an expression followed by a semicolon only.
Example:
B
= 5;
C
= d+e;
Printf(Enter
a number");
b. Compound statement
A
compound statement consists of several individual statements enclosed with a
pair of braces { }. It provides
capability for embedding statements with in other statements.
Example:
{
A
= l*b
Area
= 3.14*r*r
}
c. Control statement
A
control statements are used to create special program features such as logical
tests, loops, branches and it also contains other statements.
Example:
For(i=1;i<=100;i++)
Operator
An operator is a symbol which is used to
do mathematical or logical operation on data and variables. C has a rich set of
operators which can be classified as follows:
a. Arithmetic
Operators
Those
symbols which are used in C – Language to perform simple arithmetic operation
such as addition, subtraction, multiplication, division, etc. is called
arithmetic operators. It uses two operands so that is called binary operator
also.
Operator
Meaning Example
+ addition a+b 2+3=5
- subtraction a-b
5-1=4
* multiplication a*b 2*3=6
/ division a/b 6/2=3
% modulus a%b 3%2
= 1
b. Relational
operator
It
is required to compare the relationship between operands and bring out a
decision and program accordingly. It is also called comparison operator because
it is used to compare any two expressions. C supports the following relational
operator:
Operator Meaning Examples
< less than a
<= is less than or equal to a<=b
> greater than a>b
>= is greater than or equal to a>=b
=
= is equal to a= =b
!= is not equal to a ! = b
c. Logical
operator
Logical
operators are used to give logical value either true of false. C has following
logical operators:
Operator
Meaning Examples
&& Logical And (a>b)&&(a>c)
||
Logical Or (a>b)||(a>c)
! Logical NOT !(a==b)
Logical AND (&&)
This
operator is used to operate 2 condition or expression with relational operators
if both expression are true then displays true otherwise false.
Example:
a>b && x ==10
Logical OR (||)
the
logical OR is used to combine 2 expression or the condition evaluates to true
if any one of the 2 expression is true.
Example:
a
Logical NOT ( ! )
The
logical not operator takes single expression and evaluates to true if the
expression is false and evaluates to false if the expression is true.
Example:
! (x>=y)
Assignment Operator
It
is used to evaluate t an expression on the right of the expression and substitutes it to the value or variable
on the left of the expression. It is used to assign value of an expression into
variable.
variable
= expression
Example:
x = a+b
Shorthand operator
It
is the combination of arithmetic and assignment so it first performs arithmetic
and then only assignment operation.
operator shortened operator assignment
operator
+= a+=1 a=a+1
*= a*1 a = a*1
/= a/=b a = a/b
%= a%=b a=a%b
Unary Operator
The increment and decrement operators
are one of the unary operations which are very useful in C language. The are
extensively used in For and While loops.
Syntax:
++ variable name (prefix)
variable name ++ (postfix)
- - variable name (prefix)
variable name - - (postfix)
The increment operator + + adds the
value 1 to the current value of operand and decrement operator - - subtract 1
to the current value of operand.
Example:
suppose
m = 5;
y = ++m;
in this case y = m + 1 = 5+1 = 6
m = m + 1 = 5 + 1 = 6
again if we rewrite the above as
m = 5;
y = m++;
here, first ' m ' is assign to y so y = 5 and m = m + 1 =5 +1=6
Relational Operator
The operator which compare two
quantities and depending on their relation and take certain decisions.
These comparisons can be done with the
help of relational operators. Example: <, <=, >, >=, ==, != (not
equals to).
Special Operator
a. Comma operator: It is used to separate
expressions when multiple expressions are used in one statement. It always is
evaluated left to right and value of the left expression is descarded.
Example: j = (p = 3, p+2); ( where p+2 = 3+2=5 and p is discarded.
b. Size of Operator: It gives the byte size of data type or
variable.
example:
int
sum;
m
= sizeof(sum);
n
= sizeof(int);
c. Pointer operator: *
operator is also used for multiplication but when used with a pointer variable,
It is used to get the content of the address pointed by a pointer variable to a
particular memory location.
Format
Specifiers in C
It tells what type of the data is being processed. The syntax of format
specifier is as follows:
%c - specify a character %d - specify a decimal integer
%s - specify a string %f - specify a float
These
format specifier is used with printf ( ) and scanf( ).
scanf("%d",
&a ); asks the user to input an integer and stores it in a.
printf("the
sum of %d and %d is %d",a ,b, sum);
Basic structure of C programs:
Documentation section
Header file inclusion section
Global declaration section of user
defined function
main ( ) function section
{
.......................................
.......................................
}
Sub program section
function 1
function 2
...................................
...................................
function n
Syntax for the header file:
#include
(where #
include is a pre processor statement.
Header files
|
Uses
|
stdio.h
|
printf(), scanf(), getchar(),
putchar(), gets(), puts(), getc(), putc(), fopen(), fclose(), feof()
|
conio.h
|
clrscr(), getch()
|
ctype.h
|
toupper(), tolower(), isalpha()
|
math.h
|
pow(), sqrt(), cost(), tan(), sin(),
log()
|
stdlib.h
|
rand(), srand(), exit()
|
string.h
|
strlen(), strcpy(), strcmp(),
strcat(), strlwr(), strupr(), strrev()
|
Functions:
Formatted I/O
a. Printf():
it is widely used output function. The text which is needed to be shown as
output in the screen is shown by the help of printf() function.
syntax:
printf(control
string, arg1, arg2, ..............);
ex.
printf("hellow, friend");
printf("\nThe
total amount =Rs. %f",ta);
printf("enter
any number");
b Scanf():
It is the most frequently used input function.
syntax:
scanf(control
string, arg1, arg2, ................);
Example:
printf("enter
any number");
scanf("%d",
&a);
printf("enter
any character and string");
scanf("%c
%s", &w, wo);
Unformatted I/O
a. getchar():
it accept a character only as an input at a time.
b. putchar():
it displays just a character on the screen.
c. gets(): it
accepts a string from the user which may have space in between.
puts(): it displays string on the output device.
3 comments:
if you wanna to be a good programmer then do coding in notepad++ mac
Supportability can be characterized as far as eco-proficiency. "In least difficult terms, it implies making more products and enterprises with ever less utilization of assets,excel vba training london
Thank You Sir for this
Post a Comment