# A few days into C-programmering experience: if and else

Printable View

• 01-31-2013
TobiasK
A few days into C-programmering experience: if and else
Hello users!
As stated in the title, I am very new to C and programming languages in general.

I am currently reading various guides and such, but I've come across a syntax problem when using if and else.

I did a little code to try it. The idea is having two integers, a and b. If a and b are equal, I want the program to calculate the product of the two. Else I want it to find the average of those numbers.

I'm pretty sure my problem is lack of / misplaced brackets and tokens, but I cannot figure out the syntax! :confused:

My code is
Code:

```int main() {     int a,b;     float avg;     float square;         printf("Enter the value of a:");     scanf("%d", &a);         printf("Enter the value of b:");     scanf("%d", &b);       if (a==b)           square = a * b;           printf("%f", square);                 else     avg = (a+b)/2;     printf("%f", avg); return 0; }```
Thank you!
• 01-31-2013
Salem
Put braces around everything.
Code:

```    if (a==b) {         square = a * b;         printf("%f", square);     } else {         avg = (a+b)/2;         printf("%f", avg);     }```

Code:

```    if (a==b)           square = a * b;```
Is valid, and does what you want.

Code:

```    if (a==b)           square = a * b;           printf("%f", square);```
Is valid, but doesn't do what you want - the printf always happens, despite what the indentation might otherwise suggest.

Adding an else becomes a syntax error.
• 01-31-2013
TobiasK
Thank you, Salem! That was very helpful.

Is there any alternative to printf that I might use to be able to get the result I am looking for? Perhaps cout or something?

Never mind the underlined text. I think I might have misunderstood what you said. I'll just go try to implement the changes you pointed out
• 01-31-2013
c99tutorial
Also look at this line:

Code:

`avg = (a+b)/2;`
Becaus a and b are integers and 2 is an integer, this will perform integer division, so your answer will basically be an integer even though you store the result in a float. You should probably write it like this

Code:

`avg = (float)(a + b) / 2;`
Or equivalently

Code:

```avg = a + b; avg /= 2;```
• 01-31-2013
TobiasK
Implemented the changes both of you suggested and my tiny program now works. Yay! Onwards I go into the void that is C-programming ;)
Thank you guys! Appreciate the quick replies
• 01-31-2013
c99tutorial
Quote:

Originally Posted by TobiasK
Is there any alternative to printf that I might use to be able to get the result I am looking for? Perhaps cout or something?

In C++ cout is an alternative to printf. In C standard library, there is just printf. C++ cout uses operator overloading and polymorphic behaviour to work out how to output a value of a certain type (string, float, double, etc). Because C doesn't have a way to do this, you need to tell printf how to output something

printf("%d", myInt);
printf("%f", myFloat);

So basically there's not any way for the compiler to do this for you like there is using cout in C++. Some compilers have extensions which allow you to simulate this, but using them makes your program only work on one compiler, and usually what you gain is not worth it.

By the way, compilers will check the printf formats for you if you compile with warnings turned on. So basically if you do that, then there's basically no way you can go wrong with printf