Вы находитесь на странице: 1из 24

Computational Methods Lab

Computational Methods Laboratory


Bernhard Egwolf
University of Santo Tomas College of Science

Bernhard Egwolf

University of Santo Tomas

Computational Methods Lab

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

University of Santo Tomas

Computational Methods Lab

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

Computational Methods Lab

Start Visual C++


Start All Programs Microsoft Visual Studio... Microsoft Visual C++...

Bernhard Egwolf

University of Santo Tomas

Computational Methods Lab

Create Project
File New Project Choose Empty Project, give it the name hello, and press OK.

Bernhard Egwolf

University of Santo Tomas

Computational Methods Lab

Create Source Code File


Go to the Solution Explorer window. Click with the right mouse key on Source Files. Add New Item

Bernhard Egwolf

University of Santo Tomas

Computational Methods Lab

Create Source Code File


Choose C++ File, give it the name hello.c, and press Add.

Bernhard Egwolf

University of Santo Tomas

Computational Methods Lab

Create Your First C Program


Type your rst program into the hello.c le.

Bernhard Egwolf

University of Santo Tomas

Computational Methods Lab

Create Your First C Program

#include <stdio.h> int main() { printf( "Hello World!\n" ); getchar(); return 0; }

Bernhard Egwolf

University of Santo Tomas

Computational Methods Lab

Create Your First C Program


Debug Start Debugging (Check Output window for errors!) This compiles the program and opens a console with the output:

Press Enter to close the console.


Bernhard Egwolf University of Santo Tomas

Computational Methods Lab

Your First C Program

#include <stdio.h> int main() { printf( "Hello World!\n" ); getchar(); return 0; }

Bernhard Egwolf

University of Santo Tomas

Computational Methods Lab

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

University of Santo Tomas

Computational Methods Lab

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

University of Santo Tomas

Computational Methods Lab

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

Computational Methods Lab

Floating Point Variables


#include <stdio.h> int main() { int i, j, k; float a, b; i = 7; j = 3; k = i / j; a = 7 / 3; b = 7.0 / 3.0; printf( "k = %d\n", k ); printf( "a = %f, b = %f\n", a, b ); getchar(); return 0; }
Bernhard Egwolf University of Santo Tomas

Computational Methods Lab

Floating Point Variables


Debug Start Debugging This compiles the program and opens a console with the output (note dierence between integer and oating point division):

Press Enter to close the console.


Bernhard Egwolf University of Santo Tomas

Computational Methods Lab

Floating Point Variables

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

University of Santo Tomas

Computational Methods Lab

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

Computational Methods Lab

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

University of Santo Tomas

Computational Methods Lab

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

Computational Methods Lab

Reading an Integer Variable from Console


#include <stdio.h> int main() { int temp; printf("What is the temperature in degrees Celsius? "); scanf("%d", &temp); printf("%d degrees Celsius is nice.\n", temp); getchar(); getchar(); return 0; }

Bernhard Egwolf

University of Santo Tomas

Computational Methods Lab

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

University of Santo Tomas

Computational Methods Lab

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

Computational Methods Lab

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

University of Santo Tomas

Вам также может понравиться