question about function

Thread Starter

asdf arfw

Joined Sep 9, 2017
43
I'm wondering, where does the number behind "yes" come from?
and why I can't use a same variable in statement and parameter.
I have to define int c=a to use variable 'a' as 'c' in parameter.

upload_2017-9-16_19-55-23.png

*edited I got the first answer now, because I use function printf("%d",line(x));
then I change it into line(x); and it works now.
 
Last edited:

spinnaker

Joined Oct 29, 2009
7,830
First off your syntax is wrong.

= is the assignment operator in C.

== is the comparison operator

So instead of if (a=1) it should be if (a==1)

As for your question. I don't know what you are asking. What do you mean "here does the number behind "yes" come from"

Behind yes?? What does that means?
 

WBahn

Joined Mar 31, 2012
30,055
I'm wondering, where does the number behind "yes" come from?
I see you figured that out, which is good.

You have a logic error (I'm assuming) where you have

if (a = 1)

You are almost certainly trying to test if 'a' is equal to one, not trying to set the value of 'a' to be 1.

As written this is NOT a syntax error -- it is a perfectly valid C statement. That makes logic errors like this very difficult to spot and track down, because not only does the code compile and run, but when you look at it later you will tend to see what you want to see, namely what you meant it to be, and not what it is.

A very good habit to get into when writing C equality expressions involving a constant is to put the constant on the left of the operator. Had you written

if (1 = a)

then THIS would have been a syntax error. Any time you can turn a potential logic error into a syntax error, it is a BIG win.
 

WBahn

Joined Mar 31, 2012
30,055
As for your question. I don't know what you are asking. What do you mean "here does the number behind "yes" come from"

Behind yes?? What does that means?
Look at the screen capture of the output. On the second line he has "yes1".

What I can't make heads or tails of is, "I have to define int c=a to use variable 'a' as 'c' in parameter."
 

spinnaker

Joined Oct 29, 2009
7,830
I see you figured that out, which is good.

You have a logic error (I'm assuming) where you have

if (a = 1)

You are almost certainly trying to test if 'a' is equal to one, not trying to set the value of 'a' to be 1.

As written this is NOT a syntax error -- it is a perfectly valid C statement. That makes logic errors like this very difficult to spot and track down, because not only does the code compile and run, but when you look at it later you will tend to see what you want to see, namely what you meant it to be, and not what it is.

A very good habit to get into when writing C equality expressions involving a constant is to put the constant on the left of the operator. Had you written

if (1 = a)

then THIS would have been a syntax error. Any time you can turn a potential logic error into a syntax error, it is a BIG win.
But in this case it is a logic error or syntax error or whatever you want to call it. Not once in my life in 30 years of coding have I ever used if (a = 1) (well ar least not on purpose ;) ). Yes technically it is not an error and is a valid statement. But the if is superfluous and confusing as the statement will always result to true.

If you look at the TS's code the TS is passing a so would likely not want to assign it inside the function. Not that you can't set the value of a inside the function it just doesn't make sense in this case.
 

WBahn

Joined Mar 31, 2012
30,055
But in this case it is a logic error or syntax error or whatever you want to call it. Not once in my life in 30 years of coding have I ever used if (a = 1) (well ar least not on purpose ;) ). Yes technically it is not an error and is a valid statement. But the if is superfluous and confusing as the statement will always result to true.

If you look at the TS's code the TS is passing a so would likely not want to assign it inside the function. Not that you can't set the value of a inside the function it just doesn't make sense in this case.
It is a logic error and not a syntax error. The distinction is important. The compiler WILL catch any syntax error. It CAN'T catch logic errors. A logic error is syntactically correct code that does not perform as intended. The compiler has no way to know what the programmer intended, only what they wrote. Many compilers are pretty good at issuing warnings, however many programmers ignore warnings precisely because the code compiled without errors.

I agree that the TS almost certainly did not MEAN to set 'a' equal to 1. That's the point. A simple typo resulted in a logic error that was NOT a syntax error. The compiler can catch syntax errors, it cannot catch logic errors. By adopting a coding style that puts non-lvalue objects as the target of inadvertent assignment operators (which is an all-too-easy mistake to make), you turn these types of logic errors that the compiler can't catch into syntax errors that the compiler can.

And I've used assignment operations as predicate statements many times. Consider something like

if (c = getCharacter(fp))

where getCharacter(fp) takes a file pointer and returns the character code for the next character or a 0 if there is no next character.

Or

if (fp = fopen("myfile.txt", "wt"))

where fp is NULL if the file fails to open.

One of the classic places where putting the constant on the left can save you lots of grief is

fp = fopen("myfile.txt", "wt")
if (fp = NULL)
exit(1);
fprintf(fp, "Hello World!");

This will set fp equal to NULL, which will ensure that it is NOT trapped and guarantee an attempt to write to a NULL file pointer.

Now consider if it had been written

fp = fopen("myfile.txt", "wt")
if (NULL = fp)
exit(1);
fprintf(fp, "Hello World!");

Guess what? Syntax error pointing you right at the if() statement.

I once had a student that came to my office asking about how to open a file for writing. I showed her and explained why she should use the second form. She said, "But that looks awkward." I told her that I agreed, it is awkward at first but that it has saved me countless hours chasing down logic errors due to using '=' instead of '=='. Three hours later she came back completely frustrated because she couldn't get her program to work because it just kept crashing. I took one look and saw that she had done exactly the first form. So I told her that she chose not to practice the science of engineering by learning from my mistakes, so instead she chose to practice the art by learning from her own.
 

ArakelTheDragon

Joined Nov 18, 2016
1,362
In "C" "1=a" is forbidden normally.

"c=a" would mean in varible "c" record the value of variable "a". If you wanted to record the character constant "a", you would need to write c='a', that way no matter of the character set value of the machine you are using, you will always have 'a' written into the variable "c". When you are writing a character constant you use ' ', when you are writing a character string you write " ". For example the value of the character constant '0' is "48" in ASCII. Therefore when you write c='0', you actually write c=48 if you are using the ASCII character set. If you are using a different character set, the value in "c" will be something else, but it depends on the compiler and it will still be visualized as "a" when needed.
 

WBahn

Joined Mar 31, 2012
30,055
In "C" "1=a" is forbidden normally.

"c=a" would mean in varible "c" record the value of variable "a". If you wanted to record the character constant "a", you would need to write c='a', that way no matter of the character set value of the machine you are using, you will always have 'a' written into the variable "c". When you are writing a character constant you use ' ', when you are writing a character string you write " ". For example the value of the character constant '0' is "48" in ASCII. Therefore when you write c='0', you actually write c=48 if you are using the ASCII character set. If you are using a different character set, the value in "c" will be something else, but it depends on the compiler and it will still be visualized as "a" when needed.
And this is relevant to the TS's question and the discussion... how? Notice that nothing in the discussion had anything to do with characters or character codes.
 

MrChips

Joined Oct 2, 2009
30,806
I believe what the TS is asking is, what is the inherent value of boolean TRUE and FALSE?

The statement (a == b) is a boolean expression that returns a value of TRUE or FALSE.

The actual value assigned to a boolean expression depends on the compiler. The most common assignment is boolean FALSE is zero, boolean TRUE is non-zero.
 

spinnaker

Joined Oct 29, 2009
7,830
And this is relevant to the TS's question and the discussion... how? Notice that nothing in the discussion had anything to do with characters or character codes.
Poster has had a number of posts of late that make no sense. Only creates confusion for the TS.
 

WBahn

Joined Mar 31, 2012
30,055
I believe what the TS is asking is, what is the inherent value of boolean TRUE and FALSE?

The statement (a == b) is a boolean expression that returns a value of TRUE or FALSE.

The actual value assigned to a boolean expression depends on the compiler. The most common assignment is boolean FALSE is zero, boolean TRUE is non-zero.
I didn't get that this was the gist of what the TS was asking, but if it is then the answer is NOT dependent on the compiler.

The C standard is quite clear on this. For the relational operators (<, <=, >, >=), equality operators (==, !=) and logical operators (!, &&, ||) the result of the expression is of type int and is equal to 0 if the expression is false and equal to 1 if the expression is true. There is absolutely no wiggle room on this.

In evaluating whether an expression is true of false, if the expression evaluates to exactly zero, then it is false. Otherwise (any non-zero value) it is true. But this is separate from the value yielded by a logical/relational expression which, if true, MUST evaluate to a value of 1 of type int.
 

MrChips

Joined Oct 2, 2009
30,806
I didn't get that this was the gist of what the TS was asking, but if it is then the answer is NOT dependent on the compiler.

The C standard is quite clear on this. For the relational operators (<, <=, >, >=), equality operators (==, !=) and logical operators (!, &&, ||) the result of the expression is of type int and is equal to 0 if the expression is false and equal to 1 if the expression is true. There is absolutely no wiggle room on this.

In evaluating whether an expression is true of false, if the expression evaluates to exactly zero, then it is false. Otherwise (any non-zero value) it is true. But this is separate from the value yielded by a logical/relational expression which, if true, MUST evaluate to a value of 1 of type int.
It's always good to get clarification from an expert.
 
Top