使用教材
《“笨办法” 学C语言(Learn C The Hard Way)》
https://www.jianshu.com/p/b0631208a794
ex7.c
#include <stdio.h>
int main(int argc ,char *argv[])
{
int distance = 100;
float power = 2.345f;
double super_power = 56789.4532;
char initial = 'A';
char first_name[] = "Zed";
char last_name[] = "Shaw";
printf("You are %d miles away.\n",distance);
printf("You have %f levles of power.\n",power);
printf("You have %f awesome super powers\n",super_power);
printf("I have an initial %c.\n",initial);
printf("I have a first name %s.\n",first_name);
printf("I have a last name %s.\n",last_name);
printf("My whole name is %s %c. %s\n",
first_name,initial,last_name);
int bugs = 100;
double bug_rate = 1.2;
printf("You have %d bugs at the imaginary rate of %f.\n",
bugs,bug_rate);
long universe_of_defects = 1L*1024L*1024L*1024L;
printf("The entire universe has %ld bugs.\n",universe_of_defects);
double expected_bugs = bugs * bug_rate;
printf("You are expected to have %f bugs.\n",expected_bugs);
double part_of_universe = expected_bugs / universe_of_defects;
printf("That is only a %e portion of the universe\n",
part_of_universe);
// this makes no sense ,just a demo of something werid
char nul_byte = '\0';
int care_percentage = bugs * nul_byte;
printf("Which means you should care %d%%. \n", care_percentage);
printf("Use %% s %s for nul_byte\n", nul_byte);
printf("Use %%c %c for nul_byte\n", nul_byte);
printf("bug %p .\n",buggggggg);
return 0;
}
Makefile
CC=clang
CFLAGS=-Wall -g
clean:
rm -f ex7
-
上面的内容复制到本地的时候,如果显示
Makefile:5: *** missing separator. Stop.
,基本上是因为rm -f ex7
这一行前面留了空格键,需要改成 TAB键 (这是严格要求的); -
CC
字段指定使用clang
编译器; -
clang
编译器安装,参见
$
anno@anno-m:~/Desktop$ ls
ex7.c ex7.c~ Makefile
anno@anno-m:~/Desktop$ make ex7
clang -Wall -g ex7.c -o ex7
ex7.c:42:39: warning: format specifies type 'char *' but the argument has type
'char' [-Wformat]
printf("Use %% s %s for nul_byte\n", nul_byte);
~~ ^~~~~~~~
%c
1 warning generated.
anno@anno-m:~/Desktop$ ./ex7
You are 100 miles away.
You have 2.345000 levles of power.
You have 56789.453200 awesome super powers
I have an initial A.
I have a first name Zed.
I have a last name Shaw.
My whole name is Zed A. Shaw
You have 100 bugs at the imaginary rate of 1.200000.
The entire universe has 1073741824 bugs.
You are expected to have 120.000000 bugs.
That is only a 1.117587e-07 portion of the universe
Which means you should care 0%.
Use % s (null) for nul_byte
Use %c for nul_byte
语法
-
%%
,输出符号%
; -
%e
,输出科学计数法形式的浮点数; -
%ld
,long
类型; -
%c
,char initial = 'A';
-
%s
,char last_name[] = "Shaw";
-
char nul_byte = '\0';
,使用%c
正常输出一个空字符;
编译器
- 警告:使用%s输出一个char类型
-
报错:使用了没有定义过的变量
编译器 警告与报错
GDB
anno@anno-m:~/Desktop$ gdb ex7
网友评论