C 语言练习记录

前言

本来想写一篇日志,后来发现没有什么可以写的,就简单记录一下C语言的几个小练习,准备一个系列教程。

代码

demo1

#include <stdio.h>

int main()
{
        char greetings[6] = { 'H', 'e', 'l', 'l', 'o','\0'};

        printf("Greeting Message: %s\n", greetings);
        return 0;
}

demo2

#include <stdio.h>
#include <string.h>

int main()
{
        char str1[12] = "Hello";
        char str2[12] = "world";
        char str3[12];
        int len;
        strcpy(str3,str1);
        printf("strcpy(str3,str1): %s\n", str3);

        strcat(str1, str2);
        printf("strcat(str1,str2): %s\n", str1);

        len = strlen(str1);
        printf("strlen(str1) : %d\n", len);
        return 0;
}

demo3.c

#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;
        struct Books Book2;

        /* Book1 details */
        strcpy( Book1.title, "C Programming");
        strcpy( Book1.author, "liweibin");
        strcpy( Book1.subject, "C Programming Tutorial");
        Book1.book_id = 6495407;

        /* Book2 details */
        strcpy( Book2.title, "python");
        strcpy( Book2.author, "liweibin");
        strcpy( Book2.subject, "python Programming Tutorial");
        Book1.book_id = 6495409;

        printf("Book 1 title: %s\n", Book1.title);
        printf("Book 1 author: %s\n", Book1.author);
        printf("Book 1 subject: %s\n", Book1.subject);
        printf("Book 1 book_id: %d\n", Book1.book_id);

        printf("Book 2 title: %s\n", Book2.title);
        printf("Book 2 author: %s\n", Book2.author);
        printf("Book 2 subject: %s\n", Book2.subject);
        printf("Book 2 book_id: %d\n", Book2.book_id);

        return 0;
}

demo4.c


#include <stdio.h>
#include <string.h>

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

/* Declaration of function */
void printBook( struct Books book );

int main()
{
        struct Books Book1;
        struct Books Book2;

        strcpy(Book1.title, "C Programming");
        strcpy(Book1.author, "jacky");
        strcpy(Book1.subject, "Tutorial");
        Book1.book_id = 655121;

        strcpy(Book2.title, "Python Programming");
        strcpy(Book2.author, "jacky");
        strcpy(Book2.subject, "Programming Tutorial");
        Book2.book_id = 655122;

        printBook( Book1 );
        printBook( Book2 );
        return 0 ;
}

void printBook( struct Books book)
{
        printf("Book title: %s\n", book.title);
        printf("Book author: %s\n", book.author);
        printf("Book subject: %s\n", book.subject);
        printf("Book book_id: %d\n", book.book_id);
}

demo5.c

#include <stdio.h>
#include <string.h>

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

void printBook( struct Books *book);
int main()
{
         struct Books Book1;
         struct Books Book2;

         strcpy(Book1.title, "C programming");
         strcpy(Book1.author, "jacky.li");
         strcpy(Book1.subject, "C Progarmming Tutorial");
         Book1.book_id = 654121;

         strcpy(Book2.title, "Python programming");
         strcpy(Book2.author, "jacky.li");
         strcpy(Book2.subject, "Python Progarmming Tutorial");
         Book2.book_id = 654122;

         printBook( &Book1);
         printBook( &Book2);

         return 0;
}
void printBook( struct Books *book)
{
        printf("Book title : %s\n", book->title);
        printf("Book author: %s\n", book->author);
        printf("Book subject: %s\n", book->subject);
        printf("Book book_id: %d\n", book->book_id);
}

demo6.c

#include <stdio.h>
#include <string.h>

union Data
{
        int i;
        float f;
        char str[20];
};

int main()
{
        union Data data;
        printf("Memory size occupied by data: %d\n", sizeof(data));
        return 0;
}

demo7.c

#include <stdio.h>
#include <string.h>

union Data
{
        int i;
        float f;
        char str[20];
};

int main()
{
        union Data data;
        data.i = 10;
        data.f = 220.5;
        strcpy( data.str, "C Programming");

        printf("data.i: %d\n", data.i);
        printf("data.f: %f\n", data.f);
        printf("data.str: %s\n", data.str);

        printf("Memory size occupied by data: %d\n", sizeof(data));
        return 0;
}

demo8.c


#include <stdio.h>
#include <string.h>

union Data
{
        int i;
        float f;
        char str[20];
};

int main()
{
        union Data data;
        data.i = 10;
        printf("data.i: %d\n", data.i);

        data.f = 220.5;
        printf("data.f: %f\n", data.f);

        strcpy( data.str, "C Programming");
        printf("data.str: %s\n", data.str);

        printf("Memory size occupied by data: %d\n", sizeof(data));
        return 0;
}

demo9.c

#include <stdio.h>
#include <string.h>

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

int main()
{
        Book book;
        strcpy( book.title, "C programming");
        strcpy( book.author, "Jacky.li");
        strcpy( book.subject, "progamming language");
        book.book_id = 9527;

        printf("书标题: %s\n", book.title);
        printf("书作者:%s\n", book.author);
        printf("书类目: %s\n", book.subject);
        printf("书ID: %d\n", book.book_id);

        return 0;
}

demo10.c

#include <stdio.h>

int main()
{
        int c;

        printf("Enter a value:");
        c = getchar();

        printf("\nYou entered: ");
        putchar(c);
        printf("\n");
        return 0;
}

demo11.c

#include <stdio.h>

int main()
{
        char str[100];
        char str2[100];

        printf("Enter a value:");
        gets(str);
        printf("Why?");
        gets(str2);

        printf("\nYou said: ");
        puts(str);
        puts(str2);
        return 0;
}

demo12.c


#include <stdio.h>

int main()
{
        char str[100];
        int i;

        printf("Enter a value: ");
        scanf("%s %d", str, &i);

        printf("\nYou entered: %s %d \n", str, i);
        return 0;
}

demo13.c

#include <stdio.h>

int main()
{
        FILE *fp = NULL;

        fp = fopen("/sys/class/thermal/thermal_zone0/temp", "r");
        fprintf(fp, "This is testing for fprintf...\n");
        fclose(fp);
}

demo14.c

#include <stdio.h>

int main()
{
        FILE *fp = NULL;
        char buff[255];

        fp = fopen("/sys/class/thermal/thermal_zone0/temp", "r");
        fscanf(fp, "%s", buff);
        printf("1: %s\n", buff);

        fgets(buff, 255, (FILE*)fp);
        printf("2: %s\n", buff);

        fgets(buff, 255, (FILE*)fp);
        printf("3: %s\n", buff);
        fclose(fp);
        return 0;
}

demo15.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
   FILE *fp2 = NULL;
   float temp, deg;
   int n;

   fp2 = fopen("/sys/class/thermal/thermal_zone0/temp", "r");
   n = fscanf(fp2, "%f", &deg);
   fclose(fp2);
   temp = deg / 1000;
   fputs(temp, fp);
   printf("Temp is: %f\n", temp);
   return 0;
}

demo16.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int fibonaci(int i)
{
        if(i==0)
        {
                return 0;
        }
        if(i==1)
        {
                return 1;
        }
        return fibonaci(i-1) + fibonaci(i-2);
}

int main()
{
        FILE *fp2 = NULL;
        float temp, deg;
        int n;
        int i;
        fp2 = fopen("/sys/class/thermal/thermal_zone0/temp", "r");
        for (i=0; i<100;i++)
        {
                n = fscanf(fp2, "%f", &deg);
                temp = deg / 1000;
                printf("i: %d : %d : Temp: %f\t\n",i, fibonaci(i), temp);
        }
        fclose(fp2);
        return 0;
}

demo17.c

#include <stdio.h>
int main(int argc, char *argv[])
{
        if( argc == 2)
        {
                printf("The argument supplied is %s\n", argv[1]);
        }
        else if (argc > 2)
        {
                printf("Too many arguments supplied.\n");
        }
        else
        {
                printf("One argument expected.\n");
        }
}

demo18.c

#include <stdio.h>
void bubble_sort(int arr[],int len)
{
        int i, j, temp;
        for ( i = 0; i < len - 1; i++)
           for ( j=0; j < len - 1 - i; j++)
                   if ( arr[j] > arr[ j + 1 ])
                   {
                           temp = arr[j];
                           arr[j] = arr[j+1];
                           arr[j +1 ] = temp;
                   }
}

int main()
{
        int arr[] = { 22, 34, 3, 32, 82, 55,89, 50 ,34,37, 5, 65, 64, 35, 9 ,70};
        int len = (int) sizeof(arr) / sizeof(*arr);
        bubble_sort(arr, len);
        int i;
        for (i = 0; i < len; i++)
                printf("%d ", arr[i]);
                printf("\n");
        return 0;
}

demo19.c

#include <stdio.h>
void swap(int *a, int *b)
{
   int temp =  *a;
   *a = *b;
   *b = temp;
}

void selection_sort(int arr[], int len)
{
        int i,j;
        for ( i = 0; i < len - 1; i++ )
        {
                int min = i;
                for ( j = i + 1 ; j < len ; j++)
                        if ( arr[j] < arr[min])
                                min = j;
                swap(&arr[min], &arr[i]);
        }
}

int main()
{
        int arr[] = { 22, 34, 3, 32, 82, 55,89, 50 ,34,37, 5, 65, 64, 35, 9 ,70};
        int len = (int) sizeof(arr) / sizeof(*arr);
//      bubble_sort(arr, len);
        selection_sort(arr, len);
        int i;
        for (i = 0; i < len; i++)
                printf("%d ", arr[i]);
                printf("\n");
        return 0;
}

好了,就先来点儿基础的热热身。

树莓派串口驱动的情况

树莓派串口驱动详细

串口驱动现状

本来在树莓派上插入了一个sim800c的设备,但是这个产品带着一颗ch3401的芯片,在设备上用ls /dev/ttyUSB*竟然没有找到串口设备,内心想可能是没有驱动,于是到官方站点上下载了一下。

看更新时间是2018年3月,然后打开压缩包。

然后发现Readme里面已经告诉我们支持的内核只能是:

太老了。后来查资料发现好像这几款常见芯片已经被加入到Linux内核,早已经支持。抱着怀疑的态度查看了一下。

果然都支持了。
包括ch341, cp210x系列,pl2303, ftdi, 好吧,这下方便多了,以后在linux下做串口设备的调试就不要担心串口芯片不兼容了!
然后再通过lsusb和lsmod还有dmesg检查后发现,已经识别了。。

检查看看串口是否连上来出现一个/dev/ttyUSB0的设备:

这样才对么。我下面就可以用串口来获取一下sim800C的sim卡联网的状态信息了。

测试代码:

#!/usr/bin/env python3
import serial
import time
import operator
import os

time.sleep(2)
ser = serial.Serial('/dev/ttyUSB0', 115200)
print("串口初始化完成...")

if ser.isOpen == False:
    ser.open()
try:
    print('-'*60)
    print("初始化SIM800C")
    print("尝试测试SIM800C联网获取CCID信息...")
    time.sleep(2)
    i = 0
    while True:
        ser.write(str.encode("AT+CCID\r"))
        size = ser.inWaiting()
        if size != 0:
            response = ser.read(size)
            ccid = str(response,encoding="utf8")
            print(ccid)
        ser.write(str.encode("AT+CGMR\r"))
        size = ser.inWaiting()
        if size != 0:
            connection = ser.read(size)
            creg = str(connection, encoding="utf8")
            print(creg)
            time.sleep(1)
        ser.write(str.encode("AT+CSQ\r"))
        size = ser.inWaiting()
        if size != 0:
            csq = ser.read(size)
            csqstatus = str(csq, encoding="utf8")
            print(csqstatus)
            time.sleep(1)
        ser.write(str.encode("AT+CGACT=1\r"))
        size = ser.inWaiting()
        if size != 0:
            cgact = ser.read(size)
            cgactstatus = str(cgact, encoding="utf8")
            print(cgactstatus)
        else:
            ser.flushInput()
            time.sleep(1)
except KeyboardInterrupt:
    ser.close()

结果

今天就到这里,白了个白~

Debian 9 配置 DHCP 服务器

DHCP 很常见,自动分配 IP 地址子网掩码,默认网关等,还可以结合其他服务做远程无人值守安装服务器.

配置步骤

root@ns1:~# apt -y install isc-dhcp-server
root@ns1:~# vi /etc/default/isc-dhcp-server
# line 4: uncomment
DHCPDv4_CONF=/etc/dhcp/dhcpd.conf
# line 17,18: specify listening interfaces
# if not use IPv6, comment out it
INTERFACESv4="ens3"
INTERFACESv6="ens3"
root@ns1:~# vi /etc/dhcp/dhcpd.conf
# line 7: specify domain name
option domain-name "yoyojacky.com";
# line 8: specify nameserver's hostname or IP address
option domain-name-servers ns1.yoyojacky.com;
# line 21: uncomment
authoritative;
# add to the end
# specify network address and subnet-mask
subnet 10.0.0.0 netmask 255.255.255.0 {
     # specify default gateway
     option routers 10.0.0.1;
     # specify subnet-mask
     option subnet-mask 255.255.255.0;
     # specify the range of leased IP address
     range dynamic-bootp 10.0.0.200 10.0.0.254;
}
root@ns1:~# systemctl restart isc-dhcp-server 

基本上就搞定了,测试 linux 用 dhclient 获取一下,windows 用 ipconfig /release 和/renew.