@[toc]
【树莓派】通过SSH使用树莓派 - 走在冷风中的博客 - CSDN博客
通过FTP软件树莓派(Raspberry Pi 3)可以和电脑上传或者下载文件 - ZoharAndroid的博客 - CSDN博客
Error occurred during initialization of VM java/lang/NoClassDefFoundError: java/lang/Object - 热爱bug的亮蛋儿的博客 - CSDN博客
gcc -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
Apple LLVM version 10.0.0 (clang-1000.11.45.5)
Target: x86_64-apple-darwin18.2.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
/* print1.c -- 说明printf()的一些属性 */
#include
int main(void)
{
int ten = 10;
int two = 2;
printf("Doing it right: ");
printf("%d minus %d is %d\n", ten, 2, ten - two);
printf("Doing it wrong: ");
printf("%d minus %d is %d\n",ten);//缺少两个参数
return 0;
}
也能编译链接成功,有一个警告⚠️。
Panda-MBP:CodeList3-2 panda8z$ gcc -o print1.out print1.c
print1.c:10:23: warning: more '%' conversions than data arguments [-Wformat]
printf("%d minus %d is %d\n",ten);//缺少两个参数
~^
1 warning generated.
Panda-MBP:CodeList3-2 panda8z$ ./print1.out
Doing it right: 10 minus 2 is 8
Doing it wrong: 10 minus 0 is 1071055023
Panda-MBP:CodeList3-2 panda8z$
/* bases.c -- 以十进制,八进制,十六进制形式输出100 */
#include
int main(void)
{
int x = 100;
printf("dec = %d; octal = %o; hex = %x\n", x, x, x);
printf("dec = %d; octal = %o; hex = %#x\n", x, x, x);
return 0;
}
Panda-MBP:CodeList3-3 panda8z$ gcc -o bases.out bases.c
Panda-MBP:CodeList3-3 panda8z$ ./bases.out
dec = 100; octal = 144; hex = 64
dec = 100; octal = 144; hex = 0x64
Panda-MBP:CodeList3-3 panda8z$
/* print2.c -- printf()的更多属性 */
#include
int main(void)
{
unsigned int un = 3000000000; //int为32位
short end = 200; //short为16位系统
long big = 6537;
long long verybig = 12345678908642; //14位数
printf("un = %u and not %d\n", un, un);
printf("end = %hd and %d\n", end, end);
printf("big = %1d and not %hd\n", big, big);
printf("verybig = %1ld and not %1d\n", verybig, verybig);
return 0;
}
Panda-MBP:CodeList3-4 panda8z$ gcc -o print2.out print2.c
print2.c:12:39: warning: format specifies type 'int' but the argument has type 'long' [-Wformat]
printf("big = %1d and not %hd\n", big, big);
~~~ ^~~
%1ld
print2.c:12:44: warning: format specifies type 'short' but the argument has type 'long' [-Wformat]
printf("big = %1d and not %hd\n", big, big);
~~~ ^~~
%ld
print2.c:13:44: warning: format specifies type 'long' but the argument has type 'long long' [-Wformat]
printf("verybig = %1ld and not %1d\n", verybig, verybig);
~~~~ ^~~~~~~
%1lld
print2.c:13:53: warning: format specifies type 'int' but the argument has type 'long long' [-Wformat]
printf("verybig = %1ld and not %1d\n", verybig, verybig);
~~~ ^~~~~~~
%1lld
4 warnings generated.
Panda-MBP:CodeList3-4 panda8z$ ./print2.out
un = 3000000000 and not -1294967296
end = 200 and 200
big = 6537 and not 6537
verybig = 12345678908642 and not 1942899938
Panda-MBP:CodeList3-4 panda8z$
/* charcode.c -- 显示一个字符的编辑值 */
#include
int main(void)
{
char ch;
printf("Please enter a charactr.\n");
scanf("%c", &ch);
printf("The code for %c is %d.\n", ch, ch);
return 0;
}
Panda-MBP:CodeList3-5 panda8z$ gcc -o charcode.out charcode.c
Panda-MBP:CodeList3-5 panda8z$ ./charcode.out
Please enter a charactr.
rri
The code for r is 114.
Panda-MBP:CodeList3-5 panda8z$
/* altnames.c -- 可移植的整数类型名 */
#include
#include //支持可移植类型
int main(void)
{
int16_t me16; // me16是一个16位有符号变量
me16 = 4593;
printf("First, assume int16_t is short: ");
printf("me16 = %hd\n",me16);
printf("Next, let`s not make any assumptions. \n");
printf("instead, use a \"macro\" from inttypes.h: ");
printf("me16 = %" PRId16 "\n",me16);
return 0;
}
Panda-MBP:CodeList3-6 panda8z$ gcc -o altnames.out altnames.c
Panda-MBP:CodeList3-6 panda8z$ ./altnames.out
First, assume int16_t is short: me16 = 4593
Next, let`s not make any assumptions.
instead, use a "macro" from inttypes.h: me16 = 4593
Panda-MBP:CodeList3-6 panda8z$
书上写的源程序真正在我的mac os上编译的时候有问题,在log里可以看出
/* showf_pt.c -- 以两种方式显示浮点值 */
#include
int main(void)
{
float aboat = 32000.0;
double abet = 2.14e9;
long double dip = 5.32e-5;
printf("%f can be written %e\n", aboat, aboat);
printf("%f can be written %e\n", abet, abet);
printf("%f can be written %e\n", dip, dip);
return 0;
}
编译有问题的源程序。提示使用%Lf
和%Le
Panda-MBP:CodeList3-7 panda8z$ gcc -o showf_pt.out showf_pt.c
showf_pt.c:10:38: warning: format specifies type 'double' but the argument has type 'long double' [-Wformat]
printf("%f can be written %e\n", dip, dip);
~~ ^~~
%Lf
showf_pt.c:10:43: warning: format specifies type 'double' but the argument has type 'long double' [-Wformat]
printf("%f can be written %e\n", dip, dip);
~~ ^~~
%Le
2 warnings generated.
Panda-MBP:CodeList3-7 panda8z$ ./showf_pt.out
32000.000000 can be written 3.200000e+04
2140000000.000000 can be written 2.140000e+09
2140000000.000000 can be written 2.140000e+09
Panda-MBP:CodeList3-7 panda8z$
改成下面这样才能正常
/* showf_pt.c -- 以两种方式显示浮点值 */
#include
int main(void)
{
float aboat = 32000.0;
double abet = 2.14e9;
long double dip = 5.32e-5;
printf("%f can be written %e\n", aboat, aboat);
printf("%f can be written %e\n", abet, abet);
printf("%Lf can be written %Le\n", dip, dip);
return 0;
}
Panda-MBP:CodeList3-7 panda8z$ gcc -o showf_pt.out showf_pt.c
Panda-MBP:CodeList3-7 panda8z$ ./showf_pt.out
32000.000000 can be written 3.200000e+04
2140000000.000000 can be written 2.140000e+09
0.000053 can be written 5.320000e-05
Panda-MBP:CodeList3-7 panda8z$
/* typesize.c -- 输出类型的大小 */
#include
int main(void)
{
/* c99为类型大小提供一个%zd说明符 */
printf("Type int has a size of %u bytes.\n",sizeof(int));
printf("Type char has a size of %u bytes.\n", sizeof(char));
printf("Type long has a size of %u bytes.\n", sizeof(long));
printf("Type double has a size of %u bytes.\n", sizeof(double));
return 0;
}
按照书上代码写有问题,但是这里还是没影响到执行结果。
Panda-MBP:CodeList3-8 panda8z$ gcc -o typesize.out typesize.c
typesize.c:6:49: warning: format specifies type 'unsigned int' but the argument has type 'unsigned long' [-Wformat]
printf("Type int has a size of %u bytes.\n",sizeof(int));
~~ ^~~~~~~~~~~
%lu
typesize.c:7:51: warning: format specifies type 'unsigned int' but the argument has type 'unsigned long' [-Wformat]
printf("Type char has a size of %u bytes.\n", sizeof(char));
~~ ^~~~~~~~~~~~
%lu
typesize.c:8:51: warning: format specifies type 'unsigned int' but the argument has type 'unsigned long' [-Wformat]
printf("Type long has a size of %u bytes.\n", sizeof(long));
~~ ^~~~~~~~~~~~
%lu
typesize.c:9:53: warning: format specifies type 'unsigned int' but the argument has type 'unsigned long' [-Wformat]
printf("Type double has a size of %u bytes.\n", sizeof(double));
~~ ^~~~~~~~~~~~~~
%lu
4 warnings generated.
Panda-MBP:CodeList3-8 panda8z$ ./typesize.out
Type int has a size of 4 bytes.
Type char has a size of 1 bytes.
Type long has a size of 8 bytes.
Type double has a size of 8 bytes.
能看得出 在现在的这台MacOS上 long和double都是8个字节。也就是都是64个二进制位。
/* badcount.c -- 不正确的参数个数 */
#include
int main(void)
{
int f = 4;
int g = 5;
float h = 5.0f;
printf("%d\n", f, g); //参数太多
printf("%d %d\n", f); //参数太少
printf("%d %f\n", h, f); //参数类型不匹配
return 0;
}
Panda-MBP:CodeList3-9 panda8z$ gcc -o badcount.out badcount.c
badcount.c:9:23: warning: data argument not used by format string [-Wformat-extra-args]
printf("%d\n", f, g); //参数太多
~~~~~~ ^
badcount.c:10:17: warning: more '%' conversions than data arguments [-Wformat]
printf("%d %d\n", f); //参数太少
~^
badcount.c:11:23: warning: format specifies type 'int' but the argument has type 'float' [-Wformat]
printf("%d %f\n", h, f); //参数类型不匹配
~~ ^
%f
badcount.c:11:26: warning: format specifies type 'double' but the argument has type 'int' [-Wformat]
printf("%d %f\n", h, f); //参数类型不匹配
~~ ^
%d
4 warnings generated.
Panda-MBP:CodeList3-9 panda8z$ ./badcount.out
4
4 0
4 5.000000
/* escape.c -- 使用转义字符 */
#include
int main(void)
{
float salary;
printf("\aEnter your desired monthly salary: ");
printf(" $_____\b\b\b\b\b\b\b");
scanf("%f", salary);
printf("\n\t$.2f a month is $.2f a year.", salary, salary * 12.0);
printf("\rGee!\n");
return 0;
}
Panda-MBP:CodeList3-10 panda8z$ gcc -o escape.out escape.c
escape.c:8:17: warning: format specifies type 'float *' but the argument has type 'double' [-Wformat]
scanf("%f", salary);
~~ ^~~~~~
escape.c:9:48: warning: data argument not used by format string [-Wformat-extra-args]
printf("\n\t$.2f a month is $.2f a year.", salary, salary * 12.0);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
2 warnings generated.
Panda-MBP:CodeList3-10 panda8z$ ./escape.out
Enter your desired monthly salary:1200___
Segmentation fault: 11
Panda-MBP:CodeList3-10 panda8z$
正确的程序如下:
/* escape.c -- 使用转义字符 */
#include
int main(void)
{
float salary;
printf("\aEnter your desired monthly salary: ");
printf(" $_______\b\b\b\b\b\b\b");
scanf("%f", &salary);
printf("\n\t$%.2f a month is $%.2f a year.", salary, salary * 12.0);
printf("\rGee!\n");
return 0;
}
正确的输出如下:
Panda-MBP:CodeList3-10 panda8z$ gcc -o escape.out escape.c
Panda-MBP:CodeList3-10 panda8z$ ./escape.out
Enter your desired monthly salary: $1234567
Gee! $1234567.00 a month is $14814804.00 a year.
Panda-MBP:CodeList3-10 panda8z$