C Array Traversing In Hindi

📔 : C 🔗

पिछले topic में आपने C में array के बारे में पढ़ा और समझा कि कैसे array को define करते हैं single elements को access करते हैं। इस topic में आप सीखेंगे कि for loop और while loop की help से कैसे array को traverse कर सकते हैं।


C में Array को आप for loop और while loop दोनों से traverse कर सकते हो।

C array traversal with while loop

CopyFullscreenClose FullscreenRun
#include <stdio.h>
int main() {
  // define string array.
  char* users[5] = {"Rahul", "Ravi", "Raju", "Ram", "Shyam"};
  int index=0;
  while( index < 5) {
    printf("Index %d : %s\n", index, users[index]);
    // increase value by 1.
    index++;
  }
  return 0;
}
Output
Index 0 : Rahul
Index 1 : Ravi
Index 2 : Raju
Index 3 : Ram
Index 4 : Shyam

Note - Example में \n का use line break करने के लिए किया गया है।

C array traversal with for loop

अब same example को for loop की help से iterate करके देखेंगे।

CopyFullscreenClose FullscreenRun
#include <stdio.h>
int main() {
  // define string array.
  char* users[5] = {"Rahul", "Ravi", "Raju", "Ram", "Shyam"};
  for(int index=0; index < 5; index++) {
    printf("Index %d : %s\n", index, users[index]);
  }
  return 0;
}
Output
Index 0 : Rahul
Index 1 : Ravi
Index 2 : Raju
Index 3 : Ram
Index 4 : Shyam

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers