overview of a c program
play

Overview of a C Program Programming with C CSCI 112, Spring 2015 - PowerPoint PPT Presentation

Overview of a C Program Programming with C CSCI 112, Spring 2015 Patrick Donnelly Montana State University C Language Components Preprocessor Directives Comments The main function Variable Declarations and Data Types


  1. Overview of a C Program Programming with C CSCI 112, Spring 2015 Patrick Donnelly Montana State University

  2. C Language Components • Preprocessor Directives • Comments • The “main” function • Variable Declarations and Data Types • Executable Statements • Reserved Words • Identifiers Programming with C (CSCI 112) Spring 2015 2 / 42

  3. General Outline of a C Program preprocessor directives main function heading { declarations executable statements } Programming with C (CSCI 112) Spring 2015 3 / 42

  4. Example C Program #include <stdio.h> // printf , scanf definitions #define KMS_PER_MILE 1.609 // conversion constant int main(int argc , char ** argv) { double miles; // distance in miles double 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); } Programming with C (CSCI 112) Spring 2015 4 / 42

  5. Anatomy of a C Program Programming with C (CSCI 112) Spring 2015 5 / 42

  6. Example C Program: Preprocessor Directives #include < stdio.h > #define KMS PER MILE 1.609 int main(int argc , char ** argv) { double miles; // distance in miles double 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); } Programming with C (CSCI 112) Spring 2015 6 / 42

  7. Example C Program: Declarations #include <stdio.h> // printf , scanf definitions #define KMS_PER_MILE 1.609 // conversion constant int main(int argc , char ** argv) { double miles; double kms; // 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); } Programming with C (CSCI 112) Spring 2015 7 / 42

  8. Example C Program: Comments #include <stdio.h> // printf, scanf definitions #define KMS_PER_MILE 1.609 // conversion constant int main(int argc , char ** argv) { double miles; // distance in miles double 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 ); } Programming with C (CSCI 112) Spring 2015 8 / 42

  9. Example C Program: Executable Statements #include <stdio.h> // printf , scanf definitions #define KMS_PER_MILE 1.609 // conversion constant int main(int argc , char ** argv) { double miles; // distance in miles double 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); } Programming with C (CSCI 112) Spring 2015 9 / 42

  10. Our C Program #include <stdio.h> /* lab01.c (name of your file) * Philip J. Fry (your name), CSCI112 , Lab 01 * 08/28/2014 (current date) */ int main(void) { printf("Hello World\n"); return (0); } Programming with C (CSCI 112) Spring 2015 10 / 42

  11. Preprocessor Directives Definition: Preprocessor Directives Preprocessor Directives are commands that give instructions to the C preprocessor. Definition: Preprocessor The Preprocessor is a program that modifies your C program before it is compiled. Preprocessor Directives start with a “#” symbol. • #include • #define • #if • #else • #endif • #pragma Programming with C (CSCI 112) Spring 2015 11 / 42

  12. #include #include is used to include other source files into your source file. #include gives a program access to a library/header file. • stdio.h • math.h • time.h Libraries are a collection of useful functions and symbols. Standard libraries are predefined by the ANSI C language. • You must include stdio.h if you want to use printf and scanf library functions. • “#include < stdio.h > ” inserts their definitions into your program before compilation. Programming with C (CSCI 112) Spring 2015 12 / 42

  13. #define The #define directive instructs the preprocessor to replace every occurance of a particular text with a given constant value. The replacement occurs before compilation. Your source code: #d e f i n e PI 3.14159 #d e f i n e EULERS NUMBER 2.71828 i n t main ( void ) { double x = 5 ∗ PI ; double y = EULERS NUMBER ∗ 7 ; r e t u r n ( 0 ) ; } What actually gets compiled: i n t main ( void ) { double x = 5 ∗ 3.14159; double y = 2.71828 ∗ 7 ; r e t u r n ( 0 ) ; } Programming with C (CSCI 112) Spring 2015 13 / 42

  14. Constants #define Constants • Consists of 3 parts: ◦ “#define” ◦ constant name ◦ constant value • There is no “=” sign or “;” at the end #define MY_CONSTANT_NAME 123 #define ANSWER 42 Programming with C (CSCI 112) Spring 2015 14 / 42

  15. Comments Comments provide extra information, making it easier for humans to understand the program. These comments are completely ignored by the compiler. Comments take two forms: • /* */ - anything between asterisks is a comment. • // - anything after is a comment, but only until EOL. Comments are used for documentation that helps other programmers read and understand the program. Programs should contain a comment at the top with • the programmer’s name, • the date, and • a brief description of what the program does. COMMENT YOUR CODE! Programming with C (CSCI 112) Spring 2015 15 / 42

  16. The “main” function The heading “int main(void)” marks the beginning of the main function where your program execution begins. Every C program has a main function. Braces/Curly brackets: { } mark the beginning and end of the body of the function. A function body has two parts: • declarations : (names of variables and data types) tell the compiler what memory cells are needed in the function and how they should be processed. • executable statements : they reflect “actions” of your algorithm. These are translated into machine language by the compiler and later executed. Programming with C (CSCI 112) Spring 2015 16 / 42

  17. Declarations of Variables Variable : A memory cell used to store program data. • A variables value can change with time. • We use its name as a simple reference (instead of the actual address in memory). Variable declaration : Statement that communicates to the compiler the names of variables in the program and the data type they represent. Example: double miles; This tells the compiler to create a space for a variable of type double in memory and refer to it each time we use the name miles in our program. Programming with C (CSCI 112) Spring 2015 17 / 42

  18. Numeric Data Types Definition: Data Type Data Type: a set of values and a set of operations that can be performed on those values. Numeric Data Types: • int : Stores integer value (whole number). Examples: 65, -123 • double : Stores real number (uses a decimal point). Examples: 3.14159, 1.23e5 (123000.0) Arithmetic operations (+,-,*,/, %) can be used on ints and doubles. Programming with C (CSCI 112) Spring 2015 18 / 42

  19. Character Data Type Character Data Type: • char : An individual character value. Examples: ‘a’, ,‘5’, ‘*’ (can be a letter, digit, or special symbol) Character Declaration • Use single quotes, NOT double quotes char my_char = ’Z’; Comparison operations ( <, >, ≤ , ≥ ) can be used on ints, doubles and chars. Programming with C (CSCI 112) Spring 2015 19 / 42

  20. ASCII Code ‘A’ < ‘B’ < ‘C’ < ... < ‘X’ < ‘Y’ < ‘Z’ Programming with C (CSCI 112) Spring 2015 20 / 42

  21. Executable Statements Executable Statements : C statements used to write or code the algorithm. The C compiler translates the executable statements into machine code. Examples: • Input/Output Operations and Functions ◦ printf function ◦ scanf function • Assignment Statement (=) • return Statement/Operation Programming with C (CSCI 112) Spring 2015 21 / 42

  22. Executable Statements: Input/Output Operations Input operation : data transfer from outside the program into computer memory that is usable by the program. Input may be received from: • A program user • An external file • Another program Output operation : program results that can be displayed to the program user. Results can be printed to: • The standard console output • An external file Programming with C (CSCI 112) Spring 2015 22 / 42

Recommend


More recommend