Recent Comments

C

C is a general purpose programming language in wide use.

Download software: There is a list of free C compilers at https://www.thoughtco.com/list-of-free-c-compilers-958190. Some ones you might start with are Tiny C Compiler – Smallest Linux C Compiler, or Microsoft Visual Studio.

Cost: Free

Instructions to setup software: Follow the installation instructions for your compiler.

Hardware requirements: PC, Mac

Operating systems supported: Win, Mac, Linux

Difficulty level: 4 out of 5


TRY IT!

1. Open a file named f2c.c” in your favorite editor.

2. Copy this C code into your file.


// Example F to C program in "c" 
#include <stdio.h>  // This includes the file stdio.h into your code so it knows what the functions such as printf() mean.
const float LOW_TEMP_F_WARNING=0.;  // Program constants
const float HIGH_TEMP_F_WARNING=100.;
const int MAX_LOOP=5.;
int main() // Declaration of program 
{
  
  float temp_f; // Declaration of variables that the program will use
  float temp_c;
  int i; 
  for(i=0; i<MAX_LOOP; i++){   // loop
    printf("\nEnter the temperature in degrees F : ");     // Input the temperature to convert 
    scanf("%f",&temp_f);     // Reads the user input
    
    temp_c = (temp_f- 32)/1.8; // Math formula to convert Fahrenheit to Celsius
    
    printf("The temperature in Celsius (C) is  %f\n",temp_c); // Output the Celsius result
    
    if(temp_f > HIGH_TEMP_F_WARNING){ // Check for high temperature 
      printf("Remember to hydrate\n");
    }
    
    if(temp_f < LOW_TEMP_F_WARNING ){ // Check for low temperature
      printf("Remember to pack Long underwear\n");
    }
  }
  
  return(0); // exits the program
}

3. Save the File

4. Compile the program by typing “cc –o f2c f2c.c”

5. Run the program by typing “f2c”


ABOUT THE PROGRAM — A WALK THROUGH

1. This program

a. Prompts the user for the input temperature to convert

b. Reads the temperature and converts it to Celsius

c. Checks to see if the temperature is above 100 degrees F, and prints “Remember to hydrate” if that is true

d. Prints “Remember to pack long underwear” if the temperature is below 32 degrees

2. For a C program to run, it must have a “main” declaration. The line below shows a template for a main declaration. The executable lines of the program go between the braces. “C” statements end with a “;”.

int main() // Declaration of program 
{
: : :
};

3. A variable to hold a value is declared by “float temp_f;”. The statement “const float LOW_TEMP_F_WARNING=0.;” declares a variable that is fixed and cannot be changed.

4. The following line is statement that prints output to the screen.

printf("Remember to pack Long underwear\n");

5. An “if” statement compares the variable “f” to the static (fixed) variable “HIGH_TEMP_F_WARNING”. If the statement is true, the statements inside the “{“ and “}” are executed.

if(temp_f > HIGH_TEMP_F_WARNING){ // Check for high temperature 
: : :
}

6. A loop is done with a “for” statement. The loop will increment by a single integer, starting at 0 and stopping at MAX_LOOP-1 times.

for(i=0; i<MAX_LOOP; i++){   // loop
}

7. An include statement “#include identifies definitions to external library. The “stdio” or standard input/output library is a set of C functions to help with input and output.


TRY THIS

1. Change the temperatures used in the decisions — change the lower temperature from 60 to 30 degrees, for example. Make sure you change it in two places! Save the file and refresh the browser (or restart the web page), and enter new numbers – did the answers change at the new temperature?

2. Create a new temperature range from 30 to 60 degrees and have it display – “Bring hat and gloves!”

3. Change the wording of the phrases.

4. Add another text input – ask for the wind speed, for example.

5. Add some conditional statements that evaluate the wind chill factor.

6. Add some text to display the wind chill result.


LEARN MORE

http://www.cprogramming.com/tutorial.html

http://www.tutorialspoint.com/cprogramming/


RESOURCES, TIPS, TRICKS AND HINTS

There are many tutorials on C. You can Google for “C programming tutorials” or “Getting started in C.”

5 Comments on C

  1. If you’re providing an example of the C programming language, you probably shouldn’t use C++ style comments (“// this is a comment”) . Comments in C start with /* and end with */.

  2. program say what now // February 2, 2015 at 2:44 pm // Reply

    dis no makee sense

  3. hey c

  4. Example 2 above has an error. Semicolons are not allowed after braces.

Leave a Comment

Please don't use your real name.