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
C Articles
Page 80 of 96
Incompatibilities between C and C++
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 MoreDifference between fork() and exec() in C
In C, fork() and exec() are fundamental system calls for process management. The fork() function creates a new process by duplicating the calling process, while exec() replaces the current process image with a new program. Syntax #include pid_t fork(void); int exec(const char *path, char *const argv[]); Key Differences Aspect fork() exec() Purpose Creates new process (duplicate) Replaces current process image Process Count Increases by one Remains same Memory Image Copies parent's memory Loads new program Return Value Child PID to parent, 0 to ...
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 Morepthread_equal() in C
The pthread_equal() function is used to check whether two threads are equal or not. This function returns 0 if the threads are different, or a non-zero value if the threads are equal. Syntax int pthread_equal(pthread_t th1, pthread_t th2); Parameters th1 − First thread identifier th2 − Second thread identifier Return Value Returns a non-zero value if threads are equal, otherwise returns 0. Note: To compile and run pthread programs, use: gcc -pthread filename.c -o output Example 1: Comparing Thread with Itself In this example, we ...
Read Morepthread_cancel() in C
The pthread_cancel() function is used to send a cancellation request to a specific thread identified by its thread ID. This function allows one thread to request the termination of another thread in a controlled manner. Note: To compile and run pthread programs, use: gcc -pthread filename.c -o output Syntax int pthread_cancel(pthread_t thread); Parameters thread − The thread ID of the thread to be cancelled Return Value Returns 0 on success Returns an error number on failure Example: Thread Cancellation This example demonstrates how ...
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 MoreInteresting Facts about C Programming
C programming language has several interesting and lesser-known features that can surprise even experienced programmers. Here are some fascinating facts about C that demonstrate its flexibility and unique behaviors. Fact 1: Switch Case Labels Inside If-Else Case labels in a switch statement can be placed inside if-else blocks due to the way switch works with jump labels − #include int main() { int x = 2, y = 2; switch(x) { case 1: ...
Read MoreUse of bool in C
In C, there is no predefined datatype as bool. However, we can create a boolean type using enum or use the standard header (C99 and later). With enum, we create a boolean type where false holds value 0 and true holds value 1. Syntax typedef enum { false, true } bool; // Or using C99 standard #include Method 1: Using Enum We can create a custom boolean type using typedef enum − #include typedef enum { false, true } bool; int main() { ...
Read MoreHow to write a running C code without main()?
In C programming, it is possible to write a program without the traditional main() function. While main() appears to be the entry point from a programmer's perspective, the system actually calls _start() first, which sets up the environment before calling main(). Syntax int _start() { // Program code here _exit(status_code); } Note: To compile programs without main(), use the -nostartfiles flag with gcc to skip the standard startup files that expect a main() function. Example Here's how to create a C program using ...
Read MorePrint a long int in C using putchar() only
Here we will see how to print long int value using the putchar() function in C. We can easily print the value of some variables using printf() in C, but here the restriction is, we cannot use any other function except putchar(). As we know that the putchar() is used to print only characters. We can use this function to print each digit of the number. When one numeric value is passed, we have to add character '0' with it to get the ASCII form. Syntax void print_long(long value); int putchar(int character); Example ...
Read More