Advanced C Programming Questions and Answers - BCS Guruji

Ad

Thursday, February 16, 2023

Advanced C Programming Questions and Answers

Q. What is pointer?

Answer: A pointer is a variable that stores the memory address of another variable of same type.
syntax: int *ptr

Q. Concept of pointer to pointer with example.

Answer:  A pointer is used to store the address of a variable in C.  However, In C, we can also define a pointer to store the address of another pointer. Such pointer is known as a double pointer (pointer to pointer). The first pointer is used to store the address of a variable whereas the second pointer is used to store the address of the first pointer. 



Q. What is formal parameter and actual parameter.

Answer: 

Formal Parameter: The Formal Parameters are the variables defined by the function that receives values when the function is called. The data types of the receiving values should be included.

Actual Parameter: The Actual parameters are the values that are passed to the function when it is invoked. Only the value is mentioned.

Q. What is dynamic memory allocation?

Answer: Dynamic memory allocation allows a program to obtain more memory space, while running or to release space when no space is required.

Functions of Dynamic memory allocation:

  1. malloc () : It allocates requested size of bytes and returns a pointer first byte of allocated space.
  2. calloc(): It allocates space for an array element initialized to zero and then returns a pointer to memory.
  3. free(): It deallocates previously allocated memory. It releases space explicitely.
  4. realloc(): It re-allocates memory, i.e. changes the size of previously allocated memory.

Q. What is meaning of & and * operator in pointer?

Answer: C provides address operator ‘&’ which returns the address of variable when placed before it.
* is a unary operator that returns the value of the variable at the address specified by its operand.

Q. Give Syntax and use of following functions/ list of functions in strings.

- strlen

Answer: The strlen() function calculates the length of a given string.

- strcat
Answer: The strlen() function calculates the length of a given string.

- strcmp
Answer: The strcmp()  compares two strings character by character. If the strings are equal, the function returns 0.

- strcpy
Answer:  The strcpy() function copies the string pointed by source (including null character) to the destination.

- strrev
Answer:  The strrev() function is used to reverse the given string.


Q. Give the difference between structure and union.

Answer: 

Sr. No.FactorsStructureUnion
1DefinitionIt is a collection of logically related elements of similar or different types.It is a collection of logically related elements of similar or different types with single memory location.
2Accessing membersAll members can be accessed at any time.Only one member can be accessed at any time.
3Memory allocationMemory allocated to all members.Memory allocated for member with largest size.
4InitializationAll members can be initialized.Only the first member can be initiallized.
5SizeEqual to or greater than size of it’s members.Equal to size of largest member.
6Keyword‘struct’‘union’


Q. Define file. Explain any four file functions.

Answer: A collection of data which is stored on a secondary device like a hard disk is known as a file. 

File functions:

fopen() : Opens new or existing file.

fprintf() : Write data into file.

fscanf() : Reads data from the file.

fclose() : Close the file.


Q. Define Macro.

Answer: Macro is an abbrevation for sequence of instructions. When it is used in the program, then defined sequence of instructions are submitted over there.


Q. What are command line arguments ? How are they declared ?

Answer: Command line arguments are nothing but simply arguments that are specified after the name of program in the system's command line, and these argument values are passed on to your program during your program execution.

Declaration:

int main(int argc, char *argv[]) { /* ... */ }


Q. Explain nesting of macros.

Answer: A macro name within another macro is called nesting of macro. The called macro is "inner macro" and calling macro is "outer macro". 

Syntax:

#define identifier1(f1,f2,...,fn)string1

#define identifier2(identifier(f1,f2,....,fn)*(e1,e2,e3.....,en))string2


No comments:

Post a Comment