C++学习笔记(2)

版权声明:We alone,we together https://blog.csdn.net/u014492512/article/details/83928463

1、手动输入内容

#include <stdio.h>
#include <cstdlib>

int main()
{
    int n;
    scanf("%d", &n);
    printf("%d\n", n);
    
    int a,b;
    scanf("%d%d", &a,&b);
    printf("%d\t%d", a,b);
	
    char str1[10];
    scanf("%5s", str1);//只取前5个字符
    printf("%s\n", str1);

    char str2[10];
    gets(str2);//获取输入的内容(可以含有空格)
    printf("%s\n", str2);
    
    char str3[10];
    scanf("%[^\n]", str3);//使用正则表达式让scanf可以含有空格)
    printf("%s\n", str3);

    char str4[10];
    fgets(str4, sizeof(str4), stdin);//限制输入内容长度,,安全
    printf("%s", str4);
    
    return EXIT_SUCCESS;
}


2、占位符

#include <stdio.h>

//定义常量(常用)
#define  PI 3.1415926

int main()
{
    //定义常量(不常用)
    const float PI_2 = 3.14159;

    /**
     * 占位符
     *
     * %d:输出有符号的10进制int类型
     * %hd:输出短整型(short)
     * %ld:输出长整型(long)
     * %lld:输出长长整型(long long)
     * %u:输出10进制的无符号数
     * %f:输出10进制的浮点
     *
     * %o:输出8进制int类型
     *
     * %x:输出16进制int类型,字母以小写输出
     * %X:输出16进制int类型,字母以大写输出
     */
    printf("%s%f\n", "Global constant:", PI);
    printf("%f\n", PI_2);

    //定义10进制
    int a = -10;
    printf("%u\n", a);
    printf("%d\n", a);

    //定义8进制(以0开头)
    int b = 0566;
    printf("%o\n", b);

    //定义16进制(以0x开头)
    int c = 0x555;
    printf("%x\n", c);
    printf("%X\n", c);
    return 0;
}

3、bit为二进制数的长度(有多少个数字就是多少位)


4、 1字节=8位(1Byte = 8bit)


5、数据类型:

short<=int<=long<long long
数据类型 占用空间
short 2字节
int 4字节
long windows:4字节;Linux32:4字节;Linux64:8字节
long long 8字节
#include <stdio.h>

int main()
{
    short a = 10;
    int b = 20;
    long c = 30;
    long long d = 40;
    
    printf("%d\n", sizeof(a));
    printf("%d\n", sizeof(b));
    printf("%d\n", sizeof(c));
    printf("%d\n", sizeof(d));
    return 0;
}

6、字符型(char)

#include <stdio.h>

int main()
{
    char ch = 'a';
    //打印字符
    printf("%c\n", ch);
    //打印字母a对应10进制数
    printf("%d\n", ch);
    //1个字符的有多少字节
    printf("sizeof:%d\n", sizeof(ch));

    char ch1 = 'a';
    char ch2 = 'A';
    //ASCII码表
    printf("%d\n", ch1 - ch2);
    printf("%c\n", ch1 - 32);
    printf("%c\n", 99);
    printf("\a");

    return 0;
}

猜你喜欢

转载自blog.csdn.net/u014492512/article/details/83928463
今日推荐