c++代码格式与语法基础

本文介绍了C++编程语言的基本语法,包括`main`函数的使用,变量声明(如整数、浮点数、字符和布尔),数组操作,常量定义,字符串处理,变量交换以及条件判断。通过实例展示了这些核心概念的运用。
摘要由CSDN通过智能技术生成

c++代码格式与语法基础

#include <bits/stdc++.h> //使用万能头文件
using namespace std;

int main()
{
    cout << "Hello,World!" << endl;//利用cout将字符串输出,字符串用双引号扩起
    printf("Hello,World!");//利用printf将字符串输出
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int x=3;
    double d=3.14;
    char ch='A';
    char s[]="hello";
    bool b=ture;
    
    cout << x << '\n';//'\n是换行符号,比endl更快'//3
    cout << d << '\n';//3.14
    cout << ch << '\n';//A
    cout << s << '\n';//hello
    cout << b << '\n';//1
    
    return 0;
}

手撕代码-1(数组)

#include <bits/stdc++.h>
using namespace std;

//const表示常量,后续不可以被修改
const int N=1e5+9;
int a[N];
//开了一个大小为N的全局数组,下标为[0,N-1],自动初始化为0(在全局变量里)

int main()
{
    
    return 0;
}

手撕代码-2(typedef)

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;//将long long类型定义为ll(方便后续使用)

//const表示常量,后续不可以被修改
const int N=1e5+9;

//开一个大小为N的全局数组(类型为long long),下标为【0,N-1】,自动初始化为0
ll a[N];

int main()
{
    
    return 0;
}

手撕代码-3(字符串)

#include <bits/stdc++.h>
using namespace std;
int main()
{
    char s[]="hello";
    
    for(i=0;i<=4;i++)
    {
        cout << s[i];  //hello
    }
    cout << '\n';
    cout << s <<'\n'; //hello
    
    return 0;
}

手撕代码-4(交换变量)

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int a=5,b=3;
    int tmp=b;
    b=a;
    a=tmp;
    cout << a << '' << b << '\n';//3 5
    return 0;
}

手撕代码-5(判断)

#include <bits/stdc++.h>
using namespace std;
int main()
{
    //输出1~n中所有的偶数
    int n=10;
    for(int i=i;i<=n;i++)
    {
        if(i%2==0)
            cout << i << '\n';
    }
    return 0;
}
  • 12
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值