473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

declaring variable values

jacoder
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
9 2044
arne
315 Expert 100+
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
9,065 Expert Mod 8TB
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
315 Expert 100+
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
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
315 Expert 100+
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
9,065 Expert Mod 8TB
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
9,065 Expert Mod 8TB
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
315 Expert 100+
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
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

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

Similar topics

2
by: Sriram Chadalavada | last post by:
Hello everyone, I am a newbie to Python with experience in C programming. For my project, I am re-writing C routines as Python functions. I am currently using raw numerical values and was...
2
by: R.G. Vervoort | last post by:
is it possible to declare a variable in the onchange event of a <select> and use it later in php I would like to put the value of the selected option (in <select>) in a variable and later when i...
14
by: Eric Bantock | last post by:
Very basic question I'm afraid. Once an array has been declared, is there a less tedious way of assigning values to its members than the following: myarray=8; myarray=3; myarray=4; myarray=0;...
16
by: G Patel | last post by:
Hi, If I want to call functions that don't return int without declaring them, will there be any harm? I only want to assign the function(return value) to the type that it returns, so I don't...
3
by: jbeteta | last post by:
Hello, I have a problem declaring variables. I need to create an object oRpte as ReportClass on WebForm1.aspx and be able to use its value on WebForm2.aspx. For declaring the property oRpte()...
1
by: Ilya N. Golubev | last post by:
There is already one function processing pointer values this way. And there may easily appear more, including wrappers for this function. For purposes of further discussion, consider simplified...
3
by: mamun | last post by:
Hi all, I am trying to create variables dynamically. This is needed because the user interface can have ten different textboxes with name as txt1, txt2 and so on. I would like to get values of...
5
by: shethsheetal | last post by:
I am having some problem with the below mentioned trigger. CREATE TRIGGER D.TBA AFTER UPDATE OF TBAA.CIC ON TBAA REFERENCING NEW AS NEW_ROW FOR EACH ROW MODE DB2SQL BEGIN ATOMIC DECLARE RS...
8
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine,...
8
by: HMS Surprise | last post by:
I looked in the language but did not find a switch for requiring variables to be declared before use. Is such an option available? Thanks, jvh
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.