ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

C 语言复习

2022-07-06 16:35:24  阅读:202  来源: 互联网

标签:复习 int age printf 数组 address prices 语言


switch

We can add a “catch-all” case at the end, labeled default:
int a = 1;
switch (a) {
  case 0:
    /* do something */
    break;
  case 1:
    /* do something else */
    break;
  case 2:
    /* do something else */
    break;
  default:  
    /* handle all the other cases */
    break;
}

Loops

1. for loop

The (int i = 0; i <= 10; i++) block contains 3 parts of the looping details:

  • the initial condition (int i = 0)
  • the test (i <= 10)
  • the increment (i++)
  • 我们首先定义一个循环变量,在本例中命名为i。i是用于循环的常用变量名,与j一起用于嵌套循环(另一个循环中的循环)。这只是个惯例。
  • 变量初始化为0值,完成第一次迭代。然后它就像递增部分说的那样递增(在这个例子中是i++,递增1),所有的循环重复,直到你得到数字10。

2. while loop

while (i < 10) {

}
这个循环将是一个无限循环除非你在循环内部的某个点增加变量i。
无限循环是不好的,因为它会阻塞程序,其他什么都不会发生。

3.Do while loops
int i = 0;

do {
  /* do something */

  i++;
} while (i < 10);
/* The result i = 10 */
The block that contains the /* do something */ comment is always executed at least once, regardless of the condition check at the bottom.

Breaking out of a loop using break

在while循环中使用这个跳出循环的选项特别有趣(当然也可以使用while),因为我们可以创建看似无限的循环,当条件发生时结束,你可以在循环块中定义它:



int i = 0;
while (1) {
  /* do something */

  i++;
  if (i == 10) break;
}

Array

数组是存储多个值的变量。

在C语言中,数组中的每个值都必须具有相同的类型。这意味着您将拥有整型值数组、双精度值数组等。

你可以像这样定义一个int值数组:

const int SIZE = 5;
int prices[SIZE];

也可以在定义时同时初始化一个数组
int prices[5] = { 1, 2, 3, 4, 5 };
又或者
int prices[5]; % Array indexes start from 0, so an array with 5 items

prices[0] = 1;
prices[1] = 2;
prices[2] = 3;
prices[3] = 4;
prices[4] = 5;

又或者使用循环
int prices[5];
for (int i = 0; i < 5; i++) {
  prices[i] = i + 1;
}

C数组的有趣之处在于,数组中的所有元素都是按顺序存储的,一个接着一个。这在高级编程语言中通常不会发生。

数组索引从0开始

C不提供开箱即用的动态数组(你必须使用像链表这样的数据结构)。


Strings

在C语言中,字符串是一种特殊的数组:字符串是char值的数组:
char name[7];
char name[7] = { 'F', 'l', 'a', 'v', 'i', 'o' };

或者更方便地使用字符串字面量(也称为字符串常量),用双引号括起来的字符序列:

char name[7] = "Flavio";

You can print a string via printf() using %s:

printf("%s", name);

你注意到Flavio的长度是6个字符,但我定义的数组长度是7吗?
为什么?     这是因为字符串的最后一个字符必须是0值,即字符串结束符,我们必须为它留出空间。

记住这一点很重要,尤其是在操作字符串时。

说到字符串操作,C提供了一个重要的标准库:string.h。

这个库是必不可少的,因为它抽象了许多使用字符串的低级细节,并为我们提供了一组有用的函数。

你可以在程序中加载这个库: 

#include <string.h>
  • strcpy() to copy a string over another string
  • strcat() to append a string to another string
  • strcmp() to compare two strings for equality
  • strncmp() to compare the first n characters of two strings
  • strlen() to calculate the length of a string

Pointers

我们将内存中字节的编号称为地址(Address)或指针(Pointer)。地址从 0 开始依次增加,

对于 32 位环境,程序能够使用的内存为 4GB,最小的地址为 0,最大的地址为 0XFFFFFFFF。

指针是包含变量的内存块的地址。

当你像这样声明一个整数时:

 

int age = 37;
We can use the & operator to get the value of the address in memory of a variable:


printf("%p", &age); /* 0x7ffeef7dcb9c */

I used the %p format specified in   printf()   to print the address value.

We can assign the address to a variable:



int *address = &age;

在声明中使用int *address,并不是声明一个整型变量,而是一个指向整型的指针
我们可以使用  指针操作符  *  来获取地址所指向的变量的值:
We can use the pointer operator * to get the value of the variable an address is pointing to:


int age = 37;
int *address = &age;
printf("%u", *address); /* 37 */


这次我们再次使用指针操作符,但由于这次不是声明,它的意思是“该指针所指向的变量的值”。

在本例中,我们声明了一个age变量,并使用指针初始化该值:

#include <stdio.h>
int main(void) {
int age;
int *address = &age;
*address = 37;
printf("%u\n", *address);
printf("%u", &age);
}

37
1299571060

在使用C时,您会发现很多东西都是基于这个简单的概念构建的,因此,通过自己运行上面的示例,请确保熟悉它。

指针是一个很好的机会,因为它们迫使我们思考内存地址和数据是如何组织的。

 

数组就是一个例子。当你声明一个数组时:



int prices[3] = { 5, 4, 3 };

 prices变量实际上是指向数组第一项的指针。在这种情况下,你可以使用printf()函数获取第一项的值:
printf("%u", *prices); /* 5 */

很酷的是,我们可以通过在 prices 指针上加1来获得第二项:



printf("%u", *(prices + 1)); /* 4 */

标签:复习,int,age,printf,数组,address,prices,语言
来源: https://www.cnblogs.com/realyuan2022/p/16451312.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有