书籍整理内容:
最近在看C primer plus(加深巩固自己的C语言技巧,为以后学习C++打个基础)。
里面知识针对自己以后要查的点整理出来。
使用工具:visual studio 2013
第二、三章内容:概述、变量、基本数据类型等:https://blog.csdn.net/answerMack/article/details/103766020
第四章内容:字符串和格式化输入输出:https://blog.csdn.net/answerMack/article/details/103805900
第五章内容:运算符、表达式和语句:https://blog.csdn.net/answerMack/article/details/103855794
第六章内容:循坏、赋值运算符等:https://blog.csdn.net/answerMack/article/details/103870182
第七章内容:if、if else、?:、switch、goto、continue、逻辑运算符优先级https://blog.csdn.net/answerMack/article/details/103891048
第八章内容:字符输入输出函数、输入验证(混合输入)https://blog.csdn.net/answerMack/article/details/103953376
第九章内容:函数和指针:https://blog.csdn.net/answerMack/article/details/103978471
第十章:数组和指针:https://blog.csdn.net/answerMack/article/details/104114028
第十一章:字符串和字符串函数:https://blog.csdn.net/answerMack/article/details/105222269
目录
本章主要讲解概念、基本都是函数的文字讲解。注意一下就好,易困!!
作用域
作用域描述程序中可访问标识符的区域。
(1)块作用域:
(2)函数作用域
(3)函数原型作用域
(4)文件作用域
(6)翻译单元和文件
链接
c变量的属性:外部链接、内部链接、无链接
存储期
存储类别
自动变量
寄存器变量
块作用域静态变量
外部链接静态变量
定义变量、声明变量
内部链接静态变量
多文件
存储类别说明符
auto
register
static
extern
_Thread_local
typedef
例子:
// parta.c --- various storage classes
// compile with partb.c
#include <stdio.h>
void report_count();
void accumulate(int k);
int count = 0; // file scope, external linkage
int main(void)
{
int value; // automatic variable
register int i; // register variable
printf_s("Enter a positive integer (0 to quit): ");
while (scanf_s("%d", &value) == 1 && value > 0)
{
++count; // use file scope variable
for (i = value; i >= 0; i--)
accumulate(i);
printf_s("Enter a positive integer (0 to quit): ");
}
report_count();
return 0;
}
void report_count()
{
printf_s("Loop executed %d times\n", count);
}
// partb.c -- rest of the program
// compile with parta.c
#include <stdio.h>
extern int count; // reference declaration, external linkage
static int total = 0; // static definition, internal linkage
void accumulate(int k); // prototype
void accumulate(int k) // k has block scope, no linkage
{
static int subtotal = 0; // static, no linkage
if (k <= 0)
{
printf_s("loop cycle: %d\n", count);
printf_s("subtotal: %d; total: %d\n", subtotal, total);
subtotal = 0;
}
else
{
subtotal += k;
total += k;
}
}
存储类别和函数
随机数函数和静态变量
掷骰子
分配内存:malloc()和free()
3种创建数组的方法
/* dyn_arr.c -- dynamically allocated array */
#include <stdio.h>
#include <stdlib.h> /* for malloc(), free() */
int main(void)
{
double * ptd;
int max;
int number;
int i = 0;
puts("What is the maximum number of type double entries?");
if (scanf_s("%d", &max) != 1)
{
puts("Number not correctly entered -- bye.");
exit(EXIT_FAILURE);
}
ptd = (double *)malloc(max * sizeof(double));
if (ptd == NULL)
{
puts("Memory allocation failed. Goodbye.");
exit(EXIT_FAILURE);
}
/* ptd now points to an array of max elements */
puts("Enter the values (q to quit):");
while (i < max && scanf_s("%lf", &ptd[i]) == 1)
++i;
printf_s("Here are your %d entries:\n", number = i);
for (i = 0; i < number; i++)
{
printf_s("%7.2f ", ptd[i]);
if (i % 7 == 6)
putchar('\n');
}
if (i % 7 != 0)
putchar('\n');
puts("Done.");
free(ptd);
return 0;
}
动态分配内存:
free()的重要性
calloc函数
动态内存分配和变长数组
动态内存分配和存储类别
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int static_store = 30;
const char * pcg = "String Literal";
int main()
{
int auto_store = 40;
char auto_string[] = "Auto char Array";
int * pi;
char * pcl;
pi = (int *)malloc(sizeof(int));
*pi = 35;
pcl = (char *)malloc(strlen("Dynamic String") + 1);
strcpy_s(pcl, strlen("Dynamic String")+1,"Dynamic String");
printf_s("static_store: %d at %p\n", static_store, &static_store);
printf_s(" auto_store: %d at %p\n", auto_store, &auto_store);
printf_s(" *pi: %d at %p\n", *pi, pi);
printf_s(" %s at %p\n", pcg, pcg);
printf_s(" %s at %p\n", auto_string, auto_string);
printf_s(" %s at %p\n", pcl, pcl);
printf_s(" %s at %p\n", "Quoted String", "Quoted String");
free(pi);
free(pcl);
return 0;
}
ANSI C类型限定符
const、volatile、restrict、_Atomic+stdatomic.h库文件
const(第4章、第10章)
指针和形参声明中使用const
volatile类型限定符
restrict类型限定符
_Atomic类型限定符
并行程序:
旧关键字的新位置
关键概念
本章小结
2020-04-08
大连