0% found this document useful (0 votes)
20 views

Technical Questions Based On C Language Basics

The document discusses various technical questions about C language basics including questions about local blocks, switch statements, array handling, preprocessor directives, macros, and more. It provides explanations and differentiation of concepts like goto, long jmp(), and setjmp(). It also covers standard predefined macros like DATE, FILE, and LINE.

Uploaded by

Shail Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Technical Questions Based On C Language Basics

The document discusses various technical questions about C language basics including questions about local blocks, switch statements, array handling, preprocessor directives, macros, and more. It provides explanations and differentiation of concepts like goto, long jmp(), and setjmp(). It also covers standard predefined macros like DATE, FILE, and LINE.

Uploaded by

Shail Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

List of Technical Questions based on C Language Basics

• Define local block?


In the Context of C programming a “local block” is a code enclosed in curly braces ‘{}’.

• Variables should be stored in local blocks. Yes or no?


NO

• A switch statement is better than multiple if statements in what all situations?


Which you want to compare of single expression on to multiple values.
It is entirely optional.

• Can a switch statement be written without a default case?


Yes, a switch statement can be written without a default case. In many programming
languages, the default case is optional. If none of the specified case values match the
expression being tested in the switch statement, and there is no default case, then no
code will be executed.

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

• How do you determine whether a loop ended prematurely?


Use a flag variable with a special condition.

• Differentiate between goto, long jmp( ) and setjmp()?


A goto statement implements a local jmp() of program execution and long jmp() and
setmp() functions implement a nonlocal jump.

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

• Is right-to-left or left-to-right order guaranteed for operator precedence?


Left to right.

• Differentiate between ++var and var++?


The ++var first adds the value by one and prints the result whereas var++ first prints
the value which is assigned and then increments the value by one.

• Explain the function of the modulus operator?


Returns the remainder or signed of a division after one no. is divided by operator.

• What is the most efficient way to store flag values?


Single bits or groups of bits just large enough to hold an possible values.

• Explain "bit masking"?


the process of storing data truly as bits, as opposed to storing it as chars/ints/floats.

• Are bit fields portable?


Yes, bit field in c language are not inherently portable across different platform and
complain.

• Is it better to bitshift a value than to multiply by 2?


Code that contain bitwise shifts execute faster thsn the code containing multiplication
by power of two.

• What is meant by high-order and low-order bytes?


Both are refer to the order in which bytes are used in computer memory.
• What is the way to store 16-bit and 32-bit numbers ?
Int, short and unsigned.

• Explain macro and its usage


Macros in the C programming language are a powerful tool that allows developers to
define reusable pieces of code, constants, and even function-like constructs.

• What purpose does preprocessor fulfill for a program?


The C preprocessor is a tool that runs before the actual compilation of C program.
#define – used to define a macro
#undef – used to undefine a amcro
#include – used to include a file in the source code program.
#ifdef- used to include a section of code if a certain macro is defined by #define
#if #else #elseif.

• How can you avoid including a header more than once?


Use inclusion gaurds in your header file to process multiple inclusion.

• With the help of #include, can a file other than a .h file be included?
A .h file be included.

• What is the benefit of using #define while declaring a constant?


Enables you to declare a constant in one place and use it through out your program.

• What is the benefit of using enum while declaring a constant?


Makes the entire program easy to learn, understood and maintain the same or different
programmer.

• Why is it better to use an enum rather than a #define constant?


Lower maintenance requirement, improved program reliability and better aebuggung
capability.
• In demo versions, how are the portions of a program disabled?
In demo versions of software, portions are disabled through methods like feature
limitation, time restrictions, functionality limits, watermarking, pop-up messages,
internet dependency, and reduced capacity to encourage users to purchase the full
version.

• What is better? Using a macro or using a function?


The main benefit of using macros is faster execution time.

• Explain the best way to comment out a section of code that contains comments?
C style comment /* */ or / /

• Differentiate between #include and #include "file"?


#include generally indicates the header file that can developed created, generally reside
in the local folder.
#include” ”is used for header file in current directory.

• Can it be defined which header file to include at compile time?


In most programming languages, including C and C++, the inclusion of header files is
determined at compile time, not runtime. When you write your source code, you specify
which header files to include, and this information is used during the compilation
process.

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.

• Is it possible that include files be nested?


Include files can be nested.

• How many levels deep can you nest include files?


No limit.
• Explain the significance of concatenation operator?
The concatenation operator is C binary operator that combines two strings into one.

• The concatenation operator to create call to s type sensitive.


If you want to concatenate strings, you often use the `+` operator, but the behavior can
depend on the programming language and data types involved. Be aware of type
sensitivity when using the concatenation operator with different data types.

• Ho w do you create type-insensitive macros?


Using the concatenation operator to create a call to a type sensitive function based on
the parameter passed to the macro.

• What are the standard predefined macros?


DATE-
FILE-
LINE –

• 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:

1. Use exception handling (try-catch) in languages that support it.


2. Utilize logging mechanisms to log line numbers along with errors.
3. In C/C++, use preprocessor directives like `__LINE__`.
4. Insert debugger statements or print statements strategically for debugging.

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

• Explain the usage of #line?


Its main use is to make the compiler provide more meaningful error message.

• Explain the significance of __DATE__ and __TIME__ preprocessor commands?


The current data as character in “MM DD YYYY” format _time_: the current time as
character in “HH:MM: SS” Format.

• 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

• What is the procedure to override a defined macro?


Used the #undef processor directive.

• How do you check whether a symbol is defined?


One can use #ifed and #idef preprocessor directive.

• Do array subscripts always start with zero?


Yes array subscript or indexing is always starts with zero.
• Can we address one element beyond the end of an array?
It does, however , allow a pointer to point at one element beyond the end of the array.

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

• Can an array tag be assigned a different address?


No an Array tag cannot be assigned a different address in the C language.

• Differentiate between array_name and &array_name?


“Array” is a pointer to the first element of array but “&array” is a pointer to whole
array of 5 int.

• Why don't we use constant values to define an array's initial size?


If the size of the array could be determined at the runtime using a constant value. It
would make it difficult for the compiler to allocate the necessary memory.

• Differentiate between a string and an array?


Array: An array is a collection of similar data types such as integer character.
String: A string is a sequence of character.

• Is there an error if errno contains a non-zero number?


Yes, a non-zero value in `errno` generally indicates that an error occurred during the
execution of a system call or library function in C and C++ programming. The specific
meaning of the error code should be checked in the documentation of the relevant
function.

• What is a stream?
An idealized flow of data to which the actial input or output is mapped.

• How is a standard stream redirected?


Standard input can be redirected to read from a file or the output of another system.
• How is a redirected standard stream restored?
By using the standard library function named dup() and fdopen().
• Can we force stdout to be printed somewhere other than the screen?
Yes

• Differentiate between text and binary modes?


Binary modes means do not translate the character contained in the file.
Text modes means translate the character from ASCII on the PC to EBCDIC on the
host.

• How is it determined whether a stream function or a low-level function should be used?


Depends on the specific requirements and characteristic of the fast at band.

• How are the files listed in a directory?


Call opendir() function to open all file in present directory.
Initialize or pointer as dr=operator(“.”) ip(dr) while ((en=reddir car)!=NULL) prints all
the file name using en -> name.

• How are the files' date and time listed in a directory?


In c , one can use ‘direct.h’ header file to work with directories and retrieve information
about each directory, ‘the struct direct’ structure provides information about each
directory entry and the ‘stat’ function can be used to obtain details such as the file dat
and time.

• How are the file names sorted in a directory?


In C, the order in which file names are listed in a directory depends on the underlying
file system and the functions used for directory listing. The C standard library does not
specify a particular order. If you need a sorted list, you can collect the names into an
array and use a sorting function like `qsort` to sort them based on your criteria. The
order is typically influenced by the file system and the order in which entries appear in
the directory.

• How are the attributes of a file determined?


In c one can use ‘sat’ or ‘fsat’ function to determine the attributes of a file.
• How is the PATH viewed?
The PATH environment variable is a list of directories that the operating system
searches for executable files when a user enters a command.

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

• How can Abort, Retry and Fail messages be avoided?


Causes an abnormal end of the program and returns control to the host environment.

• How do I read and write comma-delimited text?


Reading and writing comma-delimited text ofen referred to as CSU(Comma separated
values) is a common task in programming.
CSU: will create a file pointer, which will be used to access and write to a file.

• Where are my variables stored in memory?


In c, the storage location of variables depends on the type of variable and its scope.

• Is it needed for the variables to be initialized?


Variables do not have to initialized.
• Explain the usage of a const pointer?
When we want a pointer to a constant variable as well as keep the address in the pointer
as Constant.

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

• When should we use the volatile modifier?


To indicate to the compiler that a variable value may change unexpectedly.

• Is it possible for a variable to be both const and volatile?


Yes, a variable can be both constant and a volatile.

• When should we use the const modifier?


The ‘const’ modifier in C is used to declare variable as constant , indicating that their
should not be modified after initialization.

• How reliable are floating-point comparisons?


Comparisons between two floating point values is a exact and reliable.

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

• Do any problems occur if we try to perform mathematical operations on different variable


types?
Yes, problem occurs.
• Explain operator promotion?
If an operation is specified with operands of two different types, they are converted to the

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

first rule that applies.

• When should we use type cast?


When both the data type in the equation aren’t compatible at all with each other.

• When should we not use a type cast?


To override a const or volatile declaration.

• Can a variable be declared / defined in a C header?


While one can add or declare variable in a header file. It generally not a good practice.

• Differentiate between declaring a variable and defining a variable?


Declaring a variable:
It informs the compiler about the existence and type of variable without allocating
memory.
Defining a variable:
This will not only declare the variable bit also allocates memory for it and optionally
initialize the value.

• Can we declare static variables in a header file?


Static variable can be declared in the header files or any other source file but in the
header file .They can only be initialize once while in the source file they can be
initialized any number of times.

• How does it benefit to use const for declaring constants?


It makes an code more readable by clearly indicating that a particular variable is meant
to be a constant.
It communicates the developers intent that the value should not be modified.
• When should a function be declared?
When one defines a function in one source file and one call that function in another file.

• When do we prototype a function?


To declare the signature of a function.

• How many parameters should a function have?


At least 127 parameters/arguments.

• What is a static function?


One that has its scope limited to the file in written it is declared.

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

• How do you pass an array to a function by value?


Wrapping the array in a structure and creating a variable of type of that structure and
assigning values to that array.

• Can the code be executed after it exits the main() function?


• In C and C++, functions registered through atexit() can be called
after main() returns.
• In C++, static destructors can also be called after main () returns.

• What does a function that is declared as PASCAL do differently?


Returns an value.

• Is it the same thing to use exit()or use return?


No exit function not same as a return.

• 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’

• How do I remove the leading spaces from a string?


• Initialize count = 0 to count the number of leading spaces.
• Iterate through given string and find the index(say idx) at which the leading
space character ends.
• Iterate through all the characters from that index idx and copy each character
from this index to the end to the front index.
• Finally, put ‘\0’ at the last index of the new string to terminate the string.

• How do I right-justify a string?


• One should has to create an function.

• How do I pad a string to a known length?


The printf() statement can be used with the following format specifier to print with
padding by zeros.

• How do I copy just a portion of a string?


Use string function strcpy() to copy strings.

• How do I convert a number to a string?


By using ‘sprintf’ function.

• How do I convert a string to a number?


The atoi() function is used.
• How do you determine if two strings are the same?
By using the strcmp() function <string.h> header file.

• Explain the way to print only part of a string?


substr(i, len) prints substring of length 'len' starting from index i in string

• 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 many levels of pointers are possible?


In c theoretically the level the pointer is unlimited.

• What is a null pointer?


A pointer that does not point to any memory location.

• When do we use a null pointer?


When the pointer variable does not point to a valid memory address.

• What is a void pointer?


A pointer that is not associated with any data types.

• When do we use a void pointer?


In generic functions.

• Can pointers be subtracted from each other? When is it done?


Both must point to elements of the same array or just one past the cast element of the
array object.

• Is NULL always defined as 0(zero)?


No

• Is NULL always equal to 0(zero)?


No
• What does it signify when a pointer is used in an if statement?
The pointer should point to something and in addition that something must be different
from 0.

• Can pointers be added? When is it done?


Adding two pointer is illegal in c program but pointers and integers addition is legal.

• How do you use a pointer to a function?


Declared before referencing them to a function.

• When do you use a pointer to a function?


To call function and to pass function as arguments to other function.

• Can the array be declared at runtime?


No

• Use of which of these is better: malloc() or calloc()?


MALLOC(): allocate a specified no. of bytes of memory.
The allocated memory is not initialized its content is intermediate.
CALLOC(): allocate a specified no. of blocks of memory each with a specified size.
Initialize the allocated memory to zero.

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

• Differentiate between far and near?


NEAR: In a near memory model, pointer can address only a limited amount of memory
typically up to 64 kb.
Near pointers are often for local variable and function within a single segment of
memory.

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 happens if we free a pointer twice?


This is undefined behaviour.

• Differentiate between NULL and NUL?


The C NUL is a single character that compares equal to 0.
The C NULL is a special character reserved pointer value that does not point to any
valid data object.

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

• How is the size of an allocated portion of memory determined?


Using some standard library function.

• How does free() know how much memory should be released?


To dellocate memory dynamically.

• Can we perform math operations on a void pointer?


You cannot perform pointer arithmetic on a void pointer.

• How is an address printed?


To print the address of a variable there is one unique format specifier that is o/p.
• Why should we use standard library functions instead of writing our own?
They work , they are efficient that they are portable.

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

• Differentiate between a free-standing and a hosted environment?


1) free standing environment
• No assumption of an OS.
• Limited or no support for standard libraries.
• The program may have direct control over the hardware.
2) hosted environment
• Assumes the presence od an OS.
• Has access to standard libraries providing various services.
• Common in desktop, server, and general purpose computing.

• Mention the standard functions available to manipulate strings?


a) strlen()
b)strcpy()
c)strcat()
d)strcmp()
e)strchr()
f)strlwr()
g)strup()
• How is it determined whether a character is numeric, alphabetic, and so on?
Using standard library.

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

• Is there a way to jump out of a function or functions? If yes, what?


Return statement in c

• What is a signal? What do I use signals for?


To report exceptional situation to an executing program.

• Why should variable names not be started with underscores?


Both name are used for system level identifiers and reserved for use by the compiler
and library implementation.

• What are the math functions available for integers? For floating point?
They are floor, ceil, abs, sqrt, round.

• What are multi-byte characters?


A byte sequence that encoded an ideogram no matter what encoding scheme is
employed.

• How can I manipulate strings of multi-byte characters?


While using the following functions-
a) mbstombs()
b) wesnrtombs()
c)mblen()
d) wctomb()
e)mbtoec().

You might also like