Initialising variables in C++

variable declaration in c
c initialize variables to 0
c programming variable types
types of variables in c
global variable in c
c initialize variable declaration
what is initialization in c with example
variable declaration in c programming language

I want to initialise a lot of variables (doubles and ints mostly, but some vectors as well), which will then be passed into lots of functions. What is the best way to initialise these?

I could initialise them all in the main function, this just feels a little messy, and ideally I wanted to create a separate function to initialise them in. I want to try and keep the main function as empty as I can. However, I then have the issue of not being able to return them.

Is there a proper way of doing this? I've heard of constructors, but I'm not entirely sure what they're used for, and whether or not they're something that I should be looking at here.

1) you can use function, pass variable by reference for initialisation to keep main function clean.

2) you can use templates as mention in this video .

See the code.

Initializing variables in C, A rule that hasn't been mentioned yet is this: when the variable is declared inside a function it is not initialised, and when it is declared in static� Different ways of initializing a variable in C++. Method 1 (Declaring and Initializing a variable) int a = 5; Method 2 (Initializing a variable using parenthesis) int a (5) ; Yes, they’re the same. On the other hand, for a class type they’re different. For example : struct A { A(int); }; A a(5); // This statement is to construct a; Method 3 (Initializing a variable using braces)

Based on your requirements, I would use structs with default values:

struct MyConfiguration {
    double someValue{ 5. };
    int someOtherValue{ 3 };
};

int main( int, char ** ) {
    const MyConfiguration c;

    f( c.someValue );
    g( c.someValue, c.someOtherValue );
}

For many kinds of applications it would be reasonable to read those values from a configuration file.

Variable Initialization in C programming language, Variable initialization. In c programming language, variable can be initialized in the declaration statement of any block (either it may main's block or any other� In c programming language, variable can be initialized in the declaration statement of any block (either it may main’s block or any other function’s block). While declaring a variable you can provide a value to the variable with assignment operator. Here is the syntax of the variable initialization data_type variable_name = value;

I think what you need is exactly what object-oriented languages are for.

class A
{
     private:
         int a;
         int b;
         ....
     public:
         foo();
         bar();
         ...

};

I'll recommend you to read some books, like programming principles and practice using c++ from Bjarne Stroustrup himself

Of course, this example is just an illustration of the concept of classes (the code is not even correct, but it is not important here). Read some articles or books on OO principles, and then try to design an architecture answering your needs.

C variable with examples, Declaring & initializing C variable: Variables should be declared in the C program before to use. Memory space is not allocated for a variable while declaration. It happens only on variable definition. Variable initialization means assigning a value to the variable. Rules for Defining Variables in C. Variables in C must not start with the number else the Variable will not be valid. For example (1 string, is not a valid variable). Blank space between variables is not allowed. For example, (string one is not valid, string_one is a valid variable). Keywords are not allowed to define as a variable. For example, (for is not a valid variable as it is used as a keyword in C language).

C Programming/Variables, manipulate. Each variable in C has a specific type, which determines the size and. Variables can be initialized (assigned an initial value) in their declaration. Multiple variables can be initialized in a single statement by single value, for example, a=b=c=d=e=10; NOTE: C variables must be declared before they are used in the c program. Also, since c is a case-sensitive programming language, therefore the c variables, abc, Abc and ABC are all different.

C - Variables, In computer programming, initialization (or initialisation) is the assignment of an initial value for a data object or variable. In C/C99/C++, an initializer is an optional part of a declarator. It consists of the '=' character followed by an expression or� Multiple variables can be initialized in a single statement by single value, for example, a=b=c=d=e=10; NOTE: C variables must be declared before they are used in the c program. Also, since c is a case sensitive programming language, therefore the c variables, abc, Abc and ABC are all different. Constant and Volatile Variables

Initialization (programming), In C#, types are either reference or value type. For further information on this, please see this article. Variables can either be initialized in the� Static and global variables will be initialized to zero for you so you may skip initialization. Automatic variables (e.g. non-static variables defined in function body) may contain garbage and should probably always be initialized. If there is a non-zero specific value you need at initialization then you should always initialize explicitly.

Comments
  • Do learn constructors.
  • I recommend reading about classes and how to structure your data. Just any beginner's C++ book will do. Btw - C++ and Python are only OO if you want them to (and you most probably should).
  • I do intend to learn about constructors - I just learn better by working on projects, and learning about those things where I get to a point in the project that would benefit by knowing about those things. So are constructors the thing I want to use here? Or are they just something generally useful to know, but probably not the specific thing I'm looking for here?
  • You need ctors all over the place.
  • Even if it turns out that constructors aren't the solution to your problem, learn constructors. They are almost immediately useful.
  • Your first applies if the horde of variables the asker is struggling with are all related in some logical way. Otherwise you wind up with a God Object. You may wish to expand on this point to cover some of the larger ideologies behind class design.
  • The asker is obviously a beginner, I didn't want to loose him with SOLID principles... but you are right I could expand a little my answer. Thank you