Category Archives: C Programming

Adding Color to Your Output From C

Download PDF

Adding Color to Your Programs

Adding Color

Adding color to the output of your programs can make them more visually appealing and adding to their aesthetics. Not only this, though, color will make notices or warning more noticeable to your users resulting in better acceptance. In this blog we will see how to can both add color and start making use of additional functions in C, rather than, using just the main function. So when you are ready and sat comfortably in your chair we shall begin.

ANSI Colors


We have 8 ANSI colors that we can use in our output, this can be doubled to 16 if you take into consideration that these colors can be displayed as standard or in bold for highlighting. To be able to access the colors we need to use and escape sequence followed by the correct color code, the print the text and finally reset the colors.

  • printf(“\033[0;31m”); //Set the text to the color red
  • printf(“Hello\n”); //Display Hello in red
  • printf(“\033[0m”); //Resets the text to default color
  • Escape is: \033
  • Color code is: [0;31m

It is important to reset the color to ensuring that the selected color is terminated and text returns to normal. Using the following table you can view some of the code available.

CODECOLOR
[0;31mRed
[1;31mBold Red
[0;32mGreen
[1;32mBold Green
[0;33mYellow
[01;33mBold Yellow
[0;34mBlue
[1;34mBold Blue
[0;35mMagenta
[1;35mBold Magenta
[0;36mCyan
[1;36mBold Cyan
[0mReset

Simple Hello World in Color

Working with a simple hello world program we can begin to understand how to make use of the color. Firstly we will set the color to be red and bold before moving onto using functions to set the color.

#include <stdio.h>
int main () {
  printf("\033[1;31m");
  printf("Hello world\n");
  printf("\033[0m;")
  return 0;
}

17-09-2016-17-39-39

Adding color to the output was really quite simple; however setting many colors or changing the colors many times will be repetitive. Setting the color often and using more than one color is going to be required where we want to highlight the output with information and warnings.

Using Functions

This is where function can help. It is simple to create functions for red, yellow etc. Let’s take a look.

#include <stdio.h>
void red () {
  printf("\033[1;31m");
}

void yellow {
  printf("\033[1;33m");
}

void reset () {
  printf("\033[0m");
}

int main () {
  red();
  printf("Hello ");
  yellow();
  printf("world\n");
  reset;
  return 0;
}

17-09-2016-17-50-40

We can use the newly created functions as many time as we want and it is as simple as yellow(); reset(); or red(); We, of course, can create more functions to support all colors.

Using the functions the way we have we do not need to return any values. The functions result only in printing the ANSI color codes to the terminal. As we do not return any value then we set the output parameter explicitly to void.

Moving On

It is also likely that we can reuse these function in almost any C program that has text output. In another blog we will see how we can reuse this code by creating and referencing our own header files.

The following video steps you through the process of printing in color.

 

Programming C While Loops on The Raspberry Pi

Download PDF

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.

Using getopt to Parse Arguments in C

Download PDF

Using getop in C to Read Arguments

Having seen our hello world program in a number of forms so far such as the simple hello and then using if we will now extend this further. Using getopt in the C programming language will allow us to pass options to the program in any order. Thinking for a moment about how we compile the source code we use the option -o to specify the output file. Using either of the following examples would work:

gcc -o hello hello.c
gcc hello.c -o hello

Additionally, we can add in other options, also in any order:

gcc -o hello -Wall hello.c
gcc hello.c -o hello -Wall

using getopt in C programming


Using options in a program is a lot easier than having the pass arguments in the correct order from the command line and we will now see how we achieve this in C using getopt.

Using Loops

Viewing the previous code extract you can see that we make use of a looping structure in C, the while loop. Looping in programming allows us to iterate or go though each item one be one. A simple while loop extract is shown below:

int a = 10;
while (a < 20) {
  printf("The value is: %d\n",a);
  a++;
}

We can see much like the if statement that we looked at within the previous code, the while loop is also a conditional statement. The parenthesis enclose the condition that must evaluate to true. Looping whilst is true we use printf the print the value of the variable a, noting, that the placeholder for an integer is %d. Ensuring the we don’t loop forever the last line of the loop increments the value of the variable, a++. Printing will start at 10 and then continue through to 19.

Using getopt

You may look at the while loop we employ and think it is a little more complicated, alternatively you may see that it does just a little more.  Firstly, be sure to include the header for getopt.

#include <getopt.h>

Each option that we supplied can be in one or more argument to the program and those arguments are still in the array argv. Consider the following command:

gcc -o hello hello.c
  • argv[0] = gcc
  • argv[1] = -o
  • argv[2] = hello
  • argv[3] = hello.c

We need a variable to store the current index value as we iterate through each argument looking for options.

int option_index = 0

The name or identifier that we assign to the variable is our choice.

When using getop an option, such as -o in the gcc program, can take a value.  In the extract of code used in the above screenshot we can provide an argument to the option -u. We need to store that somewhere. For this we setup the user_name array. Both of these variables, option_index and user_name we initialize with a value to prevent warnings from the compiler.

char *user_name = NULL

To assign the true value to the variable option_index we return the index from argv using the function getopt. The first argument is the argument count, acting as the highest number we need at return. Then the array to iterate through, argv followed by the possible options.  Viewing the screenshot we specify the option u and g. The option u takes and argument as it is preceded with the colon, (u:). Our program requires that we state the user name with the option -u.

while (( option_index - getopt(argc, argv, "u:g")) != -1)

The braces enclose the code block for the while loop.

Case Statements

The last element of our new main function is the case statement. The case statement starts with the keyword switch but we subsequently examine the value of the option_index use the keyword case from which it is commonly known.

Using the case statement is a great alternative to adding in many elsif statements.

switch(option_index) {
  case 'u':
    user_name = optarg;
    printf("User is selected\n");
    printf("This user is: %s\n", user_name);
    break;
  case 'g':
    printf("Group is selected\n");
    break;
  default;
    printf("Invalid option\n");
}

Viewing the code extract you can see that we search for both  the option u and g. It is possible that both may be set. Using the option -u we look for the argument and populate the user_name variable with the option supplied. We use the keyword break to close the case for u before searching for the option h.

Whilst this is still a very simple example as we start looking at our own calculations functions this becomes a valuable building block. We will look at next creating a program to convert Centigrade to Fahrenheit but later will add options similar to this so we can covert in either direction based on the option used.

Complete Source Code

#include <stdio.h>
#include <getopt.h>
int main ( int argc, char **argv) {
 int option_index = 0;
 char *user_name = NULL;
 while (( option_index = getopt(argc, argv, "u:g")) != -1){
   switch (option_index) {
     case 'u':
      user_name = optarg;
       printf("User is selected\n");
       printf("The user is %s\n",user_name);
       break;
     case 'g':
       printf("Group is selected\n");
       break;
     default:
      printf("Option incorrect\n");
      return 1;
     } //end block for switch
   }  //end block for while
 return 0;
} // end main block

 

The video follows:

Using Conditional Statements in C

Download PDF

Conditional Statements in C

Conditional Statements in C


Using conditional statements in C, such as the if statement above, we can easily create more flexible code. Having already created the hello.c code before that takes arguments to display information. We now need to investigate how to provide a prompt if the user does not supply arguments. Making the script easy to use with or without arguments and additionally making users happy.

Compiling Code with Warnings

When we compiled our code before using the GNU C compiler or gcc program we did not see any errors. Fabulous, this is what we want but there me problems lurking that we did not know about. Choosing to compile with warning options can tell us of any potential issues that exits. The option -W can enable the warning that we select, additionally, -Wall turns on most if not all warnings and is a good option to use.

gcc -Wall -o hello hello.c

Viewing the output from gcc we now see that two warning are produced and also that these both relate to the return value from the function main . We omitted to define that the function main returns an integer, not only that, we failed to specify any return value. We are C coding criminals! Functions can and often do return values, just think of a function to calculate the area of a rectangle. Taking input arguments, running calculations and as a result returning the answer. We will have both input and output arguments in this case. Any main function is required to return an integer, if we do not specify then it still works but with the return values of functions that run inside main. To control the output ourselves we should add in the required options and as a result, create better code..

Returning Values From Main

int main ( int argc, char **) {
  .....
  return 0;
}

Firstly, looking at the snippet of code we add the word int before the function to specify the data type of the return value. Int is an abbreviation for integer or any whole number. Finally, the last statement in the main function specifies the value to return. We return the value 0. 0 represents success and a value other than 0 represents a failure.

As a result of our coding intervention we can compile the code without any warnings now. Using either using -Wall or the specific warning of -Wreturn-value.

gcc -Wall -o hello hello.c
gcc -Wreturn-value -o hello hello.c

Prompting for User Input

Waiting a while before we move onto the conditional statements in C we will look at how we can prompt for user input. Using a prompt withing the program additionally allows a level of interaction to the program whilst it is running.

char name[100];
printf("Enter your name: ");
scanf("%s", name);

Analyzing the lines we have added to the hello.c file:

  • char name(100);  Firstly we declare a variable name t be of type char. A char is a single character to we set the variable to be an array with indices from 0 to 99 [100]. There is no string data type so we set up and array of characters which is what a string is.
  • scanf(“%s”, name);  Secondly, we have seen printf before and scanf is related to printf but instead of printing data it reads data. We collect keyboard data and store it to the name variable. We set up a string placeholder with %s to accept the data.

Allowing Choice

Having waited all this time this is now where we can add in conditional statements to the C program. We can use the if conditional to look at arguments that have been supplied.

//Program to prompt for user input

#include <stdio.h>
#include <string.h>
int main ( int argc, char **argv) {
  char name[100];
  if ( argc < 2 ) {
    printf("Enter a name: ");
    scanf("%s", name);
  } else {
    strcpy(name, argv[1]);
  }
  printf("Hello %s\n", name);
  return 0;
}

Firstly we see that the if statement looks for the argument count being less than 2, if (argc < 2 ).The number of supplied arguments are maintained in the integer variable argc. When looking to see if the command lines arguments have been supplied we look for < 2 NOT <1. There is always one argument, the program name therefore what we see as the first argument is in fact the 2nd argument. As a result of omitting the argument then we prompt for user input.

Additionally we use the keyword else to maintain the the alternative block of code for what should happen as a result of supplying arguments. Here we use the function strcpy to copy the string from argv[1] to the variable name. Therefore we can print the one variable name no matter if we prompt or pass the value into the program. To access strcpy we include the library string.h.

We can now compile and test the program before we have a C program that makes use of conditional statements. Next blog looks at using getopt to pass options to a program.

Your First C Program on The Raspberry Pi

Download PDF

Writing Your First C Program on The Raspberry Pi

First C ProgramAt theurbanpenguin we offer training and blogs in many languages. Some of these languages are scripted and others compiled.  With a compiled language the source code is transformed into a binary program. When using a scripted language the code is translated at run time by the script interpreter. In general compiled programs should be quicker to run than a scripts when running the same task  In writing your first C program you will learn to use a compiled language. This is where you are creating your own binary or executable programs.


Whilst C is not going to be the easiest of languages to learn it is a great way to start to understand computers and programming. In the Linux community we talk a lot about the value of Open Source programs and when you write you own program you will discover what we mean by Open Source. Many programs in Linux and other Operating systems are written in C making a good choice to learn.

The Learning Environment

We can use Graphical tools and even IDE or Integrated Design Environments to create the code. To keep this tutorial as simple as possible and reducing the need for reliance on additional programs  we will use command line text editors . In this tutorial we will start with nano to get you into the idea of creating the code. In later tutorials I will use vim. I would always recommend vim but if you have not used it before it takes a little getting used to. We want to focus and creating our first C program and not the editor.

Throughout this series of tutorials we will use a Raspberry Pi computer running the Raspbian operating system. It is not a problem using any Linux distribution on the hardware of your choice. It will be sufficient to follow through. Writing in C here is the focus and we do not mind which platform that you choose.

Logging onto the Raspberry Pi we are presented with the GUI desktop or perhaps the command line if we have not enabled the desktop. We are using the desktop but it again does not matter as we will be making use of the command line. Using the desktop allows us to display text in a larger size for the purposes of the video. Using nano as the text editor keeps the editing simple.

Creating the Code

To start we will be creating a directory to organize our code and help us locate files we need. Making sure that we are in our home directory we use the mkdir command to create the structure we need.

cd
mkdir -p src/c

Creating the directory named “src” allows storage of our source code, additionally the sub-directory “c” allows us to organize the source by language. Keeping all files organized makes baking them up easier, and that is not a bad thing.

Moving on with our code-creation we can additionally start our editor and create our first code.

nano hello.c

Using the extension “.c” , as we have here, allows us to easily know the purpose of the file. Entering our new file we can easily start adding code and we often will expect to see comments at the start of the file. Comments lines are for the reader only and are not interpreted by the compiler, additionally, they help our understanding of the code.

Adding Comments

Ensuring we add great comments to our first C program we will take a look at the two styles of comments available.

  • Single line or in-line comments: //
  • Block or multi line comments: /* */

Looking at the first lines of code now we can see the use of both comments types:

//This is an inline comment, commenting the rest of the line
/* This is a block comment that
continues across many lines
until it is closed*/

Adding comments to your code is always appreciated when the code is read by others, additionally you may appreciate it years to come when you revisit the code.

Adding Libraries

The next line that we are adding is a pre-processor command. This tells the compiler to include additional code libraries during the compilation. This libraries will contain pre-written code that we can make use of.

//This is an inline comment, commenting the rest of the line
/* This is a block comment that
continues across many lines
until it is closed*/
#include <stdio.h>

Using diamond brackets around the library name tells the compiler to locate the file, stdio.h in standard system directories rather than adding the full path. The file is actually located in /usr/include/ on the Raspbian OS.

Adding the main Function

Rapidly moving on as fast as out little fingers can type we will create the function main. Each and every C program than can be executed must have a main function. Creating main with the bare minimum we add the following code.

//This is an inline comment, commenting the rest of the line
/* This is a block comment that
continues across many lines
until it is closed*/
#include <stdio.h>
main () {

}

Using parentheses we can group together arguments that may be passed to the function and the braces contain the code. Seeing that the braces are empty we can beginning adding code to the main function.

//This is an inline comment, commenting the rest of the line
/* This is a block comment that
continues across many lines
until it is closed*/
#include <stdio.h>
main () {
  printf("Hello World\n");
}

Noting the code that we have added within the braces we use the printf function. This function is located in the stdlib file that we included earlier, additionally, we can see that pass an argument to printf. The arguments are grouped within the parentheses and we have one quoted string. Using printf we can print to the console screen. We print the text and additionally, the \n represents a new line after the text. There are other special characters that we can print such as \t representing a tab and also we can use \b representing a backspace. Adding a semi-colon to the end of each line tells the compiler that it it the line end.

Congratulating yourself with a big pat on the back this is your first C program, or at least, the source.

Compiling the Program

Having spent hours slaving over the keyboard to create the source code we are ready to compile the code. As a result we will have an executable program. Using Linux we have have gcc, the Gnu C Compiler. Raspbian has this installed by default, while other distributions may need this installing. Using the compiler is easy as we will see in the next code snippet.

gcc -o hello hello.c

Using the option -o tells the compiler the output file to create. Although we have created the binary file now we still have the source code located in the hello.c file. Making the source code available with the binary is creating Open Source Software. As a result you are  contributing to the Linux community.

Executing the Program

Having now created the program we can execute it and see the results of our labors

./hello

Extending the Program

Having created our first C program on the Raspberry Pi we can move onto are next release of the software. Rather than making do with static text we can make the program more dynamic by passing arguments.

//This is an inline comment, commenting the rest of the line
/* This is a block comment that
continues across many lines
until it is closed*/
#include <stdio.h>
main (int argc, char **argv) {
  printf("Program %s is running\n", argv[0]);
  printf("Hello %s\n", argv[1]);
}

Locating the colored test you see the additions that we have made, additionally we detail these for you. Locating the main function you will notice that it now accepts arguments and we define the number of arguments with argc. This is an int or integer datatype. The next argument listed is a character array, argv. This will contain the arguments that we pass though to the program. Adding code to printf we add string place holder with %s. These placeholder are populated in the following arguments. Using argv[0] we print the program name and argv[1] represents the first arguments passed to the program at run time.

  • argv[0] : Index 0 is the first element in the array argv and represents the program name
  • argv[1] : Index 1 is the second element in the array and represent the first argument passed to the program

Running the program with an argument will now effect the displayed message. Don’t forget to compile again.

gcc -o hello hello.c
./hello fred

The video will step you through the process. The next lesson looks at using the IF statement to determine if we have arguments or not. Without arguments we can prompt for user input.