Академический Документы
Профессиональный Документы
Культура Документы
Bernhard Egwolf
C Programming
We start with an introduction to the programming language C. Minimum requirements for C programming: Text editor for writing source code. C compiler for creating an executable program. Console for executing program. WikiBook C Programming: http://en.wikibooks.org/wiki/C_Programming
Bernhard Egwolf
Visual C++
We will use Microsoft Visual C++, an Integrated Development Environment (IDE). It can be used for C and C++ programming and it comes with text editor (typing source code), C/C++ compiler (creating executable program), debugger (nding errors), console (running program), learning resources (http://msdn.microsoft.com/en-us/visualc/), and many more useful things. Microsoft Visual C++ Express Edition is free.
Bernhard Egwolf University of Santo Tomas
Bernhard Egwolf
Create Project
File New Project Choose Empty Project, give it the name hello, and press OK.
Bernhard Egwolf
Bernhard Egwolf
Bernhard Egwolf
Bernhard Egwolf
Bernhard Egwolf
Bernhard Egwolf
Adding Comments
// Pre-compiler directive. #include <stdio.h> /* Every C program starts with the function main(). */ int main() { printf( "Hello World!\n" ); getchar(); return 0; }
Bernhard Egwolf
Integer Variables
#include <stdio.h> int main() { int i, j, k; i = 7; j = 3; k = i + j; printf( "k = %d\n", k ); getchar(); return 0; }
Bernhard Egwolf
Integer Variables
Compile hello.c and run program (Debug Start Debugging). Press Enter to close the console. Declaring integer variables: int i, j, k; int (2,147,483,648 to 2,147,483,647) unsigned int (0 to 4,294,967,295) short int (32,768 to 32,767) The basic arithmetic operators for integers are: + addition subtraction multiplication / division (integer division) % modulo
Bernhard Egwolf University of Santo Tomas
Declaring oating point variables: float a, b; oat (3.4E +/- 38 (7 digits)) double (1.7E +/- 308 (15 digits)) The basic arithmetic operators for oating point numbers are: + addition subtraction multiplication / division (be careful with integer variables)
Bernhard Egwolf
If Conditions
#include <stdio.h> int main() { int i, j; i = 7; j = 3; if(i > j) { printf( "i > j!\n" ); } else { printf( "i <= j!\n" ); } getchar(); return 0; }
Bernhard Egwolf University of Santo Tomas
If Conditions
Compile hello.c and run program (Debug Start Debugging). Press Enter to close the console. if(condition 1) { commands; } else if(condition 2) { commands; } else { commands; }
Bernhard Egwolf
If Conditions
Comparison operators: < less than > greater than <= less than or equal to >= greater than or equal to == equal to (note that a single = will not work!) ! = not equal to Logical Operators: && logical and || logical or ! logical not Example: if((x <= y) && !(y > z))
Bernhard Egwolf University of Santo Tomas
Bernhard Egwolf
If Condition
... printf("What is the temperature in degrees Celsius? "); scanf("%d", &temp); if(temp > 35) { printf("%d degrees Celsius is hot.\n", temp); } else { printf("%d degrees Celsius is nice.\n", temp); } getchar(); getchar(); ...
Bernhard Egwolf
If Condition
... scanf("%d", &temp); if(temp > 35) { printf("%d degrees Celsius is hot.\n", temp); } else if(temp < 20) { printf("%d degrees Celsius is cold.\n", temp); } else { printf("%d degrees Celsius is nice.\n", temp); } getchar(); ...
Bernhard Egwolf University of Santo Tomas
Acknowledgments
This presentation was based on tutorials from the following web pages: http://www.idleloop.com/tutorials/introC/ http://www.cprogramming.com/tutorial/c/lesson1.html http://en.wikibooks.org/wiki/C_Programming
Bernhard Egwolf