• 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 / Encapsulating Code with C Functions

Encapsulating Code with C Functions

October 10, 2016 by The Urban Penguin

Using C FunctionsEvery C program that is to be executable will require a function called main but others functions can be created. C functions encapsulate code, allowing the code to be run and maintained as its own unit. Functions may or may not return data, it is optional.  Writing C functions as we used in the last tutorial, required no return value. We simply set the terminal colour. Turning up the heat a little now we will look at using C functions to convert temperature values and we will be requiring values to be returned.

Convert Centigrade to Fahrenheit Using C Functions


Using C functions we will convert a temperature supplied in Centigrade to a displayed values in Fahrenheit. Keeping both us in Europe happy and you guys on the other side of the pond. Additionally, we could add in another function to convert in the reverse direction. We will keep that for another lesson. We will also need to ensure that we have, firstly, read the data in correctly from the user. User input will always be in the form of a string and we will need numeric data to run calculations.

The Complete Code

#include <stdio.h>
#include <stdlib.h>
float ctof ( int centigrade ) {
  float fahrenheit = (( centigrade * 9)/5)+32;
  return fahrenheit;
}
int main( int argc, char **argv ) {
  long c;
  float ret_f = 0.0;
  char *extra;
  if ( argc > 1 ) {
    c = strtol(argv[1],&extra,10);
    ret_f = ctof(c);
    printf("%ld degrees in C is %.2f degrees F\n",c,ret_f);
    return 0;
  } else {

    printf("You must supply a value\n");
    return 1;
    }
}

The Function

Defining the function happens early on and OUTSIDE of the main function.

float ctof ( int centigrade ) {
  float fahrenheit = (( centigrade * 9)/5)+32;
  return fahrenheit;
}

We allow it to return a floating decimal, (a number with decimal places). The formula is readily available from any google search. The parentheses control the order of the evaluations. The centigrade value is first multiplied by 9  and the result divided by 5. This all happens before 32 is added to the result.

Safely Converting User Data

We want to make sure that if a user types in their life story as an argument to the program, that we do not try to convert the entire input to a numeric value. We do this with the function strtol. This is supplied by the C standard library. Remember not all C functions need to be created by us.

c = strtol(argv[1],&extra,10);

Using this function we can control how much we read and here, we just read in 10 characters. Any extra is discarded into the array extra.

The complete video is shown for you below and I hope you enjoy.




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