Programming C
Programming C
Home work:
Q. WAP to accept any number then display
even or odd.
Q. WAP to input marks of any subject then
display pass or fail.
Q. WAP to input a year then display the year
is leap year or not.
Q. WAP to find whether the given number is
divisible by 5 or not.
Q. WAP to identify any number entered
through keyboard then display whether it lies
between 200 and 300 or not.
Ternary operator (? : )
Ternary operator is also known as
conditional operator as it checks
condition to make decision. This is the
only operator used in C that takes
three operands so it is named as
ternary operator.
Syntax:
Condition? Statement 1: statement 2;
B] Looping / Iteration:
Looping is the process of executing the same
program statement or block of program statement
repeatedly for specified number of times.
Mainly there are three types of loop use in C
programming. They are:
i. For loop
ii. While loop
iii. Do- while loop
i. for loop:
It is the most common type of loop which
is used to execute program statement or
block of program statement repeatedly
for specified number of times. It consists
of three expression i.e. initialization,
condition and counter (increment /
decrement).
Syntax:
for(initialization;condition;counter)
{
Statement/ block of statements;
}
5!=5*4*3*2*1
1*2*3*4*5
1 4 9 ……upto
nth term
ii. while loop: It executes the program
statement repeatedly until the given
condition is true. It is also known as entry
control or pre-test loop.
Syntax:
Initialization;
While(condition)
{
Statements;
…………………
…………………
Counter;
}
Syntax:
Initialization;
Do
{
Statements;
……………….
……………….
Counter;
} while(condition);
Features of array:
i. The entire array elements share the common
name.
ii. The elements of array are stored in contiguous
(closest / nearby) memory location.
iii. We put the array size of the fixed length as
required that means once the size is declared
then the size will be fixed at the execution time
of the program, so called static type.
Types of array:
i. One dimensional array
ii. Two /multi-dimensional array
other example:
float[3]={1.5,2.5,7.3};
char n[10]={‘n’,’e’,’p’,’a’,’l’};
ii. Multi/Two-dimensional array: Multi-
dimensional array is defined as the same as one
dimensional array except that it consists of
more than one pair of square bracket that is
subscript(index).
Syntax:
Data_type array_name[row_size][column_size];
Example:
int a[3][3];
In this example, a multi dimensional array of integer
data type consisting of three rows and three
columns.
[Note: The total number of elements in an array is
given by calculating: row_size*column_size
For e.g. int a[4][5];
The total number of elements in this array is
4*5=20.]
Initialization of multi dimensional array:
In multi dimensional array (suppose 3x3 matrix), data
items are stored in memory as below:
Column 0 column 1 column 2
[0][0] [0][1] [0][2]
Row0 100 200 300
[1][0] [1][1] [1][2]
Row1 200 150 50
[2][0] [2][1] [2][2]
Row2 350 500 600
This means
int matrix[3][3]={100,200,300,200,150,50,350,500,600};
i.e.
matrix[0][0]=100;
matrix[0][1]=200;
matrix[0][2]=300;
matrix[1][0]=200;
matrix[1][1]=150;
matrix[1][2]=50;
matrix[2][0]=350;
matrix[2][1]=500;
matrix[2][2]=600;
Q. Differences between one dimensional and multi-
dimensional array
One dimensional array Multi dimensional array
1. It consists of only 1. It consists of two
one subscript. subscript.
2. Maximum size will 2. Maximum size will be
be the size of the the product of row and
array which we column size of an array
defined in the which we defined in the
program. program.
3. It stores data 3. It stores data in matrix
either row wise or form i.e. row wise and
column wise. column wise.
4. Syntax : 4. Data_type array_name[row][column];
Data_type array_name[array_size];
5. E.g. int n[5]; 5. E.g. int n[2][3];
String
A string is an array of characters. It
contains characters, symbols,
numbers etc.
Syntax:
Char string_name[string_size];
E.g.
Char name[10];
Char address[15];
Initialization of string:
Char name[10]={‘c’,’ ‘,’p’,’r’,’o’,’g’,’r’,’a’,’m’,’\0’};
Or
Char name[10]=”c program”;
Name[0] name[1] name[2] name[3] name[4]
C P R O
Array of string:
A two dimensional array of character is called
array of string.
Syntax:
Char string_name[number of string][length of string];
E.g.
Char name[5][10];
Advantages of function:
i. Function uses code reusability.
ii. Program development will be faster.
iii. Program debugging will be easier.
iv. Larger number of programmer can be involved.
Component of function:
a.Function prototype
b.Function definition
c. Call the function or function call
a.Function prototype: It provides the following
information to the computer:
1.The type of the value returned.
2.The name of the function
3.The number and the type of the argument that
must be supplied in a function call.
Syntax:
Return_value function_name(arg1, arg2,…..argn);
Example:
int sum(int a, int b);
They are generally written at the beginning of a
program, ahead of the main ( ). It is also called
function declaration.
Example:
b=value(a);
or
value(a);
Types of function:
Two types of function
a.Library function(Built-in Function)
b.User defined function
Library function User defined function
1.Library functions are 1.User defined
pre defined function. functions are
function which are
created by the
programmer
according to need.
2.Library function 2.User defined
required header files function requires
to use it. function prototype to
use it.
3.Program 3.Program
development time is development time is
faster. slower than library
function.
4.It is called at run 4.It is called at
time. compiled time.
5.Example: printf( ), 5.Example: day( ),
strlen( ), scanf( ), simple( ), sum( ) etc.
clrscr( ) etc.
Syntax:
Struct tag_name
{
Data_type member1;
Data_type member2;
………………………………..
Data_type membern;
};
Struct tag_name v1,v2,………,vn;
Or v1,v2,…….vn;
Example:
Struct student
{
Int roll;
Char fname[50];
Char lname[40];
};
Struct student s1,s2,s3;
Or
S1,s2,s3;
Structure initialization:
Struct student
{
Int roll;
Char fname[50];
Char lname[40];
};
Struct student s1={1,”ram”,”Shrestha”};
Struct student s2={2,”Sita”,”Chaudhary”};
struct student s3={3,”Hari”,”Pandey”};
Array of structure:
It is possible to declare structure as an array
called array of structure. It is also known as the
collection of structure variable having same set of
members.
Example:
Struct student
{
Int roll;
Char fname[20];
Char lname[30];
};
Struct student s[5];
Roll: 2byte
Fname:30 byte
Lname: 30 byte
Total : 62 byte
Introduction to Union
Union is similar to structure but it differ only in its storage
location. The “union” keyword is used to define union.
Syntax:
union tag_name
{
Data_type member1;
Data_type member2;
………………………………….
Data_type membern;
};
Union tag_name v1,v2,….vn;
Example:
union student
{
int roll;
char fname[50];
char lname[40];
};
union student s;
Q. Differences between structure and union
Structure Union
1.It is the collection of data 1.Same as structure but
items having dissimilar it differ in its storage
data types. class.
2.Multiple members can be 2.Only one member can
simultaneously accessed be accessed at a time.
at a time.
3.The “struct” keyword is 3.The “union” keyword
used to define structure. is used to define
union.
4.Syntax: 4.Syntax:
Struct tag_name union tag_name
{ {
Data_type member1; Data_type
Data_type member2; member1;
……………………………….. Data_type
Data_type membern; member2;
}; ………………………………..
Struct tag_name v1,v2,………,vn; Data_type
membern;
};
union tag_name
v1,v2,………,vn;
5.Example: 5.Example:
struct student union student
{ {
int roll;
char fname[50]; int roll;
char lname[40]; char fname[50];
};
struct student s; char lname[40];
};
union student s;
Pointer
A pointer is a special type of variable that
represent memory address of a variable rather
than its value.
5
2000
Features of pointer:
a.Pointer saves memory space.
b.It assigns dynamic memory allocation.
c. The execution time is faster because data
manipulation is done directly inside memory.
d.Pointers are very efficient for solving array and
string related problems.
In other example
int a=5;
a++; // means 5+1=6
a--; // means 5-1=4
Uses of pointer:
a.Int *p; Ans: *p is declared as pointer variable
which would point to address of int data type of
value.
b.P=&a; Ans: p is initialized with the address of int
variable a.
c. Printf(“\n Address of a=%u”,&a);
Ans: &a(“address of” operator) is printing the
address of int variable a. %u is used to print
address in unsigned integer.
d.Printf(“\n Address of p=%u”,&p);
Ans : &p prints the address of pointer variable p.
e.Printf(“\n value of p=%d”,*p);
Ans : *p(“value at address” operator) prints the
value of address of pointer variable p.
Read: (getc(),getw(),fscanf(),fread())
Write:(putc(),putw(),fprintf(),fwrite())
Defining and opening data file:
Syntax:
FILE *fp;
fp=fopen(“file name”,”file mode”);
E.g.
fp=fopen(“data.txt”,”w”);
File Mode:
r = open an existing file for reading
w = open a new file for writing
a = open an existing file for appending
r+= open an existing file for both reading and writing
w+ = open a new file for both reading and writing
a+= open an existing file for both reading and writing.
Syntax:
n=ftell(fp);
rewind(): It takes file pointer as parameter and reset
the position to the start of the file.
Syntax:
rewind(fp);