declaration

Compiler error when declaring a variable inside if condition and no curly braces

为君一笑 提交于 2019-11-26 03:57:55
问题 Why does this first if compile well and the second fail? if(proceed) {int i;} // This compiles fine. if(proceed) int i;// This gives an error. (Syntax error on token \")\", { expected after this token) 回答1: Because the language spec says so: http://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html A declaration introduces an entity into a program and includes an identifier (§3.8) that can be used in a name to refer to this entity. A declared entity is one of the following: ... A local

Why #include <stdio.h> is not required to use printf()?

柔情痞子 提交于 2019-11-26 03:56:46
问题 Session transcript: >type lookma.c int main() { printf(\"%s\", \"no stdio.h\"); } >cl lookma.c Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86 Copyright (C) Microsoft Corporation. All rights reserved. lookma.c Microsoft (R) Incremental Linker Version 8.00.50727.762 Copyright (C) Microsoft Corporation. All rights reserved. /out:lookma.exe lookma.obj >lookma no stdio.h 回答1: In strict compliance mode (that means "in theory"), you invoke undefined behaviour (which

What does “default” mean after a class&#39; function declaration?

廉价感情. 提交于 2019-11-26 03:47:55
问题 I\'ve seen default used next to function declarations in a class. What does it do? class C { C(const C&) = default; C(C&&) = default; C& operator=(const C&) & = default; C& operator=(C&&) & = default; virtual ~C() { } }; 回答1: It's a new C++11 feature. It means that you want to use the compiler-generated version of that function, so you don't need to specify a body. You can also use = delete to specify that you don't want the compiler to generate that function automatically. With the

Declare variable in SQLite and use it

梦想的初衷 提交于 2019-11-26 03:37:45
问题 I want to declare a variable in SQLite and use it in insert operation. Like in MS SQL: declare @name as varchar(10) set name = \'name\' select * from table where name = @name For example, I will need to get last_insert_row and use it in insert . I have found something about binding but I didn\'t really fully understood it. 回答1: SQLite doesn't support native variable syntax, but you can achieve virtually the same using an in-memory temp table. I've used the below approach for large projects

Declaring variables inside a switch statement [duplicate]

最后都变了- 提交于 2019-11-26 03:28:12
问题 This question already has an answer here: Why can't variables be declared in a switch statement? 23 answers I saw a few answers to this issue, and I get it — you can\'t declare and assign variables inside a switch . But I\'m wondering if the following is correct at throwing an error saying error: expected expression before \'int\' Code: switch (i) { case 0: int j = 1; break; } Why would putting a call to NSLog() before it result in no errors? switch (i) { case 0: NSLog(@\"wtf\"); int j = 1;

Defining static const integer members in class definition

那年仲夏 提交于 2019-11-26 03:23:47
问题 My understanding is that C++ allows static const members to be defined inside a class so long as it\'s an integer type. Why, then, does the following code give me a linker error? #include <algorithm> #include <iostream> class test { public: static const int N = 10; }; int main() { std::cout << test::N << \"\\n\"; std::min(9, test::N); } The error I get is: test.cpp:(.text+0x130): undefined reference to `test::N\' collect2: ld returned 1 exit status Interestingly, if I comment out the call to

Why is volatile needed in C?

十年热恋 提交于 2019-11-26 03:13:57
问题 Why is volatile needed in C? What is it used for? What will it do? 回答1: Volatile tells the compiler not to optimize anything that has to do with the volatile variable. There are at least three common reasons to use it, all involving situations where the value of the variable can change without action from the visible code: When you interface with hardware that changes the value itself; when there's another thread running that also uses the variable; or when there's a signal handler that might

Is it possible to declare two variables of different types in a for loop?

半世苍凉 提交于 2019-11-26 01:35:46
问题 Is it possible to declare two variables of different types in the initialization body of a for loop in C++? For example: for(int i=0,j=0 ... defines two integers. Can I define an int and a char in the initialization body? How would this be done? 回答1: Not possible, but you can do: float f; int i; for (i = 0,f = 0.0; i < 5; i++) { //... } Or, explicitly limit the scope of f and i using additional brackets: { float f; int i; for (i = 0,f = 0.0; i < 5; i++) { //... } } 回答2: No - but technically

C# member variable initialization; best practice?

限于喜欢 提交于 2019-11-26 00:59:03
问题 Is it better to initialize class member variables on declaration private List<Thing> _things = new List<Thing>(); private int _arb = 99; or in the default constructor? private List<Thing> _things; private int _arb; public TheClass() { _things = new List<Thing>(); _arb = 99; } Is it simply a matter of style or are there performance trade-offs, one way or the other? 回答1: In terms of performance, there is no real difference; field initializers are implemented as constructor logic. The only

Variable declaration placement in C

冷暖自知 提交于 2019-11-26 00:47:00
问题 I long thought that in C, all variables had to be declared at the beginning of the function. I know that in C99, the rules are the same as in C++, but what are the variable declaration placement rules for C89/ANSI C? The following code compiles successfully with gcc -std=c89 and gcc -ansi : #include <stdio.h> int main() { int i; for (i = 0; i < 10; i++) { char c = (i % 95) + 32; printf(\"%i: %c\\n\", i, c); char *s; s = \"some string\"; puts(s); } return 0; } Shouldn\'t the declarations of c