Found 1401 Articles for C

When to use references vs. pointers in C/C++

karthikeya Boyini
Updated on 26-Jun-2020 09:35:51

407 Views

Reference variableReference variable is an alternate name of already existed variable. It cannot be changed to refer another variable and should be initialized at the time of declaration. It cannot be NULL. The operator ‘&’ is used to declare a reference variable.The following is the syntax of reference variable.datatype variable_name; // variable declaration datatype& refer_var = variable_name; // reference variableHere, datatype − The datatype of variable like int, char, float etc.variable_name − This is the name of variable given by user.refer_var − The name of reference variable.The following is an example of reference variable.Example Live Demo#include using namespace std; int ... Read More

When to use extern in C/C++

Samual Sam
Updated on 26-Jun-2020 09:18:57

10K+ Views

External variables are also known as global variables. These variables are defined outside the function and are available globally throughout the function execution. The “extern” keyword is used to declare and define the external variables.The keyword [ extern “C” ] is used to declare functions in C++ which is implemented and compiled in C language. It uses C libraries in C++ language.The following is the syntax of extern.extern datatype variable_name; // variable declaration using extern extern datatype func_name(); // function declaration using externHere, datatype − The datatype of variable like int, char, float etc.variable_name − This is the name of ... Read More

What is the correct way to use printf to print a size_t in C/C++?

karthikeya Boyini
Updated on 26-Jun-2020 09:20:48

20K+ Views

We should use “%zu” to print the variables of size_t length. We can use “%d” also to print size_t variables, it will not show any error. The correct way to print size_t variables is use of “%zu”.In “%zu” format, z is a length modifier and u stand for unsigned type.The following is an example to print size_t variable.Example Live Demo#include int main() {    size_t a = 20;    printf("The value of a : %zu", a);    return 0; }OutputThe value of a : 20In the above program, a variable of size_t length is declared and initialized with a value.size_t ... Read More

What is the difference between g++ and gcc?

Samual Sam
Updated on 26-Jun-2020 09:21:24

2K+ Views

g++GNU C++ Compiler ( g++ ) is a compiler in Linux which is used to compile C++ programs. It compiles both files with extension .c and .cpp as C++ files.The following is the compiler command to compile C++ program.g++ program.cpp -o filenameHere,filename − The name of file with .c or .cpp extension.The following is an example of using g++ compiler.Example Live Demo#include using namespace std; int main() {    int a = 20;    cout

Where are static variables stored in C/C++?

karthikeya Boyini
Updated on 26-Jun-2020 09:23:06

9K+ Views

Static variables are variables that remain in memory while the program is running i.e. their lifetime is the entire program run. This is different than automatic variables as they remain in memory only when their function is running and are destroyed when the function is over.The static variables are stored in the data segment of the memory. The data segment is a part of the virtual address space of a program.All the static variables that do not have an explicit initialization or are initialized to zero are stored in the uninitialized data segment( also known as the BSS segment). Compared ... Read More

Pre-increment and Post-increment concept in C/C++?

Samual Sam
Updated on 26-Jun-2020 09:23:37

11K+ Views

Increment operators are used to increase the value by one while decrement works opposite increment. Decrement operator decrease the value by one.Pre-increment (++i) − Before assigning the value to the variable, the value is incremented by one.Post-increment (i++) − After assigning the value to the variable, the value is incremented.The following is the syntax of pre and post increment.++variable_name; // Pre-increment variable_name++; // Post-incrementHere,variable_name − Any name of the variable given by user.Here is an example of pre and post increment in C++.Example Live Demo#include using namespace std; int main() {    int i = 5;    cout

How to sum two integers without using arithmetic operators in C/C++?

karthikeya Boyini
Updated on 26-Jun-2020 09:27:33

170 Views

The following is an example to add two numbers without using arithmetic operators.Example Live Demo#include #include using namespace std; int add(int val1, int val2) {    while(val2 != 0) {       int c = val1 & val2;       val1 = val1 ^ val2;       val2 = c

Why are global and static variables initialized to their default values in C/C++?

karthikeya Boyini
Updated on 26-Jun-2020 09:11:54

3K+ Views

Global and static variables are initialized to their default values because it is in the C or C++ standards and it is free to assign a value by zero at compile time. Both static and global variable behave same to the generated object code. These variables are allocated in .bss file and at the time of loading it allocates the memory by getting the constants alloted to the variables.The following is an example of global and static variables.Example Live Demo#include int a; static int b; int main() {    int x;    static int y;    int z = 28; ... Read More

Swapping two variable value without using third variable in C/C++

Samual Sam
Updated on 26-Jun-2020 09:12:20

438 Views

The following is an example of swapping two variables.Example Live Demo#include int main() {    int a, b;    printf("Enter the value of a : ");    scanf("%d", &a);    printf("Enter the value of b : ");    scanf("%d", &b);    a += b -= a = b - a;    printf("After Swapping : %d\t%d", a, b);    return 0; }OutputEnter the value of a : 23 Enter the value of b : 43 After Swapping : 4323In the above program, two variables a and b are declared and initialized dynamically at run time.int a, b; printf("Enter the value of ... Read More

Initialization of variable sized arrays in C

karthikeya Boyini
Updated on 26-Jun-2020 08:59:10

7K+ Views

Variable sized arrays are data structures whose length is determined at runtime rather than compile time. These arrays are useful in simplifying numerical algorithm programming. The C99 is a C programming standard that allows variable sized arrays.A program that demonstrates variable sized arrays in C is given as follows −Example Live Demo#include int main(){    int n;    printf("Enter the size of the array: ");    scanf("%d", &n);    int arr[n];    for(int i=0; i

Advertisements