Search notes:

C: if statement

Variable declaration in an if statement

A variable cannot be declared within an if statement. The following simple program, which tries to do that, when compiled with gcc, reports the error expected expression before 'int'.
#include <stdio.h>

int foo() {
  return 42;
}

int main() {

   if (int i = foo()) {  // error: expected expression before ‘int’

      printf("i = %d\n", 42);

   }
   else {

      printf("foo returned 0\n");

   }
}
Github repository about-c, path: /language/statements/compound/if/variable-declaration.c
See also the Stack Overflow question Declare variable in if statement (ANSI C)

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1758199564, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/C-C-plus-plus/C/language/statements/compound/if/index(60): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78