Found 7346 Articles for C++

Why are global variables bad in C/C++?

George John
Updated on 25-Jun-2020 09:52:17

2K+ Views

Global variables are declared and defined outside any function in the program. They hold their values throughout the lifetime of program. They are accessible throughout the execution of program.Non-const global variables are evil because their value can be changed by any function. Using global variables reduces the modularity and flexibility of the program. It is suggested not to use global variables in the program. Instead of using global variables, use local variables in the program.Use ‘g_’ as prefix of the variable name to avoid the naming collisions and for knowledge that variable is global. There is another way also that ... Read More

What are the new changes introduced in C++11?

Arjun Thakur
Updated on 30-Jul-2019 22:30:23

122 Views

C++11 is a version of standard C++ language. It was approved by International Organization for Standardization (ISO) on 12 August 2011 then C++14 and C++17. C++11 makes several additions to the core language. There are some of the new changes introduced in C++11 − nullptr − In the previous nullptr, zero used to be the value and it had a drawback of implicit conversion to integral value. The null pointer literal is represented by std::nullptr_t. In this nullptr, no implicit conversion exists. Lambdas − The lambda expression allows to define functions locally. Anonymous functions are known as lambda. We ... Read More

How to print out the contents of a vector in C++?

Chandu yadav
Updated on 12-Sep-2023 01:26:36

32K+ Views

Vectors are similar to the dynamic arrays but vectors can resize. Vectors are sequence containers that can change their size according to the insertion or deletion of elements. Containers are the objects which holds the data of same type.Vectors may allocate some extra storage for the future growth of elements in the vector. Vector elements are stored in the contiguous memory. The data is entered at the end of vector.Here is an example to print the contents of a vector in C++ language,Example Live Demo#include #include void print(std::vector const &a) {    std::cout

Do you think operator < is faster than <= in C/C++?

Arjun Thakur
Updated on 25-Jun-2020 09:45:25

950 Views

No, the operator < takes same time to execute as operator

List of C++ IDEs for Linux

Ankith Reddy
Updated on 30-Jul-2019 22:30:23

223 Views

The following are some of C++ IDEs for linux − Eclipse Galileo with CDT Plugin Eclipse is a well-known open source and cross platform IDE. It provides full functional C/C++ IDE with the following features − Code editor with support for syntax highlighting Support for folding and hyperlink navigation Source code refactoring plus code generation Tools for visual debugging such as memory, registers etc. NetBeans IDE NetBeans is free, open source and popular IDE for C/C++. These are some of its features − Support for automatic packaging of compiled application into .tar, .zip and many more archive ... Read More

How to get current time and date in C++?

George John
Updated on 25-Jun-2020 09:46:16

788 Views

Here is an example to get current date and time in C++ language,Example Live Demo#include using namespace std; int main() {    time_t now = time(0);    char *date = ctime(& now);    cout

What is the difference between printf() and cout in C++?

Chandu yadav
Updated on 25-Jun-2020 09:47:49

2K+ Views

printf()This is mainly used in C language. It is a formatting function that prints to the standard out. It prints to the console and takes a format specifier to print. It returns an integer value. It is not type safe in input parameters. It can be used in C++ language too.Here is the syntax of printf() in C and C++ language, printf(“string and format specifier”, variable_name);Here, String − Any text/message to print on console.Format Specifier  − According to the variable datatype, use format specifiers like %d, %s etc.variable_name  − Any name given to declare the variable.Here is an example of ... Read More

How do I convert a char to an int in C and C++?

Arjun Thakur
Updated on 15-Sep-2023 02:26:23

22K+ Views

In C language, there are three methods to convert a char type variable to an int. These are given as follows −sscanf()atoi()TypecastingHere is an example of converting char to int in C language, Example Live Demo#include #include int main() {    const char *str = "12345";    char c = 's';    int x, y, z;    sscanf(str, "%d", &x); // Using sscanf    printf("The value of x : %d", x);    y = atoi(str); // Using atoi()    printf("The value of y : %d", y);    z = (int)(c); // Using typecasting    printf("The value of z ... Read More

#pragma Directive in C/C++

Ankith Reddy
Updated on 25-Jun-2020 09:50:00

3K+ Views

The preprocessor directive #pragma is used to provide the additional information to the compiler in C/C++ language. This is used by the compiler to provide some special features.Here is the syntax of #pragma directive in C/C++ language, #pragma token_nameThe table of some of #pragma directives in C/C++ language is given as follows, Sr.No.#pragma Directives & Description1#pragma startupBefore the execution of main(), the function specified in pragma is needed to run.2#pragma exitBefore the end of program, the function specified in pragma is needed to run.3#pragma warnUsed to hide the warning messages.4#pragma GCC dependencyChecks the dates of current and other file. If ... Read More

Pass an array by value in C

Arjun Thakur
Updated on 25-Jun-2020 09:51:32

654 Views

Here is an example of passing array by value in C language, Example Live Demo#include float avg(float a[]) {    int i;    float avg, sum = 0.0;    for (i = 0; i < 6; ++i) {       sum += a[i];    }    avg = (sum / 6);    return avg; } int main() {    float avg1, a[] = {63, 21, 34.4, 12.5, 3, 2.2};    avg1 = avg(a);    printf("Average : %f", avg1);    return 0; }OutputHere is the outputAverage : 22.683332In the above program, The actual code of calculating average is ... Read More

Advertisements