C – 基础 – 数据类型

安装

下载离线安装包iso文件,加载之后选择安卓,如果要写c/c++记得勾选widows sdk .

VS控制台程序运行一闪而过的完美解决办法

在你的项目条目(project)上右击鼠标,在弹出菜单上选择最后一项“property/属性”,在左边的一栏里找到“配置属性->链接器->系统”,点击“系统”项后,在右边的栏的“子系统(subSystem)”将刻项的值配置为”Console(/SUBSYSTEM:CONSOLE)”。

调试时不再用F5键,改成Ctrl+F5键。

hello world

#include <stdio.h>int main(){    printf("help");    return 0;}

数据类型

位: 最小存储单位bit ,可容纳 0 和 1 。字节: byte常用存储单位。一般来说,1byte=8bit,可表示0~255整数或字符。字: word自然存储单位。现使用64位系统,即1字为64位。

#include <stdio.h>#include <inttypes.h>int main(){    // 整数类型    int i = 1024;    printf("I am %d years old \n" , i);    printf("sizeof int is %d \n", sizeof(i));    printf("i dec = %d; octal = %o ; hex = %x \n", i, i, i);    printf("i dec = %d; octal = %#o ; hex = %#x \n", i, i, i);    printf("sizeof short int is %d \n", sizeof(short int));    short si = 1024;    printf("print short use %%hd eg %hd \n" , si);    printf("sizeof long int  is %d \n", sizeof(long int));    printf("sizeof long long int is %d \n", sizeof(long long int));    long long lli = 1024;    printf("pring long long use %%lld eg %lld \n", lli);    printf("sizeof unsigned int is %d \n", sizeof(unsigned int));    printf("sizeof unsigned long int is %d \n", sizeof(unsigned long int));    // char 类型    char c = 'A';    printf("size of char is %u \n", sizeof(char));    printf("printf char use %%c eg %c or %%d eg %d \n", c, c);    unsigned char   uc = 255;    printf("char uc is %c ,int uc is %d \n", uc, uc);    // _Bool 类型 0:flase other:true    _Bool flag = 2;    printf("size of _Bool is  %d \n", sizeof(_Bool));    printf("printf _Bool eg %d \n", flag);    // 可移植类型 inttypes.h    int16_t me16 = 4593;    printf("me16 = %hd \n", me16);    printf("me16 = %"PRId16"\n", me16);    // float / double / long double 类型    float ff = 10e9;    double dd = 10e9;    long double ld = 10e9;    printf("size of float is %d \n", sizeof(float));    printf("size of double is %d \n", sizeof(double));    printf("size of long double is %d \n" , sizeof(long double));    printf("%f can ber written %e\n", ff, ff);    printf("%f can ber written %e\n", dd, dd);    printf("%f can ber written %e\n", ld, ld);    return 0;}

字符串输入输出

关于使用VS会出现scanf要用scanf_s的处理:Alt+F7,打开工程属性,c/c++ 预处理器定义,添加一行内容_CRT_SECURE_NO_DEPRECATE就OK了

#include <stdio.h>#define HI "ni hao a ! "int main(){    // 字符串输入输出    char name[40];    printf("What's your name ?\n");    scanf("%s", name);    printf("%s , %s \n", HI, name);    return 0;}

格式化输出

#include <stdio.h>#include <string.h>int main(){    // 字符串函数    char name[40]  = "qianqian.sun";    printf("size of name is %u \n", sizeof(name)); // 40    printf("length of name is %d \n ", strlen(name)); // 12    printf("\n使用修饰符 和 转换符打印表格\n");    for (int i = 0; i < 25; i++)printf("-"); printf("\n");    printf("| %5s | %5s | %5s | \n", "姓名", "年龄", "身高");    for (int i = 0; i < 25; i++)printf("-"); printf("\n");    printf("| %5s | %5d | %5.2f | \n","前前",24,1.85);    for (int i = 0; i < 25; i++)printf("-"); printf("\n");    // * 修饰符    printf("可指定宽度和精度:%*d\n", 5, 100);    printf("可指定宽度和精度:%*.*f\n", 10,2, 10.0);    int  width, precision;    scanf("%*d %d %d", &width, &precision); // 跳過第一个参数    printf(" Value = %*.*f \n", width, precision, 66.6);    return 0;}

字符的输入输出

字符与单词统计

#include <stdio.h>#include <stdlib.h>#include <ctype.h>int main(){    int ch;    FILE * fp;    char name[50] = "C:/share/PostLoanData/file.txt";    fp = fopen(name, "r");    if (NULL == fp)    {        printf("Fail to open file \n");        exit(1);    }    int count, lower_count, upper_count, word_len, word_count,cur_word;    count = lower_count = upper_count = word_len = word_count = cur_word =0;    while ((ch = getc(fp)) != EOF)    {        switch (ch)        {        case '\n':            printf("\\n");            break;        case '\t':            printf("\\t");            break;        default:            putchar(ch);            break;        }        printf(": %d |", ch);        if (++count % 10 == 0)printf("\n");        if (islower(ch)) lower_count++;        if (isupper(ch)) upper_count++;        if (isalpha(ch)) cur_word++; // 字母则累计        if (isdigit(ch)) cur_word = 0; //遇到 数字 则清零        if ((isspace(ch) || ispunct(ch)) && cur_word>0)        {            word_count++;            word_len += cur_word;            cur_word = 0; //空白 标点则判断当前是否为单词        }    }    if (cur_word>0)    {        word_count++;        word_len += cur_word;    }    fclose(fp);    printf("\n 字符总数: %5d \n", count);    printf("大写字母数: %d , 小写字母数: %d \n", upper_count, lower_count);    printf("大写字母数: %d , 小写字母数: %d \n", upper_count, lower_count);    printf("word_len = %d \n", word_len);    printf("单词总数: %d , 平均长度: %5.2f \n", word_count, (double)word_len/ word_count);    return 0;}

为什么?答:点线杆上贴着”“此处不许小便!”

C – 基础 – 数据类型

相关文章:

你感兴趣的文章:

标签云: