Found 7347 Articles for C++

How to use POSIX semaphores in C language

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

2K+ Views

The semaphore is a concept of process or thread synchronization. Here we will see how to use the semaphores in real programs.In Linux system, we can get the POSIX semaphore library. To use it, we have to include semaphores.h library. We have to compile the code using the following options.gcc program_name.c –lpthread -lrtWe can use sem_wait() to lock or wait. And sem_post() to release the lock. The semaphore initializes sem_init() or sem_open() for the Inter-Process Communication (IPC).Example#include #include #include #include sem_t mutex; void* thread(void* arg) { //function which act like thread    sem_wait(&mutex); //wait state   ... Read More

Self Destructing Code in C

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

961 Views

Here we will see how to create self-destructing code in C. The self-destructing code is basically executing the code, and then remove the executable file after executing it.This task is very simple. We need to get the executable file name to remove it. We can use the command line arguments. The argv[0] will hold the executable filename. Then using remove() function we can remove it.In the program we can see that one line is printing after the removing of that file. So now question comes how the next line is executing while current file is not present?Actually the entire converted ... Read More

Generalized Lambda Expressions in C++14

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

188 Views

In C++11, the lambda was introduced. Lambdas are basically a part of code, that can be nested inside other function call statements. By combining lambda expressions with the auto keyword, they can be used later.In C++14, these lambda expressions are improved. Here we can get the generalized lambda. For example, if we want to create a lambda, that can add integers, add numbers, also concatenate strings, then we have to use this generalized lambda.Syntax of the lambda expression is looking like this:[](auto x, auto y) { return x + y; }Let us see one example to get the better idea.Example#include ... Read More

Generating random number in a range in C

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

3K+ Views

Here we will see how to generate random number in given range using C. To solve this problem, we will use the srand() function. The current time will be used to seed the srad() function.This function cannot generate random number in any range, it can generate number between 0 to some value. So for it, we have to follow one trick. We will generate random number in between 0 to (upper – lower + 1), then add the lower limit for offsetting.Example#include #include #include void generate_random(int l, int r, int count) { //this will generate random number ... Read More

Measure execution time with high precision in C/C++

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

1K+ Views

To create high precision timer we can use the chrono library. This library has high resolution clock. This can count in nanoseconds.In this program we will see the execution time in nanoseconds. We will take the time value at first, then another time value at the last, then find the difference to get elapsed time. Here we are using blank loop to pause the effect for sometimes.Example#include #include typedef std::chrono::high_resolution_clock Clock; main() {    auto start_time = Clock::now();    for(int i = 0; i

How to find Segmentation Error in C & C++ ? (Using GDB)

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

1K+ Views

The segmentation error is one of the runtime error, that is caused because of the memory access violation, like accessing invalid array index, pointing some restricted address etc. In this article, we will see how to detect this type of error using the GDB tool.Let us see the code and respective steps to locate the error.Example#include main() {    int* ptr = NULL;    *ptr = 1; //trying to access unknown memory location    printf("%p", ptr); }Compile the code using ‘gcc –g program_name.c’, and run using ‘./a.out’Outputsoumyadeep@soumyadeep-VirtualBox:~/Cpp_progs$ ./a.out Segmentation fault (core dumped)The segmentation error occurred.Write ‘gdb ./a.out core’soumyadeep@soumyadeep-VirtualBox:~/Cpp_progs$ gdb ... Read More

mbrtowc() function in C/C++

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

140 Views

This mbrtowc() function is used to convert multibyte sequence to wide character string. This returns the length of the multibyte characters in byte. The syntax is like below.mbrtowc (wchar_t* wc, const char* s, size_t max, mbstate_t* ps)The arguments are −wc is the pointer which points where the resulting wide character will be stored.s is the pointer to multibyte character string as inputmax is the maximum number of bytes in s, that can be examinedps is pointing to the conversion state, when interpreting multibyte string.Example#include using namespace std; void display(const char* s) {    mbstate_t ps = mbstate_t(); // initial ... Read More

getopt() function in C to parse command line arguments

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

7K+ Views

The getopt() is one of the built-in C function that are used for taking the command line options. The syntax of this function is like below −getopt(int argc, char *const argv[], const char *optstring)The opstring is a list of characters. Each of them representing a single character option.This function returns many values. These are like below −If the option takes a value, then that value will be pointed by optarg.It will return -1, when no more options to procesReturns ‘?’ to show that this is an unrecognized option, it stores it to optopt.Sometimes some options need some value, If the ... Read More

Errors in C/C++

Smita Kapse
Updated on 03-Dec-2021 06:17:18

11K+ Views

In C or C++, we face different kinds of errors. These errors can be categorized into five different types. These are like below −Syntax ErrorRun-Time ErrorLinker ErrorLogical ErrorSemantic ErrorLet us see these errors one by one −Syntax errorThis kind of errors are occurred, when it violates the rule of C++ writing techniques or syntaxes. This kind of errors are generally indicated by the compiler before compilation. Sometimes these are known as compile time error.In this example, we will see how to get syntax error if we do not put semicolon after one line.Example#include main() {    printf("Hello World") }OutputError] expected ... Read More

Comparator function of qsort() in C

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

1K+ Views

In C, we get the qsort() function. This is used to sort some array using quicksort technique. In this function we have to pass the comparator function. This comparator function takes two arguments. Then compares them and get the relative order between them. These two arguments are pointers, and type casted to const void*. The syntax is like below −int comparator(const void* p1, const void* p2);The return values are of three types −Less than 0. The element pointed by p1 will go before the second one.Equal to 0. Two values are same.Greater than 0. The element pointed by p1 will ... Read More

Advertisements