For your specific case where you initially have 0 and 1, the following might be faster. You'll have to bench mark it. You probably can't do much better with plain C though; you may need to dive into assembly if you want to take advantage of "x86 trickery" that may exist.

for(int i = 0; i < size ; i++){
  array[i] *= 123456;
}

EDIT:

Benchmark code:

#include <time.h>
#include <stdlib.h>
#include <stdio.h>

size_t diff(struct timespec *start, struct timespec *end)
{
  return (end->tv_sec - start->tv_sec)*1000000000 + end->tv_nsec - start->tv_nsec;
}

int main(void)
{
  const size_t size = 1000000;
  int array[size];

  for(size_t i=0; i<size; ++i) {
    array[i] = rand() & 1;
  }

  struct timespec start, stop;

  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
  for(size_t i=0; i<size; ++i) {
    array[i] *= 123456;
    //if(array[i]) array[i] = 123456;
  }
  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &stop);

  printf("size: %zu\t nsec: %09zu\n", size, diff(&start, &stop));
}

my results:

Computer: quad core AMD Phenom @2.5GHz, Linux, GCC 4.7, compiled with

$ gcc arr.c -std=gnu99 -lrt -O3 -march=native
  • if version: ~5-10ms
  • *= version: ~1.3ms
Answer from Nicu Stiurca on Stack Overflow
๐ŸŒ
CodeCrucks
codecrucks.com โ€บ home โ€บ programs โ€บ c program to replace array elements by given value
C Program to Replace Array Elements by Given Value - CodeCrucks
March 23, 2023 - // C Program to Replace Array Elements by Given Value #include <stdio.h> int main() { int A[] = {1, 6, 5, 8, 6}, n, k, i, j, flag=0; n = sizeof(A) / sizeof(A[0]); printf("Array A :--> "); for(i=0; i<n; i++) printf("%d ", A[i]); printf("\nEnter the Element to replace :--> "); scanf("%d", &k); ...
๐ŸŒ
IncludeHelp
includehelp.com โ€บ c-programs โ€บ c-program-to-replace-even-and-odd-elements-by-1-and-0.aspx
C program to replace all EVEN elements by 0 and Odd by 1 in One Dimensional Array
/*Program to replace EVEN elements by 0 and ODD elements by 1.*/ #include <stdio.h> /** funtion : readArray() input : arr ( array of integer ), size to read ONE-D integer array from standard input device (keyboard).
๐ŸŒ
SunShine Softwares
sunshinesoftwares.com โ€บ home โ€บ questions โ€บ write a c program to replace an element from an array at specified position.
Write a C Program to replace an element from an array at specified position. - SunShine Softwares
July 15, 2022 - #include<stdio.h> #include <conio.h> void main() { int arr[100],pos,newvalue; int i,n; printf("Enter size of the array : "); scanf("%d", &n); printf("\nEnter array Elements now: "); for(i=0;i<n;i++) scanf("%d", &arr[i]); printf("\nEnter Position of Element : "); scanf("%d", &pos); printf("\nEnter New Value: "); scanf("%d", &newvalue); arr[pos-1]=newvalue; printf("\nModified Elements Are: "); for(i=0;i<n;i++) printf("\n%d", arr[i]); getch(); }
๐ŸŒ
CopyProgramming
copyprogramming.com โ€บ howto โ€บ c-replace-number-in-array-using-for-c
Replacing Numbers in Arrays Using C: Complete Guide with 2026 Best Practices
January 3, 2026 - # Recommended compilation with optimizations and warnings gcc -O2 -Wall -Wextra -Wconversion program.c -o program # For bleeding-edge optimizations clang -O3 -march=native program.c -o program ยท Clearly document the expected size, content, and modification patterns for arrays in your code. /** * Replace all occurrences of oldValue with newValue in the array. * * @param arr Pointer to array of integers (must be valid) * @param size Number of elements in the array * @param oldValue Value to search for * @param newValue Value to replace with * * @return Number of replacements made, or -1 on error */ int replaceValues(int *arr, size_t size, int oldValue, int newValue) { if(!arr || size == 0) { return -1; } int count = 0; for(size_t i = 0; i < size; i++) { if(arr[i] == oldValue) { arr[i] = newValue; count++; } } return count; }
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ c-program-for-replacing-one-digit-with-other
C Program for replacing one digit with other
In function int digitreplace(int n, int digit, int replace) Step 1-> Declare and initialize res=0 and d=1, rem Step 2-> Loop While(n) Set rem as n If rem == digit then, Set res as res + replace * d Else Set res as res + rem * d d *= 10; n /= 10; End Loop Step 3-> Print res End function In ...
Top answer
1 of 8
47

For your specific case where you initially have 0 and 1, the following might be faster. You'll have to bench mark it. You probably can't do much better with plain C though; you may need to dive into assembly if you want to take advantage of "x86 trickery" that may exist.

for(int i = 0; i < size ; i++){
  array[i] *= 123456;
}

EDIT:

Benchmark code:

#include <time.h>
#include <stdlib.h>
#include <stdio.h>

size_t diff(struct timespec *start, struct timespec *end)
{
  return (end->tv_sec - start->tv_sec)*1000000000 + end->tv_nsec - start->tv_nsec;
}

int main(void)
{
  const size_t size = 1000000;
  int array[size];

  for(size_t i=0; i<size; ++i) {
    array[i] = rand() & 1;
  }

  struct timespec start, stop;

  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
  for(size_t i=0; i<size; ++i) {
    array[i] *= 123456;
    //if(array[i]) array[i] = 123456;
  }
  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &stop);

  printf("size: %zu\t nsec: %09zu\n", size, diff(&start, &stop));
}

my results:

Computer: quad core AMD Phenom @2.5GHz, Linux, GCC 4.7, compiled with

$ gcc arr.c -std=gnu99 -lrt -O3 -march=native
  • if version: ~5-10ms
  • *= version: ~1.3ms
2 of 8
15

For a small array such as your it's no use trying to find another algorithm, and if the values are not in a specific pattern a simple loop is the only way to do it anyway.

However, if you have a very large array (we're talking several million entries), then you can split the work into threads. Each separate thread handles a smaller portion of the whole data set.

๐ŸŒ
YouTube
youtube.com โ€บ java professional
write A Program To Replace Given Array Element By Given No |Part955| C Language - YouTube
This Channel will provides full C Language Tutorials in Hindi from beginnars to Placement Level.Major Topics are seperated by using seperate playlist.This co...
Published ย  May 5, 2018
Views ย  801
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 10958967 โ€บ replacing-already-printed-numbers-in-c
replace - Replacing already printed numbers in C - Stack Overflow
August 26, 2021 - I would write a function that clears the screen and re-displays the entire board. Then call that function every time you need to update something. Its quick and easy. A better but more complicated way would be curses. ... Main problem of your code snippet is your logic behind assigning values to variables. As anyone can see, you have an array of integers but you want to assign a char to one of its elements.
๐ŸŒ
Codeforhunger
codeforhunger.com โ€บ 2021 โ€บ 05 โ€บ C program to replace array element.html
codeforhunger: C program to replace an element in an array with its position
May 15, 2021 - In these example we will learn how to replace or insert new element in an array, Write a C program to replace an element in an array with its position
Find elsewhere
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 75358154 โ€บ write-a-program-to-replace-all-even-numbers-in-an-array-with-and-print-the-arr
c - Write a program to replace all even numbers in an array with $ and print the array - Stack Overflow
You can do one of two things: Leave the array alone and add an if statement where you print. If even, print the number, else print $ Create an array of char*. Allocate 20 chars for each element if the array.
๐ŸŒ
LearnYard
read.learnyard.com โ€บ dsa โ€บ replace-elements-in-an-array-solution-in-c-java-python-javascript
Replace Elements in an Array Solution In C++/Java/Python/Javascript
June 24, 2025 - Once found, we update the element with the new value specified in the operations that is operation[i][1]. ... Try it Yourself! ... Traverse the nums array to find the index of the number to replace.
๐ŸŒ
w3resource
w3resource.com โ€บ c-programming-exercises โ€บ basic-declarations-and-expressions โ€บ c-programming-basic-exercises-124.php
C : Replace every negative or null element of an array by 1
#include <stdio.h> int main () { int array_nums[7], i, n; // Prompt user for input printf("Input 7 array elements:\n"); // Loop to read 7 integer values from the user and store them in the array for (i = 0; i < 7; i++) { scanf("%d", &n); array_nums[i] = n; } // Print a message indicating the array elements will be displayed printf("\nArray elements:\n"); // Loop to print each element of the array for (i = 0; i < 7; i++) { // Check if the element is less than or equal to 0 if (array_nums[i] <= 0) { array_nums[i] = 1; // Set the element to 1 if it's less than or equal to 0 } // Print the array e
๐ŸŒ
Brainly
brainly.in โ€บ computer science โ€บ secondary school
Write a 'C' program to swap the alternate digits of the given number - Brainly.in
February 6, 2020 - second_number=(first_number+second_number)-(first_number=second_number);//expression to replace the value. printf("The first number is: %d and the second number is: %d after replacement",first_number,second_number); //print the number after ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ c-program-to-replace-all-zeros-with-one-in-a-given-integer
C program to replace all zeros with one in a given integer.
March 26, 2021 - Refer an algorithm given below to replace all the 0โ€™s to 1 in an integer. Step 1 โˆ’ Input the integer from the user. Step 2 โˆ’ Traverse the integer digit by digit. Step 3 โˆ’ If a '0' is encountered, replace it by '1'. Step 4 โˆ’ Print the ...
๐ŸŒ
Decode School
decodeschool.com โ€บ C-Programming โ€บ Arrays โ€บ C-Program-to-replace-every-element-with-the-greatest-element-on-right-side
C Program to replace every element with the greatest element on right side | C Programming | Decode School
Get array size n and n elements of array, replace every elements with the greatest element located in right side. ... Strongly recommended to Solve it on your own, Don't directly go to the solution given below. #include<stdio.h> int main() { //write your code here }
๐ŸŒ
PREP INSTA
prepinsta.com โ€บ home โ€บ c program โ€บ c program to replace each element by its rank in the given array
Replace each element by its rank given in array in C
February 24, 2022 - Run a nested loop and find the position of the given array element in the sorted array. And replace the element with the position. Print the modified input array. ... //Write a program to Replace each element by its rank given in array in C ...