首页 > 其他分享 >C语言逗号表达式和赋值表达式

C语言逗号表达式和赋值表达式

时间:2023-10-06 11:23:11浏览次数:42  
标签:求解 int C语言 逗号 表达式 赋值

C语言逗号表达式和赋值表达式

笔试常考这个,没办法

#include <iostream>
using namespace std;
int main(int argc, char const* argv[]) {
    int a = 0, b = 0, c = 0, d = 0;
    // 1. 逗号表达式
    /*
    表达式1,表达式2,表达式3……表达式n;
    逗号表达式的求解过程是:先求解表达式1,再求解表达式2。
    整个逗号表达式的值是表达式n的值。。
    */
    a = 3 + 4, 4 * 5;    // 7
    b = (3 + 4, 4 * 5);  // 20
    cout << a << " " << b << endl;
    // 2. 赋值表达式
    // 就是赋值号最左边的值
    d = (c = 3 + 4, c * 5);  // c = 7, d = 35
    cout << c << " " << d << endl;
    c = a = b = 11;  // c = 11
    cout << c << endl;
    if ((d = []() { return 5; }()) == 5)  // 在if语句中利用赋值表达式,在STL中很常见
        cout << "d == 5" << endl;
    return 0;
}

标签:求解,int,C语言,逗号,表达式,赋值
From: https://www.cnblogs.com/3to4/p/17744344.html

相关文章

  • 实验1 C语言输入输出和简单程序编写
    1.实验任务11.c1#include<stdio.h>23intmain()4{5printf("o\n");6printf("<H>\n");7printf("II\n");89return0;10}1-1.c1#include<stdio.h>23in......
  • C语言:‘for‘ loop initial declarations are only allowed in C99 mode
    求最大公约数之穷举法mistake: because: 只允许在C99模式下使用‘for’循环初始化声明  solution:不在for()中初始化生命变量 ......
  • c语言代码(递归)练习23
    需求:求解用户给的第几位斐波那契数,斐波那契数1,1,2,3,5,9,13,22,35,55....:这位数等于前两位数相加。#define_CRT_SECURE_NO_WARNINGS1#include<stdio.h>intayue(inti){if(i<=2){return1;}else{returnayue(i-1)+......
  • c语言代码(递归)练习22
    需求:利用递归函数求解用户输入数字的阶乘。#define_CRT_SECURE_NO_WARNINGS1#include<stdio.h>intayue(inti){intx=0;intcount=1;for(x=1;x<=i;x++){count*=x;}returncount;}intayue2(intn){if(n......
  • c语言代码(递归)练习21
    需求:在不使用临时变量的情况下,利用函数求取字符串的长度#define_CRT_SECURE_NO_WARNINGS1#include<stdio.h>intmy_strlen(char*n){inti=0;while(*n!='\0'){i++;n++;}returni;}//递归函数:大事化小//my_strlen("......
  • 常用的SQL语句小结(三)---复杂查询,CASE表达式,各种连接查询
    1.复杂查询(1)普通子查询SELECTproduct_type,cnt_productFROM(SELECTProduct_type,COUNT(*)AScnt_productFROMProductGROUPBYproduct_type)ASProductSum;()里的是内层查询会首先执行,然后才会执行外层查询子查询可以多层嵌套,但是性能会下降,尽量少用多层子查询 (2)标量......
  • 实验1 C语言输入输出和简单程序编写
    1.实验任务1 task1_1源代码:1#include<stdio.h>2#include<stdlib.h>3intmain()4{5printf("0\n");6printf("<H>\n");7printf("II\n");8printf("0\n");9prin......
  • c语言代码练习20
    需求:每调用一次函数,num增加一次。#define_CRT_SECURE_NO_WARNINGS1#include<stdio.h>voidayue(int*p){(*p)++;}intmain(){intnum=0;ayue(&num);printf("num=%d\n",num);ayue(&num);printf("num=%d&......
  • c语言代码练习19
    需求:利用二分查找,查找数组中是否有用户输入的数字。#define_CRT_SECURE_NO_WARNINGS1#include<stdio.h>//这里的arr[]实际上是一个指针intayue(intarr[],inta,intp){intleft=0;intright=p-1;while(left<=right){inti=(ri......
  • 实验1_c语言输入输出和简单程序应用编程
    实验一1-1#include<stdio.h>intmain(){printf("O\n");printf("<H>\n");printf("II\n");printf("O\n");printf("<H>\n");printf("II\n");......