0
点赞
收藏
分享

微信扫一扫

初始C语言-第二节

目标践行者 2022-04-03 阅读 62
c语言

文章目录


一、常量有哪几种

  • 1.字面常量
  • 2.const 修饰的常变量
  • 3.#define定义的标识符常量
  • 枚举常量
#include<stdio.h>
//枚举常量的默认是从0开始的,依次往下递增1
enum Sex
{
    MALE;
    FEMALE;
    SECRET;
};
//括号中的MALE、FEMALE、SECRET是枚举常量
int main()
{
    //字面常量
    3.14;
    1000;
    
    //const 修饰的常变量
    const floar pi = 3.14f;
    //pi是const修饰的常变量
    pi = 5.14;
    //pi是不能直接被修改的
    
    //#define 标识符常量
    #define MAX 100
    printf("%d\n", MAX);
    printf("%d\n", MALE);
    printf("%d\n", SECRET);

注意:上边例子中的pi被称为const修饰的常变量,const修饰的常变量在c语言中只是在语法层面限制了变量pi不能被改变,但是pi本质上还是一个变量,所以叫做常变量。

二、字符串+转义字符+注释

2.1字符串

"hello world.\n"

这种由双引号(Double Quote)引起的一串字符称为字符串面值(String Literal),或则简称字符串
注:字符串的结束标志是一个\0的转移字符没在计算字符串的长度的时候\0是结束标志,不算作字符串的内容。

#include<stdio.h>
int main()
{
    char arr1[] = 'abc';
    char arr2[] = {'a', 'b', 'c'};
    char arr3[] = {'a','b','c','\0'};
    printf("%s\n", arr1);
    printf("%s\n", arr2);
    printf("%s\n", arr3);
    return 0;
}

2.2转义字符

如果我们想在屏幕上打印一个目录:c:\Windows\test.c
image.png
实际上:image.png

正是因为转义字符是的输入和输出不同。
转义字符:

举例:image.png

为什么\26、\124的值时&和T呢?
通过计算 \26 —>38 ; \124 —>84 >>ASCII中找到对应的值

例题:

1.程序输出什么?

#include<stdio.h>
int main()
{
    printf("%d\n", strlen("abcdefg"))printf("%d\n", strlen("c:\test\628\test.c"));
    return 0;
}   

image.png

2.3注释

  • 1.代码中有些不需要的代码可以直接删除,也可以注释掉。
  • 2.代码中有那些代码比较难以理解,可以加注释文字。

例如:

#include<stdio.h>
int sum(int i, int j)
{
    return i + j;
}
int main()
{
    int x = 0;
    int y = 0;
    scnaf("%d %d", &x, &y);
    int sum = Add(x , y);
    //调用函数Add完成加法
    printf("%d\n", sum);
    return 0;
}  

注释有两种风格:

  • c语言风格的注释:/xxxxxx/,
  • c++风格的注释: //xxxxxx

三、选择语句

如果积极参加比赛,就能学到知识。
如果不参加比赛,就啥也不是。image.png

#include<stdio.h>
int main()
{
    int compete = 0;
    printf("你会去参加比赛吗?(选z或则0):>");
    scanf("%d", &compete);
    if(compete == 1)
    {
        printf("掌握知识");
    }
    else
    {
        printf("啥也不是");
    }
    return 0;
}

四、循环语句

  • while语句
  • for语句
  • do while语句
#include<stdio.h>
int mian()
{
    int i = 0;
    while(i < 10)
    {
        printf("%d\n", i);
        i++;
    }
    return 0;
}
#include<stdio.h>
int main()
{
    int i = 0;
    for(i = 0;i < 10;i++)
    {
        printf("%d\n", i);
    }
    return 0;
}

五、函数

#include<stdip.h>
int main()
{
    int x = 0;
    int y = 0;
    scanf("%d %d", &x,&y);
    int sum = x + y;
    printf("%d\n", sum)
    return 0;
}

上述代码用函数写为:

#include<stdio.h>
int Add(int x,int y)
{
    return x + y;
}
int main()
{
    int i = 0;
    int j = 0;
    scanf("%d %d", &i, &j);
    int sum = Add(i, j);
    printf("%d\n", sum);
    return 0;
}

函数的特点是简化代码,代码复用。

六、数组

要存储1-10的数字,怎么存储?
C语言中给了数组的定义:一组相同类型元素的集合

6.1数组定义

int arr[10] = {1,2,3,4,5,6,7,8,9,10}//定义一个整形数组,最多放十个元素

6.2数组的下标

C语言规定:数组的每个元素都有一个下标,下标是从0开始的。
数组可以通过下标访问。

int arr[10] 1 2 3 4 5 6 7 8 9 10
下标 0 1 2 3 4 5 6 7 8 9

6.3数组的使用

#include<stdio.h>
int main()
{
    int i = 0;
    int arr[10] = {1,2.3,4,5,6,7,8,9,10};
    for(i = 0;i < 10;i++)
    {
        printf("%d\n", arr[i]);
    }
    printf("\n);
    return 0;
}

注意:
image.png

举报

相关推荐

0 条评论