ANSI C (1) —— 基础

2024-03-27 22:38
文章标签 基础 ansi

本文主要是介绍ANSI C (1) —— 基础,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

常量字符指针和字符指针的传参问题

1)
没有错误:

#include <stdio.h>
void out(const char *p){printf("%s\n",p);
}
int main(){char *str="hello";out(str);return 0;
}

2)
出现错误:
t.c:2:6: note: expected ‘char ’ but argument is of type ‘const char
void out(char *p){

#include <stdio.h>
void out(char *p){printf("%s\n",p);
}
int main(){const char *str="hello";out(str);return 0;
}

char * 变量可以传向const char *的参数,反之不行。

EOF

windows下我们按下ctrl+Z代表文件结束
linux下我们使用ctrl+D表示文件结束(在linux下使用ctrl+Z,如:
$ cat > t2
abc^Z
$ cat t2:
(nothing!)
)

$ cat > t2
abc^D

t.c:

#include <stdio.h>int main(){FILE *fin=fopen("t2","r");FILE *fout=fopen("t4","w");char ch;while(fscanf(fin,"%c",&ch)!=EOF){fprintf(fout,"(%c: %d) ",ch,ch);}return 0;
}

运行后查看输出文件t4:
(a: 97) (b: 98) (c: 99)

预处理器

C源码转化为目标代码的过程:

C源文件 ——> 预处理器 ——> 编译器

关键词:#预处理指令,宏

例子:

mydoc.c:

#include <stdio.h>
void print(){printf("hello\n");
}
int a=10;

t.c:

#include <stdio.h>/* "dir"表示用户目录,<dir>表示编译器的库目录  */#include "mydoc.c"
int main(){print();printf("%d\n",a);return 0;
}

out:

hello
10

查看库文件的源码:

cat /usr/include/stdio.h > read
vim read
: /EOF
:n

定位后:

133 #ifndef EOF
134 # define EOF (-1)
135 #endif

我们可以知道,EOF的整数值是-1

宏的便易性:
有时候,宏的替换功能可以高效实现多数据类型的操作,比普通函数更加简单。
例如下面的求最小值:

#include <stdio.h>
#define min(t1,t2)  (t1)-(t2) < 1e-7 ? (t1):(t2)
int main(){int a=34;float b=34.5;long long c=25;double d=30.9;printf("int and float: %g\n",min(a,b));printf("float and long long: %g\n",min(b,c));printf("int and double:%g\n",min(a,d));return 0;
}
/×
int and float: 34
float and long long: 25
int and double:30.9
×/

利用宏进行条件编译:

#include <stdio.h>
#define a 10
int main(){#if aprintf("a > 0\n");#endifreturn 0;
}

#error指示编译器产生错误,并显示相应的消息。

下列的程序,运行的结果linux是inf,windows是1.#INF

#include <stdio.h>
double div(int a,int b){return 1.0*a/b;
}
int main(){printf("%g\n",div(2,0));return 0;
}

我们利用预处理来改进一下:

#include <stdio.h>
double div(int a,int b){#if (b==0)#error "b can't be 0!"#endifreturn 1.0*a/b;
}
int main(){printf("%g\n",div(2,0));return 0;
}

out:

t.c: In function ‘div’:
t.c:4:7: error: #error "b can't be 0!"#error "b can't be 0!"^

#program 代表有特殊实现的代码,如Acmer常用的扩栈代码。

强大的跳跃者goto

合理的使用goto label可以使得程序跳出多重循环。

#include <stdio.h>int main(){int ans=0;for(int i=1;i<100;i++){for(int j=1;j<100;j++){for(int k=1;k<100;k++){if(i==4&&j==5&&k==6) {ans=i+j+k;goto out;}}}}out:printf("%d\n",ans);return 0;
}

指针和数组不是一样的

    FILE *fin, *fout;fin=fopen("tt","r");fout=fopen("t2","w");char *s;while(fscanf(fin,"%s",s)!=EOF){fprintf(fout,"%s ",s);}

上面这段程序并没有将字符串赋予s,下面的数组能够正常工作。字符指针不能直接被scanf输入字符串,但是可以等于赋值(char *str=”hello”;)

#include <stdio.h>int main(){FILE *fin, *fout;fin=fopen("tt","r");fout=fopen("t2","w");char s[20];int c=0;while(fscanf(fin,"%s",s)!=EOF){fprintf(fout,"%s ",s);}fclose(fin);fclose(fout);return 0;
}

字节计数器sizeof

对于sizeof(int)不能%d格式输出。
t.c:4:12: warning: format ‘%d’ expects argument of type ‘unsigned int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
正确的是输出%lu

#include <stdio.h>int main(){printf("char: %lu\n",sizeof(char));printf("int: %lu\n",sizeof(int));printf("long: %lu\n",sizeof(long));printf("long long: %lu\n",sizeof(long long));printf("float: %lu\n",sizeof(float));printf("double: %lu\n",sizeof(double));printf("8: %lu\n",sizeof(8));printf("8LL: %lu\n",sizeof(8LL));int a[10];printf("int[]: %lu\n",sizeof(a));printf("str: %lu\n",sizeof("abc"));return 0;
}/*
char: 1
int: 4
long: 8
long long: 8
float: 4
double: 8
8: 4
8LL: 8
int[]: 40
str: 4
*/

从以上结果来看,sizeof()计算的是按字节为单位的存储量。

计算参数的顺序

我们看一个有趣的例子,来说明有时参数的赋值和计算的顺序是不确定的。
源码文件的前半部分是这样的:

#include <stdio.h>
int max(int a,int b){return a>b?a:b;
}
void fun(int a,int b){
}

1)fun(++a,++a)

int main(){int a=1;fun(++a,++a);printf("%d\n",a);return 0;
}

gdb调试:

edemon@linux:~$ gdb exe
(gdb) break 5
Breakpoint 1 at 0x400534: file t.c, line 2.
(gdb) run
Breakpoint 1, fun (a=3, b=3) at t.c:6
6   }
(gdb) c
Continuing.
3
[Inferior 1 (process 3597) exited normally]

表格中的1,2,3,4代表计算或者传参的顺序,前者和后者是针对fun函数的第一个参数和第二个参数而言。

对象计算传参
前者1&23
后者1&23

2)fun(a++,++a)
当我小小的改变它后:

int main(){int a=1;fun(a++,++a);return 0;
}

gdb调试:

Breakpoint 1, fun (a=2, b=3) at t.c:6
6   }
(gdb) c
Continuing.
3
[Inferior 1 (process 3613) exited normally]
对象计算传参
前者12
后者34

3) fun(++a,a++):

int main(){int a=1;fun(++a,a++);return 0;
}

gdb调试:

Breakpoint 1, fun (a=3, b=1) at t.c:6
6   }
(gdb) c
Continuing.
3
[Inferior 1 (process 3632) exited normally]
对象计算传参
前者34
后者21

4) fun(a++,a++):

int main(){int a=1;fun(a++,a++);return 0;
}

gdb调试:

Breakpoint 1, fun (a=2, b=1) at t.c:6
6   }
(gdb) c
Continuing.
3
[Inferior 1 (process 3705) exited normally]
对象计算传参
前者43
后者21

这篇关于ANSI C (1) —— 基础的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/853626

相关文章

从基础到进阶详解Pandas时间数据处理指南

《从基础到进阶详解Pandas时间数据处理指南》Pandas构建了完整的时间数据处理生态,核心由四个基础类构成,Timestamp,DatetimeIndex,Period和Timedelta,下面我... 目录1. 时间数据类型与基础操作1.1 核心时间对象体系1.2 时间数据生成技巧2. 时间索引与数据

安装centos8设置基础软件仓库时出错的解决方案

《安装centos8设置基础软件仓库时出错的解决方案》:本文主要介绍安装centos8设置基础软件仓库时出错的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录安装Centos8设置基础软件仓库时出错版本 8版本 8.2.200android4版本 javas

Linux基础命令@grep、wc、管道符的使用详解

《Linux基础命令@grep、wc、管道符的使用详解》:本文主要介绍Linux基础命令@grep、wc、管道符的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录grep概念语法作用演示一演示二演示三,带选项 -nwc概念语法作用wc,不带选项-c,统计字节数-

python操作redis基础

《python操作redis基础》Redis(RemoteDictionaryServer)是一个开源的、基于内存的键值对(Key-Value)存储系统,它通常用作数据库、缓存和消息代理,这篇文章... 目录1. Redis 简介2. 前提条件3. 安装 python Redis 客户端库4. 连接到 Re

SpringBoot基础框架详解

《SpringBoot基础框架详解》SpringBoot开发目的是为了简化Spring应用的创建、运行、调试和部署等,使用SpringBoot可以不用或者只需要很少的Spring配置就可以让企业项目快... 目录SpringBoot基础 – 框架介绍1.SpringBoot介绍1.1 概述1.2 核心功能2

Spring Boot集成SLF4j从基础到高级实践(最新推荐)

《SpringBoot集成SLF4j从基础到高级实践(最新推荐)》SLF4j(SimpleLoggingFacadeforJava)是一个日志门面(Facade),不是具体的日志实现,这篇文章主要介... 目录一、日志框架概述与SLF4j简介1.1 为什么需要日志框架1.2 主流日志框架对比1.3 SLF4

Spring Boot集成Logback终极指南之从基础到高级配置实战指南

《SpringBoot集成Logback终极指南之从基础到高级配置实战指南》Logback是一个可靠、通用且快速的Java日志框架,作为Log4j的继承者,由Log4j创始人设计,:本文主要介绍... 目录一、Logback简介与Spring Boot集成基础1.1 Logback是什么?1.2 Sprin

MySQL复合查询从基础到多表关联与高级技巧全解析

《MySQL复合查询从基础到多表关联与高级技巧全解析》本文主要讲解了在MySQL中的复合查询,下面是关于本文章所需要数据的建表语句,感兴趣的朋友跟随小编一起看看吧... 目录前言:1.基本查询回顾:1.1.查询工资高于500或岗位为MANAGER的雇员,同时还要满足他们的姓名首字母为大写的J1.2.按照部门

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

mysql的基础语句和外键查询及其语句详解(推荐)

《mysql的基础语句和外键查询及其语句详解(推荐)》:本文主要介绍mysql的基础语句和外键查询及其语句详解(推荐),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋... 目录一、mysql 基础语句1. 数据库操作 创建数据库2. 表操作 创建表3. CRUD 操作二、外键