安装
下载离线安装包iso文件,加载之后选择安卓,如果要写c/c++记得勾选widows sdk .
VS控制台程序运行一闪而过的完美解决办法
在你的项目条目(project)上右击鼠标,在弹出菜单上选择最后一项“property/属性”,在左边的一栏里找到“配置属性->链接器->系统”,点击“系统”项后,在右边的栏的“子系统(subSystem)”将刻项的值配置为"Console(/SUBSYSTEM:CONSOLE)"。
调试时不再用F5键,改成Ctrl+F5键。
hello world
#include <stdio.h>
int main()
{
printf("help");
return 0;
}
数据类型
位: 最小存储单位bit ,可容纳 0 和 1 。
字节: byte常用存储单位。一般来说,1byte=8bit,可表示0~255整数或字符。
字: word自然存储单位。现使用64位系统,即1字为64位。
#include <stdio.h>
#include <inttypes.h>
int main()
{
// 整数类型
int i = 1024;
printf("I am %d years old
" , i);
printf("sizeof int is %d
", sizeof(i));
printf("i dec = %d; octal = %o ; hex = %x
", i, i, i);
printf("i dec = %d; octal = %#o ; hex = %#x
", i, i, i);
printf("sizeof short int is %d
", sizeof(short int));
short si = 1024;
printf("print short use %%hd eg %hd
" , si);
printf("sizeof long int is %d
", sizeof(long int));
printf("sizeof long long int is %d
", sizeof(long long int));
long long lli = 1024;
printf("pring long long use %%lld eg %lld
", lli);
printf("sizeof unsigned int is %d
", sizeof(unsigned int));
printf("sizeof unsigned long int is %d
", sizeof(unsigned long int));
// char 类型
char c = 'A';
printf("size of char is %u
", sizeof(char));
printf("printf char use %%c eg %c or %%d eg %d
", c, c);
unsigned char uc = 255;
printf("char uc is %c ,int uc is %d
", uc, uc);
// _Bool 类型 0:flase other:true
_Bool flag = 2;
printf("size of _Bool is %d
", sizeof(_Bool));
printf("printf _Bool eg %d
", flag);
// 可移植类型 inttypes.h
int16_t me16 = 4593;
printf("me16 = %hd
", me16);
printf("me16 = %"PRId16"
", me16);
// float / double / long double 类型
float ff = 10e9;
double dd = 10e9;
long double ld = 10e9;
printf("size of float is %d
", sizeof(float));
printf("size of double is %d
", sizeof(double));
printf("size of long double is %d
" , sizeof(long double));
printf("%f can ber written %e
", ff, ff);
printf("%f can ber written %e
", dd, dd);
printf("%f can ber written %e
", ld, ld);
return 0;
}
字符串输入输出
关于使用VS会出现scanf要用scanf_s的处理:Alt+F7,打开工程属性,c/c++ 预处理器定义,添加一行内容_CRT_SECURE_NO_DEPRECATE就OK了
#include <stdio.h>
#define HI "ni hao a ! "
int main()
{
// 字符串输入输出
char name[40];
printf("What's your name ?
");
scanf("%s", name);
printf("%s , %s
", HI, name);
return 0;
}
格式化输出
#include <stdio.h>
#include <string.h>
int main()
{
// 字符串函数
char name[40] = "qianqian.sun";
printf("size of name is %u
", sizeof(name)); // 40
printf("length of name is %d
", strlen(name)); // 12
printf("
使用修饰符 和 转换符打印表格
");
for (int i = 0; i < 25; i++)printf("-"); printf("
");
printf("| %5s | %5s | %5s |
", "姓名", "年龄", "身高");
for (int i = 0; i < 25; i++)printf("-"); printf("
");
printf("| %5s | %5d | %5.2f |
","前前",24,1.85);
for (int i = 0; i < 25; i++)printf("-"); printf("
");
// * 修饰符
printf("可指定宽度和精度:%*d
", 5, 100);
printf("可指定宽度和精度:%*.*f
", 10,2, 10.0);
int width, precision;
scanf("%*d %d %d", &width, &precision); // 跳過第一个参数
printf(" Value = %*.*f
", width, precision, 66.6);
return 0;
}
字符的输入输出
字符与单词统计
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
int ch;
FILE * fp;
char name[50] = "C:/share/PostLoanData/file.txt";
fp = fopen(name, "r");
if (NULL == fp)
{
printf("Fail to open file
");
exit(1);
}
int count, lower_count, upper_count, word_len, word_count,cur_word;
count = lower_count = upper_count = word_len = word_count = cur_word =0;
while ((ch = getc(fp)) != EOF)
{
switch (ch)
{
case '
':
printf("\n");
break;
case ' ':
printf("\t");
break;
default:
putchar(ch);
break;
}
printf(": %d |", ch);
if (++count % 10 == 0)printf("
");
if (islower(ch)) lower_count++;
if (isupper(ch)) upper_count++;
if (isalpha(ch)) cur_word++; // 字母则累计
if (isdigit(ch)) cur_word = 0; //遇到 数字 则清零
if ((isspace(ch) || ispunct(ch)) && cur_word>0)
{
word_count++;
word_len += cur_word;
cur_word = 0; //空白 标点则判断当前是否为单词
}
}
if (cur_word>0)
{
word_count++;
word_len += cur_word;
}
fclose(fp);
printf("
字符总数: %5d
", count);
printf("大写字母数: %d , 小写字母数: %d
", upper_count, lower_count);
printf("大写字母数: %d , 小写字母数: %d
", upper_count, lower_count);
printf("word_len = %d
", word_len);
printf("单词总数: %d , 平均长度: %5.2f
", word_count, (double)word_len/ word_count);
return 0;
}