CPS 101 Introduction to Computational ScienceWensheng ShenDepartment of Computational ScienceSUNY BrockportChapter 10: Program in CWhy C language: C is the language which can be used for both system software and application software. C is human readable, and also can be used to access hardware.The origin of C language(1) C language was developed by Dennis M. Ritchie between 1972 to 1973 at Bell Laboratory. (2) It was used to develop the famous Unix operating system in 1973 by D.M. Ritchie and K. Thompson. (3) “The C Program Language” by Brian W. Kernighan and Dennis M. Ritchie was the most famous book in C languageCharacteristics of C LanguageIt is concise, compact, flexible, and convenientThere are only 32 keywords, 9 control statementsIt is an idea language for structural programming10.1 Introduction to C languageEditing, compiling and executing using Visual Studio;A simple C codeComponents in a C codeThe structure of a function in C languageThe C++ CompilerWe use Microsoft Visual Studio.Launch Microsoft Visual Studio.Start->Programs->Microsoft Visual Studio ->Microsoft Visual C++ (whatever version)Find File menu on the pop-up window, select new, select project, select Win32 Console Application.File->New->Project->Win32 Console ApplicationSave file: Name->Location->FinishWhat is C languageC is called a compiled languageA C program, human readable, must be run through a C compiler to turn it into an executable that the computer can run (execute).A simple C codeint main(int argc, char* argv[]){printf (“Hello World from C\n”);}A simple C codeint main(int argc, char*argv[]){int a, b, sum;a=123;b=456;sum = a + b;printf(“The summation of %d and %d is %d\n”, a, b, sum); }More exampleint main(int argc, char* argv[]){int a, b, c;scanf(“%d, %d”, &a, &b);c = max(a, b);printf(“The maximum of %d and %d is %d \n“, a, b, c);return (0);}int max(int x, int, y){int z;if (x > y) z=x;else z=y;return (z);}Compiling and executing using Visual StudioCreate a new project, or open an existing project.Build the solution to create an exe file under the directory /debug.Go to the build menu to run the executable file.Compiling, building, and executing using Visual StudioIn Visual Studio:Source codeexecutable fileBuildSort3.cSort3.exeBuildEXAMPLE101100110011001110010110011001010101010101010001001110010010111001010101010010101010111111000001101010101int main(int argc, char*argv[]){int a, b, sum;// a=123;// b=456;scanf(“%d %d”, &a, &b);sum = a + b;printf(“The summation of %d and %d is %d\n”, a, b, sum); }Component of C programThe C program starts with #include <stdio.h>. This line includes the “standard I/O library” into your program. Keyboard is “standard in” and screen is “standard out”A library is simply a package of code that someone else has written for you to use to make your life easier int main(int argc, char* argv[]) declares the main function. Every C cods should have a main function somewhere, or, a C program consists of at least one function, the main function. At run time program execution starts at the first line of the main function. A function include two parts: description and body.Description of a function:type, name, parameters, the type of parameters.Example:int max (int x, int y)Type of functionname of functionType of parametersparametersBody of a function:The contents of a function should be include in the pair of “{ }”, following the description of the function.The declaration of variables.The operation of the function.int max(int x, int, y){}descriptionExample: int z;if (x > y) z=x;else z=y;return (z);bodyVariable declarationoperationEach statement or definition must be ended with “;”.Input and output are operated by the functions “scanf” and “printf” respectively.Comments are marked by the pair “/*” and “*/”. In other words, anything between “/*” and “*/”are comments without and functional ability.The printf statement allows you to send output to standard out (for us, the screen). The portion in quotes is called the format string and describes how the data is to be formatted when printed. The format string can contain string literals such as “Hello World From C!.“ To change a line or return, C use “\n”. The return 0; line causes the function to return an error code of 0 (no error) to the shell that started execution. (shell means any program that users use to type commands).10.2 Data Types in C LanguageA program consists of two partsData descriptionProcedures Program = data structure + algorithms (Nikiklaus Wirth)Program = data structure + algorithms + program design + language environmentBasic data typesC has the following data types:int --- integer values;float --- floating point values in single precision;double --- floating point values in double precision; char --- single character values;Constant and VariableConstant: integer, real (float, double), char#define PI = 3.14159 orUsing capital letters to identify constants;The values of constants cannot be changed in the program.Character constantCharacter constants in C: ‘a’, ‘X’, ‘?’,‘$’, ‘A’Special character constants:‘\n’, ‘\t’, ‘\v’Character string: “USA”, “How are you?”Variables We use variables to remember things or values. The value of a variable can be changed in a programint a;A variable has a name (in this case, a) and a type (in this case, int, an integer)We can store a value to a by initializinga = 10;We can print the value by doing:printf(“%d”, a);Variables The name of a variable must be:letters, numbers, and underscoreA variable must begin with letters or underscore, but not numbers; Examples:Sum, average, _above, lotus_1_2_3#33, 3d64, M.D.John, a>bCalculation of combined data types10+’a’+1.5-57.654*’b’double floatint charArithmetic operations and arithmetic expressions+ --- addition (3 + 4)- ---- subtraction (10 - 5)* ---- multiplication (3*6)/ ---- division (5/3)% ---- module (5%3)a*b/c – 2.5 + ‘b’Conversion of data type(double) a(int) (x+y)(float) (7%3)Self-addition and self-subtration++i, --i: let i increase or decrease by 1 before using i;i++, i--: let i increase or decrease by 1 after using i;Example (i=5):j
View Full Document