Found 1401 Articles for C

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

439 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

110 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

3K+ 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

7K+ 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

184 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

pthread_cancel() in C

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

575 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

nextafter() and nexttoward() in C/C++

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

177 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

Interesting Facts about C Programming

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

312 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

Use of bool in C

Samual Sam
Updated on 30-Jul-2019 22:30:26

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

Advertisements