Found 1401 Articles for C

How to check if a variable is NULL in C/C++?

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

22K+ Views

In C or C++, there is no special method for comparing NULL values. We can use if statements to check whether a variable is null or not.Here we will see one program. We will try to open a file in read mode, that is not present in the system. So the function will return null value. We can check it using if statement. See the code for better understanding.Example Code#include main() {    //try to open a file in read mode, which is not present    FILE *fp;    fp = fopen("hello.txt", "r");    if(fp == NULL)     ... Read More

Designated Initializers in C

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

664 Views

In C90 standard we have to initialize the arrays in the fixed order, like initialize index at position 0, 1, 2 and so on. From C99 standard, they have introduced designated initializing feature in C. Here we can initialize elements in random order. Initialization can be done using the array index or structure members. This extension is not implemented in GNU C++.If we specify some index and put some value, then it will be look like this -int arr[6] = {[3] = 20, [5] = 40}; or int arr[6] = {[3]20, [5]40};This is equivalent to this:int arr[6] = {0, 0, ... Read More

Basics of File Handling in C

Chandu yadav
Updated on 18-Nov-2020 05:06:31

914 Views

Here we will see some basic file handling operations in C. The operations are listed below:Writing into a FileReading from FileAppending in a FileWrite into a fileSee the code to get the idea how we write into a fileExample Code#include int main() {    FILE *fp;    char *filename = "sample.txt";    char *content = "Hey there! You've successfully created a file with content in c programming language.";    /* open for writing */    fp = fopen(filename, "w");    if( fp == NULL ) {       printf("%s: failed to open. ", filename);       return ... Read More

Name Mangling and extern “C” in C++

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

881 Views

In C++ we can use the function overloading feature. Using this feature, we can create functions with same name. The only difference is the type of the arguments, and the number of arguments. The return type is not considered here. Now the question comes how the C++ distinguishes overloaded functions in object code?In the object code, it changes the name by adding information about the arguments. The technique which is applied here is called the Name Mangling. C++ has no standardized technique for name mangling. So different compiler uses different techniques.Here is an example of Name Mangling. The overloaded functions ... Read More

What does it mean when a numeric constant in C/C++ is prefixed with a 0?

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

122 Views

Sometimes we may see some numeric literals, which is prefixed with 0. This indicates that the number is octal number. So octal literals contain 0 at the beginning. For example, if an octal number is 25, then we have to write 025.Example#include int main() {    int a = 025;    int b = 063;    printf("Decimal of 25(Octal) is %d", a);    printf("Decimal of 63(Octal) is %d", b); }OutputDecimal of 25(Octal) is 21 Decimal of 63(Octal) is 51

wprintf() and wscanf in C Library

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

1K+ Views

Here we will see the wprintf() and wscanf() functions in C. These are the printf() and scanf() functions for wide characters. These functions are present in the wchar.hThe wprintf() function is used to print the wide character to the standard output. The wide string format may contain the format specifiers which is starting with % sign, these are replaced by the values of variables which are passed to the wprintf().The syntax is like below −int wprintf (const wchar_t* format, ...);This function takes the format. This format is a pointer to a null terminated wide string, that will be written in ... Read More

Program for Christmas Tree in C

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

867 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

121 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

732 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

546 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

Advertisements