1 / 90

The C Programming

The C Programming. Lecture 24. Summary of Previous Lecture. Programming in real life. Introduction What is Problem Solving? Problem Solving process. Algorithm History Working Definition Examples Algorithms to Programs. Summary of Previous Lecture. Components of Algorithms

drinnon
Download Presentation

The C Programming

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. The C Programming Lecture 24

  2. Summary of Previous Lecture • Programming in real life. • Introduction • What is Problem Solving? • Problem Solving process. • Algorithm • History • Working Definition • Examples • Algorithms to Programs

  3. Summary of Previous Lecture • Components of Algorithms • Variables and values • Instructions • Sequences • Procedures • Selections • Repetitions • Documentation • Software Development Process • Top Down Algorithm Design

  4. Today’s Lecture • Algorithms and Programs • A C programming language • History of C • C, A High level language • How to get started with C. • Basic Structure of a C program • Data Storage and Data Types • Variables, Keywords, identifiers, Assignment

  5. Today’s Lecture • constant variable • printf() and scanf() functions and usage • Precedence • int and float • Unary operations • Increment and decrement operations

  6. Today’s Lecture • Comments • Error and its Types • Summary

  7. From Algorithms to Programs • Both are sets of instructions on how to do a task • Algorithm: • talking to humans, easy to understand • in plain (English) language • Program: • talking to computer (compiler) • can be regarded as a “formal expression” of an algorithm

  8. A C Programming Language • Flexible language: • Structured language • Low level activities possible • It can produce lean and efficient code • Wide availability on a variety of computers • Widely used!

  9. History of C • CPL Combined Programming Language (Barron et al., 1963) • BCPL Basic CPL (Richards, 1969) • B (Thompson, 1970) • C K&R C (Ritchie, 1972) • ANSI C American National Standards Institute C (X3J11, 1989) • C99 (JTC1/SC22/WG14, ISO/IEC 9899, 1999)

  10. 10100110 01110110 00100110 00000000 11111010 11111010 01001110 10100110 11100110 10010110 11001110 00101110 10100110 01001110 11111010 01100110 01001110 10000110 etc... A High-Level Language #include <stdio.h> int main() { printf(“Hello World”); return 0; } Source code Executable code • Compilers and linkers translate a high level program into executable machine code.

  11. How to get started? • Download Turbo C++ Version 3.0 a free software. • http://turbo-c.soft32.com/ • Install it! • Follow the step by step guide for your first program!

  12. Turbo C++ IDE Version 3 Click New to open a program window

  13. Open a new window for writing a program Output Message window

  14. Set the Directories in Option Menu

  15. Set the output directory path

  16. Write your first program here Write your program here!

  17. Compile to find errors Compile to check errors

  18. No Errors Found

  19. Execute the program by Run option RUN to execute

  20. See output by pressing Ctrl+F5 Output of your first program

  21. Basic Structure of a C Program Example: Hello World C Program: Pre Processor Directive include <stdio.h> file in this program! stdio.h contains declaration of printf used in the program #include <stdio.h> int main() { printf(“Hello World”); return 0; }

  22. Basic Structure of a C Program Example: Hello World C Program: #include <stdio.h> int main() { printf(“Hello World”); return 0; } Program control is started from the main function.

  23. Main Function = Main Gate You can enter the premises of the building through main gate! Similarly program control is entered through main function..!

  24. Basic Structure of a C Program Example: Hello World C Program: #include <stdio.h> int main() { printf(“Hello World”); return 0; } int indicates that only an integer value can come out of this function

  25. Basic Structure of a C Program Example: Hello World C Program: #include <stdio.h> int main() { printf(“Hello World”); return 0; } Curly braces mark the beginning and end of a block of instructions.

  26. Basic Structure of a C Program Example: Hello World C Program: Instruction (function call) to output “Hello World”. This will print Hello World on the output screen #include <stdio.h> int main() { printf(“Hello World”); return 0; }

  27. Basic Structure of a C Program “Statements” (lines of instructions) always end with a semi-colon (;) Example: Hello World C Program: #include <stdio.h> int main() { printf(“Hello World”); return 0; }

  28. Where to store data? • A data type is a representation of data that defines a size and valid range for data. • Built-in types: char, int, float • Type modifiers: long, short, const • User-defined types (arrays and records) • What about “strings”? • Strings are arrays of char (discussed later)

  29. Character Representation • Characters are stored as a small integer • Each character has a unique integer equivalent specified by its position in the ASCII table (pronounced “as-key”) • American Standard Code for Information Interchange

  30. Character Representation • The ASCII values range from 0 to 127 • value 0: special character ’\0’(a.k.a. NUL character) • value 127: special character <DEL> • other special characters: ’\n’’\t’’\’’’\\’etc. • various “extended” sets from 128 to 255

  31. Remember: Variables Variable Values 10 cookies 50 grams of sugar 3 slices of cake etc. This jar can contain • Are containers for values– placesto storevalues • Example:

  32. Variable • Is a logical name for a container • (an actual piece of computer memory for values) • Has a type associated with it • tells the computer how to interpret the bits • Must be declared before use: int i; float result; int i=0; char initial=’K’;

  33. myID Variable Declaration: Examples Variable int myID;

  34. Variable Declaration: Examples int myID; char myInitial = ’J’; Single “forward quotes” or apostrophe (’) rather than “back quotes” (‘)

  35. 01001010 myInitial Variable Declaration: Examples int myID; char myInitial = ’J’;

  36. 01001010 myInitial Variable Declaration: Examples int myID; char myInitial = ’J’; char myInitial = 74 ;

  37. Variable Declaration: Examples float commission = 0.05; short int myHeight = 183; /* cm */ long int mySalary = 100000000000000000000; long float chanceOfADate = 3e-500; double chanceOfA2ndDate = 1.5e-500;

  38. Variable Declaration: Examples float commission = 0.05; short int myHeight = 183; /* cm */ long int mySalary = 100000000000000000000; long float chanceOfADate = 3e-500; double chance_of_a_2nd_date = 1.5e-500; “Keywords”

  39. Keyword • ...has a special meaning in C • ...is “case-sensitive” • ...cannot be used as variable names • Examples: int, char, long, main, float, double, const, while, for, if, else, return, break, case, switch, default, typedef, struct, etc.

  40. Variable Declaration: Examples float commission = 0.05; short int myHeight = 183; /* cm */ long int mySalary = 100000000000000000000; long float chanceOfADate = 3e-500; double chanceOfA2ndDate = 1.5e-500; “Identifiers”

  41. Identifier • ...is a series of characters consisting of letters, digits and underscores ( _) • ...cannot begin with a digit • ...must not be a keyword • ...is “case-sensitive” • Examples: sUmoFA, x1, y2, _my_ID_, Main(careful!)

  42. not to be confused with == Assignment • Puts a specified value into a specified variable • Assignment operator: = <variable name> = <expression> ;

  43. Assignment: Examples float x = 2.5 ; char ch ; int number ; ch = ’\n’ ; number = 4 + 5 ; /* current value of number is 9. */ number = number * 2; /* current value of number is now 18. */

  44. Assignment • Value must have a type assignable to the variable • Value may be automatically converted to fit the new container • Example: • various.c

  45. integer = 33.33; character = 33.33; floatingPoint = 33.33; integer = floatingPoint; floatingPoint = integer; return 0; } #include <stdio.h> /* Do various assignment statements */ int main() { int integer; char character; float floatingPoint; integer = 33; character = 33; floatingPoint = 33; integer = 'A'; character = 'A'; floatingPoint = 'A'; various.c

  46. Constant Variables • ...are variables that don’t vary • ...may not be assigned to. • ...must be initialized const float Pi = 3.14159; const int classSize = 100;

  47. Constant Variables: Examples const int myID = 192; myID = 666; /* Error! */ const int passMark = 80; short char pAsSgRaDe = ’P’; const float pi = 3.1415926; /* oops */ const double golden_ratio = 1.61803398874989;

  48. Example: Constants #include <stdio.h> /* Converts an angle in degrees to radians. */ const float PI = 3.1415926; int main() { float angleInDegs; float angleInRads; printf("Enter angle in degrees:"); scanf("%f", &angleInDegs); angleInRads = PI/180*angleInDegs; printf("%f\n", angleInRads); return 0; } Converts an angle from degrees to radians output “Enter angle in degrees” input angleInDegrees angleInRadians =  / 180 * angleInDegrees output angleInRadians

  49. #include <stdio.h> /* Converts an angle in degrees to radians. */ const float PI = 3.1415926; int main() { float angleInDegs; float angleInRads; printf("Enter angle in degrees: "); scanf("%f", &angleInDegs); angleInRads = PI/180*angleInDegs; printf("%f\n", angleInRads); return 0; } Example: Constants “Global” constant variable “Local” variables more on this later...

More Related