Sei sulla pagina 1di 11

Computer Concepts and Programming [CE143] ID no.

: 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:

Memory 1000 1002 1004 1006 1008


Address
Value 1 2 3 4 5
A character array is an array of characters (string) whereas an integer
array is an array of integers. It is generally advisable to append a null
character at the end of a character array whereas it is not so in the
case of integer array. A character array can be initialized by double
quotes whereas it is not so in integer array
Example: int arr[] = {1,2,3,4,5};
char arr[] = “CSPIT”;//The ‘\0’ is automatically appended

12. What is a String? How string is stored? What is importance of null


character in strings? Explain with example.
Ans: A string is a sequence of characters. It is stored in the memory as a
character array. Null character is required at the end of string to signify the
compiler that the string has ended and the remaining elements of the
character array should be ignored. For Ex: suppose str[5] is a character
array. And we initialise it as:
str = {‘a’,’b’}; //Without null terminator
And we wish to print the array as
printf(“%s”, str);

____________________________________________________________________________
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

13. Explain the following functions defined in string.h header file.


a. strcpy() : It copies a string to another and also copies null character.
Ex: strcpy(str1, srt2); //Copies str2 to str1 (str1 = str2)
b. strncpy(): It copies first n characters of a string to another but does
does not append the null character if there is not any in the first n
characters.
Ex: strncpy(str1, str2,5); //Copies first 5 chars of str2 to str1
c. strcat(): Concatenates (Joins) two strings.
Ex: strcat(str1,str2); //Appends str2 to str1
d. strncat(): Appends first n characters from one string to another.
Ex: strncat(str1,str2, 5); //Appends first 5 chars of str2 to str1.
e. strstr(): It finds the first occurrence of a sub-string in a string.
Ex: strstr( str, “abc”); //Finds the first occurrence of “abc”
f. strlen(): It calculates the length of the string.
Ex: strlen(str); //Finds the length of the string.
g. strrev(): It reverses a string.
Ex: strrev(str); //Reverses str;
h. strcmp(): It compares two strings and returns 0 if they are same.
Ex: strcmp(str1, str2); //Compares str1 and str2 (str1 == str2)
i. strncmp(): It compares first n characters of both the strings.
Ex: strncmp(str1, str2, 5);//Compares first 4 characters of both the
string
j. strchr(): It returns a pointer to the first occurrence of a character in a
string, if there is none, then it returns NULL.
Ex: strchr(str, ‘s’); //Finds the first occurrence of ‘s’
k. strrchr(): It returns a pointer to the last occurrence of a character in a
string, if there is none, then it returns NULL.
Ex: strrchr(str, ‘s’); //Finds the last occurrence of ‘s’

____________________________________________________________________________
CSPIT (E.C.)
Computer Concepts and Programming [CE143] ID no. : 18EC089

14. Distinguish between: Recursive Function and Iterative Function.


Ans: A recursive function is a function which calls itself, whereas an
iterative function is a function which has a loop (for, while or do while)
loop in it and does not call itself.
Ex: void abc(){
printf(“hi”);
abc(); //Recursive function.
}
void def(){
int I =0;
for(I = 0; I < 5; ++I) printf(“HEllo”); //Iterative function;
}
15. Distinguish between: Call by Value and Call by Address/Pass by
pointers.
Ans: In call by value method, a function does not change the actual value of
the parameters as it makes a copy of the arguments passed to it whereas in
call by address method, a function may change the actual value of the
parameters passed to it depending upon the body of the function as it does
not make a copy of the argument, rather it takes the actual address of the
parameters. For Ex:

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

Extern: It is a storage class used to unhide a global variable if a local


variable of the same name exists.
For ex:
Int a = 10;
Void main(){
int a = 30,sum = 5;
____________________________________________________________________________
CSPIT (E.C.)
Computer Concepts and Programming [CE143] ID no. : 18EC089

If(sum > 0){


Extern int a;
Printf(“%d”, a); //Prints 10;
}
}
Register: It is similar to the auto storage class. The variable is limited to
the particular block. The only difference is that the variables declared
using register storage class are stored inside CPU registers instead of a
memory. Register has faster access than that of the main memory.
Ex: register int age;
Static: It is a storage class in which a variable is initialised only once in its
lifetime and has a local scope or global scope depending upon the
position it is declared.
Ex:
#include <stdio.h>
void sum(){
static int st = 0; //Will be only executed once
while(static <= 3){
printf(“%d ”, i);
i++;
}
printf(“\nFinished”);
}

void main(){
sum();

____________________________________________________________________________
CSPIT (E.C.)
Computer Concepts and Programming [CE143] ID no. : 18EC089

sum();
}
Will give a output:
0123
Finished
Finished

17. Explain with example how a structure is passed as function argument.


Ans: A structure can be passed as a function argument to a function in c. It
can be either call by value method or call by address method. Just like a
primitive datatype, in the call by value method, a copy of the structure is
created whereas address is passed in the call by address method which has
the potential to change the value of a structure’s member.
Ex:
struct student{
int roll;
int marks;
};
void func(struct student s1);
void main(){
struct student s;
func(s);
}
void func(struct student s1){//Passing a structure to a function
printf(“%d %d”,s1.roll, s1.marks );
}

____________________________________________________________________________
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
}

Pointers to Functions: Just like a pointer can point to address of any


variable, it can also point to functions and can be called.
For Ex:
void display();
int main()
{
void (*func_ptr)();
func_ptr=display; //Pointer pointing to function
printf("Address of functions display is %u\n",func_ptr);

____________________________________________________________________________
CSPIT (E.C.)
Computer Concepts and Programming [CE143] ID no. : 18EC089

(*func_ptr)(); //The function call


return 0;
}
void display()
{
puts("Hello there");
}

20. Explain array of pointers and pointers to structure.


Ans:
Array of Pointers: Just like normal variables, pointers can also have arrays. It
is significant to use when one has to handle table of string. For Ex:
Char *name[3] = { “CSPIT”, “CHARUSAT”, “Changa”};
Declares a 2d array of type character, and the following statement:
For(I = 0; I < 3; ++i) printf(“%s ”, name[i]);
Will print CSPIT CHARUSAT Changa. The jth character in the ith name can be
accessed as: *(name[i] +j)
Pointers to structure: Pointers can also point to structures, and the
members of this structure can be accessed by -> and not dot(.) operator.
For Ex:
struct emp{
int id;
char name[25];
}employee[5], *ptr;
ptr = employee; //it will point to employee[0];
printf(“Id: %d, name: %s”, ptr->id, ptr->name); //Id: 2, Name: CSPIT

____________________________________________________________________________
CSPIT (E.C.)
Computer Concepts and Programming [CE143] ID no. : 18EC089

21. Write difference between getc( ), getw( ), getch( ), getchar().


Ans: getc(FILE *stream): getc() reads a single character from the input
stream passed as a parameter.
getchar(): It is equivalent to getc(stdin), i.e, it reads a single character
from standard input.
getch(): It reads a single character from the standard input but without
putting anything into buffer, i.e it returns the character
immediately without waiting for the enter key.
getw(FILE *stream): It is used to read an integer from a file that has
been opened in read mode.

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

Potrebbero piacerti anche