Why C
language is so important:-
Worth to
know about C language:-
1- Oracle is written in C
2- Core liberties of android are written
in c language.
3- MySQL is written in C.
4- Almost every device driver is written
in C.
5- Major Part of Web Browser is written
in C.
6- C is World’s most popular programming
language.
For Students:-
1- C is important to build programing
skills.
2- C covers basic features of all
programming language.
3- Campus recruitment process.
4- C is most popular language for
hardware dependent programming.
History
of C language:-
1- Martin Rechards is developed BCPL
(Basic Combined programming language) in 1966.
2- Ken Thompson is developed B language
in 1969. He is also developed Unix Operating System.
3- Dennis Ritchie is developed C
language at Bell LABs of USA in 1972. He is co-developer of UNIX Operating
System.
Introduction
of C Language?
·
The C
language is a structure oriented programming language, developed at Bell
Laboratories in 1972 by Dennis Ritchie
·
C
programming language features were derived from an earlier language called “B”
(Basic Combined Programming Language – BCPL).
·
C
language was invented for implementing UNIX operating system
·
In
1978, Dennis Ritchie and Brian Kernighan published the first edition “The C
Programming Language” and commonly known as K&R C
·
In
1983, the American National Standards Institute (ANSI) established a committee
to provide a modern, comprehensive definition of C. The resulting definition,
the ANSI standard, or “ANSI C”, was completed late 1988.
Character Set: - A Set of alphabets, digits, and special
symbols are called character set.
Alphabets- C
language supports all the alphabets from the English language. Lower and upper
case letters together support 52 alphabets.
Lower case letters - a to z
UPPER CASE LETTERS - A to Z.
Digit: C language supports 10 digits which are
used to construct numerical values in C language.
Digits - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Special Symbols: - C language
supports a rich set of special symbols that include symbols to perform
mathematical operations, to check conditions, white spaces, backspaces, and
other special symbols.
Special Symbols - ~ @ #
$ % ^ & * ( ) _ - + = { } [ ] ; : ' " / ? . > , < \ | tab
newline space NULL bell backspace vertical tab etc.,
Identifiers
Identifiers
are names for entities in a C program, such as variables, arrays, functions,
structures. An identifier can be composed only of uppercase, lowercase letters,
underscore and digits, but should start only with an alphabet or an underscore.
Rules
for constructing identifiers
1. The first character
in an identifier must be an alphabet or an underscore and can be followed only
by any number alphabets, or digits or underscores.
2. They must not begin
with a digit.
3. Uppercase and
lowercase letters are distinct. That is, identifiers are case sensitive.
4. Commas or blank
spaces are not allowed within an identifier.
5. Keywords cannot be
used as an identifier.
6. Identifiers should
not be of length more than 31 characters.
7. Identifiers must be
meaningful, short, quickly and easily typed and easily read.
Valid identifiers: total
sum
average
_x
y_
mark_1 x1
Invalid identifiers
1x
- begins with a digit
char
- reserved word
x+y
- special character
C Keywords
Keywords are predefined, reserved words used in programming that have special meanings to the compiler. Keywords are part of the syntax and they cannot be used as an identifier. For example: int money;
Here, int
is
a keyword that indicates money is
a variable of
type int
(integer).
All keywords must be
written in lowercase. Here is a list of all keywords allowed in ANSI C.
C Keywords |
|||
auto |
double |
Int |
struct |
break |
else |
long |
switch |
case |
enum |
register |
typedef |
char |
extern |
return |
union |
continue |
for |
signed |
void |
do |
if |
static |
while |
default |
goto |
sizeof |
volatile |
const |
float |
short |
unsigned |
C - Variables
A variable is nothing but a name given to a storage
area that our programs can manipulate.
A variable definition tells the compiler where
and how much storage to create for the variable.
int playerScore =
95;
Here, playerScore is a variable of int
type. Here, the variable is assigned an integer value 95
.
Difference between Keyword and
Identifier:
SR.
NO. |
KEYWORD |
IDENTIFIER |
1 |
Keywords are predefined word that gets reserved for
working progs that have special meaning and cannot get used anywhere else. |
Identifiers are the values used to define different
programming items such as variables, integers, structures, unions and others
and mostly have an alphabetic character. |
2 |
It always starts with a lowercase letter. |
First character can be a uppercase, lowercase letter or
underscore. |
3 |
A keyword should be in lower case. |
An identifier can be in upper case or lower case. |
4 |
A keyword contains only alphabetical characters. |
An identifier can consist of alphabetical characters,
digits and underscores. |
5 |
Examples of keywords are: int, char, if, while, do, class
etc. |
Examples of identifiers are: Test, count1, high_speed,
etc. |
Constant: - Constants
refer to fixed values that the program may not alter during its execution.
These fixed values are also called literals.
Constants can be of any of the basic data types like an
integer constant, a floating constant, a character constant, and string literal.
Integer
Literals
An integer literal can be a decimal, octal, or hexadecimal
constant.
Ex.-
85 /* decimal */
0213 /* octal */
0x4b /* hexadecimal */
30 /* int */
Floating-point
Literals:-
A floating-point literal has an integer part, a decimal point, a
fractional part.
Ex.-
3.14159 /* Legal */
Character
Constants:-
A
character constant is a single alphabet, a single digit or a single special
symbol enclosed within single quotes. Ex-
‘a’, ‘A’, ‘5’ etc.
String
Constants:-
A
string constant is a mixed alphabet, digit or a special symbol enclosed within
double quotes. Ex- ‘Ramesh’, ‘It is
raining day’, ‘4a5b’ etc.
Data
types:-
C language supports 3 different type of data
types:
1.
Primary
data types:
These are fundamental data types in C namely integer (int), floating point (float), character (char) and double.
2.
Derived
data types:
Derived data types are nothing but primary datatypes but a
little twisted or grouped together like array, stucture, union and pointer.
These are discussed in details later.
3.
User
Defined data types:- Those data types which are defined by
the user are called user-defined
data types. Examples of such data types are structure,
union and enumeration.
For example, let’s define a structure
struct student
{
char name[100];
int roll;
float marks;
}
Here,
with this structure we can create our own defined data type in following way:
struct student info;
Here, student
is
user-defined data type where info is variable which holds name, roll number,
and marks of a student.
C
programming operators
C programming operators are
symbols that tell the compiler to perform certain mathematical or logical
manipulation. In simple terms, we can say operators are used to manipulating
data and variables.
Operators in C programming
|
||
1: Arithmetic operator |
||
2: Relational operator |
||
3: Logical operator |
||
4: Assignment operator |
||
5: Increment/Decrement operator |
||
6: Conditional operator |
||
7: Bitwise operator |
||
8: Special operator |
The data items in which any operation is carried out are
called operands.
x + y
Here x
and y
are operands
and +
is an operator and calculating sum is an operation.
Operators which require two operands are called binary operators and
which takes single operand are called unary operators.
Arithmetic operators in C
Operators used in the arithmetic operation like addition,
subtraction, multiplication or division are called arithmetic operators.
Arithmetic operator
|
Meaning
|
+ |
Addition or unary plus |
– |
Subtraction or unary minus |
* |
Multiplication |
/ |
Division |
% |
Modulo division |
Example: a+b;
Relational Operator in C
Relational operators are used to compare two operators depending
on their relation.
Example- a>b
Relational operator
|
Meaning
|
< |
is less than |
<= |
is less than or equal to |
> |
is greater than |
>= |
is greater than or equal to |
== |
is equal to |
!= |
is not equal to |
Logical Operator:-
Logical operators are
used when more than one condition is tested. They are used to combine more than
one relational expressions.
For example: a>b && c==1;
Logical operator
|
Meaning
|
&& |
logical AND |
|| |
logical OR |
! |
logical NOT |
Assignment Operator:-
The assignment operator is
used to assign a value or a result to a data item or a variable.
=
is the
assignment operator.
Example- a=4;
Assignment Operator |
Sample expression |
Explanation |
Assigns |
+= |
c += 2 |
c = c + 2 |
4 to c |
Bitwise Operator in C
Bitwise operators are used to manipulate data at a bit level.
They are used for testing or shifting the bit.
Bitwise operator
|
Meaning
|
& |
bitwise AND |
| |
bitwise OR |
^ |
bitwise exclusive OR |
<< |
shift left |
>> |
shift right |
Conditional operator in C
Conditional operator is a ternary operator that takes three
operands.
Syntax
x = exp1 ? a : b
Here, if exp1
is non zero
i.e. TRUE
then the value of a
will be
assigned to x
and if exp1
is zero
i.e. FALSE
then the value of b
is assigned
to x
.
Increment / Decrement operator in C
C provides an increment operator ++
and decrement
operator --
. The functionality of ++
is to add 1
unit to the operand and --
is to subtract
1 from the operand.
For example: ++a, --a;
Here ++a
is equal
to a = a + 1
and --b
is equal
to b = b - 1
.
There are two kinds of increment and decrement operator
i.e prefix and
postfix.
If the operator is used before the variable
i.e ++a
then it is
called prefix
increment operator.
If the operator is used after variable
i.e a++
then it is called postfix increment operator.
Operator |
Sample expression |
Explanation |
++ |
++x |
x is increased by 1,
then use the value of x |
++ |
x++ |
Use the current
value of x and then increment x by 1 |
– – |
– -x |
x is decreased by 1, then use the value
of x |
– – |
x- – |
Use the current
value of x and then decrement x by 1 |
#include
<stdio.h>
int
main()
{
int i;
i = 4;
printf( "%d\n", i );
printf( "%d\n", i++ ); //post increment
printf( "%d\n\n", i );
i = 4;
printf( "%d\n", i );
printf( "%d\n", ++i ); //preincrement
printf( "%d\n", i );
return 0;
}
OUTPUT-
4
4
5
4
5
5
Special operators in C
Besides these fundamental operators, C provides some other
special operators like comma
operator
and sizeof
operatorts.
Example- X= sizeof
(a);
Control Statement:
Programs are executed sequentially in the order which they appear.
Some time we need to execute a small part of the programs and maybe we need to
execute it again and again until a particular condition is not reached.
Control statements are the keywords that are responsible for making a
decision. Control statement specify the order of program to be executed. They
define how the control is transferred to other parts of program.
There are three types of control flow statements:
Conditional Statement or Decision-Making Statement or Branching
Statement (if, if- else, if-else if, switch)
2. Iteration Statement or Looping statement (While, do- while, for)
3. Jump Statement (break, continue, go to)
Conditional Statement: -
Conditional statements allow you to make a decision, based upon the result of a
condition. These statements are called Decision
Making Statements or Conditional
Statements. C language has provide following conditional statement.
If Statement:-
When we need to execute a
block of statements only when a given condition is true then we use if
statement.
Syntax of if
statement -
if (condition)
{
//Block of C statements here
//These statements will only execute if the
condition is true
}
The statements inside the
body of “if” only execute if the given condition returns true. If the condition
returns false then the statements inside “if” are skipped.
Program:-
1- #include<stdio.h>
Main()
{
int a,b;
clrscr();
Printf(“Enter two number”);
Scanf(“%d%d”, &a,&b);
If(a>=b);
Printf(“%d”,a);
getch();
}
2-
#include<stdio.h>
int main(){
int number=0;
printf("Enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
return 0;
}
3-
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter three numbers?");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
{
printf("%d is largest",a);
}
if(b>a && b > c)
{
printf("%d is largest",b);
}
if(c>a && c>b)
{
printf("%d is largest",c);
}
if(a == b && a == c)
{
printf("All are equal");
}
}
C If else statement
Syntax of if else statement:
if(condition) {
// Statements inside body of if
}
else {
//Statements inside body of else
}
If condition returns true then the statements inside the body of “if” are
executed and the statements inside body of “else” are skipped.
If condition returns false then the statements inside the body of “if” are
skipped and the statements in “else” are executed.
Program-
#include <stdio.h>
int main()
{
int age;
printf("Enter your age:");
scanf("%d",&age);
if(age >=18)
{
/* This statement will only execute if the
* above condition (age>=18) returns true
*/
printf("You are eligible for voting");
}
else
{
/* This statement will only execute if the
* condition specified in the "if"
returns false.
*/
printf("You are not eligible for voting");
}
return 0;
}
Switch Case Statement:-
The switch case statement is used when we have multiple options and we
need to perform a different task for each option.
Syntax of switch case statement:-
switch( expression
)
{
case value-1:
Statement-1;
Break;
case value-2:
Statement
-2;
Break;
case value-n:
Statement
-n;
Break;
default:
Statement;
Break;
}
Program-
#include <stdio.h>
int main() {
int num
= 8;
switch
(num) {
case
7:
printf("Value is 7");
break;
case
8:
printf("Value is 8");
break;
case
9:
printf("Value is 9");
break;
default:
printf("Out of range");
break;
}
return
0;
}
#include<stdio.h> //
include stdio.h library
int main(void)
{
int a, b, result;
char op; // to store the operator
printf("Enter an expression: ");
scanf("%d%c%d", &a, &op,
&b);
switch(op)
{
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
result = a / b;
break;
}
printf("Result = %d", result);
return 0; // return 0 to operating system
}
Looping Statement: - A Loop executes
the sequence of statements many times until the stated condition becomes false.
There are three following types.
1-
while
loop
2-
do-while loop
3-
For loop
While Loop-
In While
Loop in C First check the condition if condition is true then control
goes inside the loop body other wise goes outside the body. while loop will
be repeats in clock wise direction.
Syntax-
Dh-Guruji Dh-Guruji
Program-
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
i=1;
while(i<5)
{
printf("\n%d",i);
i++;
}
getch();
}
do-while
A do-while
Loop in C is similar to a while loop. A
do while loop is a control flow statement that executes a block of code at
least once, and then repeatedly executes the block, or not, depending on a
given condition at the end of the block (in while).
Syntax
Dh-Guruji
Program-
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
i=1;
do
{
printf("\n%d",i);
i++;
}
while(i<5);
getch();
}
For loop
For
Loop in C is a statement which allows code to be repeatedly executed.
For loop contains 3 parts Initialization, Condition and Increment or
Decrements. Syntax
Dh-Guruji
#include<stdio.h>
Program
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<5;i++)
{
printf("\n%d",i);
}
getch();
}
Jump Statement:- Jump statements are used to interrupt the
normal flow of program. There are four type jump statement.
1- Break Statement
2- Continue Statement
3- Goto Statement
4- Return Statement
Break Statement: - Break statement is used in terminating (exit)
the loop immediately. It’s used to terminating loops like for, while, do-while
and it is also use in Switch case terminating.
Syntax-
DH-GURUJI
Program-
#include<stdio.h>
main()
{
Int
i=1;
While(i<=10)
{
If(i==6)
Break;
printf(“value=%d\n”,
i);
i++;
}
}
Continue
Statement: - It
forces the next iteration of the loop to take place, skipping any code in
between.
Syntax-
DH-GURUJI
Program-
#include<stdio.h>
main()
{
Int I;
for(i=1;i<=10;i++)
{
For(i==6)
{
continue;
}
printf(“Value=%d\n”,i);
}
goto Statement:- goto statement is use transfer the program control from one
statement to another statement (where is level is define).
Syntax-
DH-GURUJI
Program-
#include<stdio.h>
main()
{
Int i=1;
Repeat:
Printf(“Value=%d\n”,i);
i++;
if(i<=10)
goto repeat;
}
Return
Staetement:-
·
It is used at the
end of a function to end or terminate it with or without a value.
·
It can only be
used in function that is declared with a return type such as int, float, double
etc.
·
The function
declared with void type does not return any value.
Syntax-
DH-GURUJI
Program-
#include<stdio.h>
int main()
{
printf(“this is return
statement program”);
return 0;
}
Array:-
Array is derived data type. It is a collection of similar data type. Array
can be store multiple value which can be referenced by a single name
or
·
Array is defined
as collection of related items are similar data type.
·
Array are the
collection of finite no. of homogeneous (similar type) data element.
·
Array can be
store multiple value which can be referenced by a single name.
Array
is two type
1- One dimensional array
2- Multiple dimensional array
·
Two dimensional
array
·
Three dimensional
array
·
Fourth
dimensional array etc.
One dimensional array:
- In which array, we use only one
Subscript ([ ]) to define the element of array, are called one dimensional
array.
Or
Single dimensional array
as a row where elements are stored one after another.
Syntax-
data_type arrayname[ size
];
int a[4];
Initializing of array:-
- Arrays may be initialized when they are declared, just
as any other variables.
- Place the initialization data in curly {} braces
following the equals sign.
data_type arrayname[ size
]= { values}
Ex- int a[4]= { 1,2,3,4}
Program-
1. #include<stdio.h>
2. int main(){
3. int i=0;
4. int marks[5];//declaration of array
5. marks[0]=80;//initialization of array
6. marks[1]=60;
7. marks[2]=70;
8. marks[3]=85;
9. marks[4]=75;
10. //traversal of array
11. for(i=0;i<5;i++){
12. printf("%d \n",marks[i]);
13. }//end of for loop
14. return 0;
15. }
Multi-dimensional
array: - In which array, we use more
than one Subscript ([ ]) to define the element of array, are called multi-dimensional
array.
It has following types.
·
Two dimensional
array
·
Three dimensional
array
· Fourth dimensional array etc.
Two dimensional: - In which
array, we use two Subscript ([ ]) to define the element of array, are called
two dimensional array.
Syntax-
data_type array_name[ row
] [ column];
int a[4][3];
Initializing of array:-
data_type arrayname[ row ]
[ column ]= { values}
int a[2][3]= {{2,3,4},
{5,7,9}}
Program:-
1. #include<stdio.h>
2. int main()
3. {
4. int i=0,j=0;
5. int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
6. //traversing 2D array
7. for(i=0;i<4;i++){
8. for(j=0;j<3;j++){
9. printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);
10. }//end of j
11. }//end of i
12. return 0;
13. }
Sorting: - Sorting refers to the operation of arranging data in Some given
Sequence Such as increasing order or decreasing order.
There are following types
of sorting.
1- Bubble
sort
2-
Selection sort
3-
Insertion sort
4-
Quick sort
5-
Heap sort
6-
Merge sort
7- Radix or
bucket sort
Bubble Sort: Bubble sort is a sorting algorithm that is
used to sort items in a list in ascending order. This is done by comparing two
adjacent values. If the first value is higher than the second value, then the
first value takes the position of the second value while the second value takes
the position that was previously occupied by the first value. If the first
value is lower than the second value, then no swapping is done. This process is
repeated until all the values in a list have been sorted.
Ex-
DH-GURUJI
0 Comments
thanks