User Tools

Site Tools


notes:notes:projects:c

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

notes:notes:projects:c [2010/02/25 14:46] (current)
Line 1: Line 1:
 +<WRAP left 40%>
 +~~TOC~~
 +</​WRAP>​
 +//
 +//
 +<WRAP centeralign 90% bigger>
 +<WRAP bigger fgred>​CSCS 2850: Projects</​WRAP>​
 +<WRAP muchbigger>​C Programming</​WRAP>​
 +Notes & Documentation
 +</​WRAP>​
 +<WRAP left 50%>
  
 +</​WRAP>​
 +<WRAP clear></​WRAP>​
 +
 +=====C Programming =====
 +
 +C is a general-purpose computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system.
 +\\
 +C source code is free-form witch allows arbitrary use of whitespace to format code, rather than column-based or text-line-based restrictions.
 +
 +ok So lets take a look at what a program should look like and what some of these strange new things do
 +<​code>​
 +/*this is a comment block in C */
 +
 +/​***********************************************************************
 +*the #include is a pre processor directive. in this                    *
 +*instance we are loading the stdio.h header file                       *
 +*this gives us access to the printf() scanf() functions. ​              *
 +*also the <> surrounding the stdio.h represent that we                 *
 +*want to look for the file stdio.h in the standard library directory ​  *
 +*it could also be enclosed in ""​ i belive that searches for the file in* 
 +*the current working directory. ​                                       *
 +***********************************************************************/​
 +
 +#include <​stdio.h>​
 +
 +/​*******************************************
 +*now we start the main body of our program.* ​
 +*this is the main module of our program. ​  *
 +*******************************************/​
 +
 +
 +int main()
 +{
 +
 +/​******************************************************** ​
 +*code instructions would go here! note most instructions*
 +*will be followed by a semicolon you will see this      *
 +*in some of the examples. ​ note the {} braces ​          *
 +*they are needed according to c syntax and then the     *
 +*return statement ​                                      *
 +********************************************************/​
 +
 +return 0;
 +}
 +
 +
 +</​code>​
 +
 +ok so now that we have a basic understanding of some things lets write a simple "Hello World" Program ​
 +
 +<​code>​
 +#include <​stdio.h>​
 +int main()
 +{
 +/​********************************************************
 +*use printf() to print the string Hello world! to stdout*
 +printf() will print what u put into the parentheses ​    ​* ​
 +********************************************************/ ​
 +   ​printf("​Hello World!"​);​
 +   ​return 0;
 +}
 +</​code>​
 +
 +ok so now that we have a working program lets take a look at something a bit more 
 +we will introduce some of the data types in c
 +
 +<​code>​
 +#include <​stdio.h>​
 +int main()
 +{
 +/​*******************************************
 +*Declare integer Data Type. (whole Numbers)* ​
 +*******************************************/​
 +int myage = 24;
 +int year = 2010;
 +
 +/​*****************************************************
 +*lets declare some decimals or Floating point numbers (real numbers)*
 +******************************************************/​
 +
 +float myweight = 155.5;
 +float rate = 9.99;
 +double examples = 355.66667;
 +
 +/​*******************************************************
 +*now that we have some variables of diffrent data types* ​
 +*lets print them to the screen with printf()************
 +*******************************************************/​
 +printf("​i am %d years old", myage);
 +printf("​my weight is %f pounds",​ myweight);
 +return 0; 
 +}
 +</​code>​
 +so here we have integers and floating point numbers whole numbers and real numbers\\
 +the printf() statement has been modified from out last example however\\
 +this time we are passing in variables to printf()\\
 +i use the %d in the string im printing to represent\\
 +a decimal and %f to represent floating point\\
 +here is a table of some of these codes\\
 +^Code^ ​   Meaning ​     ^
 +| %c |Character |
 +| %d |Signed integer ​  |
 +| %i |Signed integer ​  |
 +| %e |Scientific notation small e|
 +| %E |Scientific notation big E|
 +| %f |Floating Point   |
 +| %g |use Shorter of %e or %f|
 +| %G |use shorter of %E or %f|
 +| %o | OCtal    |
 +| %u |Unsigned integer|
 +| %s |String|
 +| %x |unsigned hex lowercase|
 +| %X |same but uppercase|
 +| %p |Pointer |   
 +Here is another table showing the range of what these data types can hold
 +
 +^DATA TYPE ^RANGE OF VALUES^
 +|char|-128 to 127|
 +|Int|-32768 to +32767|
 +|float|3.4 e-38 to 3.4 e+38|
 +|double|1.7 e-308 to 1.7 e+308|
 +Ok now that we've printed things to the stdout lets get some user input in our program
 +<​code>​
 +#include <​stdio.h>​
 +int main()
 +{
 +/​******************************************************
 +*first we need to make a variable to hold the input in*
 +******************************************************/​
 +
 +int age;
 +
 +/​********************
 +*ask user a question*
 +********************/  ​
 +   
 +   ​printf("​How old are you?​\n"​);​
 +
 +/​*************************
 +*use scanf() to get input*
 +**************************
 +   ​scanf("​%d",​ &age);
 +
 +return 0;
 +}
 +</​code>​
 +"​scanf"​ stands for "scan format",​ because it scans the input for valid tokens and parses them according to a specified format.\\
 +just like with printf() we need to specify to scanf() what type of data to expect thus the first parameter is %d \\
 +the next parameter &age points to the memory location of the variable age for the information to be stored into\\
 +
 +=====C++ Programming=====
 +Ok So im just going to break down a simple conversion C++ program and explain it step by step.\\
 +ok First we have our #include statement iostream this allows the use of the cin/cout functions\\
 +some what like the scanf() printf() functions in regards that they both read and write to stdout\\
 +also included in "using namespace std" this tells the compiler to treat the names in the standard\\
 +library as though we had defined them in the current program. thus eliminating the std::cout << \\
 +<code c>
 +
 +#include <​iostream>​
 +
 +using namespace std;
 +</​code>​
 +in this section we are declaring our main function and then declaring our variables\\
 +variables and data types are described briefly above in the C Programming section\\
 +<code c>
 +int main()
 +
 +{
 +
 + int selection;
 + double F;
 + double C;
 + char x = '​y';​
 + double inch;
 + double cm;
 + double pounds;
 + double kilo;
 + char yn;
 +</​code>​
 +first off in this section we have a system call.. this system call clears the screen \\
 +next a basic cout statement to print our output to the screen notice the << "​insertion Operator"​\\
 +they are needed as part of the syntax. and lastly we have another << endl this inserts a new line\\
 +after our sentance!
 +<code c>
 + system("​clear"​);​
 + cout <<"​Thank you for using this conversion program!"​ << endl;
 +</​code>​
 +do is an iterative statement in c++ in this case all the statements ​ below this will exacute until\\
 +it comes to a while condition that looks something like this\\  ​
 +while(a == a);\\
 +if the condition is true then the proccess will be repeated if not then it will continue on\\
 +In this case our while statement is almost the last line of our program before the return 0;\\
 +the reason for that is because we put our whole program into a loop so we could ask the user\\
 +if they would like to use our program again before exiting.
 +<code c>
 + do
 +
 +</​code>​
 +next again we have our very basic cout statements followed by our insertion operator\\
 +followed by our string or sentence another insertion operator and then and endline statement\\ ​
 +these lines will print to the screen and are ment as a menu for our users to choose from\\
 +<code c>
 + cout <<"​Type in the number for the conversion you would like:" <<​endl;​
 + cout <<"​1. Degrees Fahrenheit to degrees Celsius."​ << endl;
 + cout <<"​2. Degrees Celsius to degrees Fahrenheit."​ << endl;
 + cout <<"​3. Inches to Centimeters."​ << endl;
 + cout <<"​4. Centimeters to Inches."​ << endl;
 + cout <<"​5. Pounds to kilograms."​ << endl;
 + cout <<"​6. Kilograms to pounds."​ << endl;
 +</​code>​
 +ok so now that we have a menu with choices we need to be able to grab what choice the user inputs!\\
 +to do this we use the cin command and the extraction operator >> to the right of that is our variable\\
 +<code c>
 + cin >> selection;
 + system("​clear"​);​
 +</​code>​
 +next we use a switch. a switch is used to compare a variable to possible values of that variable\\
 +if the value is the same as the value in the variable then it will execute the commands for that case\\
 + 
 +<code c>
 + switch (selection)
 + {
 +</​code>​
 +
 +<code c>
 + case 1:
 + {
 + do
 + {
 + system("​clear"​);​
 + cout <<"​Enter your fahrenheit tempature: ";
 + cin >> F;
 + cout << endl;
 + C = (F - 32);
 + C = C / 1.8;
 + cout <<"​Your Fahrenheit tempature was: " << F << endl; 
 + cout <<"​Your Centigrade tempature is: "<<​ C << endl;
 + cout <<"​Would you like to enter another Tempature?"​ << endl;
 + cout <<"​y = yes \nn = no" << endl;
 + cin >> x;
 + }while(x == '​y'​ || x == '​Y'​);​
 +
 + break;
 +
 +       }
 + case 2:
 + {
 + do
 + {
 + system("​clear"​);​
 + cout <<"​Enter Your Celsius tempature : ";
 + cin >> C;
 + cout << endl;
 +
 + F = C * 1.8;
 + F = C + 32;
 +
 + cout <<"​Your Centigrade Tempature was: " << C << endl;
 + cout <<"​Your Fahrenheit tempature is: " << F << endl;
 + cout <<"​Would you like to enter another Tempature?"​ << endl;
 + cout <<"​y = yes \nn = no" << endl;
 +
 + cin >> x;
 +
 + } while(x == '​y'​ || x == '​Y'​);​
 + break;
 + }
 + case 3:
 + {
 + do 
 + {
 + system("​clear"​);​
 + cout <<"​Enter your Length in Inches: ";
 + cin >> inch;
 + cout << endl;
 + cm = inch * 2.54;
 + cout <<"​Your length in iches is: " << inch << endl;
 + cout <<"​Your length in Centimeters is: " << cm << endl;
 + cout <<"​Would you like to enter another length?"​ << endl;
 + cout <<"​y = yes \nn = no" << endl;
 +
 + cin >> x;
 +
 + } while(x == '​y'​ || x == '​Y'​);​
 + break; ​
 + }
 + case 4:
 + {
 +   do
 + {
 + system("​clear"​);​
 + cout <<"​Enter your Length in Centimeters:​ ";
 + cin >> cm;
 + cout << endl;
 + inch = cm / 2.54;
 + cout <<"​Your Length in centimeters was: " << cm << endl;
 + cout <<"​Your length in inches is: " << inch << endl;
 + cout <<"​Would you like to enter another length?"​ << endl;
 + cout <<"​y = yes \nn = no" << endl;
 +
 + cin >> x;
 + } while(x == '​y'​ || x == '​Y'​);​
 + break;
 + }
 + case 5:
 + {
 + do
 + {
 + system("​clear"​);​
 + cout <<"​Enter The weight you wish to have converted: ";
 + cin >> pounds;
 + cout << endl;
 + kilo = pounds / 2.2;
 + cout <<"​The weight in pounds is: " << pounds << endl;
 + cout <<"​The weight in kilograms is: " << kilo << endl;
 + cout <<"​Would you like to enter another weight?"​ << endl;
 + cout <<"​y = yes \nn = no" << endl;
 +
 + cin >> x;
 + } while(x == '​y'​ || x = '​Y'​);​
 + break;
 + }
 + case 6:
 + {
 + do
 + {
 + system("​clear"​);​
 + cout <<"​Enter the weight you wish to have converted: ";
 + cin >> kilo;
 + cout << endl;
 + pounds = kilo * 2.2;
 + cout <<"​The weight in kilograms is: " << kilo << endl;
 + cout <<"​The weight in pounds is: "<<​ pounds << endl;
 + cout <<"​Would you like to enter another weight?"​ << endl;
 + cout <<"​y = yes \nn = no" << endl;
 +
 + cin >> x;
 + } while(x == '​y'​ || '​Y'​);​
 + break;
 + }
 + default :
 + {
 +   cout <<"​You have entered an invalid number."​ << endl;
 +     sleep(3);
 +   system("​clear"​);​
 +
 + }
 +
 + }
 +
 + system("​clear"​);​
 + cout <<"​would you like to do another conversion?"​ << endl;
 + cout <<"​y = yes \nn = no" << endl;
 +
 + cin >> yn;
 +
 + system("​clear"​);​
 +
 + } while(yn == '​y'​ || yn == '​Y'​);​
 +
 +
 +
 + return 0;
 +
 +}
 +
 +</​code>​
 +
 +=====C/C++ Syntax=====
 +^ C          ^ C++         ^ result ​            ^
 +| printf ​    | cout << ​    | output ​            |
 +| scanf      | cin >> ​     | input              |
 +
 +=====Demonstration Code=====
 +The C Demonstration Code is located [[notes:​projects:​cpp_demo|here]]. The context of the code will be explained through other demonstrations. The code page is only meant to be a repository for gathering the code in its original form.
 +
 +Download the code using the links found at the download page.  While you can copy and paste the code directly from the wiki text, this is method is discouraged as typos in each edit may not have passed through a compiler. ​ Each major revision is compiled and tested before it is made available for download. ​ The moral of the story is, use the links.
 +
 +If you're downloading the code to a Windows machine, you'll want to open it in using something like wordpad.  ​
 +
 +Once you get the code to your shell, simply compile it using your favorite C compiler. ​ For example, if you were using gcc you would issue the following command from the directory where you downloaded the files.
 +
 +<cli>
 +:~$ gcc -o converter converter.c
 +</​cli>​
 +
 +Once the code is compiled, you can run the program using the command ./​converter.
 +
 +<cli>
 +:~$ ./converter
 +</​cli>​
 +
 +  *[[notes:​projects:​cpp_demo|C Programming Demonstration Code]]
notes/notes/projects/c.txt · Last modified: 2010/02/25 14:46 (external edit)