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
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 -
average = coffe + milk + sugar;
-
and for the average -
average = (coffe + milk + sugar) / 3;
-
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 ...
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.
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?
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;
}
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?
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 -
#include<stdio.h>
-
-
int main (void)
-
{
-
int c,b,x,y;
-
-
c = 5;
-
b = 10;
-
-
y = b, c;
-
x = (b, c);
-
-
printf("%d %d\n", x, y);
-
-
return(0);
-
}
-
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 -
#include<stdio.h>
-
-
int main (void)
-
{
-
int c,b,x;
-
-
c = 0;
-
b = 10;
-
-
if (x = b, c)
-
{
-
printf("Expression true: x = %d\n", x);
-
}
-
else
-
{
-
printf("Expression false: x = %d\n", x);
-
}
-
-
-
return(0);
-
}
-
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.
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
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)!
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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;...
|
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...
|
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()...
|
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...
|
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...
|
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...
|
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,...
|
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
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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,...
|
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...
| |