Found 33676 Articles for Programming

Program for Christmas Tree in C

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

1K+ Views

Here we will see one interesting problem. In this problem, we will see how to print Christmas tree randomly. So the tree will be flickering like Christmas tree lights.To print a Christmas tree, we will print pyramids of various sizes just one beneath another. For the decorative leaves a random character is printed from a given list of characters. The height and randomness is adjustable.Here after generating a tree, the whole screen is cleared, then generate again, that’s why this is looking like flickering tree.Example#include #include #include #include #define REFRESH_RATE 40000 #define RANDOM_NESS 5 // The ... Read More

Why strict aliasing is required in C?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

207 Views

Here we will see, why we should use the strict aliasing in C. Before discussing that part, let us see one code, and try to analyze the output.Example#include int temp = 5; int* var = &temp; int my_function(double* var) {    temp = 1;    *var = 5.10; //this will change the value of temp    return (temp); } main() {    printf("%d",  my_function((double*)&temp)); }Output1717986918If we call the function my_function, then it will return 1. We can also call this using my_function((double*)&temp). This is supposed to return 1, but here we can see that this is returning something else. This code was made to return constant 1 only. To fix this problem, we can use Strict ... Read More

Linear search using Multi-threading in C

Anvi Jain
Updated on 30-Jul-2019 22:30:26

912 Views

Here we will see how to apply the multi-threading concept to search one element in an array. Here the approach is very simple. We will create some threads, then divide the array into different parts. Different thread will search in different parts. After that, when the element is found, enable the flag to identify this.Example#include #include #define MAX 16 #define THREAD_MAX 4 int array[MAX] = { 1, 5, 7, 10, 12, 14, 15, 18, 20, 22, 25, 27, 30, 64, 110, 220 }; int key = 18; int flag = 0; //flag to indicate that item is found ... Read More

Print numbers in sequence using thread synchronization

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

786 Views

Here we will see how to print numbers in a correct sequence using different threads. Here we will create n number of threads, then synchronize them. The idea is, the first thread will print 1, then second thread will print 2 and so on. When one thread is trying to print, it will lock the resource, so no thread can use that portion.Example#include #include #include #include pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t* cond = NULL; int threads; volatile int count = 0; void* sync_thread(void* num) { //this function is used to synchronize the threads    int thread_number ... Read More

What is data type of FILE in C?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

3K+ Views

In C we have used Files. To handle files, we use the pointer of type FILE. So the FILE is a datatype. This is called the Opaque datatype. So its implementation is hidden. The definition of the FILE is system specific. This is the definition of FILE in Ubuntu System −FILE Definitionstruct _IO_FILE {    int _flags; /* High-order word is _IO_MAGIC; rest is flags. */    #define _IO_file_flags _flags    /* The following pointers correspond to the C++ streambuf protocol. */    /* Note: Tk uses the _IO_read_ptr and _IO_read_end fields directly. */    char* _IO_read_ptr; /* Current read ... Read More

Hygienic Macros in C

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

572 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

Incompatibilities between C and C++

Smita Kapse
Updated on 30-Jul-2019 22:30:26

162 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

Difference between fork() and exec() in C

Anvi Jain
Updated on 30-Jul-2019 22:30:26

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

pthread_self() in C

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

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

pthread_equal() in C

Smita Kapse
Updated on 30-Jul-2019 22:30:26

295 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

Advertisements