Programming Articles

Page 995 of 2547

The best way to check if a file exists using standard C/C++

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 18K+ Views

In C programming, checking if a file exists is a common task that can be accomplished by attempting to open the file for reading. If the file opens successfully, it exists; otherwise, it doesn't exist. Syntax FILE *fopen(const char *filename, const char *mode); Method 1: Using fopen() Function The most straightforward approach is to use fopen() to attempt opening the file in read mode − #include int main() { FILE *file; /* Try to open file for ...

Read More

How to write my own header file in C?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 2K+ Views

Creating your own header file in C allows you to organize and reuse code across multiple programs. A header file typically contains function declarations, constants, and macros that can be included in other C files. Syntax #include "your_header_file.h" Steps to Create a Custom Header File Create the header file: Write your functions and save with .h extension Include in main program: Use #include "filename.h" (quotes, not angle brackets) Ensure same directory: Both files must be in the same folder Call functions: Use the functions directly in your main program Header File: ...

Read More

How to use use an array of pointers (Jagged) in C/C++?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 699 Views

In C programming, an array of pointers (also known as jagged arrays) allows you to store multiple pointers in a single array. This is particularly useful when working with strings of different lengths or when you need to point to different memory locations dynamically. Syntax data_type *array_name[size]; Where data_type is the type of data the pointers will point to, and size is the number of pointers the array can hold. Example 1: Array of Integer Pointers The following example demonstrates how to create an array of pointers that point to integer values − ...

Read More

Write a program that produces different results in C and C++

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 193 Views

Here we will see some programs that produce different results when compiled with C and C++ compilers. These differences arise from how C and C++ handle certain language features differently. Difference 1: Character Literal Size In C, character literals are treated as int type, while in C++, they are treated as char type. This affects the result of the sizeof() operator − Example: C Compiler #include int main() { printf("The character: %c, size(%d)", 'a', sizeof('a')); return 0; } The character: a, size(4) ...

Read More

C program that won't compile in C++

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 593 Views

C++ is largely based on C, but it enforces stricter type checking and syntax rules. While most C programs compile in C++, some valid C code will not compile in C++ due to these stricter rules. Understanding these differences helps in writing portable code. Syntax There is no specific syntax for this concept, but rather a collection of C language features that C++ treats differently or rejects entirely. Example 1: Function Declaration Requirements In C, functions can be called before declaration (with implicit declaration), but C++ requires explicit declaration − #include int ...

Read More

Floating Point Operations and Associativity in C, C++ and Java

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 372 Views

In C, mathematical operations with floating point numbers do not always follow the associativity rule. This means that (a + b) + c may not equal a + (b + c) due to precision limitations and rounding errors in floating-point arithmetic. Syntax float result1 = a + (b + c); /* Right associativity */ float result2 = (a + b) + c; /* Left associativity */ Example: Floating Point Associativity Issue Here's an example demonstrating how floating point operations violate associativity − #include int ...

Read More

Undefined Behaviour in C and C++

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 15-Mar-2026 523 Views

In C programming, undefined behavior refers to situations where the language standard does not specify what should happen. When undefined behavior occurs, the program may produce unpredictable results, crash, or appear to work correctly but fail later under different conditions. Syntax /* Undefined behavior occurs when code violates C language rules */ /* Examples: dereferencing NULL pointers, buffer overflows, etc. */ Common Examples of Undefined Behavior in C Division by Zero Using Uninitialized Variables Dereferencing NULL Pointers Signed Integer Overflow Modifying String Literals 1. Division by Zero Division by zero ...

Read More

How to check if a variable is NULL in C/C++?

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 15-Mar-2026 27K+ Views

In C, there is no special method for comparing NULL values. We can use if statements to check whether a variable is null or not. NULL is a macro that represents a null pointer constant, typically defined as (void*)0. Syntax if (pointer == NULL) { /* pointer is null */ } if (pointer != NULL) { /* pointer is not null */ } Example 1: Checking File Pointer for NULL Here, we attempt to open a file in read mode that does not exist on ...

Read More

Designated Initializers in C

Arjun Thakur
Arjun Thakur
Updated on 15-Mar-2026 1K+ Views

In C90 standard we have to initialize the arrays in the fixed order, like initialize index at position 0, 1, 2 and so on. From C99 standard, they have introduced designated initializing feature in C. Here we can initialize elements in random order. Initialization can be done using the array index or structure members. This extension is not implemented in GNU C++. Syntax // Array designated initialization type array[size] = {[index] = value, [index] = value, ...}; // Range designated initialization type array[size] = {[first ... last] = value}; // Structure designated initialization struct name ...

Read More

Name Mangling and extern "C" in C++

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 1K+ Views

In C++, function overloading allows multiple functions with the same name but different parameter types or numbers. However, this creates a problem: how does the compiler distinguish between these functions in the object code? The solution is a technique called name mangling. Syntax extern "C" { // C function declarations } What is Name Mangling? Name mangling is a technique where the compiler modifies function names by adding information about parameters to create unique identifiers. C++ has no standardized name mangling scheme, so different compilers use different approaches. Example: ...

Read More
Showing 9941–9950 of 25,466 articles
« Prev 1 993 994 995 996 997 2547 Next »
Advertisements