110 likes | 324 Views
An Introduction to C Programming. Geb Thomas. Learning Objectives. Learn how to write and compile a C program Learn what C libraries are Understand the C variable types Understand how to use if and if/else statements Understand how to use the for structure.
E N D
An Introduction to C Programming Geb Thomas
Learning Objectives • Learn how to write and compile a C program • Learn what C libraries are • Understand the C variable types • Understand how to use if and if/else statements • Understand how to use the for structure
How to Write and Compile C Programs • C, C++ and Java • Compilers: Microsoft Visual C++, GCC, Borland C • Since we will be working on PCs: • Microsoft Visual C++ • Open new Win32 Console Application • Name it (in “project name”) • Click “a hello world application” • Go to file view, source files, then the name of your project.cpp
Some Things About C • Case matters, white space does not • Comments go between /* and */ • Each statement is followed by a semicolon • Execution begins in the main function: int main(int argc, char* argv[]) { /* ignore this */ /* start here */ return 0; /*end here */ }
What are C libraries? • C is a lightweight language. Most of its intelligence is compartmentalized in libraries. • Almost all c programs use the “stdio” or standard input/output library. Many also use the “math” library. • To use a library, include the header file (I.e., “stdio.h”) at the top of the file. • For most special purpose libraries (I.e., math) you need to include the library on the link line. In Visual C++, go to project->settings->object/module libraries.
C Variable Types • The most common types are: char, int, float, and double. • Strings are arrays of characters (we’ll cover arrays later). • Declare a variable before you use it: int x; /* declares an integer called x. Its value is not assigned. */ float y, z = 3.14159; /* declares two floating point numbers. z is set equal to pi */ z = 4; /* now z is equal to 4 */ myVal = 2; /* This would be an error, because myVal was not yet declared */
Logical Operators • C defines these logical operators: <, >, <=, >= and == (the equivalence operator) • You can compare any variable. Characters are compared based on their ASCII values. • All answers will be true (not zero) or false (0) • You can extend the logic with && (and), ~ (not) and || (or).
The If Statement • Syntax: if (expression) statement; • If the expression is true (not zero), the statement is executed. If the expression is false, it is not executed. • You can group multiple expressions together with braces: if (expression) { statement 1; statement 2; statement 3; }
The If/Else Statement • Syntax: if (expression) statement_1; else statement_2; • If the expression is true, statement_1 will be executed, otherwise, statement_2 will be. if (myVal < 3) printf(“myVal is less than 3.\n”); else printf(“myVal is greater than or equal to 3.\n”);
The For Loop • Syntax: for (initialization; test; increment) {statements;} • The for loop will first perform the initialization. Then, as long is test is TRUE, it will execute statements. After each execution, it will increment. for (cntr = 0; cntr < 3; cntr = cntr + 1) { printf(“ Counter = %d\n”, cntr); } Counter = 0; Counter = 1; Counter = 2;
Learning Objectives • Learn how to write and compile a C program • Learn what C libraries are • Understand the C variable types • Understand how to use if and if/else statements • Understand how to use the for structure