C++ Articles

Page 10 of 597

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

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 195 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 527 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

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

What does it mean when a numeric constant in C/C++ is prefixed with a 0?

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

When a numeric constant in C is prefixed with a 0, it indicates that the number is an octal number (base 8). Octal literals must start with 0 to distinguish them from decimal numbers. For example, the octal number 25 is written as 025 in C. Syntax 0octal_digits Where octal_digits are valid octal digits (0-7). Example: Octal Number Conversion The following program demonstrates how octal literals are converted to decimal values − #include int main() { int a = 025; /* Octal ...

Read More

Incompatibilities between C and C++

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

Here we will see some incompatibilities between C and C++. Some C codes that can be compiled using C compiler, but does not compile in C++ compiler and returns errors. Syntax // C allows various syntaxes that C++ rejects // Old-style function declarations, implicit int, multiple declarations, etc. Example 1: Old-Style Function Declarations C allows defining functions with parameter types specified after the parameter list − #include void my_function(x, y) int x; int y; { printf("x = %d, y = %d", x, y); } ...

Read More

nextafter() and nexttoward() in C/C++

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

The nextafter() and nexttoward() functions in C are used to find the next representable floating-point value after a given number in a specified direction. These functions are part of the math.h library and are useful for precise floating-point arithmetic operations. Syntax double nextafter(double x, double y); float nextafterf(float x, float y); long double nextafterl(long double x, long double y); double nexttoward(double x, long double y); float nexttowardf(float x, long double y); long double nexttowardl(long double x, long double y); Parameters x − The starting value y − The direction value. The function ...

Read More

Difference between "int main()" and "int main(void)" in C/C++?

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 3K+ Views

In C programming, you might notice two different ways to declare the main function: int main() and int main(void). While both are valid, there is a subtle but important difference in how C treats them. Syntax int main() int main(void) Key Difference In C, int main() means the function can accept any number of arguments, while int main(void) explicitly specifies that the function takes no arguments. In C++, both forms are equivalent and mean "no arguments". Example 1: Function Without void When a function is declared without void, C allows it to ...

Read More
Showing 91–100 of 5,962 articles
« Prev 1 8 9 10 11 12 597 Next »
Advertisements