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 Nishtha Thakur
Page 6 of 40
Print numbers in sequence using thread synchronization
Thread synchronization allows multiple threads to coordinate and execute in a specific order. In this example, we create n threads where each thread prints its number in sequence − thread 1 prints 1, thread 2 prints 2, and so on. We use mutex locks and condition variables to ensure proper synchronization. Note: To compile and run this program, you need to link with the pthread library: gcc program.c -lpthread -o program Syntax pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t condition_variable; pthread_mutex_lock(&mutex); pthread_cond_wait(&condition_variable, &mutex); pthread_cond_signal(&condition_variable); pthread_mutex_unlock(&mutex); Example: Sequential Number Printing with Threads This program ...
Read MoreHygienic Macros in C
Here we will see the Hygienic Macros in C. We know the usage of macros in C. But sometimes, macros do not return the desired results because of accidental capture of identifiers, where macro variables interfere with variables in the calling code. Syntax #define MACRO_NAME(params) do { /* avoid variable name conflicts */ } while(0) Problem: Non-Hygienic Macro If we see the following code, we can see that it is not working properly because the macro uses variable name 'a' which conflicts with the program's variable − #include #define INCREMENT(i) do ...
Read Morepthread_self() in C
The pthread_self() function in C is used to obtain the ID of the currently executing thread. This function provides a unique identifier for each running thread, allowing programs to distinguish between different threads during execution. Syntax pthread_t pthread_self(void); Parameters None − The function takes no parameters. Return Value Returns a pthread_t value representing the thread ID of the calling thread. Thread IDs are unique among all currently existing threads, but can be reused after a thread terminates. Example The following example demonstrates how pthread_self() works by ...
Read Morenextafter() and nexttoward() in C/C++
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 Morekbhit in C language
The kbhit() function in C checks if a key has been pressed on the keyboard without waiting for the Enter key. It returns a non-zero value if a key is available to be read, otherwise returns zero. However, kbhit() is non-standard and platform-specific (Windows/DOS only). Syntax int kbhit(void); Parameters None: The function takes no parameters. Return Value Returns a non-zero value if a key has been pressed. Returns zero if no key is pressed. Example Note: This example uses Windows-specific headers (conio.h) and will not ...
Read Moreabs(), labs(), llabs() functions in C/C++
The C standard library provides different functions to calculate the absolute value of integers based on their data types. The abs(), labs(), and llabs() functions handle int, long, and long long data types respectively, returning the non-negative value of their arguments. Syntax int abs(int n); long labs(long n); long long llabs(long long n); The abs() Function The abs() function returns the absolute value of an integer argument. It is defined in stdlib.h and works with int type data − Example #include #include int main() { ...
Read Moretrunc() , truncf() , truncl() in C language
In C, the trunc(), truncf(), and truncl() functions are used to truncate floating-point values, removing the fractional part and returning only the integer portion. These functions are part of the math.h library and are available in C99 and later standards. Installation: These functions require C99 or later. Compile with gcc -std=c99 -lm to link the math library. Syntax double trunc(double x); float truncf(float x); long double truncl(long double x); Parameters x − The floating-point value to be truncated Return Value Returns the truncated value (integer part) as ...
Read MoreHow to change the output of printf() in main()?
Here we will see how to change the output of the printf() function from main(). We will define a macro that will modify all printf() statements to use a different value than what is passed to it. We will use the #define macro to accomplish this task. This macro will be defined inside a function, allowing us to control when the printf() behavior changes. By calling the function from main(), we can control exactly when printf() starts behaving differently. Syntax #define printf(format, value) printf(format, replacement_value); Example In this example, we define a macro ...
Read MoreUninitialized primitive data types in C/C++
In C programming, uninitialized primitive data types contain unpredictable values called "garbage values". The C standard does not guarantee that these variables will be initialized to zero or any specific value. The actual values depend on what was previously stored in those memory locations. Syntax data_type variable_name; // Uninitialized variable data_type variable_name = initial_value; // Initialized variable Example: Uninitialized Variables Let's examine what happens when we declare variables without initializing them − #include int main() { char a; float b; ...
Read MoreMacros vs Functions in C
Macros and functions are two different mechanisms in C for code reuse, but they work fundamentally differently. Macros are preprocessed before compilation, meaning they are textually replaced in the code. Functions are compiled as separate code blocks that are called during execution. Syntax #define MACRO_NAME(parameters) replacement_text return_type function_name(parameters) { // function body return value; } Example: Macro vs Function Problem This example demonstrates a common issue where macros and functions produce different results due to the way macros expand − #include ...
Read More