LoginSignup
0
0

More than 5 years have passed since last update.

C > !val と val != 0の違い?

Posted at

組込みで使うライブラリのバージョンアップにおいて、下記のような修正がいくつか見られる。

previous.c
if (nb) {
...
while (!nb) {
...
current.c
if (nb != 0) {
...
while (nb == 0) {
...

読みやすさだけの変更なのか、そのバージョンアップ履歴に記載されているバグ修正と関係があるのかは不明。

#include <stdio.h>

int main(void) {
    int val;

    val = 0;
    if (!val) printf("!val\n");
    if (val != 0) printf("val != 0\n");

    val = 1;
    if (!val) printf("!val\n");
    if (val != 0) printf("val != 0\n");

    val = 5;    
    if (!val) printf("!val\n");
    if (val != 0) printf("val != 0\n");

    return 0;
}
run
!val
val != 0
val != 0
0
0
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0