How to Add Two int Numbers Using Printf Function in C

Program:
int addition(int x, int y)
{
    return printf("%*c%*c",x,'o',y,'h');
}
 
void main()
{
    printf("Sum = %d\n", addition(5, 7));
}


Output:                                                                                                                         o      hSum = 12 

Here in this program we can see that we have written "%*c". Here * is used to pass the variable length to display for the given character. So here in addition function we have passed the argument as 5 and 7. It will act as %5C %7C in the printf function.

As we all know that printf will return the number of character print successfully. So at the end it will print a total number of character which is equal to 5+7= 12.

Here There is 5 character is given to print "o" & 7 character is reserved to print h. So you can see the space between both characters.

Now if you change the number passed in argument to the function addition with 1 & 2 then you can see the difference in output.

int addition(int x, int y)
{
    return printf("%*c%*c",x,'o',y,'h');
}
 
void main()
{
    printf("Sum = %d\n", addition(1, 2));
}

Output:
o hSum = 3

Here in the output you can see only character is used to print the "o" and 2 characters is used for printing "h" in the output as we have specified 1 & 2 in the argument to addition function.


Related Post:


Removing Space from Given String & Different Methods to Count Number of Ones in Given Value

Booting Sequence for Boot Process in Linux

How to count number of 1 or set bits in a given number

What is Segmentation fault?

How to Access Private Data Members in C++ without using friend function

Process Vs Thread 

How to Add Two Numbers Without Using + Operator in C 

Overview of Function Pointer in C with Example - What is function pointer & how to use it

How to Fork child process with waitpid & execl in Linux with c example code

Share on Google Plus

About Kapil Thakar

"I am Kapil Thakar, an Embedded Engineer cum Blogger wants to learn new things. I love to share my knowledge solutions to the problems. Interested in Blogging, Creative-Writing, SEO, Website Creation, Video Making, Editing, Affiliation Programs, Online Making Money."
    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment

Related Posts Plugin for WordPress, Blogger...