22. Specify different types of decision control statements?
All statements written in a program are executed from top to bottom one by one.
Control statements are used to execute/transfer the control from one part of the
program to another depending on the condition.
If-else statement.
normal if-else statement.
Else-if statement
nested if-else statement.
Switch statement.
23. What is an r-value and l-value?
The term "r-value" refers to a data value stored in memory at a given address.
An r-value is an expression that cannot have a value assigned to it, hence it can
only exist on the right side of an assignment operator(=).
The term "l-value" refers to a memory location that is used to identify an object.
The l-value can be found on either the left or right side of an assignment
operator(=). l-value is frequently used as an identifier.
24. What is the difference between malloc() and calloc()?
calloc() and malloc() are memory dynamic memory allocating functions. The main
difference is that malloc() only takes one argument, which is the number of bytes,
but calloc() takes two arguments, which are the number of blocks and the size of
each block.
25. What is the difference between struct and union in C?
A struct is a group of complex data structures stored in a block of memory where
each member on the block gets a separate memory location to make them
accessible at once
Whereas in the union, all the member variables are stored at the same location on
the memory as a result to which while assigning a value to a member variable will
change the value of all other members.
/* struct & union definations*/
struct bar {
int a; // we can use a & b both simultaneously
char b;
} bar;
union foo {
int a; // we can't use both a and b simultaneously
char b;
} foo;
/* using struc and union variables*/
struct bar y;
y.a = 3; // OK to use
y.b = 'c'; // OK to use
union foo x;
x.a = 3; // OK
x.b = 'c'; // NOl this affects the value of x.a!
26. What is call by reference in functions?
When we caller function makes a function call bypassing the addresses of actual
parameters being passed, then this is called call by reference. In incall by reference,
the operation performed on formal parameters affects the value of actual
parameters because all the operations performed on the value stored in the address
of actual parameters.
27. What is pass by reference in functions?
In Pass by reference, the callee receives the address and makes a copy of the address
of an argument into the formal parameter. Callee function uses the address to access
the actual argument (to do some manipulation). If the callee function changes the
value addressed at the passed address it will be visible to the caller function as well.
28. What is a memory leak? How to avoid it?
When we assign a variable it takes space of our RAM (either heap or RAM)dependent
on the size of data type, however, if a programmer uses a memory available on the
heap and forgets to a delta it, at some point all the memory available on the ram will
be occupied with no memory left this can lead to a memory leak.
int main()
{
char * ptr = malloc(sizeof(int));
/* Do some work */
/*Not freeing the allocated memory*/
return 0;
}
To avoid memory leaks, you can trace all your memory allocations and think forward,
where you want to destroy (in a good sense) that memory and place delete there.
Another way is to use C++ smart pointer in C linking it to GNU compilers.
29. What is Dynamic memory allocation in C? Name the dynamic
allocation functions.
C is a language known for its low-level control over the memory allocation of
variables in DMA there are two major standard library malloc() and free. The malloc()
function takes a single input parameter which tells the size of the memory requested
It returns a pointer to the allocated memory. If the allocation fails, it returns NULL.
The prototype for the standard library function is like this:
void *malloc(size_t size);
The free() function takes the pointer returned by malloc() and de-allocates the
memory. No indication of success or failure is returned. The function prototype is like
this:
void free(void *pointer);
There are 4 library functions provided by C defined under header file to
facilitate dynamic memory allocation in C programming. They are:
malloc()
calloc()
free()
realloc()
30. What is typedef?
typedef is a C keyword, used to define alias/synonyms for an existing type in C
language. In most cases, we use typedef's to simplify the existing type declaration
syntax. Or to provide specific descriptive names to a type.
typedef <existing-type> <new-type-identifiers>;
typedef provides an alias name to the existing complex type definition. With typedef,
you can simply create an alias for any type. Whether it is a simple integer to complex
function pointer or structure declaration, typedef will shorten your code.
31. Why is it usually a bad idea to use gets()? Suggest a
workaround.
The standard input library gets() reads user input till it encounters a new line
character. However, it does not check on the size of the variable being provided by
the user is under the maximum size of the data type which makes the system
vulnerable to buffer overflow and the input being written into memory where it isn’t
supposed to.
We, therefore, use gets() to achieve the same with a restricted range of input
Bonus: It remained an official part of the language up to the 1999 ISO C standard,
but it was officially removed by the 2011 standard. Most C implementations still
support it, but at least GCC issues a warning for any code that uses it.
32. What is the difference between #include "..." and #include
...>?
In practice, the difference is in the location where the preprocessor searches for the
included file.
For #include the C preprocessor looks for the filename in the predefined
list of system directories first and then to the directories told by the user(we can use -
I option to add directories to the mentioned predefined list).
For #include "filename" the preprocessor searches first in the same directory as the
file containing the directive, and then follows the search path used for the #include
form. This method is normally used to include programmer-defined
header files.
/* struct & union definations*/
struct bar {
int a; // we can use a & b both simultaneously
char b;
} bar;
union foo {
int a; // we can't use both a and b simultaneously
char b;
} foo;
/* using struc and union variables*/
struct bar y;
y.a = 3; // OK to use
y.b = 'c'; // OK to use
union foo x;
x.a = 3; // OK
x.b = 'c'; // NOl this affects the value of x.a!
int main()
{
char * ptr = malloc(sizeof(int));
/* Do some work */
/*Not freeing the allocated memory*/
return 0;
}
malloc()
calloc()
free()
realloc()
30. What is typedef?
typedef is a C keyword, used to define alias/synonyms for an existing type in C language. In most cases, we use typedef's to simplify the existing type declaration syntax. Or to provide specific descriptive names to a type.typedef <existing-type> <new-type-identifiers>;
31. Why is it usually a bad idea to use gets()? Suggest a workaround.
The standard input library gets() reads user input till it encounters a new line character. However, it does not check on the size of the variable being provided by the user is under the maximum size of the data type which makes the system vulnerable to buffer overflow and the input being written into memory where it isn’t supposed to.We, therefore, use gets() to achieve the same with a restricted range of input
Bonus: It remained an official part of the language up to the 1999 ISO C standard, but it was officially removed by the 2011 standard. Most C implementations still support it, but at least GCC issues a warning for any code that uses it.
32. What is the difference between #include "..." and #include ...>?
In practice, the difference is in the location where the preprocessor searches for the included file.For #include
For #include "filename" the preprocessor searches first in the same directory as the file containing the directive, and then follows the search path used for the #include
