Abdullah  Kozey

Abdullah Kozey

1624706100

Understanding correctly that messy keyword ‘const’​ in C++ (III)

In C++, the qualifier ‘const’ specifies a compile-time constraint that an object or variable cannot be modified. The compiler verifies that the object or variable never changes and stops you when you try to modify it or re-assign it. However, writing ‘const’ correct code is way more challenging than just using the keyword in some lines, and then allowing the compiler to figure out what makes sense. In this two-part guide, you will get some hints on how to use it properly.

1) Constants defined with the ‘const’  qualifier are best considered as unmodifiable variables with all their properties such as type, size, linkage, and address. Macros, on the other hand, have fewer restrictions because they can expand to any series of tokens (numbers, characters, or strings), but they do not have any of the properties of a variable. Because some macros are defined to expand to numeric literals , they are sometimes thought of as constants, but they’re not. It is the compiler who sees them as _numeric literals, _ despite the fact it does not perform a type-check.

2)** Constant objects are subject to the scoping rules** for all other identifiers, whereas macros are not. Consider the following code:

#define READ_ONLY_VALUE 5

const int read_only_value = 5;
void foo ()
{
    // Only for education purposes. Do not try this at home, kids.
    #define READ_ONLY_VALUE 6 

    const int read_only_value = 6;
    static const int static_read_only_value = 7;
    std::cout<<read_only_value<<std::endl;  
}

It may be seen as obvious that read_only_value is defined twice in this snippet, one time as a global constant variable and another as a local one. When the foo() function is called, it prints the local constant variable with the value of ‘6’. In contrast, READ_ONLY_VALUE is redefined inside the foo() function (which is a bad idea), and that is allowed by the compiler with a warning. That means, the macro just ignores the scope and the symbolic name will be replaced in the object code with the value of ‘6’.

3) Some macros are defined to look like a function call. They are named (no surprise here) function-like macros. This surplus of functionality is in direct contrast to how a constant variable works:

#define MIN(a, b)  ((a) < (b) ? (a) : (b))

Since macros are always expanded by the preprocessor at the pre-compile time, they do not have the overhead of a function call. However, this also the reason why they can not be used for debugging. As mentioned before, they do not follow all the protocols of type-check safety, and because of that, they are more error-prone. The example below portrays the disadvantage of the lack of type checking.

#programming #coding #cpp #software-development #c++ #c++ (iii)

What is GEEK

Buddha Community

Understanding correctly that messy keyword ‘const’​ in C++ (III)
Abdullah  Kozey

Abdullah Kozey

1624706100

Understanding correctly that messy keyword ‘const’​ in C++ (III)

In C++, the qualifier ‘const’ specifies a compile-time constraint that an object or variable cannot be modified. The compiler verifies that the object or variable never changes and stops you when you try to modify it or re-assign it. However, writing ‘const’ correct code is way more challenging than just using the keyword in some lines, and then allowing the compiler to figure out what makes sense. In this two-part guide, you will get some hints on how to use it properly.

1) Constants defined with the ‘const’  qualifier are best considered as unmodifiable variables with all their properties such as type, size, linkage, and address. Macros, on the other hand, have fewer restrictions because they can expand to any series of tokens (numbers, characters, or strings), but they do not have any of the properties of a variable. Because some macros are defined to expand to numeric literals , they are sometimes thought of as constants, but they’re not. It is the compiler who sees them as _numeric literals, _ despite the fact it does not perform a type-check.

2)** Constant objects are subject to the scoping rules** for all other identifiers, whereas macros are not. Consider the following code:

#define READ_ONLY_VALUE 5

const int read_only_value = 5;
void foo ()
{
    // Only for education purposes. Do not try this at home, kids.
    #define READ_ONLY_VALUE 6 

    const int read_only_value = 6;
    static const int static_read_only_value = 7;
    std::cout<<read_only_value<<std::endl;  
}

It may be seen as obvious that read_only_value is defined twice in this snippet, one time as a global constant variable and another as a local one. When the foo() function is called, it prints the local constant variable with the value of ‘6’. In contrast, READ_ONLY_VALUE is redefined inside the foo() function (which is a bad idea), and that is allowed by the compiler with a warning. That means, the macro just ignores the scope and the symbolic name will be replaced in the object code with the value of ‘6’.

3) Some macros are defined to look like a function call. They are named (no surprise here) function-like macros. This surplus of functionality is in direct contrast to how a constant variable works:

#define MIN(a, b)  ((a) < (b) ? (a) : (b))

Since macros are always expanded by the preprocessor at the pre-compile time, they do not have the overhead of a function call. However, this also the reason why they can not be used for debugging. As mentioned before, they do not follow all the protocols of type-check safety, and because of that, they are more error-prone. The example below portrays the disadvantage of the lack of type checking.

#programming #coding #cpp #software-development #c++ #c++ (iii)

Abdullah  Kozey

Abdullah Kozey

1624123680

Understanding correctly that messy keyword ‘const’​ in C++ (II)

Part two: constant methods.

In C++, the qualifier ‘const’ specifies a compile-time constraint that an object or variable cannot be modified. The compiler verifies that the object or variable never changes and stops you when you try to modify it or re-assign it. However, writing ‘const’ correct code is way more challenging than just using the keyword in some lines, and then allowing the compiler to figure out what makes sense. In this three-part guide, you will get some hints on how to use it properly.

In the previous article, we saw the different usages of the keyword ‘const’ applied to types. We also observed the interaction of this identifier with pointers and had some examples of which are allowed by the compiler and which are not.

In this second part, we are going to analyze how the keyword ‘const’ is used within the methods of a class. This time we are going to define a class, and then we are going to apply ‘const’ correctness where it is needed.

Here is our relatively simple class. Do not worry, we are going to dissect it step by step:

typedef int myInt;

class AnObject
{ 
    public:
        AnObject(){};
        void printId () { std::cout<<id_<<std::endl;}
        std::string getName () { return name_; }
        int getNumber ()
        {
            if (number_ < 0)
            resetNumber();
            return number_;    
        }

        void resetNumber () { number_ = 0; }
        void helperResetNumber () { resetNumber(); }

        void setNumber (int &number) { number_= number; }
        void setName (std::string &name ) { name_= name; }
        void setId (const myInt id) { id_ = id;}
        //The Most Constant Method
        const int *const TMCMethod (const int *const& value) const
    private:

        std::string name_{""};
        myInt id_;
        int number_;
};

The first two lines that we are going to analyze are the following:

void printId () { std::cout<<id_<<std::endl;}
std::string getName () { return name_; }

As you can see, these member functions are not changing the internal state of the object, therefore they should be constant. This is done by adding the identifier ‘const’ after the parenthesis:

void printId () const { std::cout<<id_<<std::endl;}
std::string getName () const { return name_; }

In general, all functions that only retrieve and/or display information (e.g “getters’) are better defined as constants to make their purpose more clear and readable.

#software-engineering #cpp #coding #programming #‘const’​ in c++ (ii)

Tamale  Moses

Tamale Moses

1624240146

How to Run C/C++ in Sublime Text?

C and C++ are the most powerful programming language in the world. Most of the super fast and complex libraries and algorithms are written in C or C++. Most powerful Kernel programs are also written in C. So, there is no way to skip it.

In programming competitions, most programmers prefer to write code in C or C++. Tourist is considered the worlds top programming contestant of all ages who write code in C++.

During programming competitions, programmers prefer to use a lightweight editor to focus on coding and algorithm designing. VimSublime Text, and Notepad++ are the most common editors for us. Apart from the competition, many software developers and professionals love to use Sublime Text just because of its flexibility.

I have discussed the steps we need to complete in this blog post before running a C/C++ code in Sublime Text. We will take the inputs from an input file and print outputs to an output file without using freopen file related functions in C/C++.

#cpp #c #c-programming #sublimetext #c++ #c/c++

Dicey Issues in C/C++

If you are familiar with C/C++then you must have come across some unusual things and if you haven’t, then you are about to. The below codes are checked twice before adding, so feel free to share this article with your friends. The following displays some of the issues:

  1. Using multiple variables in the print function
  2. Comparing Signed integer with unsigned integer
  3. Putting a semicolon at the end of the loop statement
  4. C preprocessor doesn’t need a semicolon
  5. Size of the string matters
  6. Macros and equations aren’t good friends
  7. Never compare Floating data type with double data type
  8. Arrays have a boundary
  9. Character constants are different from string literals
  10. Difference between single(=) and double(==) equal signs.

The below code generates no error since a print function can take any number of inputs but creates a mismatch with the variables. The print function is used to display characters, strings, integers, float, octal, and hexadecimal values onto the output screen. The format specifier is used to display the value of a variable.

  1. %d indicates Integer Format Specifier
  2. %f indicates Float Format Specifier
  3. %c indicates Character Format Specifier
  4. %s indicates String Format Specifier
  5. %u indicates Unsigned Integer Format Specifier
  6. %ld indicates Long Int Format Specifier

Image for post


A signed integer is a 32-bit datum that encodes an integer in the range [-2147483648 to 2147483647]. An unsigned integer is a 32-bit datum that encodes a non-negative integer in the range [0 to 4294967295]. The signed integer is represented in twos-complement notation. In the below code the signed integer will be converted to the maximum unsigned integer then compared with the unsigned integer.

Image for post

#problems-with-c #dicey-issues-in-c #c-programming #c++ #c #cplusplus

Var keyword in C# | C# Bangla Tutorial | Advanced C#

https://youtu.be/3oPEER0LdfQ

#oop in c# #object oriented programming #object oriented concept in c# #learn oop concept #advance c# #var keyword in c#