Found 33676 Articles for Programming

Stringstream in C++

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

11K+ Views

Here we will see the string stream in C++. The string stream associates a string object with a string. Using this we can read from string as if it were a stream like cin.The Stringstream has different methods. These are like below −clear(): Used to clear the streamstr(): To get and set the string object whose content is present in streamoperator > : This is used to read from stringstream object.Let us see two examples of string streams. In the first program we will divide the words into separate strings.Example#include #include #include #include using namespace std; ... Read More

Reverse a string in C/C++ using Client Server model

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

1K+ Views

Here we will see how we can create a system, where we will create one client, and a server, and the client can send one string to the server, and the server will reverse the string, and return back to the client.Here we will use the concept of socket programming. To make the client server connection, we have to create port. The port number is one arbitrary number that can be used by the socket. We have to use the same port for client and the server to establish the connection.To start the program, start the server program first −gcc ... Read More

How to write your own header file in C?

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

15K+ Views

Here we will see how to create own header file using C. To make a header file, we have to create one file with a name, and extension should be (*.h). In that function there will be no main() function. In that file, we can put some variables, some functions etc.To use that header file, it should be present at the same directory, where the program is located. Now using #include we have to put the header file name. The name will be inside double quotes. Include syntax will be look like this.#include”header_file.h”Let us see one program to get the ... Read More

How to use POSIX semaphores in C language

Tapas Kumar Ghosh
Updated on 11-Jun-2025 14:42:30

2K+ Views

The POSIX stands for Portable Operating System which was developed by IEEE (Institute of Electrical and Electronics Engineers). This is UNIX based operating system that is used for both system calls and library functions. The semaphores are used in the process of multithreading and synchronization. For eg. file sharing and memory management. It can be named or unnamed. Multithreading is the process of executing multiple tasks in the same instance of time while synchronization is used to control the thread to work in a sequential manner. It is important for data security. Using Semaphores in C language To use ... Read More

Self Destructing Code in C

Tapas Kumar Ghosh
Updated on 13-Jun-2025 14:27:16

1K+ Views

The self-destructing code is a type of program that can delete itself. It automatically executes the program and then removes the executable file once the execution is done. In this article, we will learn how to write self-destructing code for a C program. Write a Self Destructing Code in C++ You can write a self-destructing program that deletes its own executable file after it finishes running by using the remove() function. The remove() function is a built-in function from the C standard library () that deletes the specified file from the file system. To delete the program's own executable file, ... Read More

Generalized Lambda Expressions in C++14

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

307 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

Tapas Kumar Ghosh
Updated on 02-Jun-2025 14:13:39

4K+ Views

To print random numbers within a range, we will have two input variables where we need to set the maximum and minimum values. Using these two values, we can display a random value within the specified range. Example Following is the input-output statement to understand the range of a random number: Input: min = 4, max = 14 Output: 8, 10, 7 Explanation: Any numeric value between 4 and 14 can be displayed in the specified range. Generate Random Number Within a Range Using rand() with Modulus The rand() function generates the random number while modulus operator return ... Read More

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

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

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

227 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

Advertisements