Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Lecture 01
Contents
Introduction
C tokens
Types of token
C character set
Identifiers
Keywords
Standard Keywords
Declaration statement
An identifier can be declared by
making use of the declaration
statement.
It is used to introduce the name of an
identifier along with its data type to
the compiler before its use.
Example: 1) int variable=20;
2) int a=20,b=10;
Data types
Data types
They are classified into
1) Basic data types
i) Character char
ii) Integer int
iii) Single precision floating point float
iv) Double precision floating point
double
v) No value available void
Data types
2) Derived data types
i) Array type char[], int[] etc
ii) Pointer type char*,int* etc
iii) Function type int(int,int) , float(int)
etc
3) User defined data types
iv) Structure
v) Union
vi) Enumeration
Real constant
Real constants are often known as
floating point constants .
Many parameters or quantities are
defined not only in integers but also
in real numbers.
For example, length , height , prize
etc are measured in real numbers.
Example: 2.5, 3.14 etc
String constants
String constants
Two types:
1) Qualified constants: It is created by
using const qualifier. Ex: const
char a=A;
2) Symbolic constants: It is created
with the help of the define
preprocessor directive.
Ex: #define PI 3.14124
Variables
Types of Operators
Arithmetic operators
Relational operators
Logical operators
Bitwise operators
Assignment operators
Conditional operator
Miscellaneous operator
Structure of a C program
Control structures
if statement
Program
if else statement
Program
Program
switch statement
Program
goto statement
This statement doesnt require any
condition
This statement passes control
anywhere in the program ie.. control
is transferred to another part of the
program without testing any
condition.
Expression is : goto label
Program
#include<stdio.h> even:
#include<conio.h> printf(%d is even number);
#include<stdlib.h> return;
main()
odd:
{ printf(%d is odd number);
int x; return; }
printf(enter number:);
scanf(%d,&x);
if(x%2==0)
goto even;
else
goto odd;
break statement
This keyword allows the
programmers to terminate the loop
It skips from the loop or block in
which it is defined.
The control then automatically goes
to the first statement after the loop
or block
continue statement
This statement is exactly opposite to
break
It is used for continuing next iteration
of loop statements
Loops
for loop
Program
while loop
Program
do while loop
Program
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;
/* do loop execution */
do
{
printf("value of a: %d\n", a);
a = a + 1;
}while( a < 20 );
return 0;
}
Output:
value of a:
value of a:
value of a:
value of a:
value of a:
value of a:
value of a:
value of a:
value of a:
value of a:
10
11
12
13
14
15
16
17
18
19
Arrays
Program
Insertion program
Output
Deletion program
Output
Program
Handling of character
strings
Program
strcat()
Program
strcmp()
Program
strcpy()
Program
strlen()
Program
Program
Functions
Function definition
Also
known
as
implementation
It consists of two parts:
1) Header of the function
2) Body of the function
function
Header
General form is
[return_type]
function_name
([parameter_list])
The terms enclosed within the square
brackets are optional and might bot
be present in the header but function
name is mandatory.
No two parameter lists appears same
The header of the function is not
terminated with a semicolon
Body
It consists of set of statements
enclosed within braces.
It can have non-executable and
executable statements.
The return statement is used to
return the result of the computations
done in the called function and to
return the program control back to
the calling function.
Function invocation
Functions
Functions
Functions
Functions
with
with
with
with
no input-output
inputs and no output
inputs and one output
inputs and outputs
Pass by address/reference
void swap(int *, int*);
void main()
{
int a=10,b=20;
printf(before swapping are %d %d,a,b);
swap(&a,&b);
printf(after swapping are %d %d,a,b);
}
void swap(int *x, int *y)
{
*x=*x+*y;
*y=*x-*y;
*x=*x-*y;
printf(swap function values are %d %d ,*x,*y)
}
Recursion
Points about
functions(Cont)
Variables
Three basic places in a C program
where variables will be declared:
o Inside the function
o In the definition of function
parameters
o Or outside of all functions
These variables are called local
variables, formal parameters and
global variables respectively
Local variables:
Variables declared inside a function are
called local variables
This name derives from the fact that a
variable inside a function can be used only
inside that function
Global variables:
Three sections: preprocessor directives,
global variable section and functions
The variables that are declared in the global
variable section are called global variables
and it can be used anywhere in the program.
Storage classes
Auto
Program
Register
Program
Static
Program
Extern
Program
int v=10;
void main()
{
call1(); call2(); printf(V is %d,v);
}
call1()
{
printf(the call1() value is %d, v);
}
call2()
{
prinf(the call2() value is %d,v);
}
Structures
Accessing structures
element
Arrays of structures
Size of structures
Unions
Example 1
union variables
{
char a;
int b;
float c;
double d;
};
main()
{
union variables var;
printf(the size of unions is %d,sizeof(union variables));
printf(union variable var takes %d bytes,sizeof(var));
}
Example 2
main()
{
union variables var;
printf(starting addr
printf(starting addr
printf(starting addr
printf(starting addr
printf(starting addr
}
of
of
of
of
of
var is %p,&var);
1st member is %p,&var.a);
2nd member is %p,&var.b);
3rd member is %p,&var.c);
4th member is %p,&var.d);
Example 3
union variables
{
char a;
int b;
float c;
};
main()
{
union variables var={A,2,2.5};
printf(the values of the members are %c %d
%f,var.a,var.b,var.c);
}
Example 4
main()
{
union variables var={A};
printf(first member is %c , var.a);
var.b=300;
printf(first member now is %c,var.a);
printf(second member is %d,var.b);
var.a=A;
printf(first member now is %c,var.a);
printf(second member now is %d,var.b);
}
ASCII codes
Practical applications of
Unions
Calling DOS functions
Calling BIOS functions
Interrupt programming
Example 1
enum month
{
jan=1, feb,mar,apr, ., dec
};
main()
{
clrscr();
printf( jan= %d,jan);
printf(feb=%d,feb);
printf(dec=%d,dec);
}
Example 2
enum capital
{
mumbai, hyderabad, bangalore
};
struct state
{
char name[15];
enum capital c;
}s;
main() { strcpy(s.name,Andhra pradesh);
s.c= hyderabad; clrscr(); printf(state : %s,s.name); printf( capital :
%d,s.c);
If(s.c==hyderabad)
{ printf( hyderabad is capital of %s,s.name); }
Pointers
Program
Declaring pointers
Initializing pointers
Allocating a block of
memory
File management in C
fprintf function
fscanf function
Program