html5-img
1 / 42

C Programming COP 3223 lecture 1

C Programming COP 3223 lecture 1. Raja Iqbal. About Myself. Graduate Student at the School of EECS Areas of Interest are Image Compression, Signal Processing, Hardware and ASIC Design and FPGA Architectures Work Experience Graduate Research Assistant, School of EECS

albert
Télécharger la présentation

C Programming COP 3223 lecture 1

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. C ProgrammingCOP 3223lecture 1 Raja Iqbal

  2. About Myself • Graduate Student at the School of EECS • Areas of Interest are Image Compression, Signal Processing, Hardware and ASIC Design and FPGA Architectures • Work Experience • Graduate Research Assistant, School of EECS • Intel Corporation( Summer/Fall intern) • Fedex Services (Summer Intern)

  3. Pre-requisites • It is better to have some programming experience but I will assume no programming experience. • Some logic Design background is always helpful

  4. Today’s Objectives • To recognise the purpose and structure of the course • To develop a good learning strategy • To build your first program. • To declare basic C types • To develop simple programs to do calculations • To use printf and scanf

  5. Course Objectives • To introduce the C programming language • To provide awareness of the role which C plays in modern practical software development • To study in detail the application of C in the integration and maintenance of practical projects

  6. Course Objectives On completion of this syllabus you should be able to • develop software in C using sound software engineering principles; • understand the advantages and limitations of C • demonstrate understanding of the role which C plays in practical software development;

  7. Assessment • Assignments 40% • Lab Attendance and participation in Class discussions 10% • Final Exam 25% ( Open Book) • Surprise Quizzes 25% (Open Book)

  8. Class meetings • Thursday 1730- 2000 COMM 101

  9. Books There are millions of books on C Programming. I will name few that I like. • Robert Lafore,Turbo C Programming for the PC and Turbo C++, Revised Edition, Howard W Sams & Company • Herbert Schildt, C The Complete Reference, second edition, McGrawHill • Kernighan, B W, Ritchie, D M: The C Programming Language (second edition), Prentice Hall, 1983

  10. Advice( Do nots) • Memorise the code without an understanding of underlying principles. • Get your friends to do the coursework for you. • Aim just to get a passing grade • Let the instructor proceed in tutorials when you have confusions • Start the coursework the night before it is due.

  11. Caution • The C we will use is standard C. • Any standard C system will do, e.g. Turbo C, Unix. • Experiment with what files you need to bring home, if you plan to work at home. • Avoid machine-specific features

  12. Advice • It is your responsibility to make sure that the code you are submitting runs correctly on the olympus machines. • You may use any development environment which you are comfortable with.

  13. Using the environment • For each program that you write create a project • A project may have one or more files • ".c" files contain source • ".h" files we meet later • ".txt" files are for documentation • ".cpp" files – For this course we will not use them • For this course we will have only one ".c" file

  14. The Hello World Program • Traditional first example program #include <stdio.h> void main(void) { printf(“Hello, world\n”); }

  15. Output The following will be the output of the Hello World program Hello, world -

  16. The compilation process • Preprocessing (anything beginning # is resolved) • Compiling - generates the object code for each source file • Linking (binding) - links the separate source files and the standard libraries to make an executable file.

  17. Syntax errors • You have broken the C "rules of grammar" • The compiler may report several errors for one mistake • Fix the syntax errors one at a time and recompile • Don't despair! • Warnings have to be fixed too.

  18. Link-time errors • After compilation, there may be link errors • Inconsistent function names • No "main" • More than one "main" • The compiler being awkward (save and re-invoke the studio)

  19. Run-time (semantic) errors • The program does not do what you want • This is nearly always your own error • Look carefully through the code • Else use the debugger (we meet this later)

  20. The compiler is picky • Picks on any minor error • Has no common sense • Programming can be frustrating and yet very rewarding

  21. Variables • To perform calculations we need space to hold data. This means declaring variables. • All variables must have a valid name • All variables have a type • Variables may be initialised

  22. Variable names Variable names are example of identifiers.They have the following rules: • Must begin with a letter or _ • Followed by zero or more letters, numerals or _ • No spaces allowed • Case sensitive • Not a reserve word e.g. int, if, while • Have to be meaningful

  23. 123 a123 _fred num people num_employees num-staff Are they the same? VALUE Value value VaLuE _Value_ Are the following variable names valid?

  24. Whole number types char short intorshort int long int or long Floating point types float double long double Basic types

  25. Example declarations int counter1; long num_records = 0; double length = 3.7, width = 2.7; • Separation with commas • Initialisation is optional • If uninitialised, the value could be anything;

  26. Global and local variables • A variable declared outside a function (e.g. main) is global. • A variable declared inside a function is local. • Global variables are like nuclear power • Some think they should not be used at all. • Others think that they may be used with great caution.

  27. Rules of number • Addition a+2; • Subtraction a-2; • Multiplication a*2; • Unary plus +3.7 same as 3.7; • Unary minus -33;

  28. The problem with division What is 5 divided by 2? • Answer A: 2 and a remainder of 1 • This is called integer division • Answer B: 2.5 • This is called real division • In C both are represented by /

  29. Rule for division • If both whole numbers, we get integer division. • To get remainder we use % • If either or both are floating point we get real division • 5/2 is 2 • 5%2 is 1 • 5.0/2 or 5/2.0 or 5.0/2.0 is 2.5

  30. Output of data • printf always has a format string with zero or more places for variable to be written. • One extra argument per % printf("There are %d people here.\n", num_people); • Types must match or else you will get garbage.

  31. % formats %d for integer %e for float in scientific notation %f for float in decimals %g chooses %e or %f format for you %c for ASCII character %le for a double in scientific notation %.2f for float to 2 places of decimals

  32. Other symbols \% or %% for literal % sign \" for literal " \n for new line \t for tab \\ for literal backslash

  33. Input of numbers • We use a function scanf - part of a larger family • We must precede each variable by & • We may scan in any amount of data scanf("%d %f", &my_int, & my_float); • We use fflush to clear any unmatched input fflush(stdin);

  34. Assignment • This is a valid C statement x + 2; • What does it do? • To copy or store the results of a calculation y = x + 2; • There can be multiple ='s on one line a = 5 + b = c * 2;

  35. Centigrade to Fahrenheit 1. Include stdio.h and declare main 2. Declare space for variables 3. Input the value in centigrade 4. Do the calculation 5 Output the answer.

  36. #include <stdio.h> void main (void) { float cent, fahr; printf("Enter temperature in Centigrade\n"); scanf("%f",&cent); fahr = cent * 9/5 + 32; printf("In Fahrenheit this is %.2f degrees\n",fahr); }

  37. Short cut operators • Why write a = a + b; instead write a+= b; • Works too for -=, *=, /=, %= • Why write a = a + 1; or a+=1; instead write a++ or ++a • Works too for a-- or --a • In an expression ++a done first a++ done last

  38. Precedence Order in which arithmetic operations are evaluated • (anything in brackets) highest • unary +,- • *,/,% • +, - • =, +=, -=, *= etc. lowest

  39. Unsigned qualifier • unsigned means that the +ve is doubled but the -ve range is sacrificed unsigned int population; • Int –32768 to 32767 • Unsigned int 0 - 65535

  40. Casting • You can change one type to another thus: int x; float y; : : x = (int) y; Note that this truncates and does not round

  41. Pitfalls • There is no check for overflow or underflow - you just get garbage • 3 * a+b is (3 * a) + b • Uninitialised variables contain garbage • Assigning a higher type to a lower one gives a warning - use a cast • Input of garbage gives a garbage value, unless you initialise

  42. Making code readable • Keep one statement per line • Add comments to answer questions • Do not comment the obvious • Add lots of spacing • Indent code • Try not to be too clever

More Related