Mini C program #1

Let's punish the computer: the for loop! 

   

Tutorial presented by: Jérémie F. Bellanger (Jay)
Date of publication: February 21, 2014
Last update: March 9, 2025

 

Introduction to Mini C Programs

   

   Hello everyone, and welcome to this new serie of mini tutorials, called mini C programs. smiley

   But why mini C programs, you might ask? sad

   Well, quite simply because this serie of tutorials will be based on a serie of mini programs, written in C, whose goal will be to help you familiarize yourself with some key functions of C, while having fun and creating programs, if not useful, at least fun! wink

   At first, we will stick to basic console programs, before moving on to programs that are a little more complex with the Big SDL Tutorial.

   Rather, see these mini-programs as a way to discover or improve your C while having fun (at least I hope so! cool).

   Okay, enough talking! Let's get to action! devil

  

 

Mini C program #1: Let's punish the computer: the for loop!

   

   Your computer has been naughty?! broken heart

   It crashed in the middle of some super important work (or in the middle of a game wink)?! crying

   And besides, it's not the first time!!!?!!! sad

   Well, it's time to get tough!! angry And since we're very mean, we're going to make him copy 50,000 lines!!! That'll teach him a lesson, mind you!! devil

   Ahah! The goal of our first mini-program will therefore be to make the computer copy a sentence 50,000 times, in console mode.

   But how are we going to do it? sad

   Well, simply by using a for loop! wink

   As a reminder, a for loop, in C (and in other languages ​​for that matter) needs a counter to work. We will call it i, as is often the case in computing. cheeky

   In the loop declaration, we'll initialize it to 0, and then we'll run our counter up to 50,000 by incrementing i by 1 each time we loop around (i++).

   Inside the loop, we will use a printf() to write in the console, and we will add the line number at the beginning of the sentence, to be able to check that the computer is not cheating us (you never know! laugh).

   And here we go, for our first mini-program! Not too complicated, right? wink

   Now, if you want to practice, it would be good to try to create the program, without looking at the solution below. wink

 

The code

   

   And here is the solution with the comments:

   

//We include the C libraries. 
# include <stdio.h> 
# include <stdlib.h>
 
//Program to copy 50,000 lines to the computer: 
int main( ) 
{ 
    //Counter for the loop 
    int i;
 
    //Loop: initialize i to 0, then add 1 to it 
    //each loop until it reaches 50000. 
    for ( i = 0; i <= 50000; i++) 
    { 
        //This function displays the following text on the screen 
        //%d will be replaced by the value i, on display and 
        //will allow to count the lines. 
        //\n allows to skip a line each time. 
        printf("%d times. I must not crash in class. I am a naughty computer.\n", i); 
    }
 
    //We quit when the loop is finished 
    return 0; 
}

   

And here is the result in picture:

 

You will be able to download the project files in the Downloads section.

 

 

This site uses cookies to enable you to log in. We do not store or sell any personal data. By continuing to use this website, you agree to their use. Thanks!