Professional Documents
Culture Documents
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
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
It identifies a memory
location. In order to identify
any value identifier is used.
Constants
◼ Integer variables
◼ Float variables
◼ String variables.
Rules to write variable names
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
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.
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
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.
gets()
This function is used to accept only string
values.
◼ 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
Ex:- a=10;
b=a+10;
Increment and decrement
operators
1. Increment operator (+ +)
Increment operator is used to increase the value by 1.
◼ 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.
Syntax:
if(condition)
{
statement;
}
Switch (expression)
{
case value:
statements;
case value:
statements;
default:
statements;
}
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
Ex:- continue;
Loop control structures
◼ 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.
Syntax:-
for (initial value ; test condition ; increment /decrement)
{
statements; //body of for loop
}
Syntax: do
{
statements;
statements;
}
while(condition);
Syntax:-
Data_type variable_name [subscript][subscript]
Syntax:-
strcpy(target_string , source_string);
Strcat( ):- (String concatenation)
Syntax:- strcat(string1,stirng2);
Strcmp( ):- (String comparison)
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( )
Syntax:- strlwr(string);
Single character
1) toupper( ) :- This function converts a single
character to upper case.
Syntax:- toupper(character);
Syntax:- tolower(character);
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.
➢ 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
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.
65524
10
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
◼ Call by value
◼ Call by address
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.
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);