C语言学习笔记

一、Hello World

#include <stdio.h>
 
int main()
{
   /* 我的第一个 C 程序 */
   printf("Hello, World!  
");
   return 0;
}

一个标准的hello.c文件,然后gcc hello.c -o hello,生成hello.exe(linux下生成hello.out文件),然后直接运行这个文件,屏幕输出:Hello World!

二、变量声明之extern关键字

个人理解,extern关键字用于引入外部变量,在函数作用域内调用全局变量。这一点在js中是可以直接调用的,而在python中是通过global关键字调用全局变量。

#include <stdio.h>

//外部变量(全局作用域)
int x;
int y;
int addtwonum()
{
    //函数作用域引入全局变量
    extern int x;
    extern int y;
    //给全局变量x 和 y 赋值
    x = 1;
    y = 2;
    return x+y;
}
 
int main()
{
    int result;
    // 调用函数 addtwonum
    result = addtwonum();
    
    printf("result 为: %d",result);
    return 0;
}

在不同文件之间进行变量的调用也是通过该关键字的:

#include <stdio.h>
/*外部变量声明*/
extern int x ;
extern int y ;
int addtwonum()
{
    return x+y;
}
#include <stdio.h>
  
/*定义两个全局变量*/
int x=1;
int y=2;
int addtwonum();
int main(void)
{
    int result;
    result = addtwonum();
    printf("result 为: %d
",result);
    return 0;
}

三、变量和常量的定义

#include <stdio.h>
 
int main()
{
   const int  LENGTH = 10; //常量
   #define WIDTH  5; //常量
   int area;  //变量
   
   area = LENGTH * WIDTH;
   printf("value of area : %d", area);
 
   return 0;
}

其实这块遇到了一些问题 invalid type argument of unary '*' (have 'int') 在连乘的时候,百度了一下,应该看到后面指针的时候就能解决这个疑问。

四、函数

#include <stdio.h>
 
/* 函数声明 */
int max(int num1, int num2);
 
int main ()
{
   /* 局部变量定义 */
   int a = 100;
   int b = 200;
   int ret;
   /* 调用函数来获取最大值 */
   ret = max(a, b);
   printf( "Max value is : %d
", ret );
   return 0;
}
 
/* 函数返回两个数中较大的那个数 */
int max(int num1, int num2) 
{
   /* 局部变量声明 */
   int result;
   if (num1 > num2)
      result = num1;
   else
      result = num2;
   return result; 
}

C语言中,要想在主程序里调用函数,必须事先对函数进行声明,无论是在全局声明还是局部声明,只有声明了才能用,所以上述的函数声明至关重要。

五、枚举

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

注意,枚举的第一个值默认是0,如果没有对其赋值的话,上述只是定义了一个枚举类型DAY;

//先定义枚举类,再定义枚举变量
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;
#include<stdio.h>
 
enum DAY
{
      MON=1, TUE, WED, THU, FRI, SAT, SUN
};
 
int main()
{
    enum DAY day;
    day = WED;
    printf("%d",day);
    return 0;
}

枚举类是被当做int处理的,所以上述输出3。

六、指针

 首先&符号可以访问变量在内存中的地址。

#include <stdio.h>
 
int main ()
{
   int  var = 20;   /* 实际变量的声明 */
   int  *ip;        /* 指针变量的声明 */
 
   ip = &var;  /* 在指针变量中存储 var 的地址 */
 
   printf("Address of var variable: %p
", &var  );
 
   /* 在指针变量中存储的地址 */
   printf("Address stored in ip variable: %p
", ip );
 
   /* 使用指针访问值 */
   printf("Value of *ip variable: %d
", *ip );
 
   return 0;
}

 函数指针:

#include <stdlib.h>  
#include <stdio.h>
 
// 回调函数
void populate_array(int *array, size_t arraySize, int (*getNextValue)(void))
{
    for (size_t i=0; i<arraySize; i++)
        array[i] = getNextValue();
}
 
// 获取随机值
int getNextRandomValue(void)
{
    return rand();
}
 
int main(void)
{
    int myarray[10];
    populate_array(myarray, 10, getNextRandomValue);
    for(int i = 0; i < 10; i++) {
        printf("%d ", myarray[i]);
    }
    printf("
");
    return 0;
}

七、字符串

char greeting[6] = {'H', 'e', 'l', 'l', 'o', ''};

//或者,直接这样:

char greeting[] = "Hello";


//但是,通常我们这样:
char str1[12] = "Hello";

在 C 语言中,字符串实际上是使用 null 字符 '' 终止的一维字符数组。所以上述字符数组的大小比单词 "Hello" 的字符数多一个。

1      strcpy(s1, s2);
复制字符串 s2 到字符串 s1。深克隆
2    strcat(s1, s2);
连接字符串 s2 到字符串 s1 的末尾。字符串拼接
3    strlen(s1);
返回字符串 s1 的长度。length属性
4    strcmp(s1, s2);
如果 s1 和 s2 是相同的,则返回 0;如果 s1<s2 则返回小于 0;如果 s1>s2 则返回大于 05    strchr(s1, ch);
返回一个指针,指向字符串 s1 中字符 ch 的第一次出现的位置。indexOf()
6    strstr(s1, s2);
返回一个指针,指向字符串 s1 中字符串 s2 的第一次出现的位置。

八、结构体(一种数据结构,类似于我们常见的类的声明)

#include <stdio.h>
#include <string.h>
 
struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};
 
int main( )
{
   struct Books Book1;        /* 声明 Book1,类型为 Books */
 
   /* Book1 详述 */
   strcpy( Book1.title, "C Programming");
   strcpy( Book1.author, "Nuha Ali"); 
   strcpy( Book1.subject, "C Programming Tutorial");
   Book1.book_id = 6495407;
 
   /* 输出 Book1 信息 */
   printf( "Book 1 title : %s
", Book1.title);
   printf( "Book 1 author : %s
", Book1.author);
   printf( "Book 1 subject : %s
", Book1.subject);
   printf( "Book 1 book_id : %d
", Book1.book_id);
   return 0;
}

这和java中的变量申明还是有区别的,java中无论是基本数据类型的变量还是自定义类型变量声明都可以直接用类型声明:Books Book1;

原文地址:https://www.cnblogs.com/eco-just/p/10513506.html