-
Beginner Help
hi I'm a beginner in C (specifically the syntax) so I started to go through the tutorials first
everything's fine but i'm having a small problem with printf. what I exactly dont know how to do is use printf so that I can display 2 variables and not just one
here's some sample code, ( I don't know how to fix it )
its obvious, but I'm just trying to compare 2 numbers
note: this is only a part of main(), everything works when compiled, just having troubles with syntax in the bolded lines.
Code:
printf("Enter number 1: ");
scanf("%d", &num1);
printf("Enter number 2: ");
scanf("%d",&num2);
if(num1>num2)
{
printf("%d",num1,"is greater than %d", num2);
}
else if (num2>num1)
{
printf("%d",num2,"is greater than %d", num1);
}
else
{
printf("Both numbers are equal");
}
What Im trying to do is display both numbers inputted into 1 printf function, but this doesnt work the way I need it, it just shows 1 number and nothing else.
Thanks :D
-
Code:
printf("%d is greater than %d", num1, num2);
-
For the first one,
Code:
printf("%d is greater than %d", num1, num2);
(And you might want to add a newline.)
Edit: puts() is equivalent to printf() except that it adds the newline, so you could also replace your printf()'s with puts()'s, though for some reason you don't see people use puts() very often.
Edit: I'm wrong, puts() only takes a single argument (a C string) as argument and prints it together with a newline. So you could use it to replace the last printf() but not the second or third (or the first, since you don't want a newline there). That explains why it's not used much.
-
-
hi i am a student.
the general syntax for printf statement is
printf("format string",variable);
this built in function will return an integer value.
here format string may be %d,%f or any other .
the eg to print 2 variables is
printf("The first number is %d and the second number is %d",no1,no2);
i hope ur doubt may be cleared.
all the best to be a good programmer in C.
dont lose ur hope.
my quote to u is
"Defeat the Defeat before the Defeat Defeats U".