有人可以解释一下为什么这个C代码没有编译我的else if语句?

时间:2016-10-13 21:22:54

标签: c if-statement

所以我写了这个C代码但是找不到为什么编译器没有得到else if语句的错误。

Oct 13 20:21:23 dev-company1 java: 2016-10-13 20:21:23,723 main DEBUG Initializing configuration XmlConfiguration[location=jar:file:/home/company-dev/app/current/company-server.jar!/log4j2.xml]
Oct 13 20:21:23 dev-company1 java: 2016-10-13 20:21:23,729 main DEBUG Installed script engines
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,282 main DEBUG Oracle Nashorn Version: 1.8.0_101, Language: ECMAScript, Threading: Not Thread Safe, Compile: true, Names: {nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScript, ecmascript}
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,283 main DEBUG PluginManager 'Core' found 99 plugins
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,283 main DEBUG PluginManager 'Level' found 0 plugins
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,285 main ERROR Error processing element raven ([appenders: null]): CLASS_NOT_FOUND
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,286 main ERROR Error processing element logentries ([appenders: null]): CLASS_NOT_FOUND
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,288 main DEBUG No scheduled items
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,289 main DEBUG Building Plugin[name=property, class=org.apache.logging.log4j.core.config.Property].
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,303 main TRACE TypeConverterRegistry initializing.
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,303 main DEBUG PluginManager 'TypeConverter' found 23 plugins
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,317 main DEBUG createProperty(name="pattern", value="%d{hh:mm:ss.sss} [mode=dev] [%t] %-5level %logger{36} - %msg%n")
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,317 main DEBUG Building Plugin[name=properties, class=org.apache.logging.log4j.core.config.PropertiesPlugin].
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,326 main DEBUG configureSubstitutor(={pattern=%d{hh:mm:ss.sss} [mode=dev] [%t] %-5level %logger{36} - %msg%n}, Configuration(jar:file:/home/company-dev/app/current/company-server.jar!/log4j2.xml))
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,327 main DEBUG PluginManager 'Lookup' found 13 plugins
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,328 main DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout].
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,332 main DEBUG PatternLayout$Builder(pattern="%d{hh:mm:ss.sss} [mode=dev] [%t] %-5level %logger{36} - %msg%n", PatternSelector=null, Configuration(jar:file:/home/company-dev/app/current/company-server.jar!/log4j2.xml), Replace=null, charset="null", alwaysWriteExceptions="null", noConsoleNoAnsi="null", header="null", footer="null")
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,332 main DEBUG PluginManager 'Converter' found 41 plugins
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,366 main DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.ConsoleAppender].
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,367 main DEBUG PluginManager 'Converter' found 41 plugins
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,367 main DEBUG Starting OutputStreamManager SYSTEM_OUT.false.false-2
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,381 main DEBUG ConsoleAppender$Builder(patternlayout(%d{hh:mm:ss.sss} [mode=dev] [%t] %-5level %logger{36} - %msg%n), Filter=null, target="SYSTEM_OUT", name="console", follow="null", direct="null", ignoreExceptions="null")
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,381 main DEBUG Starting OutputStreamManager SYSTEM_OUT.false.false
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,381 main DEBUG Building Plugin[name=appenders, class=org.apache.logging.log4j.core.config.AppendersPlugin].
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,382 main DEBUG createAppenders(={console})
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,382 main DEBUG Building Plugin[name=appender-ref, class=org.apache.logging.log4j.core.config.AppenderRef].
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,383 main DEBUG createAppenderRef(ref="le", level="null", Filter=null)
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,383 main DEBUG Building Plugin[name=appender-ref, class=org.apache.logging.log4j.core.config.AppenderRef].
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,383 main DEBUG createAppenderRef(ref="sentry", level="null", Filter=null)
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,383 main DEBUG Building Plugin[name=appender-ref, class=org.apache.logging.log4j.core.config.AppenderRef].
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,384 main DEBUG createAppenderRef(ref="console", level="null", Filter=null)
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,384 main DEBUG Building Plugin[name=root, class=org.apache.logging.log4j.core.config.LoggerConfig$RootLogger].
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,385 main DEBUG createLogger(additivity="null", level="DEBUG", includeLocation="null", ={le, sentry, console}, ={}, Configuration(jar:file:/home/company-dev/app/current/company-server.jar!/log4j2.xml), Filter=null)
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,386 main DEBUG Building Plugin[name=appender-ref, class=org.apache.logging.log4j.core.config.AppenderRef].
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,387 main DEBUG createAppenderRef(ref="console", level="null", Filter=null)
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,387 main DEBUG Building Plugin[name=logger, class=org.apache.logging.log4j.core.config.LoggerConfig].
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,390 main DEBUG createLogger(additivity="false", level="DEBUG", name="com.amazonaws", includeLocation="null", ={console}, ={}, Configuration(jar:file:/home/company-dev/app/current/company-server.jar!/log4j2.xml), Filter=null)
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,391 main DEBUG Building Plugin[name=loggers, class=org.apache.logging.log4j.core.config.LoggersPlugin].
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,401 main DEBUG createLoggers(={root, com.amazonaws})
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,401 main ERROR Unable to locate appender "le" for logger config "root"
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,401 main ERROR Unable to locate appender "sentry" for logger config "root"
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,402 main DEBUG Configuration XmlConfiguration[location=jar:file:/home/company-dev/app/current/company-server.jar!/log4j2.xml] initialized
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,402 main DEBUG Starting configuration XmlConfiguration[location=jar:file:/home/company-dev/app/current/company-server.jar!/log4j2.xml]
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,402 main DEBUG Started configuration XmlConfiguration[location=jar:file:/home/company-dev/app/current/company-server.jar!/log4j2.xml] OK.
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,403 main TRACE Stopping org.apache.logging.log4j.core.config.DefaultConfiguration@531d72ca...
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,403 main TRACE DefaultConfiguration notified 1 ReliabilityStrategies that config will be stopped.
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,403 main TRACE DefaultConfiguration stopping root LoggerConfig.
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,404 main TRACE DefaultConfiguration notifying ReliabilityStrategies that appenders will be stopped.
Oct 13 20:21:24 dev-company1 java: 2016-10-13 20:21:24,404 main TRACE DefaultConfiguration stopping remaining Appenders.
Oct 13 20:21:45 dev-company1 journal: Suppressed 692 messages from /system.slice/company.service
Oct 13 20:21:45 dev-company1 java: 08:21:45.045 [mode=dev] [cluster-ClusterId{value='57ffecc465bab830ccd6f372', description='null'}-dev-mongo2:27017] DEBUG org.mongodb.driver.cluster - Checking status of dev-mongo2:27017
Oct 13 20:21:45 dev-company1 java: 08:21:45.045 [mode=dev] [cluster-ClusterId{value='57ffecc465bab830ccd6f372', description='null'}-dev-mongo2:27017] DEBUG org.mongodb.driver.cluster - Updating cluster description to  {type=REPLICA_SET, servers=[{address=dev-mongo2:27017, type=REPLICA_SET_PRIMARY, roundTripTime=9.2 ms, state=CONNECTED}, {address=dev-mongo3:27017, type=REPLICA_SET_SECONDARY, roundTripTime=4.4 ms, state=CONNECTED}, {address=dev-mongo4:27017, type=REPLICA_SET_SECONDARY, roundTripTime=1.4 ms, state=CONNECTED}]

我收到一条错误,说''else'没有先前的'if'“

3 个答案:

答案 0 :(得分:6)

评论中的Jean-François Fabre pointed out,您的问题是此#define中的尾随分号:

#define OUT 0;

这会使您的代码看起来像这样:

    if(c == '\n' || c == '\t' || c == ' ') 
        state = OUT;
    ;  // The "else" would go here, but you've already completed the "if"

    else if(state == OUT) {   // Now there is no "if" to associate with
        state = IN;
        ++nw;
    }

预处理器能够引发这样的各种问题,这是在可能的情况下避免它的一个很好的理由。 (这来自一个用cpp来做肮脏,恶心的事情的人。)

在这种情况下,匿名enum是可行的方法:

enum {
    OUT = 0,
    IN = 1,
};

此外,这是一个风格问题,但我总是包括花括号,除了没有else的简单条件:

// OK
if (ok)
    foo();

// Not OK
if (ok)
    foo();
else if (x == 3) {
    lots();
    of();
    stuff();
}
else
    bad();

答案 1 :(得分:2)

OUT定义为0;

在你的第二个if语句中,这变为

if(c == '\n' || c == '\t' || c == ' ') 
    state = 0;;
else

或写得更清楚

if(c == '\n' || c == '\t' || c == ' ') 
    state = 0;
;
else

由于这个第二个分号,没有先前的if,else可能属于哪个。

答案 2 :(得分:0)

问题是如果。 宏OUT在这里:

else if(state == OUT) {
            state = IN;
            ++nw;
}

变成

else if(state == 0;) {
            state = IN;
            ++nw;
}

预处理阶段之后。更改宏,以便它没有;,它会工作。通常,宏不会持有; (除非他们有多行)