As the saying goes, it is better to have no books than to believe in books. The same is true for modern intelligent AI. Although it is powerful, the results cannot be trusted. Recently, I accidentally discovered a very ridiculous question on chatGPT. It can even give wrong answers to the most basic questions. If you believe it, it will disturb the audiovisual.
Question content
c语言中==和&&优先级哪个高?
What is the correct answer? Guess what answer chatGPT will give?
A simple question like 1+1 equals 2 is meaningless in itself, and the bosses will never write such code. But this at least proves that chatgpt is wrong sometimes and cannot be trusted too much. Of course, I still like to use it, but I should be more suspicious of the results he gave in the follow-up, and be careful.
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World\n";
int a = 5,b = 1;
if(a==5 && (b==1) ){
cout << "true";
}else{
cout << "false";
}
return 0;
}
Of course, parentheses like this, which are easy to remember and confuse, are the safest. But sometimes if you are not used to it, you will get bugs. For example, if you believe in this gpt-3.5-AI, you will deliberately generate wrong answers.
After asking twice, I gave the following answer with certainty, which confuses the public and misleads mankind!
For general questions such as 10 chickens and 10 legs, how many legs are there? If it answers wrong, it can still be understood, and it is easy to distinguish. After all, the language ability of the trained AI is not bad, but the reasoning ability is not as good as that of elementary school students. But like this kind of basic knowledge of c language, giving wrong answers is really easy to mislead people. Even if you search on Baidu, the correct results on the Internet account for the majority. I suspect that sometimes it is intentional.
ndd's chatgpt plugin answer
ndd(notepad--), a good notepad software that replaces notepad++.
The gpt plugin on it, the answer is as follows:
Bito's answer for vscode
Try another chatgpt plugin Bito:
Cursor Artifact's answer
It is said that the cursor programming artifact uses the latest gpt-4, so let’s ask it, and the answer is wrong.
Its answer is as follows:
In C, the && operator has higher precedence than the == operator. This means that expressions containing both operators will be evaluated with the && operator first.
Fortunately, I corrected its mistake, and it realized and explained it. Fortunately, it can be corrected after knowing the mistake.
new bing's answer
Finally, try Microsoft's latest new bing and see if there is any miracle.
Although AI can only be used as a reference, it must not be fully believed. But similar to this question, such a basic question can give wrong answers, this AI is sometimes really unreliable.
In comparison, Microsoft's new bing gave the correct answer and did not disappoint.
correct answer
== has higher priority.
Therefore, you must keep in mind the priority of operators in programming, and you should not be self-righteous based on your feelings, so as not to write wrong code.
The order of priority of C language operators from high to low is as follows:
priority |
operator |
name or meaning |
form of use |
Binding direction |
illustrate |
---|---|---|---|---|---|
1 |
[] |
array subscript |
array name [constant expression] |
left to right |
|
() |
Parentheses |
(expression) |
|||
. |
memberselect(object) |
object.membername |
|||
-> |
member selection (pointer) |
Object pointer -> member name |
|||
2 |
- |
minus operator |
-expression |
right to left |
unary operator |
(type) |
cast |
(data type) expression |
|||
++ |
auto increment operator |
++ variable name |
unary operator |
||
-- |
decrement operator |
-- variable name |
unary operator |
||
* |
value operator |
*pointer variable |
unary operator |
||
& |
address-of operator |
&variable name |
unary operator |
||
! |
logical NOT operator |
!expression |
unary operator |
||
~ |
bitwise negation operator |
~ expression |
unary operator |
||
sizeof |
length operator |
sizeof(expression) |
|||
3 |
/ |
remove |
expression/ expression |
left to right |
binary operator |
* |
take |
expression*expression |
binary operator |
||
% |
remainder (modulo) |
integer_expression% integer_expression |
binary operator |
||
4 |
+ |
add |
expression + expression |
left to right |
binary operator |
- |
reduce |
expression - expression |
binary operator |
||
5 |
<< |
move left |
variable << expression |
left to right |
binary operator |
>> |
move right |
variable >> expression |
binary operator |
||
6 |
> |
more than the |
expression>expression |
left to right |
binary operator |
>= |
greater or equal to |
expression >= expression |
binary operator |
||
< |
less than |
expression<expression |
binary operator |
||
<= |
less than or equal to |
expression <= expression |
binary operator |
||
7 |
== |
equal |
expression == expression |
left to right |
binary operator |
!= |
not equal to |
expression != expression |
binary operator |
||
8 |
& |
bitwise AND |
expression&expression |
left to right |
binary operator |
9 |
^ |
bitwise XOR |
expression^expression |
left to right |
binary operator |
10 |
| |
bitwise or |
expression | expression |
left to right |
binary operator |
11 |
&& |
logic and |
expression && expression |
left to right |
binary operator |
12 |
|| |
logical or |
expression || expression |
left to right |
binary operator |
13 |
?: |
conditional operator |
expression1? expression2: expression3 |
right to left |
Ternary operator |
14 |
= |
assignment operator |
variable = expression |
right to left |
|
/= |
assignment after division |
变量/=表达式 |
|||
*= |
乘后赋值 |
变量*=表达式 |
|||
%= |
取模后赋值 |
变量%=表达式 |
|||
+= |
加后赋值 |
变量+=表达式 |
|||
-= |
减后赋值 |
变量-=表达式 |
|||
<<= |
左移后赋值 |
变量<<=表达式 |
|||
>>= |
右移后赋值 |
变量>>=表达式 |
|||
&= |
按位与后赋值 |
变量&=表达式 |
|||
^= |
按位异或后赋值 |
变量^=表达式 |
|||
|= |
按位或后赋值 |
变量|=表达式 |
|||
15 |
, |
逗号运算符 |
表达式,表达式,… |
左到右 |
优先级从上到下依次递减,最上面具有最高的优先级,逗号操作符具有最低的优先级。表达式的结合次序取决于表达式中各种运算符的优先级。优先级高的运算符先结合,优先级低的运算符后结合,同一行中的运算符的优先级相同。
不同类型的运算符之间也有相应的优先级顺序。
一个表达式中既可以包括相同类型的运算符,也可以包括不同类型的运算符或者函数。当多种运算符出现在同一个表达式中时,应该先按照不同类型运算符间的优先级进行运算。
各种运算符间的优先级如下:数值运算符、字符串运算符、关系运算符、逻辑运算符。可以用括号改变优先级顺序,使得括号内的运算优先于括号外的运算。对于多重括号,总是由内到外强制表达式的某些部分优先运行。括号内的运算总是最优先计算。
引用
GDB online Debugger | Compiler - Code, Compile, Run, Debug online C, C++