DOC PREVIEW
UCSD PHYS 121 - C-Programming

This preview shows page 1-2-23-24 out of 24 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

C-ProgrammingWhy C?C How it Stacks UpWhat we will and won’t doC File TypesA typical (short) programMore on programAlternate formVariable typesOutput of previous programFeeding data to the programSlide 12ResultFor LoopsMathMath WarningsCastingTalking to the Parallel Port in WindowsSample code (stripped down to fit on slide)Looping to make a waveformHow does it look In Linux/Unix?LPT Method on WindowsDescriptionReferencesC-ProgrammingC-ProgrammingPart I: basicsPart I: basicsprep. for Lab 8prep. for Lab 8Winter 2012UCSD: Physics 121; 20122Why C?Why C?•See See http://www.tiobe.com/tpci.htmhttp://www.tiobe.com/tpci.htm02/07 02/07 rankrank02/06 02/06 rankrankmovementmovementLanguageLanguageshareshare in last in last yearyear1 1 1 1 ==Java Java 18.978% 18.978% -3.45% -3.45% 2 2 2 2 ==C C 16.104% 16.104% -2.23% -2.23% 3 3 3 3 ==C++ C++ 10.768% 10.768% -0.53% -0.53% 4 4 5 5 PHP PHP 8.847% 8.847% -0.07% -0.07% 5 5 4 4 (Visual) Basic (Visual) Basic 8.369% 8.369% -1.03% -1.03% 6 6 6 6 ==Perl Perl 6.073% 6.073% -0.63% -0.63% 7 7 8 8 Python Python 3.566% 3.566% +0.90% +0.90% 8 8 7 7 C# C# 3.189% 3.189% -0.78% -0.78% 9 9 10 10 JavaScript JavaScript 2.982% 2.982% +1.47% +1.47% 10 10 20 20  10 10 Ruby Ruby 2.528% 2.528% +2.12% +2.12%Winter 2012UCSD: Physics 121; 20123C How it Stacks UpC How it Stacks Up•As U can C, the As U can C, the C languageC language (and its (and its extensions/derivativesextensions/derivatives) dominates the software ) dominates the software community community –Java also a strong showing–Python worth a peek•AdvantagesAdvantages of C: of C:–compiled code runs FAST–allows low-level device control–a foundation of the programming world•DisadvantagesDisadvantages of C: of C:–strings are a pain in the @$$–awkward conventions (pointers can be difficult to learn)–requires a compilerWinter 2012UCSD: Physics 121; 20124What we will and won’t doWhat we will and won’t do•We will learn:We will learn:–to write simple programs–basic interface–control flow, math, printing–data types–enough to be dangerous•We won’t learn:We won’t learn:–advanced pointer operations–large projects (linking separate programs)–distinctions between public, private, external variables–enough to be really dangerousWinter 2012UCSD: Physics 121; 20125C File TypesC File Types•Source CodeSource Code–the stuff you type in: has .c extension•Compiled “Executable”Compiled “Executable”–the ready-to-run product: usually no extension in Unix, .exe in DOS•Header FilesHeader Files–contain definitions of useful functions, constants: .h extension•Object FilesObject Files–a pre-linked compiled tidbit: .o in Unix, .obj in DOS–only if you’re building in pieces and linking laterWinter 2012UCSD: Physics 121; 20126A typical (short) programA typical (short) program•Notes:Notes:–first include is so we have access to printf (standard I/O)–define the main program (must be called main) to take no arguments (thus void) and return an integer–braces surround the program–print value of integer, i, in formatted line–return zero (common return value for successful program)#include <stdio.h>int main(void){ int i=53; printf(“The illustrious variable, i, is %d\n”,i); return 0;}Winter 2012UCSD: Physics 121; 20127More on programMore on program–semicolons end each line within program–spacing is not required, but makes for easier reading–all variables must be declared before they are used–could have simply said: int i; then declared later that i=53;–the \n is a newline; the %d formats as decimal integer#include <stdio.h>int main(void){ int i=53; printf(“The illustrious variable, i, is %d\n”,i); return 0;}Winter 2012UCSD: Physics 121; 20128Alternate formAlternate form–semicolons delimit separate statements, but this program, while compact, is harder on the eyes–this time, we defined and assigned the variable in separate steps (more commonly done)–we shortened the print statement fluff–the format is now 4 characters wide, forcing leading zeros•output will be: i = 0053–could compactify even more, if sadistic#include <stdio.h>int main(void){ int i; i=53; printf(“i = %04d\n”,i); return 0; }Winter 2012UCSD: Physics 121; 20129Variable typesVariable types#include <stdio.h>int main(void){ char c; // single byte int i; // typical integer long j; // long integer float x; // floating point (single precision) double y; // double precision c = 'A'; i = 356; j = 230948935; x = 3.14159265358979; y = 3.14159265358979; printf("c = %d = 0x%02x, i = %d, j = %ld, x = %f, y = %lf\n",c,c,i,j,x,y); c = i; i = 9259852835; printf("c = %d, i = %d, x = %.14f, y = %.14lf\n",c,i,x,y); return 0;}Winter 2012UCSD: Physics 121; 201210Output of previous programOutput of previous program•Output looks like:Output looks like:•Notes:Notes:–c “wrapped” around 256 when assigned to be 356–i couldn’t handle the large value, and also wrapped•int is actually the same as long on this machine–The float can’t handle the full precision set out–broke printf line: spacing irrelevant: semicolons do the work–The d, x, ld, f, and lf format codes correspond to decimal, hex, long decimal, float, and long float, respectivelyc = 65 = 0x41, i = 356, j = 230948935, x = 3.141593, y = 3.141593c = 100, i = 669918243, x = 3.14159274101257, y = 3.14159265358979Winter 2012UCSD: Physics 121; 201211Feeding data to the programFeeding data to the program•Command line arguments allow the same program to Command line arguments allow the same program to be run repeatedly with different inputs (very handy)be run repeatedly with different inputs (very handy)•How to do it:How to do it:–main() now takes arguments: traditionally argc and argv[]–argc is the number of command line arguments•minimum is one: the command itself–argv[] is an array of strings (words)•one for each of the space-separated blocks of text following the command on the command line–C arrays are numbered starting at zero–The command line entry: one_ray -10.0 1.0 0.0 has:•argc = 4•argv[0] = one_ray; argv[1] = -10.0; etc.Winter 2012UCSD: Physics 121; 201212#include <stdio.h> // for printf(),sscanf()#include <stdlib.h> // for exit()int main(int argc, char* argv[]){ int int_val; double dbl_val; if (argc


View Full Document

UCSD PHYS 121 - C-Programming

Download C-Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view C-Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view C-Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?