Overview of a C Program
Programming with C
CSCI 112, Spring 2015
Patrick Donnelly
Montana State University
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
CSCI 112, Spring 2015
Montana State University
Programming with C (CSCI 112) Spring 2015 2 / 42
preprocessor directives main function heading { declarations executable statements }
Programming with C (CSCI 112) Spring 2015 3 / 42
#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
Programming with C (CSCI 112) Spring 2015 5 / 42
#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
#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
#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
#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
#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
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.
Programming with C (CSCI 112) Spring 2015 11 / 42
#include is used to include other source files into your source file. #include gives a program access to a library/header file.
Libraries are a collection of useful functions and symbols. Standard libraries are predefined by the ANSI C language.
scanf library functions.
program before compilation.
Programming with C (CSCI 112) Spring 2015 12 / 42
The #define directive instructs the preprocessor to replace every
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
#define Constants
#define MY_CONSTANT_NAME 123 #define ANSWER 42
Programming with C (CSCI 112) Spring 2015 14 / 42
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:
*/
Comments are used for documentation that helps other programmers read and understand the program. Programs should contain a comment at the top with
COMMENT YOUR CODE!
Programming with C (CSCI 112) Spring 2015 15 / 42
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:
compiler what memory cells are needed in the function and how they should be processed.
compiler and later executed.
Programming with C (CSCI 112) Spring 2015 16 / 42
Variable: A memory cell used to store program data.
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
Definition: Data Type
Data Type: a set of values and a set of operations that can be performed on those values. Numeric Data Types:
Examples: 65, -123
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
Character Data Type:
Examples: ‘a’, ,‘5’, ‘*’ (can be a letter, digit, or special symbol)
Character Declaration
char my_char = ’Z’;
Comparison operations (<, >, ≤, ≥) can be used on ints, doubles and chars.
Programming with C (CSCI 112) Spring 2015 19 / 42
‘A’ < ‘B’ < ‘C’ < ... < ‘X’ < ‘Y’ < ‘Z’
Programming with C (CSCI 112) Spring 2015 20 / 42
Executable Statements: C statements used to write or code the algorithm. The C compiler translates the executable statements into machine code. Examples:
Programming with C (CSCI 112) Spring 2015 21 / 42
Input operation: data transfer from outside the program into computer memory that is usable by the program. Input may be received from:
Output operation: program results that can be displayed to the program user. Results can be printed to:
Programming with C (CSCI 112) Spring 2015 22 / 42
A function takes input parameters and produces an output. A function definition specifies the return type, the input parameters, and the body. A function call is used to call or activate a function.
some work for you.
Programming with C (CSCI 112) Spring 2015 23 / 42
By calling the printf() function, we are asking for the function to print to standard output. The first argument to printf is the formatting code (in string form), and all following arguments are the the variables to be used in the placeholders.
double miles = 2.7; printf("That is %f miles .\n", miles );
Programming with C (CSCI 112) Spring 2015 24 / 42
Multiple placeholders can be specified, and the corresponding arguments are then provided in order. Different types of variables require different formatting codes.
int a = 1; double b = 2.0; char *c = "three"; printf("Easy as %d, %f, %s!\n", a, b, c);
Details on formatting codes can be found in the handout.
Programming with C (CSCI 112) Spring 2015 25 / 42
Reading input data in a program is done via the scanf() function. The concept is very much the same as the printf() function. In this case, the format code is just the variable code for what data will be received (%d, %c, %lf), and the following arguments are the variables that will receive the data.
double miles; scanf("%lf", &miles );
The “&” is the address operator in C. The “&” operator in front
Programming with C (CSCI 112) Spring 2015 26 / 42
printf()/scanf() statements
Use commas to deliminate printf/scanf arguments.
char a = ’!’; int b = 7; float c = 3.7; double d = 1.5; printf("%c | %d | %f | %lf | %c \n", a, b, c, d, a); scanf("%lf", &d);
Remember that %f is for floats, %lf is for doubles
printf("%f\n", &d); printf("%lf\n", &d); scanf("%lf", &d);
Programming with C (CSCI 112) Spring 2015 27 / 42
An assignment statement stores a value in a variable.
kms = KMS_PER_MILE * miles;
The above assigns a value (content) to the variable kms. The value assigned in this case is the result of the multiplication of the constant KMS PER MILE by the variable miles. We should interpret an assignment statement: value = expression as: value ← expression This should be read as “becomes”, “gets”, or “takes the value of” rather than “equals”.
Programming with C (CSCI 112) Spring 2015 28 / 42
Effect of kms = KMS PER MILE * miles;
Programming with C (CSCI 112) Spring 2015 29 / 42
“=” is NOT an algebraic operator. Consider the following:
sum = sum + item;
This is obviously not a correct algebraic equation (if item = 0). This statement instructs the computer to add the current value of sum to the value of item. Afterward the result is stored back into sum. Equality: To test for equality, use the “==” operator. Be sure to keep these concepts separate. Remember! Every variable has contents, even before assignment.
Programming with C (CSCI 112) Spring 2015 30 / 42
Effect of sum = sum + item;
Programming with C (CSCI 112) Spring 2015 31 / 42
Operator Meaning Examples + addition 5 + 2 = 7 5.0 + 2.0 = 7.0 − subtraction 5 −2 = 3 5.0 −2.0 = 3.0 * multiplication 5 * 2 = 10 5.0 * 2.0 = 10.0 / division 5.0 / 2.0 = 2.5 5 / 2 = 2 % remainder (mod) 5 % 2 = 1 5 / 2 = 2
Programming with C (CSCI 112) Spring 2015 32 / 42
Area of a Circle
a = πr2 can be written in C as:
area = PI * radius * radius;
where PI is a constant macro set to 3.14159.
Programming with C (CSCI 112) Spring 2015 33 / 42
Programming with C (CSCI 112) Spring 2015 34 / 42
Evaluation Tree and Evaluation for v = (p2 - p1) / (t2 - t1);
Programming with C (CSCI 112) Spring 2015 35 / 42
Evaluation Tree and Evaluation for z - (a + b / 2) + w * -y;
Programming with C (CSCI 112) Spring 2015 36 / 42
In your main() function, the return statement will transfer control from your program to the operating system. “return(0);” returns a 0 to the operating system, which indicates that the program executed without error.
to do. There still may have been logical errors. Other functions also have return statements, which can return error codes, data, or nothing at all.
Programming with C (CSCI 112) Spring 2015 37 / 42
Semicolons: ; Mark the end of a statement (which may take more than one line). Curly Braces: { } Mark the beginning and end of the body of a function. Mathematical Symbols: *,/,+,- Are mainly used to compute numeric values. Note that some have additional meaning. In particular, we will talk about * later.
Programming with C (CSCI 112) Spring 2015 38 / 42
A reserved word is a word that has special meaning to C and can not be used for other purposes. These are words that C reserves for its own uses. They are often necessary for the compiler to analyze the language’s grammar and to understand what the programmer wants to do (declaring variables, controlling data flow, etc).
“if”. Reserved words are always lowercase. See your book or the reference links on the website for a complete list of reserved words.
Programming with C (CSCI 112) Spring 2015 39 / 42
An identifier is a name given to a variable or an operation.
names. A standard identifier is an identifier that is defined in the standard C libraries and has special meaning in this standard (such as ANSI C).
non-standard way, especially when linking to other code that uses these standards.
Programming with C (CSCI 112) Spring 2015 40 / 42
Variables: We choose our own identifiers to name memory cells that will hold data. Functions: We choose our own identifiers to name operations that we define. Common Rules for Naming Identifiers:
Valid Identifers: var1, inches, KM PER MILE Invalid Identifiers: 1var, the*var, return
Programming with C (CSCI 112) Spring 2015 41 / 42
Some compilers only recognize the first 31 characters of identifier names, so avoid long identifiers. Compilers are case-sensitive.
for variables (as opposed to camel-case used in Java)
Constants and Macros usually use all uppercase
Choose meaningful identifiers that are easily understood.
Programming with C (CSCI 112) Spring 2015 42 / 42