420 likes | 672 Views
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
E N D
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 • Application development tools • Part 2 • Functions • Pointers and Arrays • Part 3 • Characters and Strings • Data Types Structures • Part 4 • File Processing • Further Topics
Outline • Introduction • Structured program development • Application development tools • Compiling applications
Introduction • Developed late 70’s • Used for development of UNIX • Powerful • If used with discipline • ANSI Standard C • ANSI/ISO 9899: 1990
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
Program Structure • Collection of • Source files • header files • Resource files • Source file layout • Function layout
Source file layout • program.c Pre-processsor directives Global declarations main() { ………. } function1() { ………… } function2() { …………. }
Function Layout vartype function(vartypes ) { local variables to function statements associated with function …….. …….. }
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"); }
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
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; }
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;
Operators • Arithmetic operations • =, -, /, %, * • Assignment operations • =, +=. -=, *=, %=, /=, ! • Increment and decrement (pre or post) operations • ++, -- • Logical operations • ||, &&, ! • Bitwise operations • |, &, ~ • Comparison • <, <=, >, >=, ==, !=
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.
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);
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
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)
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”);
Control • Sequence Structures • Selection Structures • if… else statements • switch structures • Repetition Structures • for loops • while loops
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;
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.
Repetition Using while • Execute commands until the conditions enclosed by the while statement return false. while(conditions) { statements; }
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; }
while • Good practice to always use {} in a do while loop while(conditions) { statements…; Statements…; }
do … while • Good practice to always use {} in a do while loop do { statements…; Statements…; } while(conditions)
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.
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
Counter Controlled Repetition • Components of a typical for loop structure for(expression1; expression2; expression3) statement; example for(counter=1; counter<=10, counter++) statement;
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; }
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 */
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!
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++
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”);
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”);
Invoking the Compiler • Compiling FORTRAN Programs • g77 –o mycode [options] mycode.f • Compiling c/c++ Programs • gcc –o mycode [options] mycode.c