Found 1401 Articles for C

Print a number 100 times without using loop, recursion and macro expansion in C

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

400 Views

In this section we will see how to print a number 100 times in C. There are some constraints. We cannot use loops, recursions or macro expansions.To solve this problem we will use the setjump and longjump in C. The setjump() and longjump() is located at setjmp.h library. The syntax of these two functions are like below.Example#include #include jmp_buf buf; main() {    int x = 1;    setjmp(buf); //set the jump position using buf    printf("5"); // Prints a number    x++;    if (x

Program to display hostname and IP address C

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

340 Views

In this section we will see how to see the Host name and IP address of the local system in an easier way. We will write a C program to find the host name and IP.Some of the following functions are used. These functions have a different task. Let us see the functions and their tasks.FunctionDescriptiongethostname()It finds the standard host name for the local computer.gethostbyname()It finds the host information corresponding to a host name from host databaseiten_ntoa()It converts an IPv4 Internet network address into an ASCII string into dotted decimal format.Example#include #include #include #include #include ... Read More

Modulus of two float or double numbers using C

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

6K+ Views

Here we will see how to get the modulus of two floating or double type data in C. The modulus is basically finding the remainder. For this, we can use the remainder() function in C. The remainder() function is used to compute the floating point remainder of numerator/denominator.So the remainder(x, y) will be like below.remainder(x, y) = x – rquote * yThe rquote is the value of x/y. This is rounded towards the nearest integral value. This function takes two arguments of type double, float, long double, and returns the remainder of the same type, that was given as argument. ... Read More

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

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

1K+ 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

2K+ 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

12K+ 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

247 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

653 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

1K+ 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

53 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

Advertisements