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

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?

Change the line where you print the output to:
printf("\nmaximum of %d and %d is = %d",a,b,c);
See the docs here

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

Related

Input are not able to calculate and print out in C

I've tried to build a simple calculator for physics force experiments.
//
// main.c
#include <stdio.h>
int main() {
char name[20];
int month,date;
int difference_percentage1, difference_percentage2, difference_percentage3;
double force1, force2, force3;
scanf("%s", name);
scanf("%d %d", &month, &date);
scanf("%lf %lf %lf", &force1, &force2, &force3);
double Favg=(force1 + force2 + force3)/3.000;
puts(name);
difference_percentage1=100*(force1-Favg)/Favg;
difference_percentage2=100*(force2-Favg)/Favg;
difference_percentage3=100*(force3-Favg)/Favg;
printf("%d %d %d\n", difference_percentage1, difference_percentage2, difference_percentage3);
getchar();
return 0;
}
The calculate doesn't match what I've typed for scanf().
The o's of the % mark for the %f look a bit larger to me than for the %d, so my guess is that you used the wrong % for the %f format specifier which isn't recognized by the compiler; it interprets the double value as int.
Edit: one more reason to post text instead of a screenshot of the code! (o;
Edit2: Yup, your % mark in %f is actually a "EF BC 85" in hex but should be "25"

find the sum of 2 numbers in c with input from user

#include<stdio.h>
int main(){
int num1=0;
int num2=0;
int sum=0;
printf("enter 2 numbers\n");
scanf("%d %d",&num1,&num2);
sum=num1+num2;
printf("%d",&sum);
return 0;
}
This is what i am trying but 23+23 is coming out to be 6422292 in this way.I cant find the error. Please help.
Do NOT put an "address of" operator (&) on this line:
printf("%d",&sum);
It should be
printf("%d", sum);
Hey actually the error is in the printf() function
The & is your telling to print the value stored in the sum variable
Make the following changes to your code
printf("%d", sum);
Hope you got fixed the error
#include<stdio.h>
int main() {
int a , b;
printf ("enter a\n");
scanf ("%d", &a);
printf ("enter b\n");
scanf ("%d", &b);
int sum = a + b;
printf ("the sum is : %d", sum);
return 0;
}

Enter the value of two number

Actually, I'm learning C langage and I have written a programm
Enter the value of 2 numbers as shown below.
#include<stdio.h>
int main()
{
int a, b, c;
printf("Enter two numbers to add\n");
scanf("%d%d", &a, &b);
printf("Sum of the numbers = %d\n", c);
return 0;
}
But if I enter an alphabet I'm getting some 1522222 numbers. Instead of
this I want it throws an error as invalid input if I type alphabet ie a,b,c.
How could I do that?
You can check the return value of scanf. If it is successful, it should return 2, since you're reading two values. If you get anything else, you know that the input is incorrect. Try this:
if (scanf("%d%d", &a, &b) != 2)
printf("Invalid input type!\n");
else
printf("Sum of the numbers = %d\n", a+b);
On a different note, you don't initialize c anywhere, so printing it is undefined behavior. You don't even need c for this, you can just print a+b instead.

Why my program combines two printf commands?

So here is my program. It is suposed to write out square of some intiger.
#include <stdio.h>
int main (){
int a;
printf("Type an intiger.");
scanf("%i", &a);
printf("Square of that intiger is %i", a*a);
return 0;
}
When i run a program in Eclipse it first requires me to input a number.I put in 5. And then as output it gives me
Type an intiger.Square of that intiger is 25.
It should first print "Type an intiger" and then the rest. But it just combines two printf commands. What is the problem?
You need a newline character - printf("Type an intiger.\n");
In computing, a newline, also known as a line break or end-of-line
(EOL) marker, or simply break, is a special character or sequence of
characters signifying the end of a line of text.
Also format specifier for integer is %d
scanf("%d", &a);
printf("Square of that intiger is %d", a*a);
If you want it on separate lines you can always add '\n' to the string to get a new line.
#include <stdio.h>
int main (){
int a;
printf("Type an intiger.\n");
scanf("%i", &a);
printf("Square of that intiger is %i", a*a);
return 0;
}
There is 2 problem in it. First, if you input the integer, it should be %d. Example :
scanf("%d", &a);
The second, after the input, you should print \n. So, it will be like this printf("\n");. Take a look at my code :
#include <stdio.h>
int main (){
int a;
printf("Type an intiger.");
scanf("%d", &a);
printf("\nSquare of that intiger is %d", a*a);
return 0;
}
In code::blocks it compiles fine anyway put a \n at the end of the first printf and change %i with %d

simple c program keeps crashing

#include <stdio.h>
int main(void)
{
int a,b,c;
printf("Enter values of a,b,c:");
scanf("%d %d %d",a,b,c);
printf("\nDescending order of the numbers entered:");
/*Test for Biggest Number*/
if((a>b)&&(a>c))
printf("%d",a);
else if((b>a)&&(b>c))
printf("%d",b);
else if((c>a)&&(c>b))
printf("%d",c);
/*Test for Second Biggest Number*/
if((a>b&&a<c)||(a<b&&a>c))
printf("%d",a);
else if((b>a&&b<c)||(b<a&&b>c))
printf("%d",b);
else if((c>a&&c<b)||(c<a&&c>b))
printf("%d",c);
/*Test for Smallest Number*/
if((a<b)&&(a<c))
printf("%d",a);
else if((b<a)&&(b<c))
printf("%d",b);
else if((c<a)&&(c<b))
printf("%d",c);
return 0;
}
this is a c program in which 3 numbers are entered and the program prints the in descending order. i compiled the program and the ran the program.after entering the three numbers the program would just crash. is there something wrong with my code or do i have to add something?
That's because you are not passing the address of your variables to scanf. Change
scanf("%d %d %d",a,b,c)
to
scanf("%d %d %d",&a,&b,&c)
Try to use:-
scanf("%d %d %d",&a,&b,&c)
instead of
scanf("%d %d %d",a,b,c)
as & refers to the address of your variables.
In C the parameters are passed by value so you need to pass the address (or pointer). When you pass the address (or pointer) then scanf knows where it has to put the value.

Resources