C Articles - Page 112 of 134

trunc() , truncf() , truncl() in C language

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

2K+ Views

Here we will see three functions. These functions are trunc(), truncf() and the truncl(). These functions are used to convert floating point values into truncated form.The trunc() FunctionThis function is used to truncate double type value. And return only the integer part. The syntax is like below.double trunc(double argument)Example#include #include main() {    double a, b, x, y;    x = 53.26;    y = 75.86;    a = trunc(x);    b = trunc(y);    printf("The value of a: %lf", a);    printf("The value of a: %lf", b); }OutputThe value of a: 53.000000 The value of a: 75.000000The ... Read More

setjump() and longjump() in C

Smita Kapse
Updated on 30-Jul-2019 22:30:26

3K+ Views

In this section, we will see what are the setjump and longjump in C. The setjump() and longjump() is located at setjmp.h library. The syntax of these two functions is like below.setjump(jmp_buf buf) : uses buf to store current position and returns 0. longjump(jmp_buf buf, i) : Go back to place pointed by buf and return i.These are used in C for exception handling. The setjump() can be used as try block, and longjump() can be used as throw statement. The longjump() transfers control the pointe which is pointed by setjump().Here we will see how to print a number 100 ... Read More

How to measure time taken by a function in C?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

15K+ Views

Here we will see how to calculate the time taken by the process. For this problem, we will use the clock() function. The clock() is present in the time.h header file.To get the elapsed time, we can get the time using clock() at the beginning, and at the end of the tasks, then subtract the values to get the differences. After that, we will divide the difference by CLOCK_PER_SEC (Number of clock ticks per second) to get the processor time.Example#include #include void take_enter() {    printf("Press enter to stop the counter ");    while(1) {       ... Read More

How to change the output of printf() in main()?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

340 Views

Here we will see how to change the output of the printf() function from main(). Here we will define a function that will change all of the printf() statements with the given type to another type.We will use the #define macro to do this task. This macro will be defined inside the function. We can directly put the #define line without using it in the function, but in that case always the printf() will be changed. To control it using main, we have to call the function first.Example#include void changePrintf() { //always any printf will print 50    #define ... Read More

Write a one line C function to round floating point numbers

Smita Kapse
Updated on 30-Jul-2019 22:30:26

774 Views

Here we will see how to write one-line C function, that can round floating point numbers. To solve this problem, we have to follow these steps.Take the numberif the number is positive, then add 0.5Otherwise, subtract 0.5Convert the floating point value to an integer using typecastingExample#include    int my_round(float number) {    return (int) (number < 0 ? number - 0.5 : number + 0.5); } int main () {    printf("Rounding of (2.48): %d", my_round(2.48));    printf("Rounding of (-5.79): %d",my_round(-5.79)); }OutputRounding of (2.48): 2 Rounding of (-5.79): -6

Variable Length Argument in C

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

2K+ Views

Sometimes, you may come across a situation, when you want to have a function, which can take variable number of arguments, i.e., parameters, instead of predefined number of parameters. The C/C++ programming language provides a solution for this situation and you are allowed to define a function which can accept variable number of parameters based on your requirement. The following example shows the definition of such a function.int func(int, ... ) {    .    .    . } int main() {    func(1, 2, 3);    func(1, 2, 3, 4); }It should be noted that the function func() has ... Read More

mbtowc function in C

Smita Kapse
Updated on 30-Jul-2019 22:30:25

102 Views

The C library function int mbtowc(whcar_t *pwc, const char *str, size_t n) converts a multibyte sequence to a wide character.Following is the declaration for mbtowc() function.int mbtowc(whcar_t *pwc, const char *str, size_t n)The parameters are −pwc − This is the pointer to an object of type wchar_t.str − This is the pointer to the first byte of a multi-byte character.str − This is the pointer to the first byte of a multi-byte character.n −This is the maximum number of bytes to be checked for character length.The return values are −If str is not NULL, the mbtowc() function returns the number ... Read More

Assigning multiple characters in an int in C language

Anvi Jain
Updated on 30-Jul-2019 22:30:25

367 Views

The character type data is stored by its ASCII value internally in C or C++. If we want to print a single character as integer, we will get the ASCII value. But when we are trying to print more than one character using a single quote, then it will print some strange output.Please check the following program to get the idea.Example#include int main() {    printf("%d", 'A');    printf("%d", 'AA');    printf("%d", 'ABC'); }Output65 16705 4276803The ASCII of A is 65. So at first it is showing 65 (01000001). Now for AA, it is showing 16705. This is ASCII ... Read More

Address of a function in C or C++

Akansha Kumari
Updated on 14-Jul-2025 17:24:15

3K+ Views

In C and C++, every function is stored in the computer's memory, and each function has a memory address just like all other variables. In this article, our task is to see how we can access the address of a function and display it in both C and C++. Accessing Address of a Function To access the address of a function, we simply use its name without parentheses. When we print a function name with parentheses like hello(), we're calling the function. But if we print just hello, it gives us the memory address where the function is stored. ... Read More

Implicit initialization of variables with 0 or 1 in C

Smita Kapse
Updated on 30-Jul-2019 22:30:25

302 Views

We know that we need to declare variables before using it in our code. However, we variables can be assigned with 0 or 1 without declaration. In the following example we can see this.Example#include #include x, y, array[3]; // implicit initialization of some variables int main(i) {    //The argument i will hold 1    int index;    printf("x = %d, y = %d", x, y);    for(index = 0; index < 3; index++)       printf("Array[%d] = %d", i, array[i]);       printf("The value of i : %d", i); }Outputx = 0, y = 0 ... Read More

Advertisements