Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C Articles - Page 108 of 134
581 Views
Here we will see the Hygienic Macros in C. We know the usage of macros in C. But sometimes, it does not return the desired results because of accidental capture of identifiers.If we see the following code, we can see that it is not working properly.Example#include #define INCREMENT(i) do { int a = 0; ++i; } while(0) main(void) { int a = 10, b = 20; //Call the macros two times for a and b INCREMENT(a); INCREMENT(b); printf("a = %d, b = %d", a, b); }After preprocessing the code will be like this −Example#include #define ... Read More
171 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 also returns error.We can define function using a syntax, that optionally specify the argument types after the argument list.Example#include void my_function(x, y)int x;int y; { // Not valid in C++ printf("x = %d, y = %d", x, y); } int main() { my_function(10, 20); }Outputx = 10, y = 20OutputError in C++ :- x and y was not declared in this scopeIn C, or some older version of C++, the ... Read More
4K+ Views
Here we will see the effect of fork() and exec() system call in C. The fork is used to create a new process by duplicating the calling process. The new process is the child process. See the following property.The child process has its own unique process id.The parent process id of the child process is same as the process id of the calling process.The child process does not inherit the parent’s memory lock and semaphores.The fork() returns the PID of the child process. If the value is non-zero, then it is parent process’s id, and if this is 0, then ... Read More
9K+ Views
Here we will see what will be the effect of pthread_self() in C. The pthread_self() function is used to get the ID of the current thread. This function can uniquely identify the existing threads. But if there are multiple threads, and one thread is completed, then that id can be reused. So for all running threads, the ids are unique.Example#include #include #include void* func(void* p) { printf("From the function, the thread id = %d", pthread_self()); //get current thread id pthread_exit(NULL); return NULL; } main() { pthread_t thread; // declare thread ... Read More
302 Views
The pthread_equal() function is used to check whether two threads are equal or not. This returns 0 or non-zero value. For equal threads, it will return non-zero, otherwise it returns 0. The syntax of this function is like below −int pthread_equal (pthread_t th1, pthread_t th2);Now let us see the pthread_equal() in action. In the first case, we will check the self-thread to check the result.Example#include #include #include #include #include pthread_t sample_thread; void* my_thread_function(void* p) { if (pthread_equal(sample_thread, pthread_self())) { //pthread_self will return current thread id printf("Threads are equal"); } else ... Read More
729 Views
The threa_cancel() is used to cancel one particular thread by the thread id. This function sends one cancellation request to the thread for termination. The syntax of the pthread_cancel() is like below −int pthread_cancel(pthread_t th);Now, let us see how to cancel threads using this function.Example#include #include #include #include int count = 0; pthread_t sample_thread; void* thread_one_func(void* p) { while (1) { printf("This is thread 1"); sleep(1); // wait for 1 seconds count++; if (count == 5) { //if the ... Read More
280 Views
Here we will see the effect of nextafter() and nextforward() functions in C or C++. These functions are present in the math.h or cmath library.if the functions are like nextafter(a, b) and nextforward(a, b). These functions are used to find the next representable value after a in the direction of b. The nextforward() has more precise second parameter b.Example#include #include main () { //The nextafter function() printf ("Smallest representable number after 0 towards 1 : %e", nextafter(0.0, 1.0)); printf ("Largest representable number before -1 towards 0 :%e", nextafter(0.0, -1.0)); printf ("Largest +ve representable number ... Read More
423 Views
Here we will see some interesting facts about C programming. These are like below.Sometimes the case labels of some switch statement can be placed inside if-else statement.Example#include main() { int x = 2, y = 2; switch(x) { case 1: ; if (y==5) { case 2: printf("Hello World"); } else case 3: { //case 3 block ... Read More
2K+ Views
In C there is no predefined datatype as bool. We can create bool using enum. One enum will be created as bool, then put the false, and true as the element of the enum. The false will be at the first position, so it will hold 0, and true will be at second position, so it will get value 1. Now we can use this as datatype.Example#include typedef enum { F, T } boolean; main() { boolean my_bool1, my_bool2; my_bool1 = F; if(my_bool1 == F) { printf("my_bool1 is false"); } else { ... Read More
3K+ Views
Here we will see how to implement memcpy() function in C. The memcpy() function is used to copy a block of data from one location to another. The syntax of the memcpy() is like below −void * memcpy(void * dest, const void * srd, size_t num);To make our own memcpy, we have to typecast the given address to char*, then copy data from source to destination byte by byte. Just go through the following code to get better idea.Example#include #include void custom_memcpy(void *dest, void *src, size_t n) { int i; //cast src and dest to char* char ... Read More