栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C 语言基本语法 (1)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C 语言基本语法 (1)

文章目录
  • 写在前面
  • 1、基本规则
  • 2、数据类型
    • 2.1、基本类型
      • 2.1.1、数值前缀
      • 2.1.2、数值后缀
      • 2.1.3、字符和字符串
    • 2.2、枚举类型
  • 3、变量与函数
    • 3.1、变量
    • 3.2、函数
    • 3.4、指针
      • 3.4.1、变量指针
      • 3.4.2、函数指针
  • 4、程序结构
    • 4.1、if
    • 4.2、switch
    • 4.3、三元表达式
    • 4.4、while
    • 4.5、for
    • 4.6、do...while
    • 4.5、跳出和跳转
  • 5、结构体 struct
    • 5.1、结构体定义
    • 5.2、结构体调用
    • 5.3、结构体指针
  • 6、公用体 union
  • 7、位域
  • 8、typedef 与 #define

写在前面 1、基本规则
规则说明
程序的第一行: #include 告诉 C 编译器在实际编译之前要包含 stdio.h 文件
int main() 是主函数程序从int main() 开始执行
空格、换行、注释程序不会读取注释://、、
每行以 ; 结束分号是语句结束符
区分大小写C 是区分大小写的编程语言
变量、方法命名可以以“_”开始一个标识符以字母 A-Z 或 a-z 或下划线 _ 开始,后跟零个或多个字母、下划线和数字(0-9)

实例

#include 
 
int main()
{
   
   printf("Hello, World! n");
   
   return 0;
}
2、数据类型
类型说明
基本类型char、int、short、long、float、double、long double
枚举类型enum
void类型void
派生类型指针类型、数组类型、结构类型、共用体类型和函数类型

整数类型前面都可以加一个unsigned,表示该类型的值从0开始,char前面还可以加signed,表示值为-128 到 127。
例:unsigned char a = 1; signed char b = 2;

2.1、基本类型

基本类型就是字符型、整型、浮点这三大类

2.1.1、数值前缀

前缀用来表示数字进制

  • 无前缀,默认十进制,10,31。
  • 0 ,以0开头为8进制:045,021。
  • 0b,以0b开头为2进制:0b11101101。
  • 0x,以0x开头为16进制:0x21458adf。
2.1.2、数值后缀

后缀用来表示数值类型(大写与小写同义)

  • 无后缀,整数默认int、浮点默认double
  • 2.3 为双精度double(15 位有效位)
  • 2.5f 为单精度float(6 位有效位)
  • 11L 为long
  • 11.1L为long double
  • 10ul 为无符号long
2.1.3、字符和字符串
  • 字符型,char 类型,用单引号,如char a = ‘m’;char b = 109; char c = ‘n’;
  • 字符串,通过字符指针或数组来间接实现,如char *a=“hello,world”、char b[]=“linux”、char site[7] = {‘R’, ‘U’, ‘N’, ‘O’, ‘O’, ‘B’, ‘’};

char *p=“hello,world” 这句代码的本质:指针 p 指向头"h"、尾"d" 的地址相连的一段内存

字符串操作:

#include 
#include 
 
int main ()
{
   char str1[14] = "runoob";
   char str2[14] = "google";
   char str3[14];
   int  len ;
 
   
   strcpy(str3, str1);
   printf("strcpy( str3, str1) :  %sn", str3 );
 
   
   strcat( str1, str2);
   printf("strcat( str1, str2):   %sn", str1 );
 
   
   len = strlen(str1);
   printf("strlen(str1) :  %dn", len );
 
   return 0;
}
2.2、枚举类型

枚举定义:

enum DAY
{
      MON=1, TUE, WED, THU, FRI, SAT, SUN
};
enum DAY day;
enum DAY
{
      MON=1, TUE, WED, THU, FRI, SAT, SUN
} day;
enum
{
      MON=1, TUE, WED, THU, FRI, SAT, SUN
} day;

枚举的第一个给值默认为0,后面的值依次+1,
下面是枚举遍历实例:

#include 
 
enum DAY
{
      MON=1, TUE, WED, THU, FRI, SAT, SUN
} day;

int main()
{
    // 遍历枚举元素
    for (day = MON; day <= SUN; day++) {
        printf("枚举元素:%d n", day);
    }
}

如果枚举的值不连续,则无法遍历,如下:

enum
{
    ENUM_0,
    ENUM_10 = 10,
    ENUM_11
};
3、变量与函数 3.1、变量

变量定义

// 声明并定义变量, auto有和没有一样
int a;
char a = 'l';
auto int month;

// extern 声明变量
extern double b;
extern float b = 1.1f;

// const 常量,声明常量要在一个语句内完成
const long A = 1L;

// register 存储类用于定义存储在寄存器中,通常是一个字
register int  a;

// static 修饰全局变量和局部变量的应用,它修饰的变量不会被重置
static int count=10;

// extern 是用来在另一个文件中声明一个全局变量或函数
extern int count;
3.2、函数

函数定义

int max(int num1, int num2) 
{
   int result = num1 + num2;
   return result; 
}

函数调用

t1.c

void swap(int *x, int *y)
{
   int temp;
   temp = *x;    
   *x = *y;      
   *y = temp;    
  
   return;
}

t2.c

#include 
 

void swap(int *x, int *y);
 
int main ()
{
   
   int a = 100;
   int b = 200;
 
   printf("交换前,a 的值: %dn", a );
   printf("交换前,b 的值: %dn", b );
 
   
   swap(&a, &b);
 
   printf("交换后,a 的值: %dn", a );
   printf("交换后,b 的值: %dn", b );
 
   return 0;
}
3.4、指针 3.4.1、变量指针

每一个变量都有一个内存位置,每一个内存位置都定义了可使用 & 运算符访问的地址

#include 
 
int main ()
{
    int var_runoob = 10;
    int *p;              // 定义指针变量
    p = &var_runoob;
 
   printf("var_runoob 变量的地址: %pn", p);
   return 0;
}

指针*p前面的类型int,是指针所指向的数据的类型,指针自己的类型都是 “16进制地址值”。
指针等于NULL时,指向的地址是0x0,也就是空指针。

指针是可以运算的,如下:

#include 
 
const int MAX = 3;
 
int main ()
{
   int  var[] = {10, 100, 200};
   int  i, *ptr;
 
   
   ptr = var;
   for ( i = 0; i < MAX; i++)
   {
 
      printf("存储地址:var[%d] = %pn", i, ptr );
      printf("存储值:var[%d] = %dn", i, *ptr );
 
      
      ptr++;
   }
   return 0;
}

执行结果

存储地址:var[0] = e4a298cc
存储值:var[0] = 10
存储地址:var[1] = e4a298d0
存储值:var[1] = 100
存储地址:var[2] = e4a298d4
存储值:var[2] = 200
3.4.2、函数指针

通常我们说的指针变量是指向一个整型、字符型或数组等变量,而函数指针是指向函数。
函数指针可以像一般函数一样,用于调用函数、传递参数。

#include 
 
int max(int x, int y)
{
    return x > y ? x : y;
}
 
int main(void)
{
    
    int (* p)(int, int) = & max; // &可以省略
    int a, b, c, d;
 
    printf("请输入三个数字:");
    scanf("%d %d %d", & a, & b, & c);
 
    
    d = p(p(a, b), c); 
 
    printf("最大的数字是: %dn", d);
 
    return 0;
}

回调函数:函数指针作为某个函数的参数,如下例

#include   
#include 
 
// 回调函数
void populate_array(int *array, size_t arraySize, int (*getNextValue)(void))
{
    for (size_t i=0; i 
4、程序结构 
4.1、if 
if(a == 10)
{
   
}
else if(a == 11)
{
   
}
else 
{
   
}
4.2、switch
   
   char grade = 'B';
 
   switch(grade)
   {
   case 'A' :
      printf("很棒!n" );
      break;
   case 'B' :
   default :
      printf("无效的成绩n" );
   }
4.3、三元表达式
a > 1 ? 0 : 10;
4.4、while
   while( a < 20 )
   {
      a++;
   }
4.5、for
   for( int a = 10; a < 20; a++ )
   {
      printf("a 的值: %dn", a);
   }
4.6、do…while
   do
   {
       printf("a 的值: %dn", a);
       a = a + 1;
   }while( a < 20 );
4.5、跳出和跳转
 break;     // 终止循环或 switch 语句
 continue;  // 跳出本次循环
 goto;      // 跳到某行
   LOOP:do
   {
      if( a == 15)
      {
         
         a = a + 1;
         goto LOOP;
      }
      printf("a 的值: %dn", a);
      a++;
     
   }while( a < 20 );
5、结构体 struct 5.1、结构体定义

定义结构

struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} book;


struct 
{
    int a;
    char b;
    double c;
} s1;

// 定义
struct SIMPLE
{
    int a;
    char b;
    double c;
};
// 声明变量
struct SIMPLE t1, t2[20], *t3;


//也可以用typedef创建新类型
typedef struct
{
    int a;
    char b;
    double c; 
} Simple2;
//现在可以用Simple2作为类型声明新的结构体变量
Simple2 u1, u2[20], *u3;

结构体里面可以有其他结构体

//此结构体的声明包含了其他的结构体
struct COMPLEX
{
    char string[100];
    struct SIMPLE a;
};
 
//此结构体的声明包含了指向自己类型的指针
struct NODE
{
    char string[100];
    struct NODE *next_node;
};

如果两个结构体互相包含,则需要对其中一个结构体进行不完整声明

struct B;    //对结构体B进行不完整声明
 
//结构体A中包含指向结构体B的指针
struct A
{
    struct B *partner;
    //other members;
};
 
//结构体B中包含指向结构体A的指针,在A声明完后,B也随之进行声明
struct B
{
    struct A *partner;
    //other members;
};
5.2、结构体调用

结构体变量的初始化、调用实例:

#include 
 
struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} book = {"C 语言", "RUNOOB", "编程语言", 123456};
 
int main()
{
    printf("title : %snauthor: %snsubject: %snbook_id: %dn", book.title, book.author, book.subject, book.book_id);
}
#include 
#include 
 
struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};
 
int main( )
{
   struct Books Book1;        
   struct Books Book2;        
 
   
   strcpy( Book1.title, "C Programming");
   strcpy( Book1.author, "Nuha Ali"); 
   strcpy( Book1.subject, "C Programming Tutorial");
   Book1.book_id = 6495407;
 
   
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Zara Ali");
   strcpy( Book2.subject, "Telecom Billing Tutorial");
   Book2.book_id = 6495700;
 
   
   printf( "Book 1 title : %sn", Book1.title);
   printf( "Book 1 author : %sn", Book1.author);
   printf( "Book 1 subject : %sn", Book1.subject);
   printf( "Book 1 book_id : %dn", Book1.book_id);
 
   
   printf( "Book 2 title : %sn", Book2.title);
   printf( "Book 2 author : %sn", Book2.author);
   printf( "Book 2 subject : %sn", Book2.subject);
   printf( "Book 2 book_id : %dn", Book2.book_id);
 
   return 0;
}

结构体也可以作为函数参数

#include 
#include 
 
struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};
 

void printBook( struct Books book );
int main( )
{
   struct Books Book1;        
   struct Books Book2;        
 
   
   strcpy( Book1.title, "C Programming");
   strcpy( Book1.author, "Nuha Ali"); 
   strcpy( Book1.subject, "C Programming Tutorial");
   Book1.book_id = 6495407;
 
   
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Zara Ali");
   strcpy( Book2.subject, "Telecom Billing Tutorial");
   Book2.book_id = 6495700;
 
   
   printBook( Book1 );
 
   
   printBook( Book2 );
 
   return 0;
}
void printBook( struct Books book )
{
   printf( "Book title : %sn", book.title);
   printf( "Book author : %sn", book.author);
   printf( "Book subject : %sn", book.subject);
   printf( "Book book_id : %dn", book.book_id);
}
5.3、结构体指针

定义

struct Books *struct_pointer = &Book1;

使用指向该结构的指针访问结构的成员,须使用 -> 运算符,如下

struct_pointer->title;

实例

#include 
#include 
 
struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};
 

void printBook( struct Books *book );
int main( )
{
   struct Books Book1;        
   struct Books Book2;        
 
   
   strcpy( Book1.title, "C语言基础");
   strcpy( Book1.author, "zhangsan"); 
   strcpy( Book1.subject, "编程语言");
   Book1.book_id = 6495407;
 
   
   strcpy( Book2.title, "C语言进阶");
   strcpy( Book2.author, "lisi");
   strcpy( Book2.subject, "编程语言");
   Book2.book_id = 6495700;
 
   
   printBook( &Book1 );
 
   
   printBook( &Book2 );
 
   return 0;
}
void printBook( struct Books *book )
{
   printf( "书籍标题: %sn", book->title);
   printf( "书籍作者: %sn", book->author);
   printf( "书籍分类: %sn", book->subject);
   printf( "书籍ID: %dn", book->book_id);
}
6、公用体 union

一个公用体,可以赋值多种类型,但有其中一个类型正在使用,如下

#include 
#include 
 
union Data
{
   int i;
   float f;
   char  str[20];
};
 
int main( )
{
   union Data data;        
 
   // 只有i在使用
   data.i = 10;
   printf( "data.i : %dn", data.i);
   
   // 只有f在使用
   data.f = 220.5;
   printf( "data.f : %fn", data.f);
   
   // 只有str在使用
   strcpy( data.str, "C Programming");
   printf( "data.str : %sn", data.str);
 
   return 0;
}
7、位域

位域在结构体中使用,用来限制一个数值的长度(转为二进制后的长度),如下例:

struct k{
    int a:1;
    int  :2;    
    int b:3;
    int c:2;
};

int a:1;表示该位域只能为0、1 (二进制)
int c:2;表示该位域只能为00、01、10、11 (二进制)

位域类型只能是:int(整型),unsigned int(无符号整型),signed int(有符号整型) 三种类型。
使用实例:

int main(){
    struct bs{
        unsigned a:1;
        unsigned b:3;
        unsigned c:4;
    } bit,*pbit;
    bit.a=1;    
    bit.b=7;    
    bit.c=15;    
    printf("%d,%d,%dn",bit.a,bit.b,bit.c);    
    pbit=&bit;    
    pbit->a=0;    
    pbit->b&=3;    
    pbit->c|=1;    
    printf("%d,%d,%dn",pbit->a,pbit->b,pbit->c);    
}
8、typedef 与 #define
  • typedef 仅限于为类型定义符号名称,#define 不仅可以为类型定义别名,也能为数值定义别名,比如您可以定义 1 为 ONE。
  • typedef 是由编译器执行解释的,#define 语句是由预编译器进行处理的。

typedef 实例

#include 
#include 
 
typedef struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} Book;
 
int main( )
{
   Book book;
 
   strcpy( book.title, "C 教程");
   strcpy( book.author, "BeanInJ"); 
   strcpy( book.subject, "编程语言");
   book.book_id = 12345;
 
   printf( "书标题 : %sn", book.title);
   printf( "书作者 : %sn", book.author);
   printf( "书类目 : %sn", book.subject);
   printf( "书 ID : %dn", book.book_id);
 
   return 0;
}

#define实例

#include 
 
#define TRUE  1
#define FALSE 0
 
int main( )
{
   printf( "TRUE 的值: %dn", TRUE);
   printf( "FALSE 的值: %dn", FALSE);
 
   return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/588976.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号