(compiling)

CIS241

System-Level Programming and Utilities

C Arrays and Loops

Erik Fredericks, frederer@gvsu.edu
Fall 2025

Based on material provided by Erin Carrier, Austin Ferguson, and Katherine Bowers

CIS241 | Fredericks | F25 | 30-c-arrays-loops

C arrays

All elements in a C array have the same type

For now, we are talking about static arrays

  • i.e., fixed size

Declaring:

  • int arr[10];
  • int arr[3] = {2, 4, 6};
  • char arr[5];
  • char arr[] = "hello world";
CIS241 | Fredericks | F25 | 30-c-arrays-loops

Array indexing

We still access elements like in every other language (zero-indexed)

  • int arr[3] = {2, 4, 6};
  • int x = arr[1];
  • arr[2] = 0;
CIS241 | Fredericks | F25 | 30-c-arrays-loops

For loops

unsigned int i;
for (i = 0; i < 10; i++)
{
    ...
}

Newer versions of C allow you to create an iterative variable inside the loop line

CIS241 | Fredericks | F25 | 30-c-arrays-loops

While loops

while (condition)
{
    ...
}
int i = 10;
while (i >= 0)
{
    ...
    i--;
}
CIS241 | Fredericks | F25 | 30-c-arrays-loops

Do-while loops

do
{
  ...
} while (condition);

Same as while, but the block executes at least once!

CIS241 | Fredericks | F25 | 30-c-arrays-loops

hover over hello world in vis studio why is it 12? last char is \0 (null terminated) set arr[3] = '\0' and print..