小编J..*_*..S的帖子

我想了解getchar()!= EOF

我正在阅读C语言程序,并且到目前为止已经理解了所有内容.然而,当我遇到getchar()并且putchar(),我无法理解它们的用途,更具体地说,以下代码的作用.

main()
{
    int c;
    while ((c = getchar()) != EOF)
       putchar(c);
}
Run Code Online (Sandbox Code Playgroud)

我理解main()函数,整数的声明cwhile循环.然而我对while循环中的条件感到困惑.这个C代码的输入是什么,输出是什么.

对不起,如果这是一个基本而愚蠢的问题,但我只是在寻找一个简单的解释,然后再继续阅读本书并变得更加困惑.

c putchar eof getchar

33
推荐指数
4
解决办法
6万
查看次数

C中的stdarg和printf()

<stdarg.h>头文件是用来做函数接受的参数未定义的数字,对不对?

所以,必须使用的printf()功能是接受大量的论点(如果我弄错了,请纠正我). 我在gcc的stdio.h文件中找到以下行:<stdio.h><stdarg.h>

#if defined __USE_XOPEN || defined __USE_XOPEN2K8
# ifdef __GNUC__
#  ifndef _VA_LIST_DEFINED
typedef _G_va_list va_list;
#   define _VA_LIST_DEFINED
#  endif
# else
#  include <stdarg.h>//////////////////////stdarg.h IS INCLUDED!///////////
# endif
#endif
Run Code Online (Sandbox Code Playgroud)

我无法理解其中的大部分内容,但它似乎包括在内 <stdarg.h>

所以,如果printf()使用<stdarg.h>接受可变数量的参数,并stdio.hprintf(),一个C程序中使用printf()无需包括<stdarg.h>不是吗?

我尝试了一个程序,它有printf()一个用户定义的函数接受可变数量的参数.

我尝试的程序是:

#include<stdio.h>
//#include<stdarg.h>///If this is included, the program works fine.

void fn(int num, ...)
{
    va_list vlist;
    va_start(vlist, num);//initialising va_start (predefined)

    int i; …
Run Code Online (Sandbox Code Playgroud)

c printf gcc variadic-functions

16
推荐指数
4
解决办法
3546
查看次数

C预处理器:#define一个可以在没有括号的情况下调用的宏

我想知道是否可以编写一个行为如下的宏:

void Func(int x)
{
    printf("%d",x);
}

#define func Func x //or something

int main()
{
    func 10; //<---- remove parenthesis
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,func将指向真实函数Func,而10将是没有括号的参数.

我试图new在C++中实现类似于运算符的东西但在C中.

例:

class Base* b = new(Base);
Run Code Online (Sandbox Code Playgroud)

在这种情况下,class是一个宏struct,new是一个带函数指针的函数,Base是一个为其分配内存的函数struct Base.

我想将代码重写为这样的代码:

class Base* b = new Base;
Run Code Online (Sandbox Code Playgroud)

如果我能想出一个宏,那将是可能的:)

c c-preprocessor

12
推荐指数
1
解决办法
2018
查看次数

argp和getopt有什么区别?

我认为标题是自我解释的.我正在制作一个节目,我想知道我应该使用哪两个以及为什么.

c arguments gnu getopt argp

9
推荐指数
1
解决办法
4172
查看次数

将值直接作为数组传递给C中的函数

我有一个函数接受一个整数数组作为参数并打印它.

void printArray(int arr[3])
{
   int i;
   for(i=0; i<3; ++i)
   {
      printf("\n%d", arr[i]);
   }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法像这样传递数组的值

printArray( {3, 4, 5} );
Run Code Online (Sandbox Code Playgroud)

如果我知道前面的值而不必为了将它传递给函数而创建数组

c arrays parameter-passing function-calls compound-literals

8
推荐指数
1
解决办法
135
查看次数

使用 spacy Sentenizer 进行句子分割

我正在使用 spaCy 的句子分割器来分割句子。

from spacy.lang.en import English
nlp = English()
sbd = nlp.create_pipe('sentencizer')
nlp.add_pipe(sbd)

text="Please read the analysis. (You'll be amazed.)"
doc = nlp(text)

sents_list = []
for sent in doc.sents:
   sents_list.append(sent.text)

print(sents_list)
print([token.text for token in doc])
Run Code Online (Sandbox Code Playgroud)

输出

['Please read the analysis. (', 
"You'll be amazed.)"]

['Please', 'read', 'the', 'analysis', '.', '(', 'You', "'ll", 'be', 
'amazed', '.', ')']
Run Code Online (Sandbox Code Playgroud)

标记化已正确完成,但我不确定它是否将第二句与 ( 分开,并将其作为第一句的结尾。

nlp python-3.x spacy

8
推荐指数
2
解决办法
2万
查看次数

使用Sequelize.js进行自定义验证错误

可以自定义错误

Sequelize.ValidationError

模型:

  var PaymentType = sequelize.define('payment_type' , {
      id: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      primaryKey: true,
      autoIncrement: true,
      field: 'id'
    },
    code: {
      type: DataTypes.STRING,
      allowNull: false,
      validate:{
        notEmpty: true
      },
      field: 'code'
    },
    name: {
      type: DataTypes.STRING,
      allowNull: false,
      validate:{
        notEmpty: true
      },
      field: 'name'
    }
  }, {
    timestamps: true,
    paranoid: false,
    underscored: true,
    freezeTableName: true,
    tableName: 'payment_types'
  });
Run Code Online (Sandbox Code Playgroud)

我的控制器:

  update(req, res) {
      paymentType
        .update(req.body, {
          where: {
            id: req.params.id
          }
        })
        .then( updatedRecords => {
          res.status(200).json(updatedRecords);
        }) …
Run Code Online (Sandbox Code Playgroud)

javascript node.js express sequelize.js

7
推荐指数
3
解决办法
1534
查看次数

日历日期的算术用C或C++(在给定日期添加N天)

我一直在考虑一个日期,我正在采取像输入(日,月,年): 12, 03, 87.

现在我需要找出n几天之后的日期.

我已为此编写代码,但效率不高.能不能告诉我任何好的逻辑,它运行得更快,复杂性更低.

#include <stdio.h>

static int days_in_month[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day, month, year;

unsigned short day_counter;

int is_leap(int y) {
    return ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0);
}

next_day()
{
    day += 1; day_counter++;
    if (day > days_in_month[month]) {
        day = 1;
        month += 1;
        if (month > …
Run Code Online (Sandbox Code Playgroud)

c c++ date date-arithmetic

6
推荐指数
2
解决办法
3万
查看次数

在 Windows 上使用 Cygwin64 编译器和调试器为 C 设置 VS Code

想要设置 VS Code 以与 Cygwin/Cygwin64 一起使用。已经设置了这些:

  1. 在windows上安装了Cygwin64
  2. 从 Cygwin 安装程序安装 gcc(编译器)和 gdb(调试器)包
  3. GCC 和 GDB 不在Windows路径中。
  4. 安装的 Visual Studio 代码

发布此内容是因为我花了几天时间从多个不同的来源进行设置。这是专门针对安装了 Cygwin/Cygwin64 的 Windows 的。

免责声明:我仅针对构建单个文件进行了测试。

c cygwin visual-studio-code vscode-tasks

6
推荐指数
1
解决办法
2万
查看次数

C 中内置函数的函数定义

我们stdio.h在 C 程序中包含头文件以使用内置库函数。我曾经认为这些头文件包含我们可能在程序中使用的内置函数的函数定义。但很快就发现并非如此。

当我们打开这些头文件(例如 stdio.h)时,它只有函数原型,我在那里看不到任何函数定义。我看到这样的事情:

00133 int     _EXFUN(printf, (const char *, ...));
00134 int     _EXFUN(scanf, (const char *, ...));
00135 int     _EXFUN(sscanf, (const char *, const char *, ...));
00136 int     _EXFUN(vfprintf, (FILE *, const char *, __VALIST));
00137 int     _EXFUN(vprintf, (const char *, __VALIST));
00138 int     _EXFUN(vsprintf, (char *, const char *, __VALIST));
00139 int     _EXFUN(vsnprintf, (char *, size_t, const char *, __VALIST));
00140 int     _EXFUN(fgetc, (FILE *));
00141 char *  _EXFUN(fgets, (char *, int, FILE *)); …
Run Code Online (Sandbox Code Playgroud)

c gcc built-in function-definition

5
推荐指数
1
解决办法
2416
查看次数