By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,929 Members | 634 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,929 IT Pros & Developers. It's quick & easy.

declaring variable values

jacoder
P: 13
i amm on my 4th C class ever;)
i have taken to the language ok but im stuck trying to declare the values of my 3 variables hers my effort HOPE SUMONE CAN CORRECT THIS MESSY CODE & LEND A HELPING HAND TIA

#include <stdio.h>
#ifdef _WIN32
#include <conio.h>
#else
#include <curses.h>
#endif

int main()
{

float coffee;
float sugar;
float milk;
float average;

coffee = 1.50;
sugar = 0.99;
milk = 0.89;

average = coffee,sugar,milk;

printf("\n the cost of a coffe,sugar,milk,%d is \n",average);


getch();
return 0;

}


trying
Oct 26 '06 #1
Share this Question
Share on Google+
9 Replies


arne
Expert 100+
P: 315
i amm on my 4th C class ever;)
i have taken to the language ok but im stuck trying to declare the values of my 3 variables hers my effort HOPE SUMONE CAN CORRECT THIS MESSY CODE & LEND A HELPING HAND TIA

#include <stdio.h>
#ifdef _WIN32
#include <conio.h>
#else
#include <curses.h>
#endif

int main()
{

float coffee;
float sugar;
float milk;
float average;

coffee = 1.50;
sugar = 0.99;
milk = 0.89;

average = coffee,sugar,milk;

printf("\n the cost of a coffe,sugar,milk,%d is \n",average);


getch();
return 0;

}


trying
The declaration of your variables is fine.

The only thing the compiler is complainig about is that you say you want to print an integer (%d), but you pass a float (average). Replace the %d by a %f.

Your program will then be syntactically correct. However, since the variable is called average, you may want to determine the average of the prices. On the other hand your printf text indicates you want to print out the sum.

For the sum you would do
Expand|Select|Wrap|Line Numbers
  1. average = coffe + milk + sugar;
  2.  
and for the average
Expand|Select|Wrap|Line Numbers
  1. average = (coffe + milk + sugar) / 3;
  2.  
Your current statement assigns the value of coffe to average.

And, finally, you may want to move the %f behind the "is" in your print ...
Oct 26 '06 #2

Banfa
Expert Mod 5K+
P: 8,916
Your current statement assigns the value of coffe to average.
All correct except that actually

Your current statement assigns the value of milk to average, not the value of coffee.
Oct 26 '06 #3

arne
Expert 100+
P: 315
All correct except that actually

Your current statement assigns the value of milk to average, not the value of coffee.
No, it doesn't, at least not on my compiler (gcc 4.02). I would have thought so too, as comma expressions are evaluated from left to right and the 'old' results are discarded.

Can you check that please with your C implementation?
Oct 27 '06 #4

jacoder
P: 13
No, it doesn't, at least not on my compiler (gcc 4.02). I would have thought so too, as comma expressions are evaluated from left to right and the 'old' results are discarded.

Can you check that please with your C implementation?
aha thx alot i took the %d away and added %f since its float datatype i shud have known better but its great to get an extra opinion im using DEV-C++ 4.9.9.2 to compile my code here is what i have now ;)

/*===========================*/
/* NAME: Jacoder */
/*TUTOR: A.Wizard */
/*CLASS: Declare3Vars.c */
/*=======================*/


#include <stdio.h>
#ifdef _WIN32
#include <conio.h>
#else
#include <curses.h>
#endif

int main()
{

float coffee; // here we have float type vars
float sugar;
float milk;
float average;

coffee = 1.50;
sugar = 0.99; // here we give vars values
milk = 0.89;
average = coffee,sugar,milk; // here we get average paid for a coffee



printf("\n the cost of a coffe,sugar,milk, is %f \n",average); // here we get our final result


getch();
return 0;

}
Oct 27 '06 #5

arne
Expert 100+
P: 315
aha thx alot i took the %d away and added %f since its float datatype i shud have known better but its great to get an extra opinion im using DEV-C++ 4.9.9.2 to compile my code here is what i have now ;)

/*===========================*/
/* NAME: Jacoder */
/*TUTOR: A.Wizard */
/*CLASS: Declare3Vars.c */
/*=======================*/


#include <stdio.h>
#ifdef _WIN32
#include <conio.h>
#else
#include <curses.h>
#endif

int main()
{

float coffee; // here we have float type vars
float sugar;
float milk;
float average;

coffee = 1.50;
sugar = 0.99; // here we give vars values
milk = 0.89;
average = coffee,sugar,milk; // here we get average paid for a coffee



printf("\n the cost of a coffe,sugar,milk, is %f \n",average); // here we get our final result


getch();
return 0;

}
Let it run, please ... what's the output?
Oct 27 '06 #6

Banfa
Expert Mod 5K+
P: 8,916
No, it doesn't, at least not on my compiler (gcc 4.02). I would have thought so too, as comma expressions are evaluated from left to right and the 'old' results are discarded.

Can you check that please with your C implementation?
Yes it does, you are falling fowel of the fact that the assignment operator = has a higher precedence than the , operator, try this code

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. int main (void)
  4. {
  5.     int c,b,x,y;
  6.  
  7.     c = 5;
  8.     b = 10;
  9.  
  10.     y = b, c;
  11.     x = (b, c);
  12.  
  13.     printf("%d %d\n", x, y);
  14.  
  15.     return(0);
  16. }  
  17.  
y has the value 10 because = is higher precedence than , so

y = b, c;

is evaluated as

(y = b), c;

But the comma operator actually evaluates to the type and value of the right right hand expression.

Try this

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. int main (void)
  4. {
  5.     int c,b,x;
  6.  
  7.     c = 0;
  8.     b = 10;
  9.  
  10.     if (x = b, c)
  11.     {
  12.         printf("Expression true: x = %d\n", x);
  13.     }
  14.     else
  15.     {
  16.         printf("Expression false: x = %d\n", x);
  17.     }
  18.  
  19.  
  20.    return(0);
  21. }  
  22.  
it outputs

Expression false: x = 10

because = has high precedence than , so x=b is evaluated first but the value of the entire expression is the value of c or 0.
Oct 27 '06 #7

Banfa
Expert Mod 5K+
P: 8,916
P.S. But this does mean that your original statement that average is set to the value coffee is correct. just not for the correct reasons :D
Oct 27 '06 #8

arne
Expert 100+
P: 315
OK, I see ... thanks for the detailed explanation. So, the C-World is saved and comma expressions are still evaluated from left to right :)

But your comment
Your current statement assigns the value of milk to average, not the value of coffee.
is not correct: though I hadn't understood why (thanks to you I do now) the value of coffe is assigned to average (and the expression evaluates to milk)!
Oct 27 '06 #9

jacoder
P: 13
OK, I see ... thanks for the detailed explanation. So, the C-World is saved and comma expressions are still evaluated from left to right :)

But your comment

is not correct: though I hadn't understood why (thanks to you I do now) the value of coffe is assigned to average (and the expression evaluates to milk)!
thx alot guys >.< so = i now know has presidence over , as does () have presidence over + or - i tried above codes :p i added getch for puase (); also to see console window ;)
im going to be tought sum string ("strstr") but have never done any string before do any of you more experienced programers out there have any good examples of howto make string in C thx again ,I like to say thx to you all for the feedback reespect&peaceout :D
Nov 9 '06 #10

Post your reply

Sign in to post your reply or Sign up for a free account.