0% found this document useful (0 votes)
74 views47 pages

CPF Faq Eng

Uploaded by

ghanshyamp873
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)
74 views47 pages

CPF Faq Eng

Uploaded by

ghanshyamp873
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/ 47

APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY

Frequently Asked Questions(FAQ)


Computer Programming Fundamentals (DI01000131)

Q.1 What is a flowchart? Limitation of flowchart.


⮚ Flowchart is graphical or diagrammatical representation of sequence of any problem
to be solved by computer programming language.
⮚ Flowchart is a diagrammatic representation of an algorithm.
Limitation
⮚ Complex logic: Sometimes, the program logic is quite complicated. In that case,flowchart
becomes complex and clumsy.
⮚ Alterations and Modifications: If alterations are required the flowchart mayrequire
re- drawing completely.
⮚ Reproduction: As the flowchart symbols cannot be typed, reproduction
offlowchart becomes a problem.
⮚ The essentials of what is done can easily be lost in the technical details of how itis done.
⮚ It is the total perception of the designer who draw the flowchart.
⮚ It's working differ from one compiler to another ,one language to another in sometime.
⮚ It need well-defined requirements.

Q.2 Define Algorithm with example.


⮚ An algorithm is a finite sequence of well defined steps or operations for solving aproblem
in systematic manner.
⮚ These are rules for solving any problems in proper manner.
⮚ Instructions are written in the natural language.
⮚ It is also called step by step solution.
Example: Write an algorithm to find area of circle.
Step 1: Input R
Step 2: Compute A = 3.14 * R * RStep
Step 3: Print A
Step 4: Stop
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

Q.3 List the Feature of C language.

⮚ C is portable.: ‘C’ Program can be run on any hardware.


⮚ C does not support input/output statements.: ‘C’ does not support
input/outputstatement like input and print, but support library functions printf( ) and scanf(
).
⮚ C supports bitwise operators.: The C support bitwise operator like AND, OR, NOT etc.
normally these operations do not supported by higher level languages..
⮚ C is modular language.: The program written in from of functions. A C programis group of
one or more functions. Dividing the program into small functions makes it to easy develop
and maintain the programs. Each function is easy to understand as it is small. The method of
dividing a program into small functions is called modularity.

Q.4 List out C Token.


⮚ The smallest individual unit in a C program is known as C token.
⮚ C has six types of token as given below:
1. Keywords (float,while)
2. Identifier(price, amount)
3. Constants(-15.2, 10)
4. String(“abc”, “hello”)
5. Special Symbols ([ , ] , *, ! etc)
6. Operators (+,-, *,/)

Q.5 Explain keyword.


⮚ Keywords are those words whose meaning is predefined.
⮚ The programmer cannot change the meaning of keywords.
⮚ There are 32 keywords available in c.
⮚ All the keywords must be written in lowercase.
⮚ Following is the list of keywords:

Int char float double


Long short signed unsigned
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)
If else do while
For break continue switch
Case default consulting goto
Struct union void return
Static extern volatile register
Auto enum sizeof typedef
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

Q.6 Explain identifier.


⮚ Identifier is are the words which are defined by the programmer in a program.
⮚ Identifier refers to the name of variable, array, function etc.
⮚ It consists of letters, digits and underscore.
⮚ First character must be an alphabet or underscore.
⮚ Keywords cannot be use as identifier.
⮚ Identifier name are case sensitive.
⮚ The name of identifier should be meaningful.

Q.7 Write the Basic Structure of C Program.


⮚ Documentation section:
● Documentation consists of a set of comment line(/* */) giving the name of
the program.
⮚ Header File(Link Section):
● Link section provides instruction to the compiler to link function from the system
library(Header Files like #include<stdio.h>).

⮚ Constant Declaration:
● Definition section defines all symbolic constants(e.g.-#define pi 3.14).
⮚ Global declaration section:
● There are some variable that are used more than one function. Such Variables arecalled
global variable and that is outside all the function. This section also declares
the entire user define functions.
⮚ Main () function section:
⮚ Every c program must have one main () function section. This section
contains two parts. Declaration part and executable part. Declarationpart declares all
the variables used in the executable part. These twoparts must be appearing between
opening and closing braces.

Q.8 List out Advantages of ‘C’ language.


⮚ C language contains rich set of built in functions.
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

⮚ ‘C’ operators can be used to write any complex program.


⮚ ‘C’ is a middle level language, which combines the capabilities of an assemblylanguage
and higher level language.
⮚ Programs written in c are efficient and fast, because it contains variety of datatypes
and powerful operators.

⮚ ‘C’ is highly portable, means c program written for one computer can be run onanother
with little or no modification.
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

Q.9 Explain constant in detail.


⮚ Constant means fixed value.
⮚ The value of the constant cannot change during execution of the program.
⮚ C supports following types of constant:
1. Integer
2. Real
3. Character
4. String
1. Integer constant:
⮚ Integer constant is a sequence of digits.
⮚ Integer constant can be +ve or –ve number.

⮚ Special symbols such as space, comma, currency etc are not allowed.

⮚ There are three types of integer.


Decimal: A decimal integer constant consist of any combination of digits from 0to
9.Example 123,-345,001.
Octal: An octal integer constant consists of any combination of digits from 0 to 7with a leading
0.Example:01234, 0564.
2. Real constant:
Real constant is a sequence of digits with decimal points. Such aconstant are used to represent
weight, height etc.
3. Character constant:
⮚ Character constant is a single character enclosed in a singlequotation mark.
⮚ Example: ‘a’

⮚ Back slash constants are the special type of character constants which actuallyconsists
of two characters. These are known as escape sequences.
⮚ The combination of backslash and a character is known as escape sequence.
⮚ Escape sequence start with back slash ‘\’ character.
Escape Sequence Meaning
‘\0’ End of string
‘\n’ End of line
‘\t’ Horizontal tab
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)
4. String Constant:
String constant is a sequence enclosed in a double quotationmark.
Example: ‘’Hello’’
Constant can be declared in a program using count keyword.
For Example: const PIE = 3.14;
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

Q.10 Define Variable & explain its rules.


⮚ A variable is a name which is used to store a temporary value.
⮚ The value of the variable may changes during execution of the program
Rules for naming variable:
⮚ The variable name may consist of letters (A-Z, a-z), digits (0-9) and underscoresymbol ( _ ).
● Example: int no_1; / int no1; / int NO1;

⮚ The variable name must begin with a letter. Some system permit underscore as afirst character.
● Example: int 1NO; is not valid
⮚ The length should not be more than eight characters.
⮚ Variable name should not be a keyword.
● Example: int case is not valid
⮚ A white space is not allowed in the name of variable.
● Example: int NO 1; is not valid.
⮚ Variable is case sensitive. Uppercase and lowercase are different.
⮚ Example: int MAX; int max;
● Here both MAX and max are treated as different variable.

Q.11 Define Data Type and their types.

❖ Data type is used to specify which type of value to be stored in variable.


❖ There are four basic data types supported by C.

Type Size(Bytes) Size(Bits) Range Control String

char 1 8 -128 to +128 %c

int 2 16 -32768 to +32767 %d


float 4 32 3.4 e -38 to 3.4 e +38 %f
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)
double 8 64 1.7 e -308 to 1.7 e +308 %ld
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

Q.12 Define symbolic constants & list out Rules of defining symbolic constant.
⮚ #define is a preprocessor directive which is used to defined symbolic constant.

⮚ Symbolic name is a name of the constant, which we want to define.

⮚ Value of the constant represents the value, which we want to assign to theconstant.
Rules of defining symbolic constant
1. The rules for naming symbolic constant are same as the rules for naming variables.
Symbolic names are written in capital letters to distinguish them fromthe normal
variable.
2. There should be no blank space between the pound sign ‘#’ and the worddefines.
3. ‘#’ must be the first character in the line.
4. A blank space is required between #define and symbolic name and between
thesymbolic name and the value.
5. #define statement must not end with a semicolon.
6. After defining the symbolic constant they should not be assigned any other
value in the program.
7. There is no need to declare data type for the symbolic constant.

Q.13 Define Operator. & explain Arithmetic Operators with example.


❖ An operator is a some special characters (symbol) that tells the computer
toperform certain mathematical or logical manipulations.
Arithmetic Operators
Operator Meaning

+ Addition

- Subtraction

* Multiplication

/ Division
% modulo
Ex: a+b, a-b, a*b, a%b, a/b
where a & b variables & known as Operand (variable).
Example:
int a,b,sum,sub,div,mul,mod;a
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)
= 15;
b = 3;
sum = a+b; // sum = 18
sub = a-b; // sub = 12
mul = a*b; // mul = 45
div = a / b; // div = 5
mod = a%b; // mod = 0
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

Q.14 Explain Relational Operator with example.


⮚ Compare two quantities & depending on their relation.

Operator Meaning
< Is less than
<= Is less than or equal to
> Is greater than

>= Is greater than or equal to

== Is equal to

!= Is not equal to

⮚ Relational operators are used in decision statements.


⮚ Such as while & if to decide the course of action of a running program.

⮚ Example:
int a = 15, b = 12;

Expression Result
a>b True
a<b False
a>=b True
a<=b False

a==b False

a!=b True
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

Q.15 Explain Increment & Decrement Operators in detail.


⮚ Increment Operator ++
⮚ Decrement operator --

⮚ The increment operator ++ adds 1 to the operand, while decrement operator --subtracts 1.

⮚ We use the increment and decrement statements in for and while loops.
⮚ A prefix operator first adds 1 to the operand and then the result is assigned to thevariable
on the left.
⮚ A postfix operator first assigns the value to the variable on the left and thenincrement
the operand.
⮚ Postfix Increment Operator

⮚ Example:
int a,b;
a=10;
b = a++;
printf(“a = %d”,a);
printf(“b = %d”,b);

Output:
a=11
b=10
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

⮚ Prefix Increment Operator


Example:
int a,b;
a=10;
b = ++a;
printf(“a = %d”,a);
printf(“b = %d”,b);
Output:
a=11
b=11

⮚ Postfix Decrement Operator


Example:
i
n
t
a
,
b
;
a
=
1
0
;
b = a--;
printf(“a = %d”,a);
printf(“b = %d”,b);

Output:
a=9
b=10
⮚ Prefix Decrement Operator
Example:
i
n
t
a
,
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)
b ;
; b = --a;
a printf(“a = %d”,a);
= printf(“b = %d”,b);
1
0

Output:
a
=
9
b
=
9
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

Q.16 Explain Conditional Operators (Turnery Operator) in detail.

⮚ Syntax: exp1 ? exp2 : exp3


● where, exp1, exp2 & exp3 are expressions.
⮚ exp1 is evaluated first.
⮚ If it is true then the expression exp2 is evaluated & becomes the value of anexpression.
⮚ If exp1 is false, then exp3 is evaluated and its value becomes the value of theexpressions.

⮚ Example:

a=25;
b=32;
ans=(a>b)?a:b;

In above example the answer is 35.

Q.17 Explain the if….else statement with example.


⮚ If the test expression is true then the true-clock statement are executed otherwisethe
false- clock statement are executed.

General Form :

Flowchart of if….else :
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

Example:
if(n%2==0)
{
printf(“ n is even ”);
}
else
{
printf(“ n is odd ”);
}
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

Q.18 Explain the else if ladder with example.


⮚ Used when we need multipath
decisions. General Form:
if(condition1)
statement1;
else if(condition2)
statement2;

else if(condition3)
statement3;
else
default-statement;
statement-x;

⮚ In this, the condition are evaluated from top to downwards.


⮚ When all the n conditions are become false, then the final else containing
thedefault statement will be executed.
Example
if( marks>84 )
if( marks>54 )
else if( marks>74 ) else if( marks>64 ) else
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)
G G

r r

a a

d d

e e

= =

“ “

A B

A B

” ”

; ;

G G

r r

a a

d d

e e

= =

“ “

A B

B C

” ”

; ;
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

else
Grade=“Fail”;
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

Q.19 Explain SWITCH Statement with example.


⮚ We uses an if statement to control the selection. However, the complexity ofsuch a
program increases when the number of alternatives increases.
⮚ And the program becomes more difficult to read and follow.

⮚ The switch statement tests the value of a given variable against a list of case values and when
a match is found, a block of statements associated with that caseis executed.

⮚ General Form
switch (expression)
{
case value1:case
block-
1;
value2: break;

block-
2;
break;

default:
d
e
} f
a
u
lt
-
b
l
o
c
k
;
b
r
e
a
k
;
statement-x;
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

⮚ Where, the expression is an integer expression or characters.


⮚ Value1, value2….. Are constants or constant expression and are known as case
o labels.
⮚ Case labels end with a colon(:)
⮚ When the switch is executed, the value of the expression is successfully comparedagainst the
values. If a case is found whose value matches with the value of the expression, then the block of
statement that follows the case are executed.
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

Example
switch( op)
{
case +:
a
n
s
=
a
+
b
;
b
r
e
a
k
;

case -: default:
a
n
} s
=
a
-
b
;
b
r
e
a
k
;

printf(“
wrong
choice
”);
break;
printf(“ Answer is : %d”, ans);
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)
Q.20 Explain while loop with example.
⮚ While loop is the entry control loop. Because in while loop first the condition ischecked.
⮚ Syntax:
o while (condition)
o {
o Body of the loop;
o }

⮚ {} is known as body of the loop. If body contain one statement then it is notnecessary
to enclose body of the loop in the pair of brace {}.
⮚ In while loop, the condition is evaluated first and if it is true then the statement inthe body of the
loop is executed.
⮚ After executing body , the condition is evaluated again and if it is true body isexecuted again.
This process is repeated as long as the condition is true. The control move out once the
condition is false.
⮚ Flowchart:
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

Example: Print 1 to 10 numbers using while loop.


i =1;
while (i < = 10)
{
printf( “% d \n ” , i );i =
i+1;
}

Q.21 Explain Do-while loop with example.


⮚ Do..while loop is a exit control loop. Because after the executing the body of theloop
the condition is checked.
⮚ Syntax:
▪ Do
▪ {
▪ Body of the loop
▪ }while(condition);

⮚ In do..while loop the body is executed and then the condition is checked.
⮚ If the condition is true the body is executed again and again, as long as thecondition
is true.
⮚ The control moves out of loop once, the condition become false.
⮚ The body of the loop is executed at least once even if the condition is false firsttime.
⮚ Flowchart:

Example: Print 1 to 10 numbers using do---while loop.


i =1;
do{
printf( “% d \n ” , i );i =
i+1;
} while (i < = 10);
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

Q.22 Give differences between While & Do-while loop.


While Do--while

It is entry control loop, means first condition is It is exit control loop, first body is executedand
checked, and if it is true thenbody of the loop then condition is checked and if the condition is
executes, otherwise nextstatement after the loop true, then again body of the loop will be executed,
will be executed.
otherwise next statement after the loop will be
executed
At first time, condition is false, thenbody At first time, condition is false, at least oncethe
is not executed. body is executed.

Syntax: Syntax:
while (condition) Do
{ {
Body of the loop; Body of the loop
} }while(condition);

Example: Example:
i =1; i =1;
do do
{ {
printf( “% d \n ” , i );i = printf( “% d \n ” , i );i =
i+1; i+1;
} while (i < = 10); } while (i < = 10);

Q.23 Explain for loop with example.


⮚ Syntax:
● For(Initialization ; Condition ; Increment or decrement)
● {
▪ Body of the loop;
● }
⮚ Initialization: We require one variable which is used to control the loop, which is called as control
variable. It executes only once. The control variable is initialized in the initialization expression.
⮚ Condition: The condition statement checks the value of the control variable. Ifthe
condition statement is true, the body of the loop is executed.
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

⮚ Increment/Decrement: The increment/decrement of the control variable is donein this part.


After the incrementing/decrementing the value of control variable, it is tested using condition
if condition is true than again the body of loop is executed and this process is repeated until the
condition become false.
⮚ If the body of the loop contains only one statement, then { } is not required.
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

Flowchart :

Example: Print 1 to 10 numbers using for loop.

for(i = 1; i < =10 ; i++)


{
printf(“ %d \n “, i ) ;
}

Q.24 Define Arrays with Syntax & example.


⮚ An array is a collection of variables of same data type known by a same name.
Syntax:
⮚ Data type arrayname [size] ;
⮚ Data type: it defines the type of the array element, like int, float, char,double etc.
⮚ Array name: it is the name of variable which represent array.

⮚ Size: which is represents within[ ] symbol represent the size of the array.

The above array is declared as int a[5];a[0]


= 4;
a[1] = 5;
a[2] = 33;
a[3] = 13;
a[4] = 1;
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)
4,5,33,13,1 are actual data items … ( in our case Roll numbers )
0,1,2,3,4 are index variables ..( similar to ‘i’ in for loop / Subscript variable )
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

Q.25 How to Declaration Array.


⮚ Array has to be declared before using it in C Program. Array is nothing but thecollection of
elements of similar data types.
⮚ Syntax
⮚ <data_type> array_name [size1][size2] [sizen];

Syntax Parameter Significance


data_type Data Type of Each Element of the array
Array_name Valid variable name
size Dimensions of the Array
Examples : Declaring 1D array
int roll[10]; // Array of 10 integer roll numbers
In the above example we are declaring the integer array of size 10. Array is singledimensional

Q.26 Explain Advantage & disadvantage of Array.


Advantages of Array
1. We can use one name for similar objects and save them with same name butdifferent
indexes.
2. 2- D array are used to represent matrices.
3. It can be used to implement other data structure like linked list, stack, queue ,tree, graph etc.
Disadvantages of Array
1. We must know in advance how many elements are to be stored in array.
2. If we allocate more memory than requirement than the memory space will bewasted.

Q.27 Explain Accessing an array with example.


⮚ Array elements are randomly accessed using the subscript variable.
⮚ Array can be accessed using array-name and subscript variable written inside pairof square
brackets [ ].
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)
int mark[5] = {19, 10, 8, 17, 9};

⮚ mark[0] is equal to 19
⮚ mark[1] is equal to 10
⮚ mark[2] is equal to 8
⮚ mark[3] is equal to 17
⮚ mark[4] is equal to 9
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

Q.28 Explain One Dimensional Array with example.


One Dimensional Array:

● An array is defined as an ordered set of similar data items.


● All the data items of an array are stored in consecutive memory locations in RAM.
● The elements of an array are of same data type and each item can be accessed using the same name.

Declaration of an array:-

● We know that all the variables are declared before they are used in the program.
● Similarly, an array must be declared before it is used.
● During declaration, the size of the array has to be specified.
● The size used during declaration of the array informs the compiler to allocate and reserve the
specified memory locations.

Syntax : data_type array_name[n];

Where, n is the number of data items (or) index (or) dimension. 0 to (n-1) is the range of array.

Example : int a[5];

float x[10];

Initialization of Arrays : The different types of initializing arrays:

1. At Compile time

(i) Initializing all specified memory locations.

(ii) Partial array initialization

(iii) Initialization without size.


APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)
(iv) String initialization.

2. At Run Time

Compile Time Initialization :

We can initialize the elements of arrays in the same way as the ordinary variables when they are declared. The
general form of initialization of arrays is

Data_type array_name[size]={ list of values};


APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

(I) Initializing all specified memory locations:

● Arrays can be initialized at the time of declaration when their initial values are known in advance.
● Array elements can be initialized with data items of type int, char etc.

Example : int a[5]={10,15,1,3,20};

(ii) Partial array initialization:-

● Partial array initialization is possible in c language.


● If the number of values to be initialized is less than the size of the array, then the elements will be
initialized to zero automatically.

Example : int a[5]={10,15};

Initialization with all zeros:

Example : int a[5]={0};

(iii) Initialization without size:

Consider the declaration along with the initialization.

Example : char b[]={'C','O','M','P','U','T','E','R',’\0’};

(iv) Array initialization with a string: -Consider the declaration with string initialization.

Ex:- char b[]="COMPUTER";

● Even though the string "COMPUTER" contains 8 characters, because it is a string.


● it always ends with null character.
● So, the array size is 9 bytes (i.e., string length 1 byte for null character).

Example : char b[9] = "COMPUTER"; // correct char

b[8] = "COMPUTER"; // wrong

Run Time Initialization :

● An array can be explicitly initialized at run time.


APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

● This approach is usually applied for initializing large arrays.


● scanf can be used to initialize an array.
Example :
20
int x[3];
scanf(“%d%d%d”,&x[0],&x[1],&x[2]);
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

The above statements will initialize array elements with the values entered through the key board.
OR
We can also use for loop like

for(i=0;i<3;i++)
Scanf(“%d”,&x[i]);

Display elements of array :

We can display elements of array as

printf(“%d%d%d”,x[0],x[1],x[2]);

OR
We can also use for loop like
for(i=0;i<3;i++)
printf(“%d”,x[i]);
Q.29 Explain Two Dimensional Array with example.

Two Dimensional Array:

● An array consisting of two subscripts is known as two-dimensional array.

● These are often known as array of the array.

● In two dimensional arrays the array is divided into rows and columns.

● These are well suited to handle a table of data. In


2-D array we can declare an array as : Declaration:
Syntax:
data_type array_name[row_size][column_size];
Example :
int arr[3][3];
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)
Where first index value shows the number of the rows and second index value shows the number of the columns
in the array.
Initializing two-dimensional arrays:
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

Like the one-dimensional arrays, two-dimensional arrays may be initialized by following their declaration with a
list of initial values enclosed in braces.

Ex: int a[2][3]={0,0,0,1,1,1};

initializes the elements of the first row to zero and the second row to one. The

initialization is done row by row.

The above statement can also be written as

int a[2][3] = {{ 0,0,0},{1,1,1}};

by surrounding the elements of each row by braces.

We can also initialize a two-dimensional array in the form of a matrix as shown below int

a[2][3]={

{0,0,0},
{1,1,1}
};
When the array is completely initialized with all values, explicitly we need not specify the size of the first
dimension.

Example : int a[][3] = { {0,2,3}, {2,1,2} };

If the values are missing in an initializer, they are automatically set to zero.

Example : int a[2][3] = { {1,1}, {2} };

Will initialize the first two elements of the first row to one, the first element of the second row to two and all
other elements to zero.

Run Time Initialization :


APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

● An array can be explicitly initialized at run time.


● This approach is usually applied for initializing large arrays.
● scanf can be used to initialize an array.
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

intx[2][3]; scanf(“%d%d%d%d%d%d”,&x[0][0],&x[0][1],&x[0][2] ,&x[1][0],&x[1][1],&x[1][2]); The


above statements will initialize array elements with the values entered through the key board.

OR
We can also use for loop like
for(i=0;i<2;i++)
{
For(j=0;j<3;j++)
{
Scanf(“%d”, &x[i][j]);
}
}
Display elements of array :

We can display elements of array as

printf(“%d%d%d%d%d%d”, x[0][0],x[0][1],x[0][2] ,x[1][0],x[1][1],x[1][2]); OR

We can also use for loop like for(i=0;i<2;i++)


{
For(j=0;j<3;j++)
{
printf(“%d”,x[i][j]);
}
}
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

Q.30 Explain pointer in detail.


A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory
location. Like any variable or constant, you must declare a pointer before using it to store any variable address.
The general form of a pointer variable declaration is −
type *var-name;
Here, type is the pointer's base type; it must be a valid C data type and var-name is the name of the pointer
variable. The asterisk * used to declare a pointer is the same asterisk used for multiplication. However, in this
statement the asterisk is being used to designate a variable as a pointer. Take a look at some of the valid pointer
declarations −
int *ip; /* pointer to an integer */
double *dp; /* pointer to a double */
float *fp; /* pointer to a float */
char *ch /* pointer to a character */
Example:
#include

<stdio.h> int main

() {

int var = 20; /* actual variable declaration


*/ int *ip; /* pointer variable declaration */

ip = &var; /* store address of var in pointer variable*/

printf("Address of var variable: %x\n", &var );

/* address stored in pointer variable */


printf("Address stored in ip variable: %x\n", ip );

/* access the value using the pointer */


printf("Value of *ip variable: %d\n", *ip );

return 0;
Output:
}
Address of var variable: bffd8b3c Address
stored in ip variable: bffd8b3c Value of *ip
variable: 20
Q. 31 Explain function in detail.
A function is a group of statements that together perform a task. Every C program has at least one function,
which is main(), and all the most trivial programs can define additional functions.
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)
You can divide up your code into separate functions. How you divide up your code among different functions
is up to you, but logically the division is such that each function performs a specific task.
A function declaration tells the compiler about a function's name, return type, and parameters. A function
definition provides the actual body of the function.
The C standard library provides numerous built-in functions that your program can call. For example, strcat()
to concatenate two strings, memcpy() to copy one memory location to another location, and many more
functions.
A function can also be referred as a method or a sub-routine or a procedure, etc.

Defining a Function
The general form of a function definition in C programming language is as follows −
return_type function_name( parameter list ) {
body of the function
}

A function definition in C programming consists of a function header and a function body. Here are all the parts
of a function −

● Return Type − A function may return a value. The return_type is the data type of the value the
function returns. Some functions perform the desired operations without returning a value. In this case,
the return_type is the keyword void.
● Function Name − This is the actual name of the function. The function name and the parameter list
together constitute the function signature.
● Parameters − A parameter is like a placeholder. When a function is invoked, you pass a value to the
parameter. This value is referred to as actual parameter or argument. The parameter list refers to the
type, order, and number of the parameters of a function. Parameters are optional; that is, a function
may contain no parameters.
● Function Body − The function body contains a collection of statements that define what the function
does.
Example
Given below is the source code for a function called max(). This function takes two parameters num1 and num2
and returns the maximum value between the two −
/* function returning the max between two numbers */
int max(int num1, int num2) {
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

/* local variable declaration */


int result;

if (num1 > num2)


result = num1;
else
result = num2;

return result;
}

Function Declarations
A function declaration tells the compiler about a function name and how to call the function. The actual body
of the function can be defined separately.
A function declaration has the following parts −
return_type function_name( parameter list );
For the above defined function max(), the function declaration is as follows −
int max(int num1, int num2);
Parameter names are not important in function declaration only their type is required, so the following is also
a valid declaration −
int max(int, int);
Function declaration is required when you define a function in one source file and you call that function in
another file. In such case, you should declare the function at the top of the file calling the function.

Calling a Function
While creating a C function, you give a definition of what the function has to do. To use a function, you will
have to call that function to perform the defined task.
When a program calls a function, the program control is transferred to the called function. A called function
performs a defined task and when its return statement is executed or when its function-ending closing brace is
reached, it returns the program control back to the main program.
To call a function, you simply need to pass the required parameters along with the function name, and if the
function returns a value, then you can store the returned value. For example −
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

#include <stdio.h>

/* function declaration */
int max(int num1, int num2);

int main () {

/* local variable definition */


int a = 100;
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

int b = 200;
int ret;

/* calling a function to get max value */


ret = max(a, b);
printf( "Max value is : %d\n", ret );

return 0;

/* function returning the max between two numbers */


int max(int num1, int num2) {

/* local variable declaration */


int result;

if (num1 > num2)


result = num1;
else
result = num2;

return result;
}
We have kept max() along with main() and compiled the source code. While running the final executable, it
would produce the following result −
Max value is : 200

Function Arguments
If a function is to use arguments, it must declare variables that accept the values of the arguments. These
variables are called the formal parameters of the function.
Formal parameters behave like other local variables inside the function and are created upon entry into the
function and destroyed upon exit.
While calling a function, there are two ways in which arguments can be passed to a function –
APOLLO INSTITUTE OF ENGINEERING AND TECHNOLOGY
Frequently Asked Questions(FAQ)
Computer Programming Fundamentals (DI01000131)

Sr.No. Call Type & Description

1 Call by value
This method copies the actual value of an argument into the formal parameter of the function. In this
case, changes made to the parameter inside the function have no effect on the argument.

2 Call by reference
This method copies the address of an argument into the formal parameter. Inside the function, the
address is used to access the actual argument used in the call. This means that changes made to the
parameter affect the argument.

By default, C uses call by value to pass arguments. In general, it means the code within a function cannot alter
the arguments used to call the function.

You might also like