Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C Articles
Page 38 of 96
Converting Strings to Numbers in C/C++
In C programming, converting strings to numbers is a common task that can be accomplished using several standard library functions. C provides multiple approaches to convert string representations of numbers into their corresponding numeric values. Syntax int atoi(const char *str); long atol(const char *str); double atof(const char *str); long strtol(const char *str, char **endptr, int base); double strtod(const char *str, char **endptr); Method 1: Using atoi() for Integer Conversion The atoi() function converts a string to an integer value − #include #include int main() { ...
Read MoreConvert a String to Integer Array in C/C++
In C, converting a comma-separated string to an integer array involves parsing the string character by character and extracting numeric values. This process requires traversing the string, identifying delimiters (commas), and converting individual number substrings to integers. Syntax // Function to convert string to integer array void convertStringToArray(char str[], int arr[], int *size); Example: Using Character-by-Character Parsing This approach traverses the string and builds integers by accumulating digits until a comma is encountered − #include #include void convertStringToArray(char str[], int arr[], int *size) { int i = 0, j = 0, num = 0; int len = strlen(str); for (i = 0; i = '0' && str[i]
Read MoreHow to add "graphics.h" C/C++ library to gcc compiler in Linux
In C programming, the graphics.h library provides functions for creating graphics programs. However, this library is not part of the standard C library and requires special installation on Linux systems. Here we'll show how to install and use the graphics library with gcc compiler. Installation Requirements Before using graphics.h, you need to install the required packages and compile the libgraph library on your Linux system. First, install the build essentials and required dependencies − sudo apt-get install build-essential sudo apt-get install libsdl-image1.2 libsdl-image1.2-dev guile-2.0 guile-2.0-dev libsdl1.2debian libart-2.0-dev libaudiofile-dev libesd0-dev libdirectfb-dev libdirectfb-extra libfreetype6-dev ...
Read MoreHow arrays are passed to functions in C/C++
In this tutorial, we will be discussing how arrays are passed to functions in C. Understanding this concept is crucial for proper array handling in C programming. In C, arrays are never passed by value to functions. Instead, when you pass an array to a function, what actually gets passed is a pointer to the first element of the array. This is called "array decay" − the array name decays into a pointer. Syntax // Method 1: Array notation void function_name(data_type array_name[]); // Method 2: Pointer notation void function_name(data_type *array_name); // Method ...
Read MorePredefined Identifier __func__ in C
The __func__ is a predefined identifier in C that provides the name of the current function. It was introduced in C99 standard and is automatically available in every function without any declaration. Syntax __func__ The __func__ identifier is implicitly declared as if the following declaration appears at the beginning of each function − static const char __func__[] = "function-name"; Example 1: Basic Usage Here's a simple example showing how __func__ returns the current function name − #include void function1(void) { printf("Current function: ...
Read MorePrint * in place of characters for reading passwords in C
In C programming, when handling passwords, it's important to hide the actual characters from being displayed on screen for security reasons. This involves replacing each character of the password with an asterisk (*) symbol. Let's take an example to understand the problem − Input: password Output: ******** Syntax for(int i = 0; i < strlen(password); i++){ printf("*"); } Example 1: Using Predefined Password String The below program demonstrates how to replace each character of a password string with asterisks ? #include #include ...
Read MorePrint 1 2 3 infinitely using threads in C
In C programming, we can print the sequence "1 2 3" infinitely using multiple threads with proper synchronization. This demonstrates thread coordination using mutexes and condition variables to ensure orderly execution. Syntax pthread_create(&thread_id, NULL, thread_function, argument); pthread_mutex_lock(&mutex); pthread_cond_wait(&condition, &mutex); pthread_cond_signal(&condition); pthread_mutex_unlock(&mutex); Installation: On Linux systems, compile with: gcc filename.c -lpthread Example This program creates three threads that print numbers 1, 2, and 3 in sequence infinitely. Each thread waits for its turn using condition variables − #include #include pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER; pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER; ...
Read MorePrint 2D matrix in different lines and without curly braces in C/C++
Here, we will see how to print a 2D matrix in C programming language without using curly braces. This technique uses a clever approach to eliminate the need for braces in nested loops. Curly braces are separators in C that define separate code blocks in the program. Without curly braces, defining scopes is difficult, but we can use a shorthand technique to achieve the same result for simple operations. Syntax for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) ...
Read MoreInteger to Roman in C
Given a decimal number n, we have to convert this into Roman numeral. The value n lies in the range 1 to 4000. Roman numerals use specific symbols and follow subtraction rules for certain combinations. Number Numeral 1 I 4 IV 5 V 9 IX 10 X 40 XL 50 L 90 XC 100 C 400 CD 500 D 900 CM 1000 M 4000 MMMM ...
Read MoreC program for file Transfer using UDP?
File transfer using UDP in C enables transferring files between client and server using User Datagram Protocol. This implementation includes XOR encryption for basic security and handles file existence checking. Syntax int socket(int domain, int type, int protocol); int sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen); int recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen); Algorithm The server starts and waits for a filename from the client. The client sends a filename to the server. The server receives the filename ...
Read More