echho 发布的文章

 //假设你发现前台运行的一个程序需要很长的时间,但是需要干其他的事情,
//你就可以用 Ctrl-Z ,终止这个程序,然后可以看到系统提示:
[1]+ Stopped /root/bin/rsync.sh

//jobs 显示当前暂停的进程
jobs
[1]+ Running /root/bin/rsync.sh &

// bg N 使第N个任务在后台运行
bg 1

//如果想把它调回到前台运行,可以用fg 
//fg N 使第N个任务在前台运行
fg 1

//默认bg,fg不带 N 时表示对最后一个进程操作

 总结:

    (1) CTRL+Z停止进程并放入后台

    (2) jobs 显示当前暂停的进程

    (3) bg N 使第N个任务在后台运行(%前有空格)

    (4) fg N 使第N个任务在前台运行

    默认bg,fg不带N时表示对最后一个进程操作!

--> linux下:ctrl-c  发送 SIGINT 信号给前台进程组中的所有进程。常用于终止正在运行的程序。ctrl-z  发送 SIGTSTP 信号给前台进程组中的所有进程,常用于挂起一个进程。ctrl-d  不是发送信号,而是表示一个特殊的二进制值,表示 EOF。ctrl-\  发送 SIGQUIT 信号给前台进程组中的所有进程,终止前台进程并生成 ...

#include <stdio.h>
#include <string.h>
char * s_gets(char * st, int n);
#define MAXTITL   40
#define MAXAUTL   40
#define MAXBKS   100              //最大书籍数

struct book {                     //建立book结构声明
    char title[MAXTITL];
    char author[MAXAUTL];
    float value;
};

int main(void)
{
    struct book library[MAXBKS]; //book 类型结构的数组
    int count = 0;
    int index;
    
    printf("Please enter the book title.\n");
    printf("Press [enter] at the start of a line to stop.\n");
    while (count < MAXBKS && s_gets(library[count].title, MAXTITL) != NULL
           && library[count].title[0] != '\0')

//library[count].title[0] != '\0'判断字符串中的首字符是否为空字符
//按下 Enter 键相当于输入了一个字符串
    {
        printf("Now enter the author.\n");
        s_gets(library[count].author, MAXAUTL);
        printf("Now enter the value.\n");
        scanf("%f", &library[count++].value);
        while (getchar() != '\n')
            continue;          //清理输入行
        if (count < MAXBKS)
            printf("Enter the next title.\n");
    }
    
    if (count > 0)
    {
        printf("Here is the list of your books:\n");
        for (index = 0; index < count; index++)
            printf("%s by %s: $%.2f\n", library[index].title,
                   library[index].author, library[index].value);
    }
    else
    	printf("No books? Too bad.\n");
    
    return 0;
}

char * s_gets(char * st, int n)
{
    char * ret_val;
    char * find;
    
    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        find = strchr(st, '\n');   // 查找换行符
        if (find)                  //如果地址不是NULL
            *find = '\0';          // 在此处放置一个空字符
        else
            while (getchar() != '\n')
                continue;          // 处理输入行中剩余的字符
    }
    return ret_val;
}

--> #include <stdio.h> #include <string.h> char * s_gets(char * st, int n); #define MAXTITL 40 #define MAXAUTL 40 #define MAXBKS 100 //最大书籍数 struct book { ...

/*char *s_gets(char *st, int n)是为了获取字符st中的n个元素,
并将其结果返回为s_gets的字符指针。功能类似于fgets函数,
但它解决了fgets函数会读取'\n'的问题。*/

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

char * s_gets(char *st, int n);
int main()
{
    
    char name[20];
    printf("输入姓名\n");
    s_gets(name,20);
    printf("姓名为:%s\n",name);

    return 0;
}

char * s_gets(char *st, int n)
{
    char * ret_val;
    char * find;
                                     //表示从标准输入流中读取n-1个元素到字符st中,再将
    ret_val = fgets(st,n,stdin);     //再将指针ret_val指向指针st               
    if (ret_val)                      //if语句为了避免fgets函数返回值为NULL的情况
    {
        find  = strchr(st, '\n');      //查找换行符
        if (find)
            *find =  '\0';             //替换为空字符
        else
            while (getchar() != '\n')  //清除IO缓冲区 
                  continue;
    }
    return ret_val;
}

--> /*char *s_gets(char *st, int n)是为了获取字符st中的n个元素,并将其结果返回为s_gets的字符指针。功能类似于fgets函数,但它解决了fgets函数会读取'\n'的问题。*/#include <stdio.h>#include <string.h>char * s_gets(char *st, int n);int main(){ ...

一
ifndef是 if not define 的缩写,一种宏定义。它是预处理功能中三种
(宏定义,文件包含和条件编译)中的第三种--条件编译

#ifndef

#define X

...

#endif


c语言在对程序进行编译时,会先根据预处理命令进行预处理,C语言编译系统包括预处理,

编译和链接等部分。

#ifndef X  //先测试是否被宏定义

#define X 

程序段1//如果X没有被宏定义过,定义X,并编译程序段1;

#else

程序段2 //如果X已经定义过了则编译程序段2 的语句,忽视程序段1.

#endif


ifndef和endif要一起使用,不能存在丢失。

假如你有一个C源文件,它包含了多个头文件,比如头文件A和头文件B,头文件B又包含了头文件A,
则最终该源文件包含了两次头文件A。
如果你在头文件A里定义了结构体或者类类型
那么问题来了,编译时会报大量的重复定义错误。
例如要编写头文件  eatline.h

//在头文件开头写上两行:

#ifndef EATLINE_H_      //一般是文件名的大写

#define EATLINE_H_      

//头文件结尾写上一行:

#endif

这样一个工程文件里同时包含两个test.h时,就不会出现重定义的错误了。
当第一次包含test.h时,由于没有定义_TEST_H,条件为真,
这样就会包含(执行)#ifndef _TEST_H和
#endif之间的代码,当第二次包含test.h时前面一次已经定义了_TEST_H,条件为假,
#ifndef _TEST_H和#endif之间的代码也就不会再次被包含,
这样就避免了重定义了.

--> 一ifndef是 if not define 的缩写,一种宏定义。它是预处理功能中三种(宏定义,文件包含和条件编译)中的第三种--条件编译#ifndef#define X...#endif二c语言在对程序进行编译时,会先根据预处理命令进行预处理,C语言编译系统包括预处理,编译和链接等部分。#ifndef X  //先测试是否被宏定义#define X 程序段1//如果X没有被宏定义过,定义X...

[label]进制转换[/label][alert]编写进制转换函数来接受两个参数,且第二个参数在 2~10 范围内,然后以第二个参数中指定的进制打印第一个参数的数值[/alert]

#include <stdio.h>
void to_base_n (int x, int base);//转换进制函数
int main()
{
    int number, b, count;
    printf("输入数字\n");
    while(scanf("%d",&number) == 1) //检测输入是否为数字
    {
        printf("输入 2~10 中的一个数字\n");
        while((count = scanf("%d",&b)) == 1 && (b < 2 || b > 10)) //检测输入是否为数字且在范围内
        {
            printf("输入需要在 2~10 范围内: ");
        }
        if (count != 1)
        break;
        printf("Base %d equivalent: ", b);
        to_base_n(number, b);
        putchar('\n');
        printf("输入一个数字 (q to quit):\n");
    }
    printf("Done!\n");
    return 0;
 }
 
void to_base_n (int x, int base)
{
    int r;
 
    r = x % base; //除以进制数求余
    if (x >= base)
    to_base_n(x / base, base); //递归
    putchar('0' + r);
    return;
}

--> [label]进制转换[/label][alert]编写进制转换函数来接受两个参数,且第二个参数在 2~10 范围内,然后以第二个参数中指定的进制打印第一个参数的数值[/alert]#include <stdio.h>void to_base_n (int x, int base);//转换进制函数 int main() { int number, b, count; ...