Programming

C


C is a general purpose programming language in wide use.

Download software: There is a list of free C compilers at http://www.thefreecountry.com/compilers/cpp.shtml. Some ones you might start with are Tiny C Compiler – Smallest Linux C Compiler, Mingw, or Microsoft Visual Studio 2012 Express.

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 temperatature 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 Farenheit to Celcius
    
    printf("The temperature in Celisus (C) is  %f\n",temp_c); // Output the Celcius 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.”

Comments about “C”

Write a comment about “C”

Nickname:

Type your comment:

  • 1. Safety. Do the following:
      a. Show your counselor your current, up-to-date Cyber Chip.
      b. Discuss first aid and prevention for the types of injuries or illnesses that could occur during programming activities, including repetitive stress injuries and eyestrain.

    2. History. Do the following:
      a. Give a brief history of programming, including at least three milestones related to the advancement or development of programming.
      b. Describe the evolution of programming methods and how they have improved over time.

    3. General knowledge. Do the following:
      a. Create a list of 10 popular programming languages in use today and describe which industry or industries they are primarily used in and why.
      b. Describe three different programmed devices you rely on every day.

    4. Intellectual property. Do the following:
      a. Explain how software patents and copyrights protect a programmer.
      b. Describe the difference between licensing and owning software.
      c. Describe the differences between freeware, open source, and commercial software, and why it is important to respect the terms of use of each.

    5. Projects. Do the following:
      a. With your counselor’s approval, choose a sample program. Then, as a minimum, modify the code or add a function or subprogram to it. Debug and demonstrate the modified program to your counselor.
      b. With your counselor’s approval, choose a second programming language and development environment, different from those used for requirement 5a and in a different industry from 5a. Then write, debug, and demonstrate a functioning program to your counselor, using that language and environment.
      c. With your counselor’s approval, choose a third programming language and development environment, different from those used for requirements 5a and 5b and in a different industry from 5a or 5b. Then write, debug, and demonstrate a functioning program to your counselor, using that language and environment.
      d. Explain how the programs you wrote for requirements 5a, 5b, and 5c process inputs, how they make decisions based on those inputs, and how they provide outputs based on the decision making.

    6. Careers. Find out about three career opportunities in programming. Pick one and find out the education, training, and experience required. Discuss this with your counselor and explain why this career might be of interest to you.

  • Earning the Cyber Chip can help you learn how to stay safe while you are online and using social networks or the latest electronic gadgets.

    Topics include cell phone use, texting, blogging, gaming, cyberbullying, and identity theft. Find out more about the Cyber Chip at www.scouting.org/cyberchip.