C
Presented by,
[Link]
Introduction
C is a high level programming language.
It is often called as middle level language.
It combines the elements of high level languages with the functionalism of
assembly language
It can be compiled on different platforms.
History
C was developed by Dennis M. Ritchie in Bell labs in 1972.
It was invented to write an operating system called UNIX.
It is easy to learn.
Later, it is used for the development of application software also.
Todays most popular LINUX OS and MySQL have been written in C.
Features
Portability: C programs are portable i.e. they can run on any platforms.
Powerful: C programs provide wide variety of datatypes, functions, control and
loop structures.
Efficient use of pointers: Pointers have direct access to memory.
C provides low level features that is easier to write assembly codes.
It provides high level features as that is user friendly.
Compiler design
Compiler is a computer program that is primarily used to convert source code
into machine code.
In C 4-pass compilers are used.
1st pass: 1st pass is pre-processor. It typically do macro substitutions.
2nd pass: 2nd pass is the heart of the compiler. It translates the source code into the
intermediate language.
3rd pass: 3rd pass is optimizer. It improves the generated intermediate code.
4th pass: 4th pass is the back end. It translates the optimized code into real assembly language
or some form of binary executable code.
Structure
Pre-processor statements
global declarations
function1 (main){
local declaration
.
code
}
function2(sub program){
local declaration
.
code
Pre-processor
Pre-processor
Syntax
Description
Macro
#define
This macro defines constant value.
Header file inclusion
#include<file-name>
The source code of the file
file_name is included in the
main C program.
Conditional compilation
Other directives
#ifdef, #ifndef, #if, #else, #endif
#undef, #pragma
Set of commands are included in
source program before compilation
with respect to the
condition.
#undef is used to undefine a
defined macro variable. #pragma
is used to call a function before
and after main function in a C
program.
Example
#include <stdio.h>
#define r 4
#define pi 3.14
#define unit m
void main(){
int c;
c=2*pi*r;
printf(Circumference= %d %c,c,unit);
getch();
}
Output:
Circumference =25.12 m
Program:
#include <stdio.h>
#define radius 10
int main()
{
#ifdef radius
printf(radius is defined");
#else
printf(radius is not defined");
#endif
return 0;
}
Output:
radius is defined.
Program:
#include <stdio.h>
#define radius 10
int main()
{
#ifndef circle
{
printf(circle is not defined");
}
#else
printf(cicle is already defined in the program);
#endif
return 0;
}
Output:
circle is not defined.
Program:
#include <stdio.h>
#define a 10
int main()
{
#if (a==10)
printf(a is added and is equal to 10");
#else
printf(a is added and not equal to 10");
#endif
return 0;
}
Output:
a is added and is equal to 10.
Program:
#include <stdio.h>
#define a 10
void main()
{
printf("First defined value for a: %d",a);
#undef height
#define a 60
printf("value of a after undef & redefine: %d",a);
}
Output:
First defined value for a: 10
Value of a after undef & redefine: 60
#include <stdio.h>
void fun1( );
void fun2( );
#pragma startup fun1
#pragma exit fun2
int main( ) {
printf ( "Now we are in main function" ) ;
return 0; }
void fun1( ) {
printf("Function1 is called before main function call") ; }
void fun2( ) {
printf ( "Function2 is called just before end of main function" ) ; }
Output:
Function1 is called before main function call
Now we are in main function
Function2 is called just before end of main function
Operators
An operator is a symbol that tells the compiler to perform specific mathematical or logical functions.
i.
Arithmetic operators
ii.
Relational operators
iii. Logical operators
iv.
Bitwise operators
v.
Assignment operators
vi. Misc operators
Type
Operators
Description
Arithmetic
+
*
/
%
Addition
Subtraction
Multiplication
Division
Modulus
Relational
>
>=
<
<=
==
!=
Greater than
Greater than or equal to
Less than
Less than or equal to
Equal to
Not equal to
Logical
&&
||
!
Logical AND
Logical OR
Logical NOT
Assignment operators
=, +=, -=, *=, /=, %=
assignment
Type
Operators
Description
Bitwise operators
&
|
^
<<
>>
~
Bit wise AND
Bit wise OR
Bit wise XOR
Left shift
Right shift
Complement
Other operators
++
-size of
?:
Increment
Decrement
size of operator
Ternary(conditional operator)
Program:
#include<stdio.h>
void main()
{
int x=4;
int y=2;
Output:
printf(The sum is %d,x+y);
The sum is 6
printf(The difference is %d,x-y);
The difference is 2
printf(The product is %d,x*y);
The product is 8
printf(The quotient is %d,x/y);
The quotient is 2
printf(The rem is %d,x%y);
getch();
}
The rem is 0
Program:
#include<stdio.h>
void main()
{
int a=2;
int b=5;
if(a && b) { Output:
printf(Condition1 is true); } Condition1 is true
if(a || b) {
Condition2 is true
printf(Condition2 is true); } Condition is false
if(!(a && b) {
printf(Condition3 is true); }
else {
printf(Condition is false); }
getch(); }
Program:
#include<stdio.h>
void main()
{
int a=2;
int c;
Output:
c=a; printf(The value is %d, c);
The value is 2
c += a; printf(The 2nd value is %d, c);
The 2nd value is 4
c -= a; printf(The 3rd value is %d, c);
The 3rd value is 0
c *= a; printf(The 4th value is %d, c);
The 4th value is 4
c /= a; printf(The 5th value is %d, c);
The 5th value is 1
c %= a; printf(The 6th value is %d, c);
getch();
}
The 6th value is 0
Program:
#include<stdio.h>
void main()
{
int a=60;
int b=13;
Output:
printf(The binary AND = %d, a & b);
The binary AND =12
printf(The binary OR = %d, a| b);
The binary OR =61
printf(The complement = %d, ~a);
The complement = -61
printf(The left shift = %d, a << 2);
The left shift = 240
printf(The right shift = %d, a >> 2); The right shift = 15
printf(The binary XOR= %d, a^b);
getch();
}
The binary XOR = 49
Program:
#include<stdio.h>
void main()
{
int a =10;
int b= 5;
int c=++a + ++b;
printf(The value of c prefix = %d, c);
c=a++ + b++;
printf(The value of c postfix=%d,c);
getch();
}
Output:
The value of c prefix= 17
The value of c postfix=17
Program:
#include<stdio.h>
void main()
{
int a=10;
printf(The size of variable = %d, sizeof(a) );
(a == 10) ? printf(equal) : printf(not equal);
getch();
}
Output:
The size of variable = 2
equal
THANK YOU