BROCKPORT CPS 101 - Chapter 10: Program in C

Unformatted text preview:

CPS 101 Introduction to Computational ScienceWensheng ShenDepartment of Computational ScienceSUNY BrockportChapter 10: Program in CWhy 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 LanguageIt is concise, compact, flexible, and convenientThere are only 32 keywords, 9 control statementsIt is an idea language for structural programming10.1 Introduction to C languageEditing, compiling and executing using Visual Studio;A simple C codeComponents in a C codeThe structure of a function in C languageThe C++ CompilerWe 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 ApplicationSave file: Name->Location->FinishWhat is C languageC is called a compiled languageA 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 codeint main(int argc, char* argv[]){printf (“Hello World from C\n”);}A simple C codeint 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 StudioCreate 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 StudioIn Visual Studio:Source codeexecutable fileBuildSort3.cSort3.exeBuildEXAMPLE101100110011001110010110011001010101010101010001001110010010111001010101010010101010111111000001101010101int 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 programThe 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 parametersparametersBody 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 declarationoperationEach 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 LanguageA program consists of two partsData descriptionProcedures Program = data structure + algorithms (Nikiklaus Wirth)Program = data structure + algorithms + program design + language environmentBasic data typesC 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 VariableConstant: 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 constantCharacter 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 underscoreA 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 types10+’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

BROCKPORT CPS 101 - Chapter 10: Program in C

Download Chapter 10: Program in C
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 10: Program in C and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 10: Program in C 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?