Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Tapas Kumar Ghosh
Page 11 of 19
Undefined Behaviour in C and C++
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 MoreHow to check if a variable is NULL in C/C++?
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 MorePrint \"Hello World\" in C/C++ without using header files
In C/C++, we typically use header files to access standard library functions. The printf() function is declared in the "stdio.h" header file and is used to print data to the console. However, it's possible to print "Hello World" without including any header files by declaring the function prototype ourselves. Syntax int printf(const char *format, ...); Method 1: Using printf() Declaration in C In C, we can declare the printf() function prototype directly in our program without including stdio.h. The compiler will link to the standard library automatically − int printf(const char *format, ...
Read MoreWhen to use extern in C/C++
In C, the extern keyword is used to declare a variable or function that is defined elsewhere in the program. It tells the compiler that the variable or function exists, but its actual definition (memory allocation) is in another file or later in the same file. Syntax // Variable declaration using extern extern datatype variable_name; // Function declaration using extern (optional) extern return_type function_name(parameters); Parameters: datatype − The data type of the variable (int, char, float, etc.) variable_name − Name of the variable to be declared return_type − Return type of the ...
Read MoreWhat is the correct way to use printf to print a size_t in C/C++?
In C programming, size_t is an unsigned integer type used to represent the size of objects and is commonly returned by functions like sizeof and strlen. To correctly print size_t variables, we should use the %zu format specifier instead of %d or other format specifiers. Syntax printf("%zu", size_t_variable); The %zu format specifier consists of − z − length modifier that specifies the argument corresponds to a size_t type u − conversion specifier for unsigned decimal integer Example 1: Basic size_t Printing This ...
Read MorePre-increment and Post-increment concept in C/C++?
Both pre-increment and post-increment are used to increase a variable's value by 1, but they behave differently in expressions. Pre-increment (++i) increases the value before it is used, while post-increment (i++) increases it after the current expression is evaluated. Syntax // Pre-increment ++variable_name; // Post-increment variable_name++; Key Differences: Pre-increment (++i) − Increments the value first, then uses the new value in the expression Post-increment (i++) − Uses the current value in the expression, then increments it Pre-Increment Operator (++x) The pre-increment operator increments the variable's value by ...
Read Moreexit(), abort() and assert() in C/C++
In C programming, the exit(), abort(), and assert() functions are used for program termination and debugging. Each function serves a different purpose and is defined in different header files. The exit() Function The exit() function is used to terminate a program immediately in a normal way. It is defined in the header file and allows the program to return a status code to the operating system. Syntax void exit(int status_value); Parameters: status_value − The termination status code (0 indicates success, non-zero indicates error) Example In this example, the ...
Read MoreSwap two variables in one line in C/C+
In C programming, swapping two variables means exchanging the values stored in them. There are multiple ways to implement swapping using a single line statement. This article demonstrates various approaches to swap variable values efficiently. Syntax // General concept variable1 = new_value_of_variable2; variable2 = new_value_of_variable1; Example Scenario Let's consider the following input and expected output − Input: int a = 5; int b = 10; Output: a = 10 b = 5 Method 1: Using Arithmetic Operations This method uses arithmetic operations to swap variables without temporary storage. ...
Read MoreWhat are Wild Pointers in C/C++?
In C, a wild pointer is a pointer that has not been initialized to a valid memory address. When a pointer is declared but not assigned a value, it contains garbage data and points to an unknown memory location, making it unpredictable and dangerous to use. Syntax data_type *pointer_name; // Uninitialized pointer (wild pointer) Example: Wild Pointer Behavior Here's an example demonstrating a wild pointer that attempts to access uninitialized memory − #include int main() { int *ptr; // Wild pointer - ...
Read MoreHow will you print numbers from 1 to 100 without using loop in C?
You can print numbers from 1 to 100 without using traditional loops by employing alternative control structures like recursive functions and goto statements. These methods achieve the same repetitive behavior through different mechanisms. Method 1: Using Recursion Recursion allows a function to call itself repeatedly until a base condition is met. We can use this property to print numbers sequentially without explicit loops − #include void printNumbers(int n) { if (n > 100) return; printf("%d ", n); ...
Read More