C++学习第一天

1. hello world

#include<iostream>
using namespace std;

int main(){

    cout << "Hello C++  World!!!" << endl;

    system("pause");

    return 0;
}

2. 注释和变量

#include<iostream>
using namespace std;

//1、单行注释

//2、多行注释

/*
    main是一个程序的入口
    每个程序必须有这个函数
    有且只有一个
*/

int main(){

    //在屏幕中输出  Hello World!  字样
    cout << "Hello World!" << endl;

    //变量创建语法
    int a = 10;
    cout << "a = " << a << endl;

    system("pause");

    return 0;
}

3. 常量

#include<iostream>
using namespace std;

//常量定义方式
//1. #define 宏常量
//2. const修饰的变量
# define Day 7

int main(){

    cout << "一周共有: " << Day << "天" << endl;

    //2. const修饰的变量
    const int month = 12;
    cout << "一年共有: " << month << "个月" << endl;

    system("pause");
    return 0;
}

4. 命名规则

/*
标识符命名规则:
1、标识符不可以是关键字
    int int = 10;
2、标识符只能由字母、数字、下划线组成,且不能以数字开头
    int abc = 10;
    int _abc = 10;
    int _123abc = 10;
    int 123abc = 40; // x
3、标识符严格区分大小写
*/

//建议: 给变量起名的时候,最好

5. 整型和浮点型

#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;


int main(){
   //整型
   //1.短整型 (-32768 ~ 32767)
   short a = 1;
   //2.整型
   int b = 32768;
   //3.长整型
   long c = 1;
   //4.长长整型
   long long d = 1;
   

   cout << "a = " << a << endl;
   //sizeof()方法是用来计算变量所占内存空间的大小
   cout << "short 类型所占内存空间为: " << sizeof(a) << endl;

   cout << "b = " << b << endl;
   cout << "int 类型所占内存空间为: " << sizeof(b) << endl;

   cout << "c = " << c << endl;
   cout << "long 类型所占内存空间为: " << sizeof(c) << endl;

   cout << "d = " << d << endl;
   cout << "long long 类型所占内存空间为: " << sizeof(d) << endl;


   return 0;
}

#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;


int main(){
   // 1. 单精度  float
   // 2. 双精度  double
   // 默认情况下  输出一个小数,会显示6为有效数字

   float f1 = 3.1415926f;//默认是单精度
   cout << "f1 = " << f1 << endl;

   double d1 = 3.1415926;
   cout << "d1 = " << d1 << endl;

   // 科学计数法
   float f2 = 3e2; // 3 * (10)^2 = 300
   cout << "f2 = " << f2 << endl;

   float f3 = 3e-2; // 3 * (10)^-2 = 0.03
   cout << "f3 = " << f3 << endl;

   return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值