1 / 40

Introduction to the C Programming Language

Introduction to the C Programming Language. Michael Griffiths Corporate Information and Computing Services The University of Sheffield Email m.griffiths@sheffield.ac.uk. Course Outline. Part 1 Introduction to C Programming Structured Program Development and Program Control

kerry
Télécharger la présentation

Introduction to the C Programming Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to the C Programming Language Michael Griffiths Corporate Information and Computing Services The University of Sheffield Email m.griffiths@sheffield.ac.uk

  2. Course Outline • Part 1 • Introduction to C Programming • Structured Program Development and Program Control • Application development tools • Part 2 • Functions • Pointers and Arrays • Part 3 • Characters and Strings • Data Types Structures • Part 4 • File Processing • Further Topics

  3. Outline • Introduction • Structured program development • Application development tools • Compiling applications

  4. Introduction • Developed late 70’s • Used for development of UNIX • Powerful • If used with discipline • ANSI Standard C • ANSI/ISO 9899: 1990

  5. Program Development • Edit • Create program and store on system • Preprocessor • Manipulate code prior to compilation • Compiler • Create object code and store on system • Linker • Link object code with libraries, create executable output • Loader • Execution • CPU executes each instruction

  6. Program Structure • Collection of • Source files • header files • Resource files • Source file layout • Function layout

  7. Source file layout • program.c Pre-processsor directives Global declarations main() { ………. } function1() { ………… } function2() { …………. }

  8. Function Layout vartype function(vartypes ) { local variables to function statements associated with function …….. …….. }

  9. Hello World /*Program1: Hello World*/ #include <stdio.h> main() { printf("Welcome to the White Rose Grid!\n"); /*Welcome banner on several lines*/ printf("Welcome to the \n \t White Rose Grid!\n"); }

  10. Features of Hello World • Lots of comments • Enclosed by /* */ • Statements terminated with a ; • Preprocessor statement • #include <stdio.h> • Enables functions to call standard input ouput functions (e.g. printf, scanf) • Not terminated with a ; • Printf uses escape sequence characters e.g. \n newline \t tab character

  11. Standard Conforming Hello World /*Program1: Hello World*/ #include <stdio.h> int main(void) { printf("Welcome to the White Rose Grid!\n"); /*Welcome banner on several lines*/ printf("Welcome to the \n \t White Rose Grid!\n"); return 0; }

  12. Variable Types

  13. Variables • Other types using unsigned and long • long double, long int, short int, unsigned short int • Precision and range machine dependent • Variables of the same type are compared using the comparison operator == • Variable declaration using the assignment operator = float myfloat; float fanother=3.1415927;

  14. Operators • Arithmetic operations • =, -, /, %, * • Assignment operations • =, +=. -=, *=, %=, /=, ! • Increment and decrement (pre or post) operations • ++, -- • Logical operations • ||, &&, ! • Bitwise operations • |, &, ~ • Comparison • <, <=, >, >=, ==, !=

  15. Input and Output Using stdio.h • printf • Provides formatted input and output • Input for printf is a format specification followed by a list of variable names to be displayed • printf(“variable %d is %f\n”, myint, myfloat); • scanf • Provided an input format and a list of variables • scanf(“%d”, &myint); • Note variable name has & in front • Programarith.c is an example of the arithmetic operaions, printf and scanf.

  16. Example of printf and scanf • Example program arith.c • Worksheet problem 3, use the decrement (--) and increment operators(++) to modify the variables sum and difference • Add a line requesting the user to input a floating point variable called f1 • Add a line to multiply the new variable f1 by the variable sum • Add a line to display the variable f1 /*Request input from the user*/ printf("Enter the first integer\n"); scanf("%d", &i1); /*Read in the integer*/ printf("Enter the second integer\n"); scanf("%d", &i2);

  17. Escape characters

  18. Format Specifiers for printf and scanf

  19. Compilation • To compile the program myprog.c using the GNU C Compiler • gcc myprog.c –o myprog • Example compile arith.c • Modify program arith.c to test the effect of the decrement and increment operations • Modify program arith.c and test the assignment operations

  20. Starting devshed • Create a directory ccourse • Download the following file into your ccourse directory http://wrgrid.group.shef.ac.uk//downloads/courses/c_cic6006/introtoc.exe • Open a DOS console window and change directory to the ccourse directory to expand the downloaded zip file type introtoc • From the start menu find the devshed application and start the dev C++ integrated development environment (IDE)

  21. Compiling the examples using devshed • From devshed menu select file and open project or file • Move to the ccourse directory and select one of the example files • From the menu select execute and compile • Using the DOS window you should see a ..exe file just type the name to run and test the program • To run the program under deveshed you may need to do the following • Before the return statement in the main function add the line • system(“PAUSE”);

  22. Control • Sequence Structures • Selection Structures • if… else statements • switch structures • Repetition Structures • for loops • while loops

  23. Conditional Statements Using if…else • The if statement allows decision making functionality to be added to applications. • General form of the if statement is: if(condition) statement;

  24. Using else • An alternative form of the if statement is if(condition) statement; else statement; If the condition is true the first statement is executed if it is false the second statement is executed.

  25. Repetition Using while • Execute commands until the conditions enclosed by the while statement return false. while(conditions) { statements; }

  26. Simple if Example Demonstrating Syntax int main(void) { float f1; printf("Enter a floating point number.\n"); scanf("%f",&f1); if(f1<0) printf("Please enter a value gerater 0\n"); else if (f1>100) printf("f1 is greater than 100\n"); else printf("The value is %f\n",f1); return 0; }

  27. while • Good practice to always use {} in a do while loop while(conditions) { statements…; Statements…; }

  28. do … while • Good practice to always use {} in a do while loop do { statements…; Statements…; } while(conditions)

  29. While example demonstrating a countdown int main (void) { int n; printf( "Enter the starting number\n"); scanf("%d".&n); while (n>0) { printf("%d, ",n); --n; } printf("FIRE!\n"); return 0; } Try this code then modify it by introducing a second variable m initialised to 10. Use the && operator to add a test m<10 to the Condition in the while statement.

  30. Example of while and if statement usage Continue counting until Maximum number of files entered (5) while(files<=5) { printf("Enter file location(1=Titania, 2=Maxima): "); scanf("%d", &result); if(result==1) ntitania_files = ntitania_files+1; else if(result==2) nmaxima_files = nmaxima_files+1; else nother_files=nother_files+1; files++; }/*End of while file processing loop*/ Request and get user input Use conditions to update variables Increment counter

  31. Counter Controlled Repetition • Components of a typical for loop structure for(expression1; expression2; expression3) statement; example for(counter=1; counter<=10, counter++) statement;

  32. for loop example • Example program for.c • Modify the program so that it performs a count down main() { int counter, nsteps=10; /* initialisation, repetition condition and increment */ /* are all included in the for structure header */ for(counter=1; counter<=nsteps; counter++) printf("%d\n", counter); return 0; }

  33. Multiple selection Structures Using Switch • Used for testing variable separately and selecting a different action switch(file) { case 'm': case 'M': ++nMaxima; break; case 't': case 'T': ++nTitania; break; default: /*Catch all other characters*/ ++nOther; break; } /*End of file check switch */

  34. Practical Examples – basic coding Inspect, Compile and run the following Finding a root using the Newton-Raphson method While statement Finding a root by method of bisection If statement, while statement And simple one line function!

  35. Application Development Tools • Minimal for Windows (MinGW) • http://www.mingw.org/ • Bloodshed • c++ open source for windows • Available on managed windows • http://www.bloodshed.net/ • Eclipse with the c development toolkit • Open source and available for windows and linux • Available on iceberg • http://www.eclipse.org/ • Salford (C and FORTRAN) • Obtain from IT centre, not open source, site license is available • Microsoft visual c++

  36. Starting a New Project in devshed • From the menu select File and New Project • From the dialog that opens • Set the name of the project to the same name as the course c file • Click the c-project • Select project type console application • Add a source file to the project • From the menu select project and Add to Project • If there is a main.c remove that file from the project pane (normally on the left hand side) • Before the return statement in the main function add the line • system(“PAUSE”);

  37. Starting a New Project in devshed • From the menu select File and New Project • From the dialog that opens • Set the name of the project to the same name as the course c file • Click the c-project • Select project type console application • Add a source file to the project • From the menu select project and Add to Project • If there is a main.c remove that file from the project pane (normally on the left hand side) • Before the return statement in the main function add the line • system(“PAUSE”);

  38. Compilers

  39. Invoking the Compiler • Compiling FORTRAN Programs • g77 –o mycode [options] mycode.f • Compiling c/c++ Programs • gcc –o mycode [options] mycode.c

  40. Options Used with Both gnu and Portland Compilers

More Related