首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

C 语言从增 运算

2013-08-09 
C 语言自增 运算在C语言的面试题目中 经常会考到自增和自减的运算符,而且还是在同一行对同一个变量进行操

C 语言自增 运算
在C语言的面试题目中 经常会考到自增和自减的运算符,而且还是在同一行对同一个变量进行操作。

#include<stdio.h>
int main(){
        int i=1;
        i=i++ + ++i;
        printf("i=%d\n",i);
        int x=1;
        int y=0;
        y=x++ + ++x;
        printf("x=%d  y=%d\n",x,y);
        int z=0;
        z=z++;
        printf("z=%d\n",z);
        z=0;
        z=++z;
        printf("z=%d\n",z);
}

在Ubuntu下 用gcc 命令编译的结果:
i=5
x=3  y=4
z=1
z=1
为什么 i =5  y=4   第一个z是1 呢?
[解决办法]
自己写代码搞不清楚算符优先级请多加括号。
看别人代码搞不清楚算符优先级能调试的话请单步调试对应汇编。
看别人代码搞不清楚算符优先级不能调试的话想办法照写一小段孤立的可调试的代码然后单步调试对应汇编。
看别人代码搞不清楚算符优先级不能调试的话且没有办法照写一小段孤立的可调试的代码然后单步调试对应汇编的话只能参考算符优先级表猜了(提醒:并不能100%猜对)。
//C++ Operators
//  Operators specify an evaluation to be performed on one of the following:
//    One operand (unary operator)
//    Two operands (binary operator)
//    Three operands (ternary operator)
//  The C++ language includes all C operators and adds several new operators.
//  Table 1.1 lists the operators available in Microsoft C++.
//  Operators follow a strict precedence which defines the evaluation order of
//expressions containing these operators.  Operators associate with either the
//expression on their left or the expression on their right;    this is called


//“associativity.” Operators in the same group have equal precedence and are
//evaluated left to right in an expression unless explicitly forced by a pair of
//parentheses, ( ).
//  Table 1.1 shows the precedence and associativity of C++ operators
//  (from highest to lowest precedence).
//
//Table 1.1   C++ Operator Precedence and Associativity
// The highest precedence level is at the top of the table.
//+------------------+-----------------------------------------+---------------+
//
[解决办法]
 Operator         
[解决办法]
 Name or Meaning                         
[解决办法]
 Associativity 
[解决办法]

//+------------------+-----------------------------------------+---------------+
//
[解决办法]
 ::               
[解决办法]
 Scope resolution                        
[解决办法]
 None          
[解决办法]

//
[解决办法]
 ::               
[解决办法]
 Global                                  
[解决办法]
 None          


[解决办法]

//
[解决办法]
 [ ]              
[解决办法]
 Array subscript                         
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 ( )              
[解决办法]
 Function call                           
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 ( )              
[解决办法]
 Conversion                              
[解决办法]
 None          
[解决办法]

//
[解决办法]
 .                
[解决办法]
 Member selection (object)               
[解决办法]
 Left to right 
[解决办法]

//
------解决方案--------------------


 ->               
[解决办法]
 Member selection (pointer)              
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 ++               
[解决办法]
 Postfix increment                       
[解决办法]
 None          
[解决办法]

//
[解决办法]
 --               
[解决办法]
 Postfix decrement                       
[解决办法]
 None          
[解决办法]

//
[解决办法]
 new              
[解决办法]
 Allocate object                         
[解决办法]
 None          
[解决办法]

//
[解决办法]
 delete           
------解决方案--------------------


 Deallocate object                       
[解决办法]
 None          
[解决办法]

//
[解决办法]
 delete[ ]        
[解决办法]
 Deallocate object                       
[解决办法]
 None          
[解决办法]

//
[解决办法]
 ++               
[解决办法]
 Prefix increment                        
[解决办法]
 None          
[解决办法]

//
[解决办法]
 --               
[解决办法]
 Prefix decrement                        
[解决办法]
 None          
[解决办法]

//
[解决办法]
 *                
[解决办法]
 Dereference                             


[解决办法]
 None          
[解决办法]

//
[解决办法]
 &                
[解决办法]
 Address-of                              
[解决办法]
 None          
[解决办法]

//
[解决办法]
 +                
[解决办法]
 Unary plus                              
[解决办法]
 None          
[解决办法]

//
[解决办法]
 -                
[解决办法]
 Arithmetic negation (unary)             
[解决办法]
 None          
[解决办法]

//
[解决办法]
 !                
[解决办法]
 Logical NOT                             


[解决办法]
 None          
[解决办法]

//
[解决办法]
 ~                
[解决办法]
 Bitwise complement                      
[解决办法]
 None          
[解决办法]

//
[解决办法]
 sizeof           
[解决办法]
 Size of object                          
[解决办法]
 None          
[解决办法]

//
[解决办法]
 sizeof ( )       
[解决办法]
 Size of type                            
[解决办法]
 None          
[解决办法]

//
[解决办法]
 typeid( )        
[解决办法]
 type name                               
------解决方案--------------------


 None          
[解决办法]

//
[解决办法]
 (type)           
[解决办法]
 Type cast (conversion)                  
[解决办法]
 Right to left 
[解决办法]

//
[解决办法]
 const_cast       
[解决办法]
 Type cast (conversion)                  
[解决办法]
 None          
[解决办法]

//
[解决办法]
 dynamic_cast     
[解决办法]
 Type cast (conversion)                  
[解决办法]
 None          
[解决办法]

//
[解决办法]
 reinterpret_cast 
[解决办法]
 Type cast (conversion)                  
[解决办法]
 None          
[解决办法]

//
[解决办法]
 static_cast      


[解决办法]
 Type cast (conversion)                  
[解决办法]
 None          
[解决办法]

//
[解决办法]
 .*               
[解决办法]
 Apply pointer to class member (objects) 
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 ->*              
[解决办法]
 Dereference pointer to class member     
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 *                
[解决办法]
 Multiplication                          
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 /                
[解决办法]
 Division                                
------解决方案--------------------


 Left to right 
[解决办法]

//
[解决办法]
 %                
[解决办法]
 Remainder (modulus)                     
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 +                
[解决办法]
 Addition                                
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 -                
[解决办法]
 Subtraction                             
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 <<               
[解决办法]
 Left shift                              
[解决办法]
 Left to right 
------解决方案--------------------



//
[解决办法]
 >>               
[解决办法]
 Right shift                             
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 <                
[解决办法]
 Less than                               
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 >                
[解决办法]
 Greater than                            
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 <=               
[解决办法]
 Less than or equal to                   
[解决办法]
 Left to right 
[解决办法]

//
------解决方案--------------------


 >=               
[解决办法]
 Greater than or equal to                
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 ==               
[解决办法]
 Equality                                
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 !=               
[解决办法]
 Inequality                              
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 &                
[解决办法]
 Bitwise AND                             
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 ^                


[解决办法]
 Bitwise exclusive OR                    
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 
[解决办法]
                
[解决办法]
 Bitwise OR                              
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 &&               
[解决办法]
 Logical AND                             
[解决办法]
 Left to right 
[解决办法]

//
[解决办法]
 
[解决办法]
               
[解决办法]
 Logical OR                              
[解决办法]
 Left to right 
[解决办法]

//
------解决方案--------------------


 e1?e2:e3         
[解决办法]
 Conditional                             
[解决办法]
 Right to left 
[解决办法]

//
[解决办法]
 =                
[解决办法]
 Assignment                              
[解决办法]
 Right to left 
[解决办法]

//
[解决办法]
 *=               
[解决办法]
 Multiplication assignment               
[解决办法]
 Right to left 
[解决办法]

//
[解决办法]
 /=               
[解决办法]
 Division assignment                     
[解决办法]
 Right to left 
[解决办法]

//
[解决办法]
 %=               
[解决办法]
 Modulus assignment                      


[解决办法]
 Right to left 
[解决办法]

//
[解决办法]
 +=               
[解决办法]
 Addition assignment                     
[解决办法]
 Right to left 
[解决办法]

//
[解决办法]
 -=               
[解决办法]
 Subtraction assignment                  
[解决办法]
 Right to left 
[解决办法]

//
[解决办法]
 <<=              
[解决办法]
 Left-shift assignment                   
[解决办法]
 Right to left 
[解决办法]

//
[解决办法]
 >>=              
[解决办法]
 Right-shift assignment                  
[解决办法]
 Right to left 
[解决办法]

//
------解决方案--------------------


 &=               
[解决办法]
 Bitwise AND assignment                  
[解决办法]
 Right to left 
[解决办法]

//
[解决办法]
 
[解决办法]
=               
[解决办法]
 Bitwise inclusive OR assignment         
[解决办法]
 Right to left 
[解决办法]

//
[解决办法]
 ^=               
[解决办法]
 Bitwise exclusive OR assignment         
[解决办法]
 Right to left 
[解决办法]

//
[解决办法]
 ,                
[解决办法]
 Comma                                   
[解决办法]
 Left to right 
[解决办法]

//+------------------+-----------------------------------------+---------------+


不要迷信书、考题、老师、回帖;
要迷信CPU、编译器、调试器、运行结果。
并请结合“盲人摸太阳”和“驾船出海时一定只带一个指南针。”加以理解。
任何理论、权威、传说、真理、标准、解释、想象、知识……都比不上摆在眼前的事实!

不要写连自己也预测不了结果的代码!

热点排行