POP Set - 2 Solution
POP Set - 2 Solution
1. a) Explain the structure of C program in detail. Write a sample program to demonstrate the components
in the structure of C program.
Ans:
The general structure of a C program contains following sections:
1. Documentation section: This section consists of comment lines which include the name of the program, the
name of the programmer, the author and other details like time and date of writing the program. Documentation
section helps anyone to get an overview of the program.
Example: 1)Multiline comments(/*------*/) & 2)Singleline comments(//--------)
2. Link section: The link section consists of the header files of the functions that are used in the program. It
provides instructions to the compiler to link functions from the system library such as using the #include
directive.
Example: #include<stdio.h> , #include<math.h>
3. Definition section: All the symbolic constants are written in the definition section. Macros are known as
symbolic constants.
Example: #define PI 3.14 , #define MAXSIZE 10
4. Global declaration: The global variables that can be used anywhere in the program are declared in the
global declaration section. This section also declares the user defined functions.
5. Main function: It is necessary to have one main() function section in every C program. This section contains
two parts, declaration and executable part. The declaration part declares all the variables that are used in
executable part. These two parts must be written in between the opening and closing braces. Each statement in
the declaration and executable part must end with a semicolon (;). The execution of the program starts at
opening braces and ends at closing braces.
i) Declaration part: The declaration part declares all the variables used in the executable part.
ii) Executable part: There is at least one statement in the executable part. These two parts must appear
between the opening and closing braces. The program execution begins at the opening brace and ends at the
closing brace. The closing brace of the main function is the logical end of the program. All statements in
the declaration and executable part end with a semicolon.
6. User defined functions: The subprogram section contains all the user defined functions that are used to
perform a specific task. These user defined functions are called in the main() function. If the program is a
multifunction program then the sub program section contains all the user-defined functions that are called in the
main () function. User-defined functions are generally placed immediately after the main () function, although
they may appear in any order.
2. a) Explain the various rules for forming identifiers names. Give examples for valid and invalid identifiers
for the same.
Ans:
Variable is a named location in a memory where a program can manipulate the data.
The rules to construct variables are:
They can contain letters, digits and underscores.
They should not begin with any special characters except underscore (_).
They are case sensitive.
They cannot contain whitespaces or special characters like !, #, %, etc.
Reserved words cannot be used as variables(such as int, float, char etc.).
ii) Global variables scope: Global variables are defined outside of a function, usually on top
of the program. The global variables will hold their value throughout the lifetime of your
program and they can be accessed inside any of the functions defined for the program. A
global variable can be accessed by any function. That is, a global variable is available
for use throughout your entire program after its declaration.
Example:
#include <stdio.h>
int g;
void main()
{
int a, b;
a = 10;
b = 20;
g = a + b;
printf ("value of a = %d, b = %d and Sum = %d\n", a, b, g);
}
6. a) Differentiate between call by value and call by reference. Using suitable example.
Ans:
Call by value Call by reference
A copy of the value is passed into the function An address of value is passed into the function
Changes made inside the function is limited to the Changes made inside the function validate outside of
function only. The values of the actual parameters do the function also. The values of the actual parameters
not change by changing the formal parameters. do change by changing the formal parameters.
Actual and formal arguments are created at the Actual and formal arguments are created at the same
different memory location memory location
Example: Example:
#include<stdio.h> #include<stdio.h>
void swapping(int a, int b); void swapping(int *a, int *b);
void main() void main()
{ {
int x,y; int x,y;
printf(“Enter x, y values\n”); printf(“Enter x, y values\n”);
scanf(“%d %d”,&x,&y); scanf(“%d %d”,&x,&y);
printf(“Before swapping\n”); printf(“Before swapping\n”);
printf(“%d %d”,x,y); printf(“%d %d”,x,y);
swapping(x,y); swapping(&x,&y);
} }
void swapping(int a, int b) void swapping(int *a, int *b)
{ {
int temp; int temp;
temp = a; temp = *a;
a = b; *a = *b;
b = temp; *b = temp;
printf(“After swapping\n”); printf(“After swapping\n”);
printf(“%d %d”,a,b); printf(“%d %d”,*a,*b);
} }
7. a) Mention various operations that can be performed on string using built-in functions. Explain any two
function.
Ans:
i) strlen( )
ii) strupr( )
iii) strlwr( )
iv) strcmp( )
v) strcat( )
vi) strcpy( )
vii) strrev( )
viii) strstr( )
ix) strchr( )
x) strrchr( )
1)strlen: This string function is basically used for the purpose of computing the length of string.
Syntax: strlen(str);
Example:
#include<stdio.h>
void main()
{
char str[10]= “COMPUTER”;
int len = strlen(str);
printf(“The length of string is = %d”,len);
}
2)strupr: This string function is used to convert the string to uppercase.
Syntax: strupr(str);
Example:
#include<stdio.h>
void main()
{
char str[10]= “computer”;
printf(“The uppercase of string is = %s”,strupr(str));
}
b) Develop a program using pointer to compute the sum, mean and standard deviation of all element
stored in array of N real number.
Ans: #include<stdio.h>
#include<math.h>
int main()
{
float a[10], *ptr, mean, std, sum=0, sumstd=0;
int n,i;
printf("\n Enter the no of elements\n");
scanf("%d",&n);
printf("\n Enter the array elements\n");
for(i=0;i<n;i++)
scanf("%f",&a[i]);
ptr=a;
for(i=0;i<n;i++)
{
sum=sum+ *ptr;
ptr++;
}
mean=sum/n;
ptr=a;
for(i=0;i<n;i++)
{
sumstd=sumstd + pow((*ptr - mean),2);
ptr++;
}
std= sqrt(sumstd/n);
printf("\n Sum=%.3f\t",sum);
printf("\n Mean=%.3f\t",mean);
printf("\n Standard deviation=%.3f\t",std);
return 0;
}
puts():
puts() is used to write(display) string on standard output device until newline character not found. It terminates
the line with a newline character. It returns an EOF(-1) if an error occurs and returns a positive number on
success.
Syntax: puts(str);
Example:
#include<stdio.h>
#include<string.h>
void main()
{
char name[50];
printf(“Enter your name\n”);
scanf(“%s”,name);
printf(“Your name is : ”);
puts(name);
}
fgetc(): It returns the next character from stream, and EOF if the end of file is reached, or if there
is an error.
Syntax: int fgetc(FILE *stream);
9 Syntax: Syntax:
struct [structure name] union [union name]
{ {
member definition; member definition;
member definition; member definition;
... ...
member definition; member definition;
}; };
10 Example structure declaration: Example union declaration:
struct sVALUE union uVALUE
{ {
int ival; int ival;
float fval; float fval;
char *ptr; char *ptr;
}s; }u;
Here size of struct is 8 Bytes. Here size of union is 4 Bytes.
c) How to detect END-OF-FILE.
Ans:
The End of the File (EOF) indicates the end of input. In C, getc() returns EOF when end of file is reached.
getc() also returns EOF when it fails. So, only comparing the value returned by getc() with EOF is not
sufficient to check for actual end of file. To solve this problem, C provides feof() which returns non-zero
value only if end of file has reached, otherwise it returns 0. For example, consider the following C program
to print contents of file test.txt on screen. In the program, returned value of getc() is compared with EOF
first, then there is another check using feof(). By putting this check, we make sure that the program
prints “End of file reached” only if end of file is reached. And if getc() returns EOF due to any other reason,
then the program prints “Something went wrong”.
Example:
#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen("test.txt", "r");
int ch = getc(fp);
while (ch != EOF)
{
putchar(ch);
ch = getc(fp);
}
if (feof(fp))
printf("\n End of file reached.");
else
printf("\n Something went wrong.");
fclose(fp);
getchar();
return 0;
}