Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you print multiple variables inside a string using printf?

I want to find the maximum value of two numbers, and print it. I want to print all three numbers. I am using the following code.

#include<stdio.h>
#include<conio.h>
main()
{
     //clrscr();
     int a,b,c;
     printf("insert two numbers:");
     scanf("%d%d", &a, &b);
     c = (a>b) ? a : b;
     printf("\nmaximum of %d",a," and %d",b,"  is = %d" c);
     getch();

}

However, I receive two syntax errors (Please find the attached figure). Could anybody help me out with it?

like image 884
aghd Avatar asked Jan 04 '16 19:01

aghd


People also ask

How do I print multiple variables when printing?

There are following methods to print multiple variables, Method 1: Passing multiple variables as arguments separating them by commas. Method 2: Using format() method with curly braces ({}) Method 3: Using format() method with numbers in curly braces ({0})

How printf takes multiple parameters?

The first parameter is fixed and is the format string. It includes text to be printed literaly and marks to be replaced by the text obtained from the additional parameters. Thus, printf is invoked with as many parameters as marks are included in the format string, plus one (the format string itself).

How do you print a string containing % in printf?

To print a percent-sign character, use %%. For non decimal floating-point numbers, signed value having the form [-]0xh. hhhhp[sign]ddd, where h is a single hexadecimal digit, hhhh is one or more hexadecimal digits, ddd is one or more decimal digits, and sign is + or -.

How do I print a variable using printf?

The following line shows how to output the value of a variable using printf. printf("%d", b); The %d is a placeholder that will be replaced by the value of the variable b when the printf statement is executed. Often, you will want to embed the value within some other words.


2 Answers

Change the line where you print the output to:

printf("\nmaximum of %d and %d is = %d",a,b,c);

See the docs here

like image 161
Aaditya Gavandalkar Avatar answered Oct 19 '22 01:10

Aaditya Gavandalkar


printf("\nmaximum of %d and %d is = %d",a,b,c);
like image 20
LCO TEC BAJA Avatar answered Oct 19 '22 00:10

LCO TEC BAJA