CPF Faq Eng
CPF Faq Eng
⮚ 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.
⮚ ‘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)
⮚ Special symbols such as space, comma, currency etc are not allowed.
⮚ 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)
⮚ 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.12 Define symbolic constants & list out Rules of defining symbolic constant.
⮚ #define is a preprocessor directive which is used to defined symbolic constant.
⮚ 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.
+ 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)
Operator Meaning
< Is less than
<= Is less than or equal to
> Is greater than
== Is equal to
!= Is not equal to
⮚ 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)
⮚ 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)
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)
⮚ Example:
⮚
a=25;
b=32;
ans=(a>b)?a:b;
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)
else if(condition3)
statement3;
else
default-statement;
statement-x;
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)
⮚ 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)
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)
⮚ 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:
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);
Flowchart :
⮚ Size: which is represents within[ ] symbol represent the size of the array.
⮚ 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)
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.
Where, n is the number of data items (or) index (or) dimension. 0 to (n-1) is the range of array.
float x[10];
1. At Compile time
2. At Run Time
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
● 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.
(iv) Array initialization with a string: -Consider the declaration with string initialization.
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]);
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.
● In two dimensional arrays the array is divided into rows and columns.
Like the one-dimensional arrays, two-dimensional arrays may be initialized by following their declaration with a
list of initial values enclosed in braces.
initializes the elements of the first row to zero and the second row to one. The
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.
If the values are missing in an initializer, they are automatically set to zero.
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.
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 :
() {
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)
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 () {
int b = 200;
int ret;
return 0;
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)
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.