After Midsems Flashcards

1
Q

What does Functions syntax consists of?

A

A function definition consists of function header and function body.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does function header consists of?

A

● Return_type: The function always starts with a return type of the function.
But if there is no return value then the void keyword is used as the return
type of the function.
● Function_Name: Name of the function which should be unique.
● Parameters: Values that are passed during the function call.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does function body consists of?

A

● Local Variable: A local declaration of the variables that are needed by the
function.
● Function Statements: The statements that perform the task of the function.
● Return Statements: It returns the value evaluated by the function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Whats Function calling?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Show the memory layout in a C program

A

HEAP (Dynamic-Free Memory)
STACK (Local Variables)
GLOBAL(STATIC)
PROGRAM CODE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

In how many ways can a value be passed in a function?

A

While calling a function, there are two ways in which arguments can be
passed to a function −
a. Call by value
b. Call by reference

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is Recursion!?

A

Recursion is the process which comes into existence when a function calls a
copy of itself to work on a smaller problem.
● Any function which calls itself is called recursive function, and such
function calls are called recursive calls.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Explain calling by reference in functions?

A

● Call by reference:
○ Both actual and formal parameters refers to same memory location.
○ This method copies the address of (&) an actual argument into the formal
parameter.
○ The value of the actual parameters can be modified by changing the
formal parameters since the address of the actual parameters is passed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Explain calling by value in functions?

A

● Call by value:
○ This method copies the actual value of an argument into the formal
parameter of the function.
○ In this case, we can not modify the value of the actual parameter by the
formal parameter.
○ Different memory is allocated for actual and formal parameters since

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are local variables?

A

Variables that are declared inside a function or block are called local
variables.
● They can be used only by statements that are inside that function or block of
code.
● Also known as internal, private or automatic variable.
● Local variables are not known to functions outside their own.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Explain abt Global Variable

A

Global Variables

● Global variables are defined outside a function, usually on top of the
program.
● Global variables hold their values throughout the lifetime of your program
and they can be accessed inside any of the functions defined for the program.
● A program can have same name for local and global variables but the value
of local variable inside a function will take preference.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the 4 types of storage classes?

A

There are four types of storage classes in C
1. Automatic
2. External
3. Static
4. Register

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Explain Automatic Storage classes

A

Storage Classes in C: Automatic

● Automatic variables are allocated memory automatically at runtime.
● The life of the automatic variables is limited to the block in which they are
defined.
● The scope of the automatic variables is limited to the block in which they
are defined.
● The automatic variables are initialized to garbage by default.
● The memory assigned to automatic variables gets freed upon exiting from
the block.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Explain Extern Storage classes

A

● The external storage class is used to tell the compiler that the variable
defined as extern is declared with an external linkage elsewhere in the
program.
● The variables declared as extern are not allocated any memory. It is only
declaration and intended to specify that the variable is declared elsewhere
in the program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Explain Static Storage classes

A

Storage Classes in C: Static

● The variables defined as static specifier can hold their value between the
multiple function calls.
● Local Scope: Static local variables are available only to the function or the
block in which they are defined.
● Global Scope: Static global variables are available for the entire program.
● Default initial value of the static integral variable is 0 otherwise null.
● The keyword used to define static variable is static.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Explain Register Storage classes

A

The variables defined as the register is allocated the memory into the CPU
registers depending upon the size of the memory remaining in the CPU.
● Its scope is limited to the function it is defined in.
● Lifetime is till the end of the function block
● We can not dereference the register variables, i.e., we can not use &
operator for the register variable.
● The access time of the register variables is faster than the automatic
variables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Methods to Pass an Array as an Argument are!?

A

Methods to Pass an Array as an Argument:

  1. return_type function(type arrayname[])
  2. return_type function(type arrayname[SIZE])
  3. return_type function(type *arrayname)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Methods to return an Array from a function are?

A

We can return an array from a function in C using four ways
1. Returning the array passed to function
2. Returning dynamically created array
3. Return array using static array
4. Returning array using struct

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Explain pointers in C

A

The pointer in C language is a variable which stores the address of another
variable.
● This variable can be of type int, char, array, function, or any other pointer.
● The size of the pointer depends on the architecture usually 8 bytes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Whats Deference operator?

A

● When used in declaration (int* ptr), it
creates a pointer variable.

● When not used in declaration, it act as a
dereference operator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What are the uses of Pointers in C?

A

Uses of Pointers in C:

  1. To pass arguments by reference
  2. For accessing array elements
  3. To return multiple values
  4. Dynamic memory allocation
  5. To Implement data structures
22
Q

What are the advantages of Pointers in C?

A

Advantages of Pointers in C:

● Pointers are used for dynamic memory allocation and deallocation.
● An Array or an structure can be accessed efficiently with pointers
● Pointers are useful for accessing memory locations.
● Pointers are used to form complex data structures such as linked lists,
graphs, trees, etc.
● Pointers reduces length of the program and its execution time as well.

23
Q

What are Disadvantages of Pointers in C?

A

Disadvantages of Pointers in C:

● Memory corruption can occur if an incorrect value is provided to pointers
● Pointers are Complex to understand.
● Pointers are majorly responsible for memory leaks.
● Pointers are comparatively slower than variables in C.
● Uninitialized pointers might cause segmentation fault.

24
Q

What are Void Pointers?

A

Void Pointers in C:

● A void pointer is a pointer that has no associated data type with it.
● A void pointer can hold address of any type and can be typecasted to any
type.
● Void pointers cannot be dereferenced without typecasting.
● Example:
○ void* Ptr;

25
Q

The formula for multidimensional array address is?

A

In general, for Row x Col array the formula will be
○ base address + (Row * Col) * sizeof(array datatype)

26
Q

Difference between static Memory allocation and Dynamic memory allocation?

A

Static Memory Allocation:
a) Memory is allocated at compile time.
b) Memory can’t be increased while
executing program.
c) Used in array.

Dynamic memory allocation:
a) Memory is allocated at run time.
b) Memory can be increased while executing program.
c) Used in array and linked list.

27
Q

Whats a malloc() Function?

A

● “malloc” or “memory allocation” method in C allocates single
block of requested memory.
● It doesn’t initialize memory at execution time, so it has garbage
value initially.
● It returns NULL if memory is not sufficient.

ptr=(cast-type*)malloc(byte-size)

28
Q

Whats a calloc() Function?

A

● “calloc” or “contiguous allocation” method in C is used to
dynamically allocate the specified number of blocks of memory of the
specified type.
● It returns NULL if memory is not sufficient.
● It is very much similar to malloc() but has two different points and these
are:
○ It initializes each block with a default value ‘0’.
○ It has two parameters or arguments as compare to malloc().

ptr=(cast-type*)calloc(number, byte-size)

29
Q

Whats a realloc() Function?

A

● “realloc” or “re-allocation” method in C is used to dynamically change
the memory allocation of a previously allocated memory.
● In other words, if the memory previously allocated with the help of
malloc or calloc is insufficient, realloc can be used to dynamically
re-allocate memory.
● re-allocation of memory maintains the already present value and new
blocks will be initialized with the default garbage value.
● The syntax of realloc() function:
○ ptr=realloc(ptr, new-size)

30
Q

Whats a free() Function?

A

● The memory occupied by malloc() or calloc() functions must be
released by calling free() function.
● Otherwise, it will consume memory until program exit.
● Let’s see the syntax of free() function.
○ free(ptr)

31
Q

Whats a Memory leak?

A

Memory Leak

● Memory leak occurs when programmers create a memory in heap and
forget to delete it.
● The consequences of memory leak is that it reduces the performance
of the computer by reducing the amount of available memory.
● To avoid memory leaks, memory allocated on heap should always be
freed when no longer needed.

32
Q

Whats a Dangling Pointer?

A

Dangling Pointer

● A pointer pointing to a memory location that has been deleted (or freed)
is called dangling pointer.
● The dangling pointer errors can be avoided by initializing the pointer to
the NULL value.

33
Q
A

● A structure is a keyword that creates user-defined data types in C.
● A structure creates a data type that can be used to group elements of
possibly different types into a single type.
● Each element of a structure is called a member.
● The struct keyword is used to define the structure.

34
Q
A

● A structure is a keyword that creates user-defined data types in C.
● A structure creates a data type that can be used to group elements of
possibly different types into a single type.
● Each element of a structure is called a member.
● The struct keyword is used to define the structure.

35
Q
A

Limitation of Structure in C:

● We cannot use operators like +, - etc. on Structure variables. But we
can use arithmetic operation on structure variables.
● C structures do not permit functions inside Structure.
● C Structures cannot have static members inside their body.

36
Q

Self-referential Structures in C:

A

● The self-referential structure is a structure that points to the same type
of structure.
● It contains one or more pointers that ultimately point to the same
structure.
● The self-referential structure is widely used in dynamic data structures
such as trees, linked lists, etc.

37
Q

Whats a Union in C!?

A

● A union is a user-defined type similar to structs in C except for one
key difference.
● Structures allocate enough space to store all their members, whereas
unions can only hold one member value at a time.
● It allows storing different data types in the same memory location.
● Unions provide an efficient way of using the same memory location
for multiple purposes.

38
Q

Similarities between Structure and Union in C?

A

● Both are user-defined data types used to store data of different types
as a single unit.
● Their members can be objects of any type, including other structures
and unions or arrays.

39
Q

Why shall we use fgets() over gets()!?

A

● Note: The gets() function can also be used to take input from the user.
However, it is not safe to use.
● It’s because gets() allows you to input any length of characters. Hence,
there might be a buffer overflow.
● It is used to read string from user until newline character not
encountered.

40
Q

Explain strcat() function?

A

● Function strcat can append a copy of the source string at the end of the
destination string.
● In the strcat() operation, the destination string’s null character will be
overwritten by the source string’s first character,
● The strcat() function takes two arguments:
○ dest
○ src

41
Q

Explain strcpy() function?

A

● strcpy() is a standard library function in C and is used to copy one
string to another.
● Syntax:
○ char* strcpy(char* dest, const char* src);
● Parameters: This method accepts the following parameters:
○ dest: Pointer to the destination array where the content is to be
copied.
○ src: string which will be copied.
● The strcpy() function returns a pointer to the destination string.

42
Q

Explain strlen() function?

A

● The strlen() function calculates the length of a given string.
● The strlen() function is defined in string.h header file.
● It doesn’t count null character ‘\0’.
● Syntax:
○ int strlen(const char *str);

43
Q

Explain strcmp() function?

A

● This function takes two strings as arguments and compare these two
strings lexicographically means it starts comparison character by
character.
● Syntax:
○ int strcmp(const char *leftStr, const char *rightStr );
● In the above prototype, function strcmp takes two strings as
parameters and returns an integer value based on the comparison of
strings.
● This function can return three different integer values based on the
comparison:
○ Zero ( 0 ): when both strings are found to be identical.
○ Greater than zero ( >0 ): when the first not matching character
in leftStr have the greater ASCII value than the corresponding
character in rightStr.
○ Less than Zero ( <0 ): when the first not matching character in
leftStr have lesser ASCII value than the corresponding character
in rightStr.

44
Q

Explain strlwr() function?

A

● The strlwr( ) function is a built-in function in C and is used to convert
a given string into lowercase.
● Syntax:
○ char *strlwr(char *str);
● Parameter:
○ str: This represents the given string which we want to convert
into lowercase.

● Returns: It returns the modified string obtained after converting the
characters of the given string str to lowercase.

45
Q

Explain strrev() function?

A

● The strrev() function is used to reverse the given string.
● Syntax:
○ char *strrev(char *str);
● Parameter:
○ str: The given string which is needed to be reversed.
● Returns: This function doesn’t return anything but the reversed string
is stored in the same string.

46
Q

Why do we handle files in C?

A

● Reusability: The file-handling process keeps track of the information
created after the program has been run.
● Portability: Without losing any data files can be transferred to
another in the computer system.
● Efficient: A large amount of input may be required for some
programs. File handling allows you to easily access a part of a code
using individual commands which saves a lot of time and reduces the
chance of errors.
● Storage Capacity: Files allow you to store data without having to
worry about storing everything simultaneously in a program.

47
Q

Explain the two types of files in C?

A

● Text Files:
○ Text files contain data in the form of ASCII characters and are
generally used to store a stream of characters.
○ Each line in a text file ends with a new line character (‘\n’).
○ Text files are used to store the source code.
● Binary Files:
○ Binary files contain data that is stored in a similar manner to how it is
stored in the main memory.
○ Instead of ASCII characters, it is stored in binary format.
○ The binary files can be created only from within a program and their
contents can only be read by a program.

48
Q

Explain the two types of files in C?

A

● Text Files:
○ Text files contain data in the form of ASCII characters and are
generally used to store a stream of characters.
○ Each line in a text file ends with a new line character (‘\n’).
○ Text files are used to store the source code.
● Binary Files:
○ Binary files contain data that is stored in a similar manner to how it is
stored in the main memory.
○ Instead of ASCII characters, it is stored in binary format.
○ The binary files can be created only from within a program and their
contents can only be read by a program.

49
Q

Explain the two types of files in C?

A

● Text Files:
○ Text files contain data in the form of ASCII characters and are
generally used to store a stream of characters.
○ Each line in a text file ends with a new line character (‘\n’).
○ Text files are used to store the source code.
● Binary Files:
○ Binary files contain data that is stored in a similar manner to how it is
stored in the main memory.
○ Instead of ASCII characters, it is stored in binary format.
○ The binary files can be created only from within a program and their
contents can only be read by a program.

50
Q

Explain the two types of files in C?

A

● Text Files:
○ Text files contain data in the form of ASCII characters and are
generally used to store a stream of characters.
○ Each line in a text file ends with a new line character (‘\n’).
○ Text files are used to store the source code.
● Binary Files:
○ Binary files contain data that is stored in a similar manner to how it is
stored in the main memory.
○ Instead of ASCII characters, it is stored in binary format.
○ The binary files can be created only from within a program and their
contents can only be read by a program.