初识C语言的简单程序和相关知识

本文详细介绍了C语言中的局部变量、全局变量、数据类型的概念,包括它们的作用域和生命周期。此外,还讲解了基本的运算符,如取模、位操作、移位等,并举例展示了数组、字符串、枚举常量和常量类型的使用。通过实例代码,阐述了C语言中变量的声明、初始化和访问,以及函数调用和指针的运用。
摘要由CSDN通过智能技术生成

 局部变量 全局变量 数据类型

#include<stdio.h>

int main()

 

{

{

int a = 10;

printf("a = %d\n",a)//ok 局部变量的生命周期

}

printf("a = %d\n",a)//error  局部变量的生命周期

//extern int g_val;//声明外部符号的变量  未声明的符号

//printf("g_val = %d\n", g_val);

return 0;

}

/*int num = 20;*///全局变量 定义在代码块({})之外的变量

//double f = 5.0;

//printf("%f\n", f);

//double f = 3.14;

//printf("%lf\n", f);

//打印整型十进制数据

//%d 打印字符格式的数据

//short int 短整型

//int 整型

//long 长整型

//%d 打印整型 %c打印字符  %f打印浮点数 小数  %p以地址的形式打印 %x 打印16进制数字

//年龄

//short age = 20;//向内存申请两个字节 16个bit 存放20

//*float weight = 95.6f;*///向内存申请四个字节 存放小数

//int num = 10;//局部变量 定义在代码块({})内部的变量

//printf("%d\n", num);

//int num1 = 0;

//int num2 = 0;

//int sum = 0;//C语言规定变量 要定义到当前代码块的

//scanf("%d%d", &num1, &num2);//输入数据- 输入函数scanf

//sum = num1 + num2;

//printf("sum = %d\n",sum);

//局部变量的作用域

/* int num = 0;

{

printf("num = %d\n", num);

}*/

#include<stdio.h>

int main()

{

int num1 = 0;

int num2 = 0;

int sum = 0;

scanf("%d%d", &num1, &num2);

sum = num1 + num2;

printf("sum = %d\n", sum);

return 0;

}

C语言编译器(C语言编程软件)完全攻略 (biancheng.net)

#include<stdio.h>

int main()

{

int num1 = 0;

int num2 = 0;

int sum = 0;

scanf("%d%d", &num1, &num2);//scanf是c语言提供的 scanf_s不是c语言提供给我们的 是vs编译器提供的

sum = num1 + num2;

printf("sum = %d\n", sum);

return 0;

}

#include<stdio.h>

#define MAX 10 //定义标识符常量

int main()

{

int arr[MAX] = { 0 };

printf("%d", MAX);

//define 定义的标识符常量

//int arr[10] = { 0 };

//printf("%d\n", &arr);

//printf("%d\n", &arr + 1);

//printf("%p\n", arr);

//printf("%p\n", arr + 1);//const int n = 10;//n是变量 但是又有常属性 所以是常变量

//int arr[10] = { 0 };

n = 20;

const int num = 4;//const常属性

//const修饰常变量

printf("%d\n", num);

// num = 8;

// printf("%d\n", num);

3;//字面常量

100;

3.14;

return 0;

}

#include<stdio.h>

//枚举常量

//枚举一一列举

//性别 男 女

//星期;1 2 3 4 5 6 7

//枚举关键字 enum

//enum Sex

//{

// MALE,

// FEMALE,

// SECRET,

// printf("%d\n", MALE);//0

// printf("%d\n", FEMALE);//1

// printf("%d\n", SECRET);//2

enum Color

{

RED,

YELLOW,

BLUE,

};

int main()

{

enum Color color = BLUE;

return 0;

}

常量类型

#include<stdio.h>

#define MAX 10 //定义标识符常量

int main()

{

int arr[MAX] = { 0 };

printf("%d", MAX);

return 0;const int num = 4;//const常属性

//const修饰常变量

}

//枚举常量

enum Color

{

RED,

YELLOW,

BLUE,

};

int main()

{

enum Color color = BLUE;

return 0;

}

#include<stdio.h>

enum Color

{

RED,

YELLOW,

BLUE,

};

int main()

{

enum Color color = BLUE;

color = YELLOW;

//BLUE = 6; 枚举常量不能改 err

return 0;

}

字符串

双引号引起来的一串字符

#include<stdio.h>

int main()

{

char arr1[] = "adc";//数组 "abc"--'a''b''c'\0 字符串的结束标志‘\0’

char arr2[] = { 'a','b','c',0 };//数据在计算机存储的是2进制

printf("%s\n", arr1);//#av$  A=65  a=97

printf("%s\n", arr2);//ASCLL 编码  ASCLL码值

return 0;

}

#include<stdio.h>

int main()

{

char arr1[] = "abc";

char arr2[] = { 'a','b','c' };

printf("%d\n", strlen(arr1));//strlen-- string length 计算字符串长度的

printf("%d\n", strlen(arr2));

return 0;

}

Strlen 计算字符串长度 遇到\0 结束

\0为转义字符  

转义字符 转变原来的意思

c语言转义字符

??) -->三字母词

斜杠可以转义斜杠

#include<stdio.h>

int main()

{

printf("c:\\test\\32\\test.c");//\t水平制表符

//*printf("a\nb\nc\n");

return 0;

}

#include<stdio.h>

#include<string.h>

int main()

{//32  为十进制26  作为ASCLL码值代表的字符

printf("%d\n", strlen("c:\test\32\test.c"));//\32是两个八进制数字 32作为8进制代表的哪个十进制数字 作为ASCLL码值对应的字符

return 0;

}

/*printf("%s\n","\"");*/

//printf("c:\\test\\32\\test.c");//\t水平制表符

*printf("a\nb\nc\n");

ASCLL表

#include<stdio.h>

#include<string.h>

int main()

{//32  为十进制26  作为ASCLL码值代表的字符

printf("%c\n",'\x61');//\32是两个八进制数字 32作为8进制代表的哪个十进制数字 作为ASCLL码值对应的字符

return 0;

}

/*printf("%s\n","\"");*/

//printf("c:\\test\\32\\test.c");//\t水平制表符

*printf("a\nb\nc\n");

选择语句

#include<stdio.h>

#include<string.h>

int main()

{

int input = 0;

printf("da dd asd \n");

printf("dsad adasd?(1/0):\n");

scanf("%d", &input);

if  (input == 1)

printf("hao\n");

else

printf("mai\n");

return 0;

}

#include<stdio.h>

int main()

{

int a = 0;

printf("加入\n");

while(a<2000)

{

printf("一行代码\n");

a++;

printf("%d\n", a);

}

printf("hao\n");

return 0;

}

#include<stdio.h>

int Add(int x, int y)

{

int z = x + y;

return z;

}

int main()

{

int num1 = 2;

int num2 = 1;

int sum = 0;

int a = 3;

int b = 4;

sum = num1 + num2;

sum = Add(num1, num2);

sum = Add(a, b);

printf("sum = %d\n", sum);

return 0;

}

#include <stdio.h>

#include <math.h>

void main()

{

double x, s;

printf("input number:\n");

scanf("%if", &x);

s = sin(x);

printf("sin of %if is %if\n", x, s);

return 0;

}

数组

下标

下标默认是从0开始的

#include <stdio.h>

#include <math.h>

int main()

{

/*int a = 1;

int b = 2;

int c = 3;

int d = 4;*/

int arr[10] = {1,2,3,4,5,6,7,8,9,10};//0-9下标

int i = 0;

while (i < 10)

{

printf("%d", arr[i]);

i++;

}

//定义一个存放10个整数数字的数组

//printf("%d\n", arr[4]);//下标的方式访问元素

//char ch[20];

//float arr2[5];

return 0;

}

操作符

#include <stdio.h>

#include <math.h>

int main()

{

int a = 5 % 2;//取模

printf("%d\n", a);

return 0;

}

#include <stdio.h>

#include <math.h>

int main()

{

int a = 1;//整型1占4个字节-32bit位

a << 1;//00000000000000000000000000000001 左边丢弃右边补0

//<<左移   >>右移

//移位操作符

//int a = 5 % 2;//取模

//printf("%d\n", a);

return 0;

}

#include <stdio.h>

#include <math.h>

int main()

{

int a = 1;//整型1占4个字节-32bit位

int b = a << 1;//00000000000000000000000000000001

printf("%d\n", b);

printf("%d\n", a);

//<<左移   >>右移

//移位操作符

//int a = 5 % 2;//取模

//printf("%d\n", a);

return 0;

}

#include <stdio.h>

#include <math.h>

int main()

{

//位操作(2进制)  &按位与 |按位或  ^按位异或

int a = 3;//0011 异或的计算规律 对应的二进制为相同则为0 相异则为1

int b = 5;//0101

int c = a|b;

printf("%d\n",c);

return 0;

}

#include <stdio.h>

#include <math.h>

int main()

{

//位操作(2进制)  &按位与 |按位或  ^按位异或

int a = 3;//0011

int b = 5;//0101

int c = a^b;

printf("%d\n",c);

return 0;

}

#include <stdio.h>

#include <math.h>

int main()

{

int a = 10;

printf("%d\n", a);

printf("%d\n",!a);

//0表示假  非0 表示真  !变成假或真

//单目操作符  双目操作符  三目操作符

/*int a = 10;

int b = 20;

a + b;*/

//双目操作符

//int a = 10;

//a = 20;//=等于赋值  == 判断相等

//a = a + 10;

//a += 10;

//a = a-20;

//a -= 20;

//a = a & 2;

//a &= 2;//复合赋值符

//位操作(2进制)  &按位与 |按位或  ^按位异或

//t a = 3;//0011

//t b = 5;//0101

//t c = a ^ b;

//intf("%d\n", c);

return 0;

}

#include <stdio.h>

#include <math.h>

int main()

{

int sz = 0;

int arr[20] = { 0,1 };

printf("%d\n", sizeof(arr));

//计算数组的元素个数

// 数组总大小/每个元素的大小=个数

sz = sizeof(arr) / sizeof(arr[0]);

printf("%d\n", sz);

//int a = 10;//sizeof 计算的是变量//类型所占空间的大小单位是字节

//printf("%d\n", sizeof(a));//4

//printf("%d\n", sizeof(int));//

//printf("%d\n", sizeof a);

return 0;

}

#include <stdio.h>

#include <math.h>

int MAX(int x, int y)

{

if (x > y)

return x;

else

return y;

}

int main()

{

int num1 = 10;

int num2 = 20;

int max = 0;

max = MAX(num1, num2);

printf("max = %d\n", max);

return 0;

}

#include <stdio.h>

#include <string.h>

int main()

{

int a = 0;//四个字节,32bit位

int b = ~a;//按位取反  0010》》》1101

printf("%d\n", b);//原码,反码,补码

return 0;

}

#include <stdio.h>

#include <string.h>

int main()

{

int a = 10;

//int b = a++;//后置++ 先使用,再++ 10

int b = ++a;//前置++ 先++  再使用  11

printf("a = %d\n  b= %d\n", a, b);

return 0;

}

单目操作符

#include <stdio.h>

#include <string.h>

int main()

{

int a = 10;

//int b = a++;//后置++ 先使用,再++ 10

//int b = ++a;//前置++ 先++  再使用  11

//int b = a--;// a=9   b=10

int b = --a;//a=9 b=9

printf("a=%d\nb=%d\n", a, b);

return 0;

}

#include <stdio.h>

#include <string.h>

int main()

{

int a =(int)3.140;//强制类型转换 double转换int

return 0;

}

#include <stdio.h>

#include <string.h>

int main()

{

//0表示假    一切非0表示真   

//&&  逻辑与  //|| 逻辑或

int a = 3;

int b = 3;

int c = a || b;

printf("c = %d", c);

}

#include <stdio.h>

#include <string.h>

int main()

{

int a = 10;//exp1? exp2:exp3;  三目操作符  条件操作符

int b = 20;

int max = 0;

max = (a > b ? a : b);

if (a > b)

max = a;

else

max = b;

return 0;

}

#include <stdio.h>

#include <string.h>

int Add(int x, int y)

{

int z = 0;

z = x + y;

return z;

}

int main()

{

int arr[10] = { 0 };

arr[4];//[]- 下标引用操作符

int a = 10;

int b = 20;

int sum = Add(a, b);//()函数调用操作符、、& * . ->

return 0;

}

auto局部变量 自动变量

Register 寄存器  

寄存器  高速缓存 内存 硬盘 cpu-中央处理器

#include <stdio.h>

int main()

{

    auto int a = 10;//局部变量  自动变量  自动创建 自动销毁

    return 0;

}

#include <stdio.h>

int main()

{

   /// register int a = 10;//建议把a变成寄存器变量

    int a = 10;

    a = -2;

    //int 定义的变量是有符号的

    //signed int

    //unsigned  无符号的

    //struct 结构体 关键字

    //union 联合体 共用体

    //volatile c语言断位的一些关键字

    return 0;

}

#include <stdio.h>

int main()

{

   //typedef - 类型定义 类型重定义

    typedef unsigned int u_int;//别名  unsigned int = u_int

    unsigned int num = 20;

    u_int num2 = 20;

    return 0;

}

#include <stdio.h>

void test()

{

int a = 1;

a++;

printf("a=%d\n", a);

}

int main()

{

    int i = 0;

while (i<5)

{

test();

i++;

}

    return 0;

}

#include <stdio.h>

//void test()

//{

// static int a = 1;//a是一个静态的局部变量 static 修饰局部变量 局部变量的生命周期边长

// a++;//static修饰全局变量  改变了变量的作用域 让静态的全局变量只能在自己所在的源文件内部使用

// 出了源文件 就没办法再使用了  static 修饰函数

// printf("a=%d\n", a);

//}

//int main()

//{

//    int i = 0;

// while (i<5)

// {

// test();

// i++;

}

//    return 0;

//}

int main()

{

extern int g_val;//extern --声明外部符号的

printf("g_val = %d\n", g_val);

return 0;

}

#include <stdio.h>

//#define MAX 100//#define可以定义宏-带参数  定义标识符常量

#define MAX(X,Y)( X>Y?X:Y)

int main()

{

int a = 10;

int b = 20;

int max = MAX(a, b);

printf("max=%d\n", max);//宏的方式

max = MAX(a, b);

printf("max=%d\n", max);

return 0;

}

#include <stdio.h>

int main()

{

    int a = 10;

    int* p = &a;//有一种变量是用来存放地址的-指针变量 

    printf("%p\n", &a);

    printf("%p\n", p);

    *p = 20;//解引用操作符/间接访问操作符

    printf("%d\n", a);

    return 0;

}

#include <stdio.h>

int main()

{

    char ch = 'w';

    char* pc = &ch;

    *pc = 'a';

    printf("%c\n", ch);

    printf("%d\n", sizeof(pc));

    return 0;

}

#include <stdio.h>

int main()

{

    double d = 3.14;

    double* pd = &d;

    printf("%lf\n", *pd);

    *pd = 5.5;

    printf("%lf\n", *pd);

    return 0;

}

#include <stdio.h>

int main()

{

    double d = 3.14;

    double* pd = &d;

    printf("%d\n", sizeof(pd));

    printf("%lf\n", *pd);

    *pd = 5.5;

    printf("%lf\n", *pd);

    return 0;

}

结构体 char int double ----

#include <stdio.h>

int main()

{

struct Book

{

char name[20];//C语言程序设计

short price;

};

struct Book b1 = { "C语言程序设计",55 };

printf("书名:%s\n",b1.name);

printf("价格:%d\n",b1.price);

    return 0;

}

#include <stdio.h>

struct Book

{

char name[20];//C语言程序设计

short price;

};

int main()

{

struct Book b1 = { "C语言程序设计",55 };

printf("书名:%s\n",b1.name);

printf("价格:%d\n",b1.price);

b1.price = 100;

printf("价格:%d\n", b1.price);

struct Book* pd = &b1;

printf("%s\n", (*pd).name);

printf("%s\n", pd->name);//结构体指针指向成员

printf("%d\n", pd->price);

    return 0;

}

#include <stdio.h>

#include <string.h>

struct Book

{

char name[20];//C语言程序设计

short price;

};

int main()

{

struct Book b1 = { "C语言程序设计",55 };

strcpy(b1.name ,"c++");//strcpy - string copy 字符串拷贝

printf( "%s\n",b1.name);

    return 0;

}

C语言是一门 结构化的程序设计语言

  1. 顺序结构
  2. 选择结构
  3. 循环结构

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值