0% found this document useful (0 votes)
52 views16 pages

PST Unit 2 Notes

C language

Uploaded by

Abhinav Kk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views16 pages

PST Unit 2 Notes

C language

Uploaded by

Abhinav Kk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

PROBLEM SOLVING TECHNIQUES

UNIT-2

1)Define variable.Give an example.


•Variables are the name given to the memory location to hold a value within the
program .
•A variable change during the execution of a program.
Syntax:<datatype> variablelist;
Ex : int a,b;
float x,y;
a=10;

2.Define constants. Give an example.


constants are the fixed values of a variable. They do not change during the execution
of a program .
Ex : Integer: int max=100;
Real:float pi= 3.142;

3.Define c-tokens with an example.


c-tokens are the smallest and the basic elements of a c programming. C tokens includes
identifiers variables, keywords, operators, special symbols, strings.
Ex: Keywords: int, float,char etc.,
Variables: int a,b;

4.Explain formatted input and output with an example.


Formatted input :-
scanf( ):- It is a statement used to accept different types of values.
Syntax:
scanf(<format specifier>list of variables)
Ex:
int a;
float b;
char c;
scanf(“%d %f %c”, &a,&b,&c);

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

5.Explain the use of break and continue.


Break:
The break keyword is used to terminate any of the looping constructs.
This keyword is always used in the connection with „if‟ within a looping construct.

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;
………………..
………………..
}

6.Explain goto statements with examples.?


It is an unconditional branching statement where the control of the program transfers
from one part of the program to another directly.
Syntax:
go to<label name>
Ex:
{
if(a%2==0) go to even;
………………
………………..
………………..
Yogesha S N
H.O.D of BCA
Vasavi Jnana Peetha First Grade College Page 2
even: printf(“\n Even number”);
go to stop;
}

7.Define pointer with an example.


A pointer is a variable which holds the address of another variable.
Ex:
Int num=10;
Int *iptr;
iptr = &num;

8.How to initialize an array of pointer.


Array of pointer is also known as table of strings can be initialized as :
char *state_name[3]= { “Karnataka”,“Tamil Nadu”,“Kerala” };

9.Define array with example.


Array is a bounded collection of elements of same datatypes.
Ex: int Regno[5]= {10,20,30,40,50};
Regno[0] Regno[1] Regno[2] Regno[3] Regno[4]
10 20 30 40 50

10.What are command line argument?


When a parameter is supplied to a program during the execution then such as
parameter is called command line argument.
Syntax:
main(int argc, char*argv[ ] )
Ex: copy a file from FILE_A to FILE_B
(name of the program COPYPRG.c)
argc -3
argv[0]-> COPYPRG.c
argv[1]-> FILE_A
argv[2]-> FILE_B

Yogesha S N
H.O.D of BCA
Vasavi Jnana Peetha First Grade College Page 3
11).Explain the structure of c program?

i)Documentation Section: This section consists of the description of the program.


It is specified at the start of the program in the form of comments.
Comments: Comments are not executed.
2 types
a)Single line comment: ex:sum=a+b; //formula
b)multiline comment:
ex:
/* write a C++ program to find
Sum of two numbers */
ii)Pre-processor section
In a C program, the statements which are starting with the “#” symbol are called
preprocessor directives.
Ex: #include<stdio.h>
iii)Definition section:
The definition section defines all symbolic constants.
Ex: #define pi=3.142

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

12).Explain if and else if structures

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?

 It is a multiple way branching statement similar to else if ladder


where one of the case statement block will be executed depending on the
expression.
 If there are more than two alternatives to be selected, multiple selection
construct is used.

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?

18)Explain different data types supported by C?

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

 Here, Condition 1 is tested. If it is TRUE, Statement 1 is executed control


transferred out of the structure. Otherwise, Condition 2 is tested. If it is TRUE,
Statement 2 is executed control is transferred out of the structure and so on.
 If none of the condition is satisfied, a statement called default statement is executed.
Example:
if( marks > = 85 )
PRINT “Distinction”
else
if( marks > = 60 )
PRINT “First Class”
else
if( marks > = 50 )
PRINT “Second Class”
else
if( marks > = 35 )
PRINT “Pass”
else
PRINT “FAIL”

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

21)What is formal arguments and actual arguments with an examples?


formal arguments is a variable declared in a function heading.
actual arguments is a variable listed in a call to a function.

22)What is local variable and global variable with an examples?


Local variable is a variable declared with in the main( ).
Example:
main( )
{
int x, y; //x and y are local variables;
…………;
}

Global variable is a variable declared outside the main( ).


Example:

int a, b; // a and b are global variables


void main( )
{
int p, q; // p and q are local variables
………….
}

23) How to declare a pointer?(or Syntax or General form)


The general form is,
datatype *variablename;
Where,
datatype is any valid data type such as int,float,char,double.
* indicates pointer variable.
variablename is the name of pointer variable.

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:

int num = 25;


int *iptr;
iptr = &num; // Address operator &

25) What is Pointer operator?Give an example?

* is a unary operator that returns the value at that address of its operand.

int num = 25;


int *iptr; // Pointer operator *
iptr = &num;
26) What is Call by value?
Argument copy to the function so that original value can‟t be change.

27) What is Call by address?


Argument address to the function so that original value can be change.
28) Explain Array of pointers?
An array of pointers means that it is a collection of addresses.

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

You might also like