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;
}

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

发布者

yoyojacky

我是骑驴玩儿漂移, 喜欢玩儿电子,喜欢的编程语言, C, shell, python, 爱玩儿的开发板: 树莓派, arduino,STM32系列, 还有3D 打印机,四轴飞行器,业余时间也喜欢玩儿吉他,非洲鼓. 欢迎来光临我的小站~

发表评论

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据