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. 16 1749
mdh wrote:
printf( "Then for \" if ( i) , if (j) and if (k)\"\n");
You cannot have a function named "if"
jmcgill wrote:
You cannot have a function named "if"
I thought you could (page 56, K&R II);
mdh wrote:
jmcgill wrote:
You cannot have a function named "if"
I thought you could (page 56, K&R II);
You are thoroughly confused about the use of the if-statement. Re read
that page and the previous one very carefully.
Regards,
Bart.
Bart wrote:
mdh wrote:
>jmcgill wrote:
>>You cannot have a function named "if"
I thought you could (page 56, K&R II);
You are thoroughly confused about the use of the if-statement. Re read
that page and the previous one very carefully.
I have a feeling he needs to back up a bit more than that. K&R2 is a
great book, but might not be the best tutorial for someone working from
scratch, on his own.
Bart wrote:
>
You are thoroughly confused about the use of the if-statement.
I could give a smart answer, but I will refrain. The reason some of us
ask, is that we wish to be enlightened. One of the ways of
enlightenment is to write little things that are causing confusion. So,
to be told that I am confused is a pointless exercise.
If you look at page 56 it says:
"Since an if simply tests the numeric value of an expression, certain
coding shortcuts are possible. The most obvious is writing
if(expression)
instead of
if (expression != 0).
I was simply trying to see if the !=0 includes negative values, hence
the program, which may not work for the reasons you say, but that does
not mean one should not try and get a better understanding.
Anyway, thanks for your help.
mdh wrote:
Bart wrote:
You are thoroughly confused about the use of the if-statement.
I could give a smart answer, but I will refrain. The reason some of us
ask, is that we wish to be enlightened. One of the ways of
enlightenment is to write little things that are causing confusion. So,
to be told that I am confused is a pointless exercise.
I was about to explain it at length, but I just realized that I'm
repeating what's written there in K&R. Since you have the book I
thought that it would be pointless, so I just suggested that you
re-read more carefully.
If you look at page 56 it says:
"Since an if simply tests the numeric value of an expression, certain
coding shortcuts are possible. The most obvious is writing
if(expression)
instead of
if (expression != 0).
I was simply trying to see if the !=0 includes negative values, hence
the program, which may not work for the reasons you say, but that does
not mean one should not try and get a better understanding.
Yes, but it also says that the general syntax is:
if (expression)
statement;
else
statement;
You can't just use the 'if' as though it were an expression. You can
only use it in a complete statement as above.
Regards,
Bart.
jmcgill wrote:
Bart wrote:
mdh wrote:
jmcgill wrote: You cannot have a function named "if"
I thought you could (page 56, K&R II);
You are thoroughly confused about the use of the if-statement. Re read
that page and the previous one very carefully.
I have a feeling he needs to back up a bit more than that. K&R2 is a
great book, but might not be the best tutorial for someone working from
scratch, on his own.
Don't assume too much. There are languages where the if statement is
can be used as an expression, and it's quite concievable that he's
confused precicely for this reason.
Regards,
Bart.
/* consider this */
#include <stdio.h>
int
main (int argc, char **argv)
{
int i, j, k;
if( argc == 4 ){
i = atoi(argv[1]);
j = atoi(argv[2]);
k = atoi(argv[3]);
printf ("Given i=%d, j=%d and k=%d \n", i, j, k);
printf ("Then for \" if(i) , if(j) and if(k)\"\n");
printf ("the Answers are %d %d %d\n",
(i ? 1 : 0), (j ? 1 : 0), (k ? 1 : 0) );
} else {
printf("usage: %s i j k\n", argv[0]);
}
return 0;
}
mdh wrote:
>>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!! :-)
If you do want to use a conditional as an expression, the ternary
operator is your friend. See my previous post for an example. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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.
|
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)?
|
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...
|
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;
| |
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...
|
by: guthena |
last post by:
Write a small C program to determine whether a machine's type is
little-endian or big-endian.
|
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.");
}
|
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...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |