How can a number be converted to a string?

Can a number be converted to a string?

  • A pointer to an array of char items that must be converted is sent to the function, and a format string must be put in a buffer as a string.

#include<stdio.h> 
#include<conio.h> 
#include <math.h>
int main() 
    char str[80]; 
 
    sprintf(str, "The value of PI = %f", M_PI); 
    puts(str);

    getch();
}
Below is the output after running the above code:

Output: Value of Pi = 3.141593

Number to String

Why n++ execute faster than n+1 ?

  • Because n++ is a unary operation, it just requires one variable. n = n + 1 is a binary operation that adds overhead to take longer (also binary operation: n += 1), whereas n = n + 1 is a binary operation that adds overhead to take longer (also binary operation: n += 1). 
  • However, on current systems, it is dependent on a number of factors, including CPU architecture, C compiler, code use, and other considerations such as hardware issues.
  • Even if you write n = n + 1 in a contemporary compiler, it will be transformed to n++ when it enters the optimized binary, and it will be equivalently efficient.

Post a Comment

Previous Post Next Post