this is my first time using Stack Overflow, and I must warn you that I only started to learn C earlier this week. I am trying to write a simple program that will find which color has the greatest value out of the RGB spectrum. It has a manual input for each color, but when It is finished, I am getting 2 odd numbers ranging in the millions. Could somebody help a noob? Here is the code that I’ve written so far:
#include <stdio.h>
int main()
{
int mm, m;
int hh, h;
int r, g, b, c;
printf("Enter Value For Red (0-255)\n");
scanf("%d", &r);
printf("Enter Value For Green (0-255)\n");
scanf("%d", &g);
printf("Enter Value For Blue (0-255)\n");
scanf("%d", &b);
if ( r > g );
if ( r > b );
mm = r;
if ( g > b );
m = b;
if ( b > g );
m = g;
if ( r < b );
mm = b;
m = g;
if ( g > r );
if ( g > b );
mm = g;
if ( r > b );
m = b;
if ( b > r );
m = r;
if ( g < b );
mm = b;
m = r;
printf("%d\n", &mm);
printf("%d\n", &m);
}
Should be:
The
&gets the address of the variable. When you’re setting a variable viascanf()you need to give the address so that it can be set. When you’re printting the value of a variable viaprintf()you need to just provide the name, not the address.What you’re seeing right now is the address of where your variables are stored on the stack as a decimal number.
Side note, your code doesn’t work the way you think it does:
It takes more then indentation to get this working, right now it’s doing the following:
everytime you run it, because the if’s are being chucked. You can’t have
;after anifstatement or it doesn’t do what’s below it. Also use curly brackets{ }to make a scope to do more then one thing:Is what you wanted.