You are on page 1of 103

LANGUAGE

A language is a communication media


between two parties

Programming languages: A
programming language is a language
used in writing programs to direct
processing steps to be carried out by a
computer.
Programming languages are
classified into 3 categories

1) Low level language (or) Machine


Language (or) Binary language
2. Middle level language (or) Assembly
language
3) High level language
Machine level Language

Machine code is the fundamental


language of a computer and is normally
written as strings of binary 1’s and 0’s.
Programs written in machine language
can be executed very fast by the
computer. This is mainly because the
CPU directly understands machine
instructions and no translation of the
program is required.
Assembly language

The language, which substitutes


letters and symbols for the
numbers in the machine language
program, is called an assembly
language or symbolic language.
The translator program that
translates an assembly code into
the computer’s machine code is
called assembler.
High level language

Every instruction, which the


programmer writes in a high-level
language, is translated into many
machine language instructions.
Compiler: A compiler is software
that translates a program from high
level language to machine language.

Interpreter: An interpreter is
software that translates a program
from high level language to machine
language.
Differences between Compiler
and interpreter
Compiler Interpreter
1. Compiler converts all 1. Interpreter converts line by
statements at a time, if errors line, at the time of
are there, it displays all interpreting any error is
errors as a list. there, it displays that error.
2. After Compiling the whole 2. After interpreting the first line,
program, if the program is if it is error free then it will
error free then it will execute execute that line.
the program. 3. It does not create any
3. After Compilation, it creates executable file, every time
an executable file, using that we need to interpret the
executable file we can run program.
the program any number of 4. It is slow.
times.
4. It is fast.
What is C?
C is a programming language
developed at AT & T’s Bell
Laboratories of USA in 1972. It was
designed and written by a man
named Dennis Ritche.
Features of C

◼ Portability or machine independent


◼ Sound and versatile language
◼ Fast program execution.
◼ An extendible language.
◼ Tends to be a structured language.
Character set of the C
language
This indicates group of characters which are useful to
write the programming statements. It contains 3 parts.
◼ Alphabets: C language supports upper case letters
(A to Z), lower case letters from 6(a to z). But both
are different. C is a case sensitive language.
◼ Numeric digits: C language contains the numeric
digits from 0 to 9.
◼ Special Characters: The characters other than
alphabets and numeric digits are called as special
characters.
Identifiers

It identifies a memory
location. In order to identify
any value identifier is used.
Constants

It is a quantity which does not change


its value during executing a program.

Constants are 4 types


1. Integer constants
2. Float constants
3. Single character constants
4. String constants
◼ Integer Constants: These are the numbers without
decimal point or exponent.
Ex:- int a=10;
data type varaible=value;

◼ Float constants: These are the numbers with decimal


point or exponent.
Ex:- float pie=3.14;

◼ Single Character Constants: It is any one of the


single character enclosed in single quotation.
Ex:- char ch=’a’;//alphabate charcter
char section=’b’;
char ch=‘2’;//numeric character
char ch=‘$’;//special character

◼ String constants: Group of characters enclosed with in


double quotations.
Variables
It is a value that changes its value during
program execution. Variables are 4
types.

◼ Integer variables
◼ Float variables

◼ Single character variables

◼ String variables.
Rules to write variable names

◼ A variable name contains maximum of


31 characters.
◼ A variable name includes alphabets
and numbers, but it must start with an
alphabet.
◼ It cannot accept any special
characters, blank spaces except
under score( _ ).
◼ It should not be a reserved word.
key words (or) Reserved
words
Keywords are the words whose meaning has
already been explained to the C compiler. The
keywords cannot be used as variable names.
They are
auto break case char const
default do double else continue
enum extern float for goto
if int long register return
short signed sizeof static struct
switch typedef union unsigned void
volatile while
Data types
Data type Range Bytes Format
signed char -128 to +127 1 %c
unsigned char 0 to 255 1 %c
short signed int -32,768 to +32,767 2 %d
short unsigned 0 to 65,535 2 %u
int
long signed int -2147483648 to 4 %ld
+2147483647
long unsigned int 0 to 4294967295 4 %lu
Float -3.4e38 to +3.4e38 4 %f
Double -1.7e308 to +1.7e308 8 %lf

long double -1.7e4932 to 1.7e4932 10 %lf


Data types
Data type Range Bytes Format

unsigned 0 to 255 1 %c
char
int -32,768 to +32,767 2 %d
long -2147483648 to 4 %ld
+2147483647
Float -3.4e38 to +3.4e38 4 %f
Double -1.7e308 to +1.7e308 8 %lf
Input and Output functions

1. Formatted I/O functions


Input function – scanf()
Output function – printf()

2. Unformatted I/O functions


Input function – gets()
Output function – puts()
Formatted I/O functions

Output Function
printf( ): This function is used to display the
information on the screen. It displays the
variable values and the string values. This
function belongs to stdio.h.

Syntax: printf(“control string” ,variable list);


Examples:-
int a=10;
printf(“\n The value of a is %d”,a);

float pie=3.14;
printf(“The value of pie is %f”,pie);

char ch=‘A’;
printf(“Ch = %c”,ch;

char name[20]=“ORBIT”
printf(“Name = %s”,name;
Input functions

Scanf( ):- It is an input function, that is


used to accept the values into variables at
the time of executing a program. This
function belongs to stdio.h .

Syntax: Scanf(“control string”, variable list);


Examples:-
int a;
scanf(“%d”, &a);

float b;
scanf(“%f”,&b);

char ch;
scanf(“%c”,&ch);

char name[20];
scanf(“%s”,name);
Unformatted I/O functions

◼ Output function

puts()
This function is used to execute only string
values.

Syntax:- puts(string variable);

Ex:- char name[20]=“ORBIT”;


puts(name;
◼ Input function

gets()
This function is used to accept only string
values.

Syntax:- gets(string variable);

Ex:- char name[20];


gets(name;
Structure of a C program
/* Comment line section */ → Documentation
optional
# header file section → when we use
predefined functions
main( )
{
Declaration part; → To declare the
variables
which are used in the
program
Initialization part; → To initialize i.e. giving
values to the variables.
Execution part; → To execute as output.
}
A C program starts with the include
statement. This statement is used to link
the header files into our program. This
statement should start with # symbol.

Ex:- # include <stdio.h>


stdio.h stands for standard input output .
header file
Comment Line Section

In order to give documentation for a


program comment line is includes.
It is enclosed in between a /* */
because in the compilation process
these lines are not includes.
Header file section

‘C’ program consists of predefined


functions for that reason we should
include the files in which they are
defined. Which are saved with .h as
extension.
Main( )

The compilation and the


running process is done with
the help of Main( ). A ‘C’
program definitely requires
Main( ).
Declaration part

This area is where we declare all


the variables which are used in the
program.
Initialization part

Here, we give values to the


declared variables.
Executable part

The output is seen from the


executable part. By using output
functions.
Programming rules

A ‘C’ program should be written in


lower case. Every statement should
end with (;) . The pair of { } should
be matched.
Compilation and Execution
process
After writing the program we should
compile by pressing on the Compile
option of Compile menu. This
converts the text format that is
source code into Binary format
which is called as object code. After
that by pressing Run option the
output can be seen.
Operators

◼ Arithmetic operators
◼ Relational operators
◼ Logical operators
◼ Assignment operators
◼ Increment or decrement operators
◼ Conditional operators
Arithmetic Operators
Arithmetic operators are used to perform
arithmetic operations like addition,
subtraction, multiplication, division and
remainder.
Operator Name Meaning Example
+ Plus Addition a+b
- Minus Subtraction a-b
* Asterisk Multiplication a*b
/ Slash Division a/b
% Modulator Reminder a%b
Relational operators
These operators are used to check the relation between
the values.
Note:- When we used relational operators the output is
seen in the form of 0 or 1. If the relation is true the
output will be in 1. If the relation is false the output will
be 0.
Operator Example Result
Ex:- a=10 > a>b 1
b=5 < a<b 0
>= a>=b 1
<= a<=b 0
== a==b 0
!= a!=b 1
Logical operators
logical operators are used to combine
two or more relational expressions. C
language contains 3 logical operators.

Operator Name
&& And
|| Or
! Not
&& : (And)
Statement:- When all arguments are
true then only the total condition is
true. Any one of the arguments is
false the total condition is false.
Truth table
Expressinn1 && Expression2 Result
T T T
T F F
F T F
F F F
|| : (Or)
Statement:- When one of the
argument is true the total condition is
true. If all the arguments are false
then only the condition is false.
Expression1 || Truth table
Expression2 Result
T T T
T F T
F T T
F F F
! (Not)
Statement:- This is the negative
operator, we can place the ‘not’
operator before any one of the
condition. It returns the opposite
result of the condition. i.e. It converts
true to false and false to true.
Truth table
! expression result
T F
F T
Assignment operator

C language contains equal to (=)


operator to assign a value or
expression into a variable.

Ex:- a=10;
b=a+10;
Increment and decrement
operators
1. Increment operator (+ +)
Increment operator is used to increase the value by 1.

◼ Pre Increment( ++a)


First the value will be incremented, and then the
new value will be executed.
Ex:- printf(“%d”,++a);

◼ Post Increment(a++)
First the value will be executed and then one value will
be incremented.
Ex:- printf(“%d”,a++);
2) Decrement operator ( - - )
Decrement operator is used to decrease the
value by 1.

◼ Pre Decrement (--a)


If the value is decreased before execution it is
called as pre decrement.
Ex:- printf(“%d”,--a);

◼ Post Decrement (a--)


If the value is decreased after the execution it is
called as post decrement.
Ex:- printf(“%d”,a--)
Conditional operators
C language contains conditional operators (?, : ). By
using conditional operators we can get the output in the
form of statements also. ? and : are called as
conditional or temporary operators.

Syn:- (condition) ? printf(“value 1”) : printf(“ value 2”);

Ex:- (a>b) ? printf(“a greater than b”):printf(“a is


less than b”);

If the condition is true then value1 will be executed, if


the condition is false then value2 will be executed.
Conditional statements
The statements which are executed
according to some condition are
called as conditional statements.

They are 3 types


◼ If
◼ If-else
◼ Nested if-else
If
Statement: This statement is used to perform
conditional operations.

Syntax:
if(condition)
{
statement;
}

If the condition is true then statement will be


executed. If the condition is false then it will not
execute statement.
If-else
Syntax:
if(condition)
{
statement1;
}
else
{
statement2;
}
If the condition is true then statement1 will be
executed, if the condition is false then
statement2
Will be executed.
Nested if-else
Syntax:
if(condition1)
{
statement1;
}
else
if(condition2)
{
statement2;
}
else
{
statement3;
}
If the condition1 is true then statement1 will be executed
and the remaining statements will be skipped. If the
condition1 is false then it will check the condition2, if it is
Switch statement
It is used to execute one of the options from no. of options. It
is also called as multi branching statement.

Switch (expression)
{
case value:
statements;
case value:
statements;
default:
statements;
}

The expression value may be int or character type. The


switch statement evaluates the expression. It will select one
of the cases based on expression. It will execute default
statements when no case is selected.
Break statement
It is used to exit from a looping
statement. We can use this in for,
while , do while, and switch
statement.

Ex:- break;
Switch case
Syntax: switch(expression)
{
case 1:
{
statement;
}
break;
case 2:
{
statement;
}
break;
default:
{
statement;
}
}
Goto statement
It is used to transfer the control
from one place to another place in
the program. It is also called as
unconditional jumping statement

Syntax:- goto <label name> ;

Ex:- goto orbit;


Continue statement

It is used to transfer the control


to the beginning of the looping
statements.

Ex:- continue;
Loop control structures

LOOPING:- The process of executing a


block of code repeatedly is called as
looping. They are 4 types

◼ For loop
◼ While loop
◼ Do while loop
◼ Nested for loops
All the four loops are used for same process but
there syntax is different.
All the four loops have 3 steps is common.

◼ Initialization:- It is the starting point of the loop


process.

◼ Condition:- In this step logical test is applied. It


is true the block of code is executed. Till the
condition fails.

◼ Increment / Decrement:- In order to proceed or


stop the loop increment or decrement is
required.
For loop
It is used to execute a set of statements
repeatedly as long as the given condition is true.

Syntax:-
for (initial value ; test condition ; increment /decrement)
{
statements; //body of for loop
}

Ex:- for(i=1; i<=10; i++)


While loop
It is used to execute a set of statements
repeatedly as long as the given condition is true.

Syntax:- while (condition)


{
statements; //body of while loop
}
First the condition is tested, if it is true the body
of the while loop will be executed first time,
again the control is transferred back to while
loop and checks the condition, If it is false the
control will come out of the while loop.
Do While Loop
This statement executes a set of statements as
long as the condition is true.

Syntax: do
{
statements;
statements;
}
while(condition);

This is similar to while loop, but the difference is


this loop will execute the statements at least
once.
Nested for loop
In order to get the output in the form
of rows and columns we use nested
for loops. If the outer loop condition
is true it enters into the inner loop.
Till the outer loop condition is false
the looping process is continued.
ARRAYS
An array is contiguous area in the memory
referred by a common name. The array
allows the storage of a number of data values
in one variable. Each array element i.e. each
individual data item is referred by specifying
the array name followed by one or more
subscripts. Each subscript is enclosed with
square brackets. Each subscript indicates the
position of the element in the array. Arrays
are divided into 2 types.

1. Single dimensional array or One dimensional


array
2. Two dimensional array
One dimensional array
The array which is using only 1 subscript is
known as one dimensional array. In one
dimensional array the elements are
represented in single row or single column.

Syntax:- Data_type variable_name[subscript]


Ex:- int a[5]={40, 50, 70,90,100};
Float b[4]={20.5, 40.9, 45.7, 23.8};
Char c[5]={‘O’,’R’,’B’,’I’,’T’};
Multi dimensional array
The array which is using two subscripts is known
as 2 – dimensional array

Syntax:-
Data_type variable_name [subscript][subscript]

Ex:- int a[2][2]={{1,2,},{3,4}};

The above declaration states that ‘a’ is an two


dimensional Array variable in which the values
are stored in 2 rows and in 2 columns
String Functions
A character array is defined as a string.
A group of characters is called a string.
We cannot handle with string same like
integers or real values. For example, if
you want to compare an integer value
with another int value. We can do that by
using = = operator. But the same
procedure cannot be applied for strings.
In order to handle with strings there are
some string handling functions.
Strlen( )

This function is used to find the length


of a string.
Syntax:- strlen(string);

Note:- When we are using string


functions we have to include a header
file <string.h>
Strcpy( )

This function copies a string from


one string variable to another string
variable.

Syntax:-
strcpy(target_string , source_string);
Strcat( ):- (String concatenation)

This function adds a string at the


end of another string

Syntax:- strcat(string1,stirng2);
Strcmp( ):- (String comparison)

This compares one string with


another string.

Syntax:-
strcmp(string1, string2);
Stricmp( )
This function is similar to strcmp, but
this function ignores the case
sensitivity.

Syntax:-
stricmp(string1, string2);
Strrev( )
This function reverses the given
string.

Syntax:- strrev(string);
Strupr( )
This function converts the given
string into upper case (capital letters)

Syntax:- strupr(string);
Strlwr( )

This function converts the given


string into lower case.

Syntax:- strlwr(string);
Single character
1) toupper( ) :- This function converts a single
character to upper case.

Syntax:- toupper(character);

2) tolower( ) :- This function converts a single


character to lower case.

Syntax:- tolower(character);

◼ Note:- when we are using single character


functions we have to include a header file
<ctype.h>
Functions
A function is a set of statements
that performs a specific task.
Functions are 2 types

1. Pre defined functions or Standard


functions
2. User defined functions
Pre defined Functions

These functions which are already


defined (in built) along with C are
called as pre defined functions.

Ex:-
printf( ), scanf( ), pow( ), strlen( ),
strcpy( ), toupper( ),……
User defined functions
These functions defined by the user in
order to work with complex program and
to develop the efficiency of the program
are called as user defined functions. In
order to define a function we have 3
types.

• Function definition
• Function prototype
• Invoking a function
Function definition

➢ Function heading
• Return type
• Function name
• Specification of parameters
a. Return type:- Return is called as
data type mention according to the
type of value returned from the
function.

Note:- When there is no return


statement we should mention void as
a default return type.
b. Function name:- Here we
mention the name of a function.
It is just like a variable name
b. Specification of parameters:- The
specify the number of values are
variables taken by the called function
from main program.

Note:-The parameters mentioned in the


main program for calling a function are
called as actual parameters.
The parameters mentioned in the
function which are received from the
main program are called formal
parameters.
➢ Block of code
The code which is written with in the
braces of a function is called as block of
code.

➢ Return statement
This is the last statement of a function it
is to return a value from the user defined
function to the main program.
Function Prototype

It is declared in the main program about


the function.

Note:- Function prototype is optional if


the function is defined before the main. It
is compulsory if the function is defined
after the main.
Invoking a Function

From here we call the function. A


function as no live when it is not
Invoked.
Uses of functions

– Dividing the task into sub tasks.


– If a particular task is repeated number of times in a
program we can create a function and call that
function wherever we want.
– Whenever a function is called in a statement the
control will be transferred to that called function.
After executing all statements it returns back to the
calling function.
– The length of the source program will be reduced by
using functions at appropriate places.
– A function can be used in other programs also.
Syntax of a function

Return_type function_name (argument


list)
{
declarations;
statements;
-------------
return value;
}
Return statement

It returns a value of the expression to


the calling function.

Syntax:-return(expression)
Ex:- return(a);
return(a+b);
return a;
Recursive function
Pointers
A pointer is a variable which holds the address
of another variable. Since addresses are whole
numbers pointers would always contain whole
numbers.

Consider the following declaration


Ex:- int i=10;

The declaration tells the C compiler


◼ Reserve space in memory to hold the integer
value.
◼ Associate the name ‘i’ with memory location.
◼ Store the value 10 at this location.
Memory map
i

65524
10

‘i’ - Location name


10 - Value at location
65524 – Location number or address.

Declaration of a pointer:

Int a, *x;
Float b, *y;
Char c, *z;
Pointer variable is same as normal variable. But a *
symbol is added before the variable.
Usage of pointers

As pointers work with addresses accessing


through addresses first then through a normal
variable. When the values are called with the
help of address by a function the modifications
done to the variables shows the reflection.
Indirectly we can get the value of a variable by
using *. So * is called as indirection operator.
Whatever the data type may be a pointer
variable occupies only 2 bytes of memory.
Parameter passing mechanism

◼ Call by value
◼ Call by address

◼ Call by value:- When the function calls the


parameters by taking values the modifications
done to the parameters by the function
doesn’t reflect.

◼ Call by address:- When the parameters are


called by address the changes done to the
values within the function shows the
reflection.
Structures
Structure is a user defined data type. A
structure is a collection of variables that is
referenced under a single name. A structure
contains a number of data types grouped
together. These data types may or may not
be of the same type.

Definition of a structure: - Structure is a


collection of variables which are of dissimilar
data types.
Declaring a structure:-

struct <structure name>


{
structure element 1;
structure element 2;
---
structure element n;
};
structure variable;
Ex:- struct book
{
int pages;
char author[20];
float price;
};
struct book b;

◼ The above declaration states that struct is a


keyword which is called as a data type in variables
is declared which are of different data types.

◼ In the above declaration book is called Tag name


and pages, author, price are called as data
members of the structure
Declaring a variable for a structure
Ex:- Student S1;

Here, s1 is a variable of a structure to


which we can give 3 details.
The data members of the structure are
called by the variable using ( . ) operator.

For example:- S1.sno


S1.name
S1.marks
Features of a structure

With the help of structure we can


maintain a data in a systematic order.
We can handle a structure within a
structure. And also we can call the
structure variable through functions and
Arrays also can be implemented through
structures. Pointer to structure is also
possible.
Files
A file is a collection of information on a storage device
with a particular file name.

Uses of a file:-
◼ Stores the data in the form of a file and we can
retrieve it whenever we require.
◼ Using the data from the file in different programs.

Opening a file:- we can open a file by using fopen( )


Syntax: - fopen(file name, opening mode);
Ex: - FILE *fp;
fp=fopen(“file1.txt”,”w”);
File opening modes

◼ w :- Writing the data into a file. If the file


already exists its contents will be over
written.

◼ r:- Reading data from a file.

◼ a:- Adds data into an existing file.


(Appending)
◼ w+ :- We can write data into file and we
can read data from the file. If the file
already exists its contents will be over
written, else creates a new file.

◼ r+ :- Reading existing contents, writing


new contents, modifying existing
contents of a file.

◼ a+ :- Reading existing contents,


appending new contents at the end of a
file.
Closing a file

When the file processing is over, the


file must be closed. We can close a file
by using fclose( ).

Syntax:- fclose(file pointer);


Ex:- fclose(fp);
File functions
getc( )
This function is used to read a character
from a file.
Syntax :- getc(file pointer);
Ex:- getc(fp);

putc( )
This function is used to write a character into
a file.
Syntax:- putc(character, file pointer);
Ex:- putc(ch,fp);
fprintf( )
This function writes formatted data into a file.
Syntax:-
fprintf(file pointer, “formatted string”, list of
variables)
Ex:- fp=fopen(“student.dat”,”w”);
fprintf (fp , “%d %s %d”, sno ,name, marks);

fscanf( )
This function reads formatted data from a file.
Syntax:-
fscanf(file pointer, “formatted string”, list of
variables)
Ex:- fp=fopen(“student.dat”,”r”);
fscanf(fp , “%d %s %d”, &sno, name, &marks);

You might also like