3 - ICS103 Programming in C Lecture 3 Introduction to C(2 1 Outline Overview of C History Philosophy Why C What's Missing General form of a C program C

3 - ICS103 Programming in C Lecture 3 Introduction to C(2 1...

This preview shows page 1 - 8 out of 25 pages.

1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Image of page 1
2 Outline Overview of C History & Philosophy Why C? What’s Missing? General form of a C program C Language Elements Preprocessor Directives Comments The “main” function Variable Declarations and Data Types Executable Statements Reserved Words Identifiers Formatting Numbers in Program Output
Image of page 2
3 Executable Statements /* Converts distances from miles to kilometers */ #include <stdio.h> /* printf, scanf definitions */ #define KMS_PER_MILE 1.609 /* conversion constant */ int main(void) { double miles, //distance in miles kms; //equivalent distance in kilometers //Get the distance in miles printf("Enter the distance in miles> "); scanf("%lf", &miles); //Convert the distance to kilometers kms = KMS_PER_MILE * miles; //Display the distance in kilometers printf("That equals %f kilometers.\n", kms); return (0); }
Image of page 3
4 Executable Statements Executable Statements: C statements used to write or code the algorithm. C compiler translates the executable statements to machine code. Input/Output Operations and Functions printf Function scanf Function Assignment Statements return Statement
Image of page 4
5 Input/Output Operations and Functions Input operation - data transfer from the outside world into computer memory Output operation - program results can be displayed to the program user Input/output functions - special program units that do all input/output operations printf = output function scanf = input function Function call - in C a function call is used to call or activate a function Calling a function means asking another piece of code to do some work for you
Image of page 5
6 The printf Function function name printf(“That equals %f kilometers.\n”, kms); function arguments format string print list place holder 
Image of page 6
7 Placeholders Placeholder always begins with the symbol % It marks the place in a format string where a value will be printed out or will be inputed (in this case, kms) Format strings can have multiple placeholders, if you are printing multiple values Placeholder Variable Type Function Use %c char printf/scanf %d int printf/scanf %f double printf %lf double scanf newline escape sequence – ‘\n’ terminates the current line
Image of page 7
Image of page 8

You've reached the end of your free preview.

Want to read all 25 pages?

  • Spring '10
  • baleh
  • miles, printf

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes