Showing posts with label c faqs with question and answers. Show all posts
Showing posts with label c faqs with question and answers. Show all posts

What is the difference between a string copy (strcpy) and a memory copy (memcpy)? When should each be used?

What is the difference between a string copy (strcpy) and a memory copy (memcpy)? When should each be used?

Question: What is the difference between a string copy (strcpy) and a memory copy (memcpy)? When should each be used?

Answer: The strcpy() function is designed to work exclusively with strings. It copies each byte of the source string to the destination string and stops when the terminating null character () has been moved. On the other hand, the memcpy () function is designed to work with any type of data. Because not all data ends with a null character, you must provide the memcpy () function with the number of bytes you want to copy from the source to the destination.
keywords:
difference between strcpy() and memcpy with example
how can i remove the trailing spaces from a string?
difference between memcpy and strncpy
how can i remove the trailing spaces from a string in c
difference between memcpy and memmove
c copy string using memcpy


How can I convert a number to a string?

How can I convert a number to a string?

Question; How can I convert a number to a string?
Answer: The standard C library provides several functions for converting numbers of all formats (integers, longs, floats, and so on) to strings and vice versa The following functions can be used to convert integers to strings:

Function Name Purpose

iota() Converts an integer value to a string.

ltoa () Converts a long integer value to a string.

ultoa () Converts an unsigned long integer value to a string.

The following functions can be used to convert floating-point values to strings:

Function Name Purpose

ecvt() Converts a double-precision floating-point value to a string without an embedded decimal point.

fcvt() Same as ecvt(), but forces the precision to a specified number of digits.

gcvt() Converts a double-precision floating-point value to a string with an embedded decimal point.


strtod() Converts a string to a double-precision floating-point value and reports any “leftover” numbers that could not be converted.

strtol() Converts a string to a long integer and reports any “leftover” numbers that could not be converted.

strtoul() Converts a string to an unsigned long integer and reports any “leftover” numbers that could not be converted.

keywords:
convert number to string c++
int to string java
convert number to string python
javascript number to string format
convert number to string typescript
int to string c
convert number to string javascript stack overflow
how to convert string to int in react native

Question: How can I convert a string to a number?

Question: How can I convert a string to a number?

Question: How can I convert a string to a number?

Answer: The standard C library provides several functions for converting strings to numbers of all formats (integers, longs, floats, and so on) and vice versa.

The following functions can be used to convert strings to numbers:

Function Name Purpose

atof() Converts a string to a double-precision floating-point value.

atoi() Converts a string to an integer.

atol() Converts a string to a long integer.
keywords:
convert string to int javascript
convert string to int c++
convert string to int in c
convert string to number python
convert string to int c#
convert char to int
convert string to number typescript
convert string to int in vue js


How many levels of pointers can you have?

How many levels of pointers can you have?

Question :How many levels of pointers can you have?

Answer: The answer depends on what you mean by “levels of pointers.” If you mean “How many levels of indirection can you have in a single declaration?” the answer is “At least 12.”

int i = 0;
int *ip01 = & i;
int **ip02 = & ip01;
int ***ip03 = & ip02;
int ****ip04 = & ip03;
int *****ip05 = & ip04;
int ******ip06 = & ip05;
int *******ip07 = & ip06;
int ********ip08 = & ip07;
int *********ip09 = & ip08;
int **********ip10 = & ip09;
int ***********ip11 = & ip10;
int ************ip12 = & ip11;
************ip12 = 1; /* i = 1 */

The ANSI C standard says all compilers must handle at least 12 levels. Your compiler might support more.
Keywords:
how many levels of indirection in pointers can you have in a single declaration?
when should we use pointers in a c program?
how to call a function without using the function name to send parameters
what is null pointer

what is far pointer?where will you use far pointer?

What is far pointer?when will you use far pointer?

Sometimes you can get away with using a small memory model in
most of a given program. There might be just a few things that don’t fit
in your small data and code segments. When that happens, you can
use explicit far pointers and function declarations to get at the rest of
memory. A far function can be outside the 64KB segment most
functions are shoehorned into for a small-code model. (Often, libraries
are declared explicitly far, so they’ll work no matter what code model
the program uses.) A far pointer can refer to information outside the
64KB data segment. Typically, such pointers are used with farmalloc()
and such, to manage a heap separate from where all the rest of the
data lives. If you use a small-data, large-code model, you should
explicitly make your function pointers far.
Keywords:
far pointer syntax in c
far pointer can access which memory location
how to use far pointer
dangling pointer complex pointer near pointer far pointer huge pointer
complex pointer in c
pointers in c
types of pointers in c
wild pointer in c

c faqs

C faqs

Question1: Difference between arrays and pointers?
 Answer: Pointers are used to manipulate data using the address. Pointers use * operator to access the data pointed to by them
 Arrays use subscripted variables to access and manipulate data. Array variables can be equivalently written using pointer expression.

Question2: What is the purpose of realloc ( )?
 Answer: The function realloc (ptr,n) uses two arguments. The first argument ptr is a pointer to a block of memory for which the size is to be altered. The second argument n specifies the
new size. The size may be increased or decreased. If n is greater than the old size and if sufficient space is not available subsequent to the old region, the function realloc ( )
may create a new region and all the old data are moved to the new region.

Question3: What is static memory allocation and dynamic memory allocation?
Answer: Static memory allocation: The compiler allocates the required memory space for a declared variable. By using the address of operator, the reserved address is obtained and this address may be assigned to a pointer variable. Since most of the declared variable has static memory, this way of assigning pointer value to a pointer variable is known as static memory allocation. Memory is assigned during compilation time.

Dynamic memory allocation: It uses functions such as malloc ( ) or calloc ( ) to get memory dynamically. If these functions are used to get memory dynamically and the values returned by these functions are assigned to pointer variables, such assignments are known as dynamic memory allocation. Memory is assigned during run time.

Question4: How are pointer variables initialized?
Answer: Pointer variable are initialized by one of the following two ways
              Ø Static memory allocation
              Ø Dynamic memory allocation

Question5: What is a pointer variable?
Answer: A pointer variable is a variable that may contain the address of another variable or any valid address in the memory.

Question6: What is a pointer value and address?
Answer: A pointer value is a data object that refers to a memory location. Each memory location is numbered in the memory. The number attached to a memory location is called the address of the location.
Keywords:
difference between arrays and pointers
purpose of realloc in c
static memory allocation and dynamic memory allocation in c
pointer variable initialization in c
pointer variable in c
what is a pointer value and address in c
difference between array and pointer in c in tabular form
differentiate between array of pointers and pointers to array
relation between array and pointer in c
difference between array and pointer in c in hindi
array of pointers in c
difference between array and pointer in hindi
which of arrays or pointers are faster
difference between array and structure
static memory allocation in c geeksforgeeks
difference between static and dynamic memory allocation? - quora
advantages and disadvantages of static and dynamic memory allocation
dynamic memory allocation in c++
static memory allocation in c++ with example program
when is memory allocated and deallocated in c? answer both static and dynamic memory.
static and dynamic memory in computer organization
dynamic memory allocation in c pdf
declaration and initialization in c
how to initialize a pointer c++
accessing a variable through its pointer
pointer arithmetic in c
pointer to pointer in c
pointer and array in c
pointer operator in c
pointer assignment in c
pointer in c example
pointers in c pdf
use of pointers in c
double pointers in c
types of pointers in c
pointers in c geeks for geeks
list of c programs on pointers
pointers and arrays in c
use of pointers in c
pointers in c geeks for geeks
list of c programs on pointers
what is pointer in c++
pointers in c pdf
pointer in c example
types of pointers in c
double pointers in c

c faqs question and answers

C faqs question and answers

C interview question1:How do I write code that executes certain function only at program termination?
Answer: Use atexit( ) function as shown in following program.
#include
main( )
{
int ch ;
void fun ( void ) ;
atexit ( fun ) ;
// code
}
void fun( void )
{
printf ( "\nTerminate program......" ) ;
getch( ) ;
}
C interview question2:What are memory models?
Answer: The compiler uses a memory model to determine how much memory is allocated to the program. The PC divides memory into blocks called segments of size 64 KB. Usually, program uses one segment for code and a second segment for data. A memory model defines the number of segments the compiler can use for each. It is important to know which memory model can be used for a program. If we use wrong memory model, the program might not have enough memory to execute. The problem can be solved using larger memory model. However, larger the memory model, slower is your program execution. So we must choose the smallest memory model that satisfies our program needs. Most of the compilers support memory models like tiny, small, medium, compact, large and huge.
C interview question3:How does C compiler store elements in a multi-dimensional array?
Answer:The compiler maps multi-dimensional arrays in two ways—Row major order and Column order. When the compiler places elements in columns of an array first then it is called column-major order. When the compiler places elements in rows of an array first then it is called row-major order. C compilers store multidimensional arrays in row-major order. For example, if there is a multi-dimensional array a[2][3], then according row-major order, the elements would get stored in memory following order:
a[0][0], a[0][1], a[0][2], a[1][0], a[1][1], a[1][2]
C interview question4:If the result of an _expression has to be stored to one of two variables, depending on a condition, can we use conditional operators as shown below?
( ( i < 10 ) ? j : k ) = l * 2 + p ;
Answer: No! The above statement is invalid. We cannot use the conditional operators in this fashion. The conditional operators like most operators, yields a value, and we cannot assign the value of an _expression to a value. However, we can use conditional operators as shown in following code snippet.
main( )
{
int i, j, k, l ;
i = 5 ; j = 10 ; k = 12, l = 1 ;
* ( ( i < 10 ) ? &j : &k ) = l * 2 + 14 ;
printf ( "i = %d j = %d k = %d l = %d", i, j, k, l ) ;
}
The output of the above program would be as given below:
i = 5 j = 16 k = 12 l = 1
C interview question5:How can I find the day of the week of a given date?
Answer: The following code snippet shows how to get the day of week from the given date. 
dayofweek ( int yy, int mm, int dd )
{
/*Monday = 1 and Sunday = 0 */
/* month number >= 1 and <= 12, yy > 1752 or so */
static int arr[ ] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 } ;
yy = yy - mm < 3 ;
return ( yy + yy / 4 - yy / 100 + yy / 400 + arr[ mm - 1] + dd ) % 7 ;
}
void main( )
{
printf ( "\n\n\nDay of week : %d ", dayofweek ( 2002, 5, 18 ) ) ;
}
C interview question6: What's the difference between these two declarations?
struct str1 { ... } ;
typedef struct { ... } str2 ;
Answer : The first form declares a structure tag whereas the second declares a typedef. The main difference is that the second declaration is of a slightly more abstract type -- its users don't necessarily know that it is a structure, and the keyword struct is not used when declaring instances of it.
C interview question7: How do I print the contents of environment variables?
Answer: The following program shows how to achieve this:
main( int argc, char *argv[ ], char *env[ ] )
{
int i = 0 ;
clrscr( ) ;
while ( env[ i ] )
printf ( "\n%s", env[ i++ ] ) ;
}
main( ) has the third command line argument env, which is an array of pointers to the strings. Each pointer points to an environment variable from the list of environment variables.
keywords:
memory models in embedded c
data memory in microcontroller
memory models in c
ram in embedded system
composing memory in embedded system
memory mapping in 8051
external memory in embedded system
memory organization of 8051
data in embedded c
program termination in c
how to end a program in c
exit(0) in c
c exit codes
exit() function in c example
how to exit c program output screen
exit(-1) in c
c quit
explain exit statement in c with example
multi dimensional array in c
two dimensional array in c definition
three dimensional array in c
two dimensional array in c++
two dimensional array in c using pointers
two dimensional array in c pdf
two dimensional string array in c
sum of elements in 2d array in c
if statement
logical operators
if statement python
boolean expression
conditional statement
if statement logic
if else statement
python if greater than or equal to
conditional operator c#
conditional operator java
conditional operator javascript
conditional operator c++
conditional operator in c
list operators used in if conditional statement in python
ternary operator
list operators used in if conditional statement in java
conditional operator in python



c answers

C answers

C interview question1: What is a stack ?
Answer: The stack is a region of memory within which our programs temporarily store data as they execute. For example, when a program passes parameters to functions, C places the parameters on the stack. When the function completes, C removes the items from the stack. Similarly, when a function declares local variables, C stores the variable's values on the stack during the function's execution. Depending on the program's use of functions and parameters, the amount of stack space that a program requires will differ.

C interview question2:Allocating memory for a 3-D array
Answer: #include "alloc.h"
#define MAXX 3
#define MAXY 4
#define MAXZ 5
main( )
{
int ***p, i, j, k ;
p = ( int *** ) malloc ( MAXX * sizeof ( int ** ) ) ;
for ( i = 0 ; i < MAXX ; i++ )
{
p[i] = ( int ** ) malloc ( MAXY * sizeof ( int * ) ) ;
for ( j = 0 ; j < MAXY ; j++ )
p[i][j] = ( int * ) malloc ( MAXZ * sizeof ( int ) ) ;
}
for ( k = 0 ; k < MAXZ ; k++ )
{
for ( i = 0 ; i < MAXX ; i++ )
{
for ( j = 0 ; j < MAXY ; j++ )
{
p[i][j][k] = i + j + k ;
printf ( "%d ", p[i][j][k] ) ;
}
printf ( "\n" ) ;
}
printf ( "\n\n" ) ;
}
}

C interview question3:How to distinguish between a binary tree and a tree?
Answer: A node in a tree can have any number of branches. While a binary tree is a tree structure in which any node can have at most two branches. For binary trees we distinguish between the subtree on the left and subtree on the right, whereas for trees the order of the subtrees is irrelevant.

Consider two binary trees, but these binary trees are different. The first has an empty right subtree while the second has an empty left subtree. If the above are regarded as trees (not the binary trees), then they are same despite the fact that they are drawn differently. Also, an empty binary tree can exist, but there is no tree having zero nodes.

C interview question4:How do I use the function ldexp( ) in a program?
Answer: The math function ldexp( ) is used while solving the complex mathematical equations. This function takes two arguments, a double value and an int respectively. The order in which ldexp( ) function performs calculations is ( n * pow ( 2, exp ) ) where n is the double value and exp is the integer. The following program demonstrates the use of this function.
#include
void main( )
{
double ans ;
double n = 4 ;
ans = ldexp ( n, 2 ) ;
printf ( "\nThe ldexp value is : %lf\n", ans ) ;
}
Here, ldexp( ) function would get expanded as ( 4 * 2 * 2 ), and the output would be the ldexp value is : 16.000000

C interview question5:Can we get the mantissa and exponent form of a given number?
Answer:The function frexp( ) splits the given number into a mantissa and exponent form. The function takes two arguments, the number to be converted as a double value and an int to store the exponent form. The function returns the mantissa part as a double value. Following example demonstrates the use of this function.
#include
void main( )
{
double mantissa, number ;
int exponent ;
number = 8.0 ;
mantissa = frexp ( number, &exponent ) ;
printf ( "The number %lf is ", number ) ;
printf ( "%lf times two to the ", mantissa ) ;
printf ( "power of %d\n", exponent ) ;
return 0 ;
}
Keywords:
c interview questions
c++ faq
faq on pointers in c
faq on arrays in c
faq videos
c tutorial
faqs on strings in c
steve summit
faq on pointers in c
faq on arrays in c
c++ faq
programming in c textbook
c tutorial
c programming resources
c language reference
cinterviews.com

c interview answers

C interview answers

C interview question1: What is atexit() ?
Answer:Function atexit( ) recevies parameter as the address of function of the type void fun ( void ). The function whose address is passed to atexit( ) gets called before the termination of program. If atexit( ) is called for more than one function then the functions are called in "first in last out" order. You can verify that from the output.


#include
void fun1( )

{

printf("Inside fun1\n");

}

void fun2( )

{

printf("Inside fun2\n");

}

main( )

{

atexit ( fun1 ) ;

/* some code */
atexit ( fun2 ) ;
printf ( "This is the last statement of
program?\n" );
}

C interview question2 How do I write a user-defined function, which deletes each character in a string str1, which matches any character in string str2?
Answer: The function is as shown below:

Compress ( char str1[], char str2[] )
{
int i, j, k ;
for ( i = k = 0 ; str1[i] != ‘\0’ ; i++ )
{
for ( j = 0 ; str2[j] != ‘\0’ && str2[j] !=
str1[i] ; j++ );
if ( str2[j] == ‘\0’ )
str1[k++] = str1[I] ;
}
str1[k] = ‘\0’
}

C interview question3:How does free( ) know how many bytes to free?
Answer: The malloc( ) / free( ) implementation remembers the size of each block allocated and returned, so it is not necessary to remind it of the size when freeing.

C interview question4:What is the use of randomize( ) and srand( ) function?
Answer: While generating random numbers in a program, sometimes we require to control the series of numbers that random number generator creates. The process of assigning the random number generators starting number is called seeding the generator. The randomize( ) and srand( ) functions are used to seed the random number generators. The randomize( ) function uses PC's clock to produce a random seed, whereas the srand( ) function allows us to specify the random number generator's starting value.

C interview question5:How do I determine amount of memory currently available for allocating?
Answer: We can use function coreleft( ) to get the amount of memory available for allocation. However, this function does not give an exact amount of unused memory. If, we are using a small memory model, coreleft( ) returns the amount of unused memory between the top of the heap and stack. If we are using a larger model, this function returns the amount of memory between the highest allocated memory and the end of conventional memory. The function returns amount of memory in terms of bytes.

C interview question6:How does a C program come to know about command line arguments?
Answer: When we execute our C program, operating system loads the program into memory. In case of DOS, it first loads 256 bytes into memory, called program segment prefix. This contains file tables,environment segment, and command line information. When we compile the C program the compiler inserts additional code that parses the command, assigning it to the argv array, making the arguments easily accessible within our C program.

C interview question7:When we open a file, how does functions like fread( )/fwrite( ), etc. get to know from where to read or to write the data?
Answer: When we open a file for read/write operation using function like fopen( ), it returns a pointer to the structure of type FILE. This structure stores the file pointer called position pointer, which keeps track of current location within the file. On opening file for read/write operation, the file pointer is set to the start of the file. Each time we read/write a character, the position pointer advances one character. If we read one line of text at a step from the file, then file pointer advances to the start of the next line. If the file is opened in append mode, the file pointer is placed at the very end of the file. Using fseek( ) function we can set the file pointer to some other place within the file.

Keywords:

c interview questions
c++ faq
faq on pointers in c
faq on arrays in c
faq videos
c tutorial
faqs on strings in c
steve summit
faq on pointers in c
faq on arrays in c
c++ faq
programming in c textbook
c tutorial
c programming resources
c language reference