C Articles - Page 124 of 134

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

Tapas Kumar Ghosh
Updated on 04-Jun-2025 17:41:51

25K+ Views

We should use "%zu" to print the variables of size_t length. We can use "%d" also to print size_t variables, it may not always produce an error. The correct way to print size_t variables is use of "%zu", because compiler standard %zu is defined as a format specifier for this unsigned type. Following is the description of "%zu" format: z is a length modifier. u stands for an unsigned type. C Example to Print Value of size_t Variable In this example, we demonstrate a C program that correctly prints a ... Read More

What is the difference between g++ and gcc?

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

3K+ 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

10K+ 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++?

Tapas Kumar Ghosh
Updated on 21-May-2025 18:16:42

12K+ Views

Both pre-increment and post-increment are used to represent the expression of increasing a value by adding 1. Their behavior are different because pre-increment (++i) increases the value before it is used in an expression, while post-increment (i++) increases it after. Below is the key-terms of pre-increment and post-increment in C/C++: 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. Syntax of Using Pre and Post Increment Operators The following is the basic syntax of pre-increment and post-increment ... Read More

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

karthikeya Boyini
Updated on 29-Apr-2025 18:59:46

363 Views

Given are the two integer variables, a and b. To calculate the sum without using any arithmetic operators such as + or -. We can achieve this by using bitwise operations, which allow us to calculate the sum. Here, we have some approaches in C and C++ to solve this problem as follows: Sum of Two Integers Using Bitwise XOR Sum of Two Integers Using Vector Sum of Two Integers Using malloc() and free() Sum of Two Integers Using Bitwise XOR (^) To perform the sum ... Read More

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

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

552 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

How to print a variable name in C?

karthikeya Boyini
Updated on 26-Jun-2020 09:00:16

1K+ Views

The following is an example to print variable name.Example Live Demo#include #define VariableName(name) #name int main() {    int name;    char ch;    printf("The variable name : %s", VariableName(name));    printf("The variable name : %s", VariableName(ch));    return 0; }OutputThe variable name : name The variable name : chIn the above program, the variable names are printed by defining the method before main()#define VariableName(name) #nameTwo variables of different datatypes are declared. By using the defined function, variable names are printed.int name; char ch; printf("The variable name : %s", VariableName(name)); printf("The variable name : %s", VariableName(ch));

exit() vs _Exit() in C/C++

Revathi Satya Kondra
Updated on 02-May-2025 13:39:59

760 Views

In C/C++, both exit() and _Exit() are used to terminate a program. The exit() performs cleanup like flushing output, closing files, and calling functions, while _Exit() ends the program immediately without doing any cleanup. Now, let us learn the difference between exit() and _Exit() individually in C/C++. C++ exit() Function The exit() function is used to clean up before terminating the program. It calls functions registered with atexit(), flushes file buffers, and returns control to the operating system. Syntax Following is the syntax for exit() function: void exit(int status_value); Example In this example, we print "Program is running..." and ... Read More

return statement vs exit() in main() C++

karthikeya Boyini
Updated on 23-Sep-2024 18:19:14

2K+ Views

return statement The C++ return statement terminates the execution of function and it returns the control to the calling function. It calls the constructor as well as the destructor. It returns an integer value for “int main()”. The following is the syntax of return statement. return expression; Here, expression − The expression or any value to be returned. The following is an example of return statement. Example #include using namespace std; class Method { public: Method() { cout

Advertisements