• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Urban Penguin

The Urban Penguin - Linux Training

  • Home
  • About
  • Live Online Courses
  • Shop
  • Programming
    • Master Editing Text Files Using VIM
    • Learn Shell Scripting with BASH
    • PERL Scripting in Linux
    • Ruby Scripting in Linux
    • Scripting with PowerShell
    • Learn C Programming using Linux and the Raspberry Pi
    • General Java Tutorials
    • Java 7 OCA Exam 1ZO-803
  • OS Tutorials
    • Red Hat and CentOS Training
      • Red Hat Enterprise Linux System Administration 1 – RH124
      • RHCSA – System Admin 2 – RH134
      • RHCE – EX294 – Automation With Ansible
    • Learning Ubuntu
    • LPI Training
      • LPI Linux Essentials
      • LPIC-1 Linux Administrator
      • LPIC-2 Certified Linux Engineer
      • LPIC-3 Senior Level Certification
        • LPIC-3 Exam 300 : Mixed Environments
        • LPIC-3 Exam 303 : Security
        • LPIC-3 Exam 304 : Virtualization and High Availability
    • Linux Technologies
      • Apache HTTPD Server
      • Learning PHP
      • Learning PUPPET
      • Learning SAMBA
      • Linux File-Systems
      • Monitoring with Nagios Core
      • MYSQL
      • openLDAP Directories on Linux
You are here: Home / C Programming / Programming C While Loops on The Raspberry Pi

Programming C While Loops on The Raspberry Pi

September 12, 2016 by The Urban Penguin

Writing C While Loops

c while loopsIn the last blog we did use a C while loop to help us iterate though the supplied arguments, as a result we can then check what code we need to run. Using a simpler loop we can see more detail on how they work and where we can use these tools. Using a C while loop we will count down from 10 to 0 and finally print finished to the screen.

Very Simple Code

If we just want to see a while loop up and running then our code can be very quick and we can have the program up and running in little time.

#include <stdio.h>

int main () {

  int c = 10;
  while ( c > -1 ) {
    printf("%d ", c);
    c--;
  }
  printf("\nFinished\n");
  return 0;
}

Looking at the code it is good an does do what we want but the count down is too quick. We test for the counter variable c being greater than -1. Starting at 10 this means that we count from 10 until 0. Using printf we can print the variable and then we decrease the value of the counter c by 1. This is with the line c–;. If we need to count up rather than down we would increment with c++;. Running the code though we do see the numbers appear at the same time.

Slowing Down the Loop

The C while loop that we use happens just a little to frantically for us and we need to allow it to chill a little. The sleep function can help with this and we will find it in the unistd.h. The function sleep(1) will pause for 1 second.

#include <stdio.h>
#include <unistd.h>

int main () {

  int c = 10;
  while ( c > -1 ) {
    printf("%d ", c);
    sleep(1);
    c--;
  }
  printf("\nFinished\n");
  return 0;
}

This now need to be compiled and executed again. We should see the program now waits 10 seconds before displaying anything but still prints all at once. This is because printf will buffer when printing to a console. We need to flush the buffers each time with fflush.

The Working Example

#include <stdio.h>
#include <unistd.h>

int main () {

  int c = 10;
  while ( c > -1 ) {
    printf("%d ", c);
    fflush(stdout);
    sleep(1);
    c--;
  }
  printf("\nFinished\n");
  return 0;
}

Using fflush from the stdio.h we are able to flush the named stream, stdout in this case. Now we print each time and cont down from 10 to 0 more slowly. We now have now seen how to create working C While loops.

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to print (Opens in new window)

Filed Under: C Programming

Primary Sidebar

Newest Video

The Urban Penguin On Youtube

Categories

Pages

  • About The Urban Penguin
  • Contact Us
  • Shop
    • Basket
    • Checkout
    • My Account
  • LPI Training from The Urban Penguin
    • Live and Pluralsight hosted courses
    • Complete Linux Essentials
    • LPIC-3 Senior Level Certification
      • LPIC-3 Exam 300 : Mixed Environments
      • LPIC-3 Exam 303 : Security
      • LPIC-3 Exam 304 : Virtualization and High Availability
    • LPIC-2 Certified Linux Engineer
    • LPIC-1 Linux Administrator
    • LPI Linux Essentials for Raspberry Pi
    • LPI Linux Essentials
  • Operating System Tutorials
    • Linux Foundation Training
    • Solaris 11 OCA 1ZO-821
    • Learning Ubuntu
    • Learning SUSE
    • Red Hat and CentOS Training
      • RHCE – EX294 – Automation With Ansible
      • RHCSA – System Admin 1 – RH124
      • RHCSA – System Admin 2 – RH134
  • Scripting – the power of repetition!
    • Java 7 OCA Exam 1ZO-803
    • General Java Tutorials
    • Learn C Programming using Linux and the Raspberry Pi
    • Ruby Scripting in Linux
    • Scripting with PowerShell
    • PERL Scripting in Linux
    • Learn Shell Scripting with BASH
    • Master Editing Text Files Using VIM
  • Linux Technologies
    • Learning PUPPET
    • openLDAP Directories on Linux
    • Monitoring with Nagios Core
    • Linux File-Systems
    • Learning SAMBA
    • Apache HTTPD Server
    • Learning PHP
    • MYSQL
  • OpenStack
    • Pluralsight
    • Udemy
    • Raspberry Pi Tutorials
    • Citrix Videos
  • Online Instructor-led Courses
    • Red Hat Enterprise Linux System Administration 1 – RH124
    • SELinux Masterclass
    • Bash Scripting Masterclass
    • Nftables Masterclass

© 2021 The Urban Penguin · All Rights Reserved