PST Unit 2 Notes
PST Unit 2 Notes
UNIT-2
Formatted output :-
printf( ) :- It is a statement used to print the values using field-width specification and
escape sequence so the output looks neat and presentable.
Syntax:
printf(<format specifier>list of variables)
Yogesha S N
H.O.D of BCA
Vasavi Jnana Peetha First Grade College Page 1
Ex:
int a;
float b; char c;
scanf(“%d %f %c”, &a,&b,&c);
printf(“%d %f %c”, a,b,c);
Syntax:
while(condition)
{
…………….. if(condition) break;
………………..
………………..
}
Continue:-
When the continue is encountered, it transfers the control back to the loop condition
by skipping the rest of the statements of the body of the loop. The continue keyword
only works with loops.
Syntax:
while(condition)
{
……………..
if(condition) continue;
………………..
………………..
}
Yogesha S N
H.O.D of BCA
Vasavi Jnana Peetha First Grade College Page 3
11).Explain the structure of c program?
Yogesha S N
H.O.D of BCA
Vasavi Jnana Peetha First Grade College Page 4
iv)Global declaration section: variables that are used in more than one function.It
declared in the global declaration section that is outside of all the functions. This
section also declares all the user-defined functions.
v)Braces { }
{ is a begining execution of a program.
} is a ending execution of a program.
vi)main() Function Section:
Every C program must have one main function section. This section contains two
parts; the declaration part and the executable part
1.Declaration part: The declaration part declares all the variables used in the
executable part.
2.Executable part: There is at least one statement in the executable part.
vii)Subprogram Section:
The subprogram section contains all the user-defined functions that are called in the
main ( ) function.
Example: /* write a c++ program to find sum of two numbers */
#include<iostream.h>
#include<conio.h>
void main( )
{
int a,b,sum;
clrscr( );
printf(“enter two numbers\n”);
scanf(“%d%d”,&a,&b);
sum=a+b;
printf(“sum of two numbers is %d”,sum);
getch( );
}
Output
Enter two numbers
2 3
Sum of two number is 5
i)IF STATEMENT:
It is a one way branching statement.
Here, the test condition is tested which results in either a TRUE
or FALSE value. If the result of the test condition is TRUE then the
Statement 1 is executed.
If the condition returns false then the statements inside “if” are
skipped.
Yogesha S N
H.O.D of BCA
Vasavi Jnana Peetha First Grade College Page 5
Syntax:
If(condition)
{
Statements
}
Ex: if( amount > = 5000 )
discount = amount * (10/100);
net-amount = amount – discount;
ii)ELSE IF STATEMENT:
It is a two way branching statement.
Here, the test condition is tested. If the test-condition is TRUE,
statement-1 is executed. Otherwise Statement 2 is executed.
An else if statement is a conditional statement that executes
sequence of statements if condition is false.
Syntax:
If(condition)
{
Statement (true block)
}
else
{
Statement (false block)
}
Ex: if(a>b)
{
BIG=a;
printf("%d", big);
}
else
{
BIG=b;
printf("%d", big);
}
Yogesha S N
H.O.D of BCA
Vasavi Jnana Peetha First Grade College Page 6
13)Explain different types of looping/iteration in C?
Looping or iteration is a execution of a statement again and again.
Yogesha S N
H.O.D of BCA
Vasavi Jnana Peetha First Grade College Page 7
14)Explain switch case with syntax and example?
Syntax: switch(expression)
{
case label-1; statement-1
………………………
break;
case label-2; statement-2
………………………
break;
case label-2; statement-2
………………………
break; default:
default statement;
}
Ex: switch(dayno)
{
case 1: printf(“Monday”);
break;
case 2:
printf(“Tuesday”);
break;
case 3:
printf(“Wednesday”);
break;
default:printf(“Invalidnumber”);
}
Yogesha S N
H.O.D of BCA
Vasavi Jnana Peetha First Grade College Page 8
15)What are functions. Explain syntax and example.
A function is a named group of statements developed to solve a sub-problem and
returns a value to other functions when it is called.
Syntax
Return_type_specifier function_name(arguments list)
{
Local-variable declaration;
Executable statement-1;
Executable statement-2;
------------ Body of the function
Executable statement-n;
Return(expression);
}
Where ,
Return type-specifiers are data types like char, int, float or void .
Function-name: is the name of the function
Argument-list with declaration: is the list of arguments or parameters or variables with
their declaration. Each argument should be declared separately. Each declaration should
be separated by comma. The list should be enclosed within parenthesis.
Local-variable declaration: is the declaration of the variables that are used within the
function.
Executable statements: are the statements that perform the necessary operations to
solve the problem.
Local declaration and executable statements are together called as body of the function
Example:
int add (int x, int y)
{
int c;
c=x+y;
return (sum);
}
Yogesha S N
H.O.D of BCA
Vasavi Jnana Peetha First Grade College Page 9
16)Explain different categories of user define functions.
Yogesha S N
H.O.D of BCA
Vasavi Jnana Peetha First Grade College Page 10
17)Explain call by value and call by reference?
Yogesha S N
H.O.D of BCA
Vasavi Jnana Peetha First Grade College Page 11
19)Explain if – else - if statement or Nested if statement?
This structure helps the programmer to decide the execution of a statement from
multiple statements based on a condition.
There will be more than one condition to test
This statement is also called as multiple-way branch.
The general form of if – else – if
statement is:
if (Test Condition 1)
Statement 1;
else
if (Test Condition 2)
Statement 2;
else
………..
else
if( test Condition N)
Statement N;
else
Default Statement
Yogesha S N
H.O.D of BCA
Vasavi Jnana Peetha First Grade College Page 12
20)What is function prototype?Write a syntax with an example
A function prototype is a declaration of the function that tells the program about
the type of the value return by the function and the number and type of
arguments.
Syntax:
return_type_specifier function-name ( type arg1, type arg2….)
example:
int biggest(int x, int y, int z);
Yogesha S N
H.O.D of BCA
Vasavi Jnana Peetha First Grade College Page 13
example: int *iptr;
float *fptr;
char *cptr;
24) What is address operator?Give an example?
& is a unary operator that returns the memory address of its operand.
Example:
* is a unary operator that returns the value at that address of its operand.
Example:
int *iptr[3];
int i=10, j=20, k=30
iptr[0] = &i; *iptr[0] = 10;
iptr[1] = &j; *iptr[1] = 20;
iptr[2] = &k; *iptr[2] = 30;
29).Define Arrays.Give an example.
Definition: It is a homogeneous data type.It means same name and same
data type.
Ex:int a[3];
int a[0];
Yogesha S N
H.O.D of BCA
Vasavi Jnana Peetha First Grade College Page 14
int a[1];
int a[2];
30).What is subscript?what are the significance of subscript?
Definition: It is a position of each element in an array.
Significance:
All are positive integers
Cannot be used negative sign
Ranges from 0 to n-1
31).Explain different types of arrays?
Types:
a)One dimensional array(1-D):It is a only one subscript.
syntax: datatype arrayname[size];
where,data type is a valid data type such as int,float,char,double.
Array name is a any name of array.
Size is a value from 0 to N-1.
example: int a[4];
b) Two dimensional array(2-D):It is a two subscripts.
syntax: datatype arrayname[rowsize][columnsize];
where,data type is a valid data type such as int,float,char,double.
Array name is a any name of array.
rowsize represent size1,columnsize represent size2
example: int a[2][3];
c) Multi dimensional array(Multi-D):It is a more than two subscripts.
syntax: datatype arrayname[S1] [S2] [S3]-----[Sn];
where,data type is a valid data type such as int,float,char,double.
Array name is a any name of array.
Yogesha S N
H.O.D of BCA
Vasavi Jnana Peetha First Grade College Page 15
[S1] [S2] [S3]-----[Sn] are n number of Sizes.
example: int a[2][4][6][8];
32).Explain 1-d array initialization?
Ex 1: int a[4]={ 10,20,30,40};
In above declaration,
A[0] represent 10
A[1] represent 20
A[2] represent 30
A[3] represent 40
Ex 2: int a[4]={ 10,20};
In above declaration,
A[0] represent 10
A[1] represent 20
A[2] represent 0
A[3] represent 0
33). Explain 2-d array initialization?
Ex 1: int a[2][2]={ 10,20,30,40};
In above declaration,
A[0][0] represent 10 A[0] [1] represent 20
A[1] [0] represent 30 A[1] [1] represent 40
Ex 2: int a[2][3]={ 10,20}
In above declaration,
A[0][0] represent 10 A[0] [1] represent 20 A[0] [2] represent 0
A[1] [0] represent 0 A[1] [1] represent 0 A[1] [2] represent 0
Yogesha S N
H.O.D of BCA
Vasavi Jnana Peetha First Grade College Page 16