Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
: 18EC089
11. Draw how integer array of 5 elements is stored in memory. How does a
character array differ from integer array? Explain with example.
Ans: Suppose we have the array as
int arr[5] = {1,2,3,4,5};
As the size of int is 2 bytes. Therefore the elements of this array will be
stored as:
____________________________________________________________________________
CSPIT (E.C.)
Computer Concepts and Programming [CE143] ID no. : 18EC089
The Compiler will not know when to stop printing the elements and will
print garbage value once it does not obtain any character in the
initialization -- “ab|-]” //ab and then garbage value
____________________________________________________________________________
CSPIT (E.C.)
Computer Concepts and Programming [CE143] ID no. : 18EC089
Call by Value:
void sum(int a, int b){ a = 10; b = 9; }
void main(){
int a = 3,b= 4;
sum(a,b);
printf(“%d %d”, a,b); //Prints 3 4
}
____________________________________________________________________________
CSPIT (E.C.)
Computer Concepts and Programming [CE143] ID no. : 18EC089
Call by address:
void sum(int *a, int *b){ *a = 9; *b = 10; }
void main(){
int a = 3, b = 4;
sum(&a,&b);
printf(“%d %d”, a,b); //Prints 9 10
}
16. What is meant by the scope, visibility and longevity of a variable within
a program? Explain following storage class specifier with example.
(1)Auto (2) Extern (3) Register (4) Static
Ans: The scope of a variable is the range of program statements that can
access that variable. The lifetime of a variable is the interval of time in
which storage is bound to the variable. A variable is visible within its
scope and invisible or hidden outside it.
Auto: The variables defined using auto are called as local variables and
their scope is limited to that block only. It is the default storage class.
Ex:
void show(){
auto int a = 5;
printf(“%d”, a);
}//The scope and visibility of a is over
void main(){
sum();
____________________________________________________________________________
CSPIT (E.C.)
Computer Concepts and Programming [CE143] ID no. : 18EC089
sum();
}
Will give a output:
0123
Finished
Finished
____________________________________________________________________________
CSPIT (E.C.)
Computer Concepts and Programming [CE143] ID no. : 18EC089
18. Explain chain of pointers & write down rules for Pointer Operations.
Ans: It is basically a pointer pointing to another pointer, and hence creating
a chain of pointers as shown.
V
A
ad
ld
r
u
e
s
s
2
1
P1 P2 Variable
It is written as:
Int x = 10;
Int *p = &x;
int **p1 = *p2;
Rules for Pointer Operations:
1. Integer value can be added or subtracted from a pointer variable. It
will be incremented as value*(size of the type). Ex pt = pt + 1, will point
to 5002 if it initially had an address of 5000 and the type of value pt is
pointing to is integer.
2. When two pointer points to the same array then one pointer variable
can be subtracted from other.
3. Two pointers pointing to same data type can be compared using
relational operators.
4. Pointer variable cannot be multiplied or divided by a constant.
____________________________________________________________________________
CSPIT (E.C.)
Computer Concepts and Programming [CE143] ID no. : 18EC089
19. What is scale factor of a pointer? Explain function returning pointers &
pointers to functions.
Ans: Scale factor of a pointer is the quantity of increment in the value of a
pointer by adding 1. For ex: the scale factor of int is 2 and the scale factor of
float is 4.
Function returning pointers: In this case, a pointer is returned by a
function. The pointer returned can be of any type, and using this, a function
can return multiple values (array).
For ex:
Int *add5(int arr[], int n){
int A1[10], i;
For(I =0 ;I <n;++i){
A1[i] = arr[i];
A1[i] += 5;
}
return a1; //Returns the array
}
Void main(){
int o[] = {3,4,5};
int *arr = add5(o, 3);
printf(“%d %d %d \t %d %d %d”, o[0], o[1], o[2], arr[0], arr[1],
arr[2]); //Prints 3 4 5 8 9 10
}
____________________________________________________________________________
CSPIT (E.C.)
Computer Concepts and Programming [CE143] ID no. : 18EC089
____________________________________________________________________________
CSPIT (E.C.)
Computer Concepts and Programming [CE143] ID no. : 18EC089
22. What are the functions used for Random access and Sequential access
of file?
Ans: fseek(): It is used to move file pointer to a specific position. Its syntax
is: fseek(FILE *pointer, long int offset, int pos);
where,
pointer = the file pointer
offset = number of bytes to offset from position
pos = position from where the offset is added
For Ex: fseek(fp, 0, SEEK_END); //points to end of file
ftell(): The ftell() function returns the current file position of the
specified stream. It can be used to get the total size of a file after
moving file pointer at the end of file. For ex:
FILE *fp;
int length;
fp = fopen(“file.txt”, “r”);
fseek(fp,0,SEEK_END);
length = ftell(fp);
rewind(): The rewind() function sets the file pointer at the beginning of
the stream. It is useful if you have to use stream many times.
For Ex: rewind(fp);
____________________________________________________________________________
CSPIT (E.C.)
Computer Concepts and Programming [CE143] ID no. : 18EC089
23. What is the significance of EOF? What is the purpose of closing the file
during the execution of the program?
Ans: EOF is defined in the library called stdio.h. EOF is a constant defined as
a negative integer value which denotes that the end of the file has reached.
A file needs to be closed after a read or write operation to release the
memory allocated by the program. In C, a file is closed using
the fclose() function which returns 0 on success and EOF on failure. Its
syntax is: fclose(FILE *fp);
24. What is a command line argument? Which command line arguments are
taken by the main function? What do they represent?
Ans: When we pass some values from the command line to a c program, they
are called command line arguments. The main function takes the following
command line arguments:
argc: refers to the number of arguments passed.
argv[]: It is a pointer array which points to each argument passed to the
program.
For Ex:
#include <stdio.h>
void main(int argc, char *argv[]){
if(arg == 2) printf(“%s %s supplied”, argv[0], argv[1] );
else if(arg > 2) printf(“Too many arguments…”);
else printf(“Very few arguments”);
}
____________________________________________________________________________
CSPIT (E.C.)