C语言学习第一天 CLion 的安装 和 变量

1.安装CLion

不用说学习一门语言最重要的肯定是先安装软件,因为本人习惯了用idea 所有想都没想就 使用了 clion,百度上有很多关于clion的安装文档,就不再多说了.

安装可以参考此篇文章
Window10上CLion极简配置教程

2.不用说学习一门新语言第一件事肯定是打印Hello World

直接上代码 (这个安装好clion应该会直接有)

#include <stdio.h>
int main() {
    printf("hello world");
    void aaa();
    return 0;
}

3.输出格式

格式 意义
%d 输出整数
%f 输出浮点数
%6d 输出整数,最少6位宽
%6f 输出浮点数,最少6位宽
%.2f 输出浮点数,小数点后2位
%6.2f 输出浮点数,最少6位字符宽,小数点后2位
%c 输出单个字符
%% 输出百分号%
\n 换行

4.变量

常用的变量类型

short 、int、long 、float 、 double 、 char

整型:

short 占据的内存大小是2 个byte(字节);
int占据的内存大小是4 个byte;
long占据的内存大小是4 个byte;


#include <stdio.h>
int main() {

//可以先声明不赋值  变量必须先声明才能使用
    int a, b, c, d;
    //也可以定义的时候直接赋值
    short e = 10, f = 20;
    long g = 1000000000;

    //用 = 号 赋值
    a = 10;
    b = 20;
    // C中的常用计算符号 加减乘除   + - * /
    c = a + b;
    d = a * b + g;
    //再次赋值后会替换原先的值
    g = f / 10;

    //进行输出
    printf("%d\n", a + b);  //30
    printf("%d\n", a * b); //200
    printf("%d\n", c);  //30
    printf("%d\n", d);  //1000000200
    printf("%d\n", e); //10
    printf("%ld\n", g); //2
    return 0;
}

//整个项目只能有一个main方法,因此测试的使用可以调用方法来写
//或者在CMakeLists.txt 文件中添加
代码如下
#include <stdio.h>
int main() {
    printf("hello world");
    //c语言中调用方法也必须声明
    void aaa();
    aaa();
    return 0;
}

void test01() {
    //可以先声明不赋值  变量必须先声明才能使用
    //一般最常用的是int
    int a, b, c, d;
    //也可以定义的时候直接赋值
    short e = 10, f = 20;
    long g = 1000000000;

    //用 = 号 赋值
    a = 10;
    b = 20;
    // C中的常用计算符号 加减乘除   + - * /
    c = a + b;
    d = a * b + g;
    //再次赋值后会替换原先的值
    g = f / 10;

    //进行输出
    printf("%d\n", a + b);  //30
    printf("%d\n", a * b); //200
    printf("%d\n", c);  //30
    printf("%d\n", d);  //1000000200
    printf("%d\n", e); //10
    printf("%ld\n", g); //2
}
浮点型:

float占据的内存大小是4 个byte;
double占据的内存大小是8 个byte;

#include <stdio.h>
int main() {
//    printf("hello world");
    //c语言中的方法必须先声明再调用
//    void test01();
//    test01();

    void test02();
    test02();
    return 0;
}

void test02(){
	 //一般最常用的是double
    double a = 10;
    float b = 20;
    double c = a / b; 
    printf("%f\n", c); //0.500000
}
字符型:

char占据的内存大小是1 个byte。

#include <stdio.h>
int main() {
//    printf("hello world");
    //c语言中的方法必须先声明再调用
//    void test01();
//    test01();

//    void test02();
//    test02();

    void test03();
    test03();
    return 0;
}
    
void test03() {
    //字符型
    char c = 'c';
    //c语言的字符串类型定义要加 * 号
    char  *cc ;
    cc = "aa";

    printf("%c", c); // a
    printf("%s", cc); //cc
}
发布了88 篇原创文章 · 获赞 114 · 访问量 3022

猜你喜欢

转载自blog.csdn.net/hongchenshijie/article/details/103515618
今日推荐