Technical Questions Based On C Language Basics
Technical Questions Based On C Language Basics
• Is it possible for the last case of a switch statement to skip including the break?
The last case of a switch statement doesn’t require a break statement.
• Barring the for statement, where else is the comma operator used?
Where the expression 1 is valve first and after than expression is returned for the
expression.
• Explain lvalue?
An I value is an expression reference , such as a variable name, an array subscript
reference a dereference pointer or a function call that returns a reference.
• Can an array be an lvalue?
An array name cannot be an I value.
• Explain rvalue?
Refers to data value that is stored at same address in memory.
• With the help of #include, can a file other than a .h file be included?
A .h file be included.
• Explain the best way to comment out a section of code that contains comments?
C style comment /* */ or / /
Header files provide declarations for functions, types, and other entities that are defined
in other source files or libraries. The #include directive is used to include a header file in
your source code.
• Explain the way to make a program print the line number where an error occurs?
To make a program print the line number where an error occurs:
• Explain the way to make a program print the name of a source file where an error occurs?
In C, you can achieve a similar result by using the __FILE__ macro, which expands to
the name of the current source file as a string.
• How do you tell whether a program was compiled using C versus C++?
By looking at the file extension of the source code files.
• What is a pragma? What is #line used for?
A pragma is a compiler directive that allows you to provide additional information to
the compiler.
#line – to cause error message to refer to the original source file instead of the generated
program.
• How do you make sure that a program follows the ANSI C standard?
ANSI(American National standard Institute)
Use one of the option , -std =c90 or -std=ISO9899:1990
• Is it possible to tell the size of an array passed to a function with the help of the size of
operator?
No, it is not possible to tell the size of the array passed to a function with the help of the
size of operator.
• What is better to navigate an array of values? To use a pointer or to use a subscripted array
name?
Pointer is more efficient.
• What is a stream?
An idealized flow of data to which the actial input or output is mapped.
• How can a file be opened so that other programs can update it at the same time?
To allow multiple programs to update a file simultaneously in C:
1. Use the `flock` function for advisory locks on the entire file or the `fcntl` function for
locks on specific ranges.
2. Apply a shared lock (`LOCK_SH`) for read access or an exclusive lock
(`F_WRLCK`) for write access.
3. Ensure all programs cooperate and honor the locks to avoid conflicts.
4. Release the lock using `flock` or `fcntl` when done with file operations.
These mechanisms help coordinate access and prevent conflicts when multiple
programs are interacting with the same file.
• How can it be made sure that my program is the only one accessing a file?
In C, you can take several measures to ensure exclusive access to a file:
1. File Permissions:
- Use functions like `chmod` to set appropriate file permissions, allowing only your
program's user to access the file.
2. File Locking:
- Implement file locking using functions like `flock` or `fcntl` to prevent simultaneous
access by multiple programs.
3. Encryption:
- Encrypt the file content using cryptographic libraries to make it unreadable without
the proper decryption key.
4. Authentication:
- Implement user authentication mechanisms within your program to control access to
the file.
5. Obfuscation:
- Apply code obfuscation techniques to make it harder for others to understand and
tamper with your program.
6. Token-Based Authorization
- Use tokens to authorize access, ensuring only valid requests from your program can
access the file.
Remember to handle errors and edge cases appropriately in your code for a robust
implementation.
• How can it be prevented that another program from modify a part of a file that I am
modifying?
Set file permission: By setting appropriate file permission, one can control who has
access to read , write or modify the file.
• When should the register modifier be used? How does it help if at all it does?
The register modifier hints to the compiler that the variable will be the heavily used and
should be kept in the CPU’s register.
• How do you determine the maximum value that a numeric variable can hold?
In c one can determine the maximum value that a numeric variable can hold by using
the constant defined in the <limits.h> header for integer types and the <float.h> header
for floating point type.
smallest type that can hold both values. The result has the same type as the two operands
wind up having. Interpret the rules, read the following table from the top down, and stop at the
• Is it necessary for a function to contain a return statement if it does not return a value?
No, a function does not always have to an explicit return statement.
• Differentiate between a string copy (strcpy) and a memory copy (memcpy)? Explain the
usage of each
Memcpy() function is used to copy a specific number of bytes from one memory to
another.
Strcpy() is used to copy the contents of one string into another.
• How do I remove the trailing spaces from a string?
• Input string from user, store it in some variable say str.
• Initialize a variable to store last index of a non-white space character, say index
= -1.
• Run a loop from start of the str till end.
• Inside the loop if current character is a non-white space character, then update
the index with current character index.
• Finally, you will be left with last index of non-white space character.
Assign NULL character at str[index + 1] = ‘\0’
• What is indirection?
A unary operator that can be used to obtain the value stored at the memory location
referenced by a pointer variable.
• How is an array that can hold more than 64KB of data declared?
There is no fixed limit to the size of an array in c.
FAR: In a far memory model pointer can address a larger amount can address a larger
amount of memory than near pointer.
Are used when data or function can be located in different segment of memory.
• When is the far pointer used?
Access the information stored outside the computers memory from the current segment.
• What is a stack?
In c, the stack refers to a region of memory used for the storage of local variables,
function parameter and other related data during the execution of a program.
• What is a heap?
The region of memory used for dynamic memory allocation.
• What is a "null pointer assignment" error? Explain bus errors, memory faults, and core
dumps?
These all are serious errors symptoms of a wild pointers or subscript.
• To define the standard library functions that we use, what header files do we need?
Included with the c compiler these header files provide acceleration once definition for
all variables and predefined functions or method found in c standard library for
example the header files-
a) stdio.h
b)stdlib.h
c)string.h
• How are functions that take a variable number of arguments written?
A function that takes a variable no. of arguments is called a variable function.
• What is a "locale"?
A key concept for application programs is that of a program's locale. The locale is an
explicit model and definition of a native-language environment. The notion of a locale is
explicitly defined and included in the library definitions of the ANSI C Language
standard.
• What are the math functions available for integers? For floating point?
They are floor, ceil, abs, sqrt, round.