Campus Monk C
Campus Monk C
Answer:
C has characteristics of both assembly-level i.e. low-level and higher-level languages. So
as a result, C is commonly called a middle-level language. Using C, a user can write an
operating system as well as create a menu-driven consumer billing system.
Answer:
1. It is Simple And E cient.
2. C language is portable or Machine Independent.
3. C is a mid-level Programming Language.
4. It is a structured Programming Language.
5. It has a function-rich library.
6. Dynamic Memory Management.
7. C is super fast.
8. We can use pointers in C.
9. It is extensible.
Answer:
The individual elements of a program are called Tokens. There are following 6 types of
tokens are available in C:
• Identi ers
• Keywords
• Constants
• Operators
• Special Characters
• Strings
Answer:
• printf() is used to print the output on the display.
• scanf() is used to read formatted data from the keyboard.
Answer:
The string mentioned "abxdef" is an array, and the expression is equal to "abxdef"[5].
The answer is 'f'.
fi
ffi
Q6. What is a built-in function in C?
Answer:
Built-function is also known as library functions that are provided by the system to make
the life of a developer easy by assisting them to do certain commonly used prede ned
tasks. For example, if you need to print output or your program into the terminal, we use
printf() in C. The most commonly used built-in functions in C are scanf(), printf(), strcpy,
strlwr, strcmp, strlen, strcat, etc.
Answer:
A preprocessor is a software program that processes a source le before sending it to be
compiled. Inclusion of header les, macro expansions, conditional compilation, and line
control are all possible with the preprocessor.
Answer:
#line is used as a preprocessor to re-set the line number in the code, which takes a
parameter as line number. Here is an example for the same.
Answer:
The function takes the string as an input that needs to be converted to an integer.
Syntax: int atoi(const char *string)
Return Value:
• On successful conversion, it returns the desired integer value
• If the string starts with alpha-numeric char or only contains alpha-num char, 0 is
returned.
• In case string starts with numeric character but is followed by alpha-num char, the
string is converted to integer till the rst occurrence of alphanumeric char.
Answer:
The function takes a pointer to an array of char elements that need to be converted, and a
format string needs to be written in a bu er as a string
Syntax: int sprintf(char *str, const char *format, ...)
Answer:
fi
fi
ff
fi
fi
When a function in C calls a copy of itself, this is known as recursion. To put it another
way, when a function calls itself, this technique is called Recursion. Also, this function is
known as recursive function.
Answer:
After you compile the C source, the symbol names need to be intact in the object code. If
we introduce function overloading in our source, we should also provide name mangling
as a preventive measure to avoid function name clashes. Also, as C is not a strictly typed
language many things(ex: data types) are convertible to each other in C. Therefore, the
complexity of overload resolution can introduce confusion
Q13. What is the di erence between global int and static int declaration?
Answer:
The di erence between this is in scope. A truly global variable has a global scope and is
visible everywhere in your program.global_temp is a global variable that is visible to
everything in your program, although to make it visible in other modules, you'd need an ”
extern int global_temp; ” in other source les if you have a multi- le project.
A static variable has a local scope but its variables are not allocated in the stack segment
of the memory. It can have less than global scope, although - like global variables - it
resides in the .bss segment of your compiled binary.
Answer:
A pointer is a variable that stores or points to another variable's address. The value of a
variable is stored in a normal variable, whereas the address of a variable is stored in a
pointer variable.
Answer:
• const char* p is a pointer to a const char.
• char const* p is a pointer to a char const.
Since const char and char const are the same, it's the same.
Answer:
In C, a pointer can also be used to store the address of another pointer. A double pointer
or pointer to pointer is such a pointer. The address of a variable is stored in the rst
pointer, whereas the address of the rst pointer is stored in the second pointer.
Answer:
n++ being a unary operation, it just needs one variable. Whereas, n = n + 1 is a binary
operation that adds overhead to take more time (also binary operation: n += 1). However,
in modern platforms, it depends on few things such as processor architecture, C
compiler, usage in your code, and other factors such as hardware problems. While in the
modern compiler even if you write n = n + 1 it will get converted into n++ when it goes
into the optimized binary, and it will be equivalently e cient.
Answer:
Typecasting is the process to convert a variable from one datatype to another. If we want
to store the large type value to an int type, then we will convert the data type into another
data type explicitly.
Syntax: (data_type)expression;
Answer:
Macro on a high-level copy-paste, its de nitions to places wherever it is called. Due to
which it saves a lot of time, as no time is spent while passing the control to a new
function and the control is always with the callee function. However, one downside is the
size of the compiled binary is large but once compiled the program comparatively runs
faster.
Answer:
Enumeration, also known as Enum in C, is a user-de ned data type. It consists of
constant integrals or integers that have names assigned to them by the user. Because the
integer values are named with enum in C, the whole program is simple to learn,
understand, and maintain by the same or even di erent programmer.
Answer:
If a variable is used frequently, it should be declared with the register storage speci er,
and the compiler may allocate a CPU register for its storage to speed up variable lookup.
Answer:
• 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 identi er.
Answer:
calloc() and malloc() are memory dynamic memory allocating functions. The main
di erence 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.
Answer:
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.
Answer:
When we caller function makes a function call bypassing the addresses of actual
parameters being passed, then this is called call by reference. In call by reference, the
operation performed on formal parameters a ects the value of actual parameters
because all the operations performed on the value stored in the address of actual
parameters.
ff
ff
ff
ff
fi
Q27. What is pass by reference in functions?
Answer:
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.
Answer:
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.
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.
Answer:
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);
Answer:
fi
fi
typedef is a C keyword, used to de ne 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 speci c descriptive names to a type.
typedef provides an alias name to the existing complex type de nition. 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.
Answer:
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 bu er over ow
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.
Q32. What is the di erence between #include "..." and #include <...>?
Answer:
In practice, the di erence is in the location where the preprocessor searches for the
included le.
For #include < lename> the C preprocessor looks for the lename in the prede ned list of
system directories rst and then to the directories told by the user(we can use -I option to
add directories to the mentioned prede ned list).
For #include " lename" the preprocessor searches rst in the same directory as the le
containing the directive, and then follows the search path used for the #include
< lename> form. This method is normally used to include programmer-de ned header
les.
Q33. What are dangling pointers? How are dangling pointers di erent from memory
leaks?
Answer:
The dangling pointer points to a memory that has already been freed. The storage is no
longer allocated. Trying to access it might cause a Segmentation fault. The dangling
pointer points to a memory that has already been freed. The storage is no longer
allocated. Trying to access it might cause a Segmentation fault.
A memory leak is something where the memory allocated is not freed which causes the
program to use an unde ned amount of memory from the ram making it unavailable for
every other running program(or daemon) which causes the programs to crash. There are
fi
fi
fi
fi
fi
fi
ff
fi
ff
fi
fi
fi
fi
fi
fi
fi
ff
fi
ff
fi
fi
fl
various tools like O pro le testing which is useful to detect memory leaks on your
programs.
Answer:
In C double-quotes variables are identi ed as a string whereas single-quoted variables
are identi ed as the character. Another major di erence being the string (double-quoted)
variables end with a null terminator that makes it a 2 character array.
Answer:
• Near Pointer: In general, the near pointer can be considered because it is used to
hold the address, which has a maximum size of just 16 bits. We can't store an
address with a size larger than 16 bits using the near pointer. All other smaller
addresses that are within the 16-bit limit, on the other hand, can be stored.
Because we can only access 64kb of data at a time, you might assume the 16 bits
are insu cient. As a result, it is regarded as one of the near-pointer's biggest
drawbacks, which is why it is no longer commonly used.
• Far Pointer: A far pointer is considered a pointer of size 32 bits. It can, however,
use the current segment to access information stored outside the computer's
memory. Although, in order to use this type of pointer, we usually need to allocate
the sector register to store the data address in the current segment.
Q36. Which structure is used to link the program and the operating system?
Answer:
The le structure is used to link the operating system and a program. The header le
"stdio.h" (standard input/output header le) de nes the le. It contains information about
the le being used like its current size and its memory location. It contains a character
pointer that points to the character which is currently being opened. When you open a
le, it establishes a link between the program and the operating system about which le is
to be accessed.
Q37. Suppose a global variable and local variable have the same name. Is it possible
to access a global variable from a block where local variables are de ned?
Answer:
No. This isn’t possible in C. It’s always the most local variable that gets preference.
Answer:
Yes, we can compile a program without main() function Using Macro
Answer:
To override a de ned macro we can use #ifdef and #undef preprocessors as follows:
• #ifdef A
• #undef A
• #endif
• #de ne A 10
If macro A is de ned, it will be unde ned using undef and then de ned again using de ne.
Answer:
To call a function before the main(), pragma startup directive should be used.
Example:
#pragma startup fun
void fun()
{
printf("In fun\n");
}
main()
{
printf("In main\n");
}
This pragma directive, on the other hand, is compiler-dependent. This is not supported by
gcc. As a result, it will ignore the startup directive and produce no error. But the output, in
that case, will be In main.
Answer:
The di erence between the Source Code and Object Code is that Source Code is a
collection of computer instructions written using a human-readable programming
language while Object Code is a sequence of statements in machine language, and is the
output after the compiler or an assembler converts the Source Code.
The lastly, Object Code is the way the changes are re ected. When the Source Code is
modi ed, each time the Source Code needs to be compiled to re ect the changes in the
Object Code.
Q44. What are header les and what are its uses in C programming?
Answer:
In C header les must have the extension as .h, which contains function de nitions, data
type de nitions, macro, etc. The header is useful to import the above de nitions to the
source code using the #include directive. For example, if your source code needs to take
input from the user do some manipulation and print the output on the terminal, it should
have stdio.h le included as #include <stdio.h>, with which we can take input using
scanf() do some manipulation and print using printf().
fi
ff
fi
ff
fi
fi
fi
fl
fl
fi
fi
Q45. When is the "void" keyword used in a function?
Answer:
The keyword “void” is a data type that literally represents no data at all. The most obvious
use of this is a function that returns nothing.The other use for the void keyword is a void
pointer. A void pointer points to the memory location where the data type is unde ned at
the time of variable de nition. Even you can de ne a function of return type void* or void
pointer meaning “at compile time we don’t know what it will return.
Answer:
A dynamic data structure (DDS) refers to an organization or collection of data in memory
that has the exibility to grow or shrink in size, enabling a programmer to control exactly
how much memory is utilized. Dynamic data structures change in size by having unused
memory allocated or de-allocated from the heap as needed.
Dynamic data structures play a key role in programming languages like C, C++, and Java
because they provide the programmer with the exibility to adjust the memory
consumption of software programs.
Answer:
• A variable which is declared as static is known as a static variable. The static
variable retains its value between multiple function calls.
• Static variables are used because the scope of the static variable is available in the
entire program. So, we can access a static variable anywhere in the program.
• The static variable is initially initialized to zero. If we update the value of a variable,
then the updated value is assigned.
• The static variable is used as a common value which is shared by all the methods.
• The static variable is initialized only once in the memory heap to reduce the
memory usage.
Answer:
in C, every local variable of a function is known as an automatic (auto) variable. Variables
which are declared inside the function block are known as a local variable. The local
variables are also known as an auto variable. It is optional to use an auto keyword before
the data type of a variable. If no value is stored in the local variable, then it consists of a
garbage value.
Answer:
fl
fi
fi
fl
fi
The argument passed to the main() function while executing the program is known as
command line argument.
example:
main(int count, char *args[])
{
//code to be executed
}
Answer:
The getch() function reads a single character from the keyboard. It doesn't use any bu er,
so entered data will not be displayed on the output screen.
The getche() function reads a single character from the keyword, but data is displayed on
the output screen. Press Alt+f5 to see the entered character.
Answer:
The ANSI stands for " American National Standard Institute." It is an organization that
maintains the broad range of disciplines including photographic lm, computer
languages, data encoding, mechanical parts, safety and more.
Answer:
huge pointers have explicit selector. When you perform pointer arithmetic the selector
can be modi ed.
Q53. What are the functions to open and close the le in C language?
Answer:
The fopen() function is used to open le whereas fclose() is used to close le.
Answer:
Yes, by holding the base address of array into a pointer, we can access the array using a
pointer.
Q55. Who is the main contributor in designing the C language after Dennis Ritchie?
Answer:
Brain Kernighan
Q56. What is the use of a semicolon (;) at the end of every program statement?
fi
ff
fi
fi
fi
fi
ff
Answer:
It is majorly related to how the compiler reads( or parses) the entire code and breaks it
into a set of instructions(or statements), to which semicolon in C acts as a boundary
between two sets of instructions.
Q57. Can you tell me how to check whether a linked list is circular?
Answer:
Circular linked list is a variation of a linked list where the last node is pointing to the rst
node's information part. Therefore the last node does not point to null.
———————————————————
Q1. How can you remove duplicates in an array? Illustrate with a code.
Q2. Write a program to get the higher and lower nibble of a byte without using shift
operator?
Q4. Write a program to check the given number format is in binary or not.
Q6. Write a program to Add Two Numbers Without Using the Addition Operator and
Subtract Two Number Without Using Subtraction Operator.
Q8. Write a programCheck whether the number is EVEN or ODD, without using any
arithmetic or relational operators.
Q13. Write a program to nd the node at which the intersection of two singly linked
lists begins.
Example 2:
Input: s = "()[]{}"
Output: true
Example 3:
Input: s = "(]"
Output: false
————————————————
TEST YOURSELF
A. An alphabet
B. A number
C. A special symbol other than underscore
D. Both (B) and (C)
A. Call by Value
B. Call by reference
C. Call by address
D. Call by name
Q4. In the switch statement, each case instance value must be _______?
A. Constant
B. Variable
C. Special symbol
D. None to the above
Q7. The number of binary trees that can be formed using 5 nodes are
A. 30
B. 42
C. 108
D. 36
Q8. What is the correct syntax to access the value of struct variable book{ price, page }?
A. String
B. Queue
C. List
D. All of the above
fl
fl
Q10. A binary tree with 27 nodes has _______ null branches.
A. 54
B. 27
C. 26
D. None of the above
————————————