Sei sulla pagina 1di 156

C programming

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

Declaration and definition


Declaration only introduces the name
of an identifier along with its type to
the compiler before it is used and no
memory space is allocated to the
identifier.
Definition of an identifier means the
declaration of an identifier plus
reservation of space for it in the
memory.

Constants and Variables

Rules for constructing integer


constants

Rules for constructing character


constants

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

Rules for constructing variable


names

Operators & Expressions

Types of Operators

Arithmetic operators

Relational operators

Logical operators

Bitwise operators

Increment & Decrement


operators

Assignment operators

Conditional operator

Miscellaneous operator

Function call operator ()


Array subscript operator []
Member select operator i) . ii) ->
Indirection operator *
Conditional operator
Comma operator ,
Sizeof operator
Addressof operator &

Structure of a C program

Rules to write a C program

printf(): writing output


function

scanf(): getting user input

Control structures

Conditional & Loop


structures

if statement

Program

if else statement

Program

Nested if else statement

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

Difference between break and


continue
Break:
1) Exits from current block or loop
2) Control passes to next statement
3) Terminates the program
Continue:
1) Loop takes next iteration
2) Control passes at the beginning of loop
3) Never terminates the program

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

Single dimensional array

Program

Insertion program

Output

Deletion program

Output

Two dimensional array

Program

Handling of character
strings

Declaring and initializing string


variables

Reading and writing strings

Program

String handling functions

strcat()

Program

strcmp()

Program

strcpy()

Program

strlen()

Program

strupr(), strlwr(), strrev()

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

Function with no input and output

Function with input and no


output

Function with input and one output

Functions with inputs and


outputs
More than one value can be indirectly
returned to the calling function by
making the use of pointers.
It is of two types:
1) Pass by value
2) Pass by address

Pass arguments by value


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)
}

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

Points about
functions(Cont)

Functions with arrays

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

Arrays within structures

Structures within structure

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

Enumerated data type


Keyword is enum.
The programmer can create his/her
own data type and define what
values the variables of these data
type can hold.
Example: enum month {jan,feb,
..,dec};
Here, identifier jan refers to 0, feb
refers to 1 and so on.

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

Accessing the address of a


variable

Program

Declaring pointers

Initializing pointers

Accessing a variable through its


pointer

Pointers & Arrays

Dynamic memory allocation

Memory allocation functions

Allocating a block of
memory

Allocating multiple blocks of


memory

Releasing the used space

Altering the size of a block

Pointers and character


strings

Pointers and functions

Pointers and structures

File management in C

High level I/O functions

Defining and opening a file

To close a file : fclose(file_pointer);

I/O operations on files

Writing to & Reading from a


file

fprintf function

fscanf function

Command line arguments

How do these parameters get into


these program

Program

Thank you !!!

Potrebbero piacerti anche