473,612 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Little program

mdh
Can someone look at this little program and tell me why the last line
is not compiling.

#include <stdio.h>

int main (){
int i, j, k;
i=0;
j=9;
k=-8;
printf("Given i=0, j=9 and k= -8 \n");
printf( "Then for \" if ( i) , if (j) and if (k)\"\n");
printf("the Answers are %d %d %d ", if(i), if(j), if(k) ); /**parse
error before 'if'**/
return 0;
}

It's probably something really silly, or I am missing something, which
is more likely!! but all in the quest to learn C!!!!
Thanks in advance.

Oct 13 '06
16 1760
mdh
Bart wrote:
You can't just use the 'if' as though it were an expression. You can
only use it in a complete statement as above.

Yes....I am beginning to see that!! :-)

What I had missed was that presumably when an expression does not
evaluate to '0', it could also be a negative value. (I had assumed it
would have to be '0'. This came up in a later exercise when using
recursion to express an integer as a string (exercise 4-12). It is
strange that in the process of learning, one thinks one understands a
concept until one gets to understand it better, invariably through
making a mistake.

Oct 13 '06 #11
mdh

jmcgill wrote:
/* consider this */

Thanks...
I am 2 pages away from starting pointers!!! I will keep this for later
enlightenment!! !

To both of you, thanks for your help.

BTW....K&R is certainly challenging, but even more certainly, worth it.

Oct 14 '06 #12
"mdh" <md**@comcast.n etwrites:
Bart wrote:
You can't just use the 'if' as though it were an expression. You can
only use it in a complete statement as above.

Yes....I am beginning to see that!! :-)

What I had missed was that presumably when an expression does not
evaluate to '0', it could also be a negative value. (I had assumed it
would have to be '0'. This came up in a later exercise when using
recursion to express an integer as a string (exercise 4-12). It is
strange that in the process of learning, one thinks one understands a
concept until one gets to understand it better, invariably through
making a mistake.
That's correct. In an if statement "if (foo)", the expression "foo"
is evaluated and the condition is considered false if foo is equal to
zero (which can mean different things for different types), and true
if foo has any non-zero value.

But that's not the problem with the code you posted. You're using
something like "if (foo)" in a context that requires an expression.
"if (foo)" isn't an expression; it's a fragment of a statement. (An
expression, with a semicolon added, can be a statement, but a
statement cannot in general be used as an expression.)

"if (foo)" doesn't return or yield a value; it evaluates the condition
and then affects the program's control flow based on the result.

As jmcgill points out, the conditional operator, also known as the
ternary operator (because it happens to be the only operator that
takes three operands), can give you the effect of an if/else in an
expression context. Use it sparingly; it can easily be used to write
nearly illegible code. Something like
a = (b ? c : d);
is often clearer if it's written as
if (b) {
a = c;
}
else {
a = d;
}

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 14 '06 #13
mdh wrote:
printf("the Answers are %d %d %d ", if(i), if(j), if(k) );
The if is a reserved keyword and in the example they are not
expressions and have no values.

Oct 14 '06 #14
mdh

Keith Thompson wrote:
"
That's correct. ....snip.... But that's not the problem with the code you posted. You're using
something like "if (foo)" in a context that requires an expression..... .

Thank you Keith.

Oct 14 '06 #15

mdh wrote:
Can someone look at this little program and tell me why the last line
is not compiling.

#include <stdio.h>

int main (){
int i, j, k;
i=0;
j=9;
k=-8;
printf("Given i=0, j=9 and k= -8 \n");
printf( "Then for \" if ( i) , if (j) and if (k)\"\n");
printf("the Answers are %d %d %d ", if(i), if(j), if(k) ); /**parse
error before 'if'**/
return 0;
}
You cant use "if" statemant here but you can do the same with this line
printf("the Answers are %d %d %d ", ( i!=0) , (j!=0) , (k!=0) );

Oct 14 '06 #16
mdh

Chief wrote:
You cant use "if" statemant here but you can do the same with this line
printf("the Answers are %d %d %d ", ( i!=0) , (j!=0) , (k!=0) );
thanks

Oct 15 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3422
by: Ron Stephens | last post by:
I posted to my web site a fun little program called merlin.py today. Please keep in mind that I am a hobbyist and this is just a little hack, if you look at the code you will see that it is still possible to write spaghetti code, even with Python. I apologize, and I do intend to clean up the code, but it may take awhile. For now it works, with some bugs. It is a composite of a few scripts. The first, based on a script Max M uploaded to...
8
1943
by: Usman | last post by:
Huy everyone , Well I am not a big C++ programmer , I am just a little young kid on it tryint to learn . Actually I was given an assignment last week by my teacher which I solved completely but was unable to go through one question.
38
3310
by: Martin Marcher | last post by:
Hi, I've read several questions and often the answer was 'C knows nothing about .' So if C knows that little as some people say, what are the benefits, I mean do other languages know more or is it a benefit that C knows nearly nothing (what I can think about is that C is the largest common divisor defined on most available platforms)?
0
1189
by: Kenneth Lantrip | last post by:
After some cleaning of some of my personal directories and files, I stumbled upon this little program I wrote some time ago. A scammer was trying to introduce me into his little pyramid scam. So to prove it was a bad idea, I decided to write this little program to find some confidence in the dicision to not get mixed up in his plot to rule the world. Anyway, Here is the program for your amusement. I only had to change a couple of...
54
2967
by: ash | last post by:
i am writing this program (for exercise1-9 in k&r-2nd edition) which removes extra spaces in string example- "test string" will be "test string" #include<stdio.h> #include<string.h> #include<conio.h> main() { char str,st;
102
5050
by: BoogieWithStu22 | last post by:
I am running into a problem with a web page I have created when viewing it in IE6 on some machines. The page has a database lookup. The user enters an account name or number and clicks a lookup button. This hits an AS400 database looks for matches and returns a dataset that is used to populate a datagrid. The user then selects one of the entries from the list, this entry is used to populate a couple of textboxes on the page and a couple...
23
14168
by: guthena | last post by:
Write a small C program to determine whether a machine's type is little-endian or big-endian.
23
7030
by: Niranjan | last post by:
I have this program : void main() { int i=1; if((*(char*)&i)==1) printf("The machine is little endian."); else printf("The machine is big endian."); }
8
9981
by: marknvicf | last post by:
I have read a bit about the endian differences, got a couple of jar/libs to help (Android and com.mindprod), but still am not sure how to fix this program that I have (written in JDK 1.4 by someone else who is not here anymore). I have some complicated structures in this program that I don't know how to fix. I have fixed DataInputStream and DataOutputStream but using com.mindprod and now have LEDataInput and Output Streams (Little Endian). The...
0
8615
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8568
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8253
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6081
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5536
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4047
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4110
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1699
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1414
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.