Set - 3

Question 6 :

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.


Question 7 :

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


Question 8 :

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

itoa() 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.


Question 9 :

Is it possible to execute code even after the program exits the main() function?

Answer :

The standard C library provides a function named atexit() that can be used to perform cleanup operations when your program terminates.
You can set up a set of functions you want to perform automatically when your program exits by passing function pointers to the at exit() function.


Question 10 :

What is the stack ?

Answer :

The stack is where all the functions' local (auto) variables are created. The stack also contains some information used to call and return from functions.

A stack trace is a list of which functions have been called, based on this information. When you start using a debugger, one of the first things you should learn is how to get a stack trace.

The stack is very inflexible about allocating memory; everything must be deallocated in exactly the reverse order it was allocated in. For implementing function calls, that is all that's needed. Allocating memory off the stack is extremely efficient. One of the reasons C compilers generate such good code is their heavy use of a simple stack.

There used to be a C function that any programmer could use for allocating memory off the stack. The memory was automatically deallocated when the calling function returned. This was a dangerous function to call; it's not available anymore.