1 / 15

Munster Programming Training

Munster Programming Training. What is C? . C is a programming language. It communicates to the computers what to do. It solves various problems from scientific to entertaining. Why C? It is simple, fast, procedural etc. It has several extensions C++, C# … It is the language of Sciences.

moriah
Télécharger la présentation

Munster Programming Training

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Munster Programming Training

  2. What is C? C is a programming language. • It communicates to the computers what to do. • It solves various problems from scientific to entertaining. Why C? • It is simple, fast, procedural etc. • It has several extensions C++, C# … • It is the language of Sciences. A C program is a sequence of C routines which must include main.

  3. The words of C? C programs communicate to the computer using “words”. C Key words are names for: • Data types: void, int, long, double, float • Statements: if, while, for, Programmers words / user words. • Names for variable, routines. Rules: • User names use letters, digits, _. • C is case sensitive Example: a, b, c, A, B1,C_1, max_two_numbers

  4. What is a C program like? # include <stdio.h> # include <math.h> int main(int args, char ** argc) { printf("Hello from C \n"); return 1; } Sections of C programs: • Include declarations • The header of main. • The block of main. • Declare all the variables. • Read all the inputs. • Generate the output. • return 1; # include <stdio.h> # include <math.h> int main(int args, char ** argc) { // nothing in the program return 1; }

  5. More on these examples • Include declarations for the libraries we use. • stdio.h  for input/output. • math.h  for maths functions. • stdlib.h for standard c functions. • In the main function • // are comments • printf(); is a C function to print/display/write etc. • scanf (); is a C function to read an input value. • A sequence of characters between ” ” is a string.

  6. Another program. # include <stdio.h> # include <math.h> int main(int args, char ** argc) { int a, b; double mean; printf("a="); scanf("%d", &a); printf("b="); scanf("%d", &b); mean=(a+b)/2.; printf("mean=%lf \n\n", mean); return 1; } Three variables a, b, mean. • They are declared. • The inputs a, b are read. • The output mean is calculated and written. Each command ends with “;”. Writing style: • indent your lines • write several statement on the same line • align brackets • Finish each line with ; Same rules as in Maths: +, -, /, *, () etc

  7. Read and Write printf(“format”, var) for writing scanf(“format”,&var) for reading printf(“text %spec \n”, var1); printf(“text %s1 text %s2 text …\n”, var1,var2,…); - format specificators: %d, %ld, %f, %lf. - \n special character for newline. scanf(“%spec”,&var); No text is allowed in the format. printf("a="); scanf("%d", &a); printf(“a=%d”,a);

  8. Read and Write - Examples Read an int variable a. printf(“a=”); scanf(“%d”,&a); Write an int variable a. printf(“a = %d \n”, a); Read two variables x and y. printf(“Type the Values for x and y”); scanf(“%d %d”,&x, &y); Write two int variables x,y. printf(“a = %d b= %d \n”, x, y);

  9. C Data Types. In Mathematics there are several sets of numbers: N, Z, Q, R. In C there are several data types (sets of C numbers): • Integers: char, short, int, long. • Reals: float and double. int -2^15, 2^15-1 unsigned int 0, 2^16 long -2^31, 2^31-1 unsigned short 0, 2^32 float 6 exact decimals double 10 exact decimals C literals: integer numbers: sequence_of_digits. real numbers: sequence_of_digits.sequence_of_digits characters: ‘ch’ string: “sequence_of_characters”

  10. C Declaration. In a C program ALL VARIABLES MUST BE DECLARED once. Types of declarations: • type var; • type var=value; • type var1,var2,..; Example: • int a,b,c; • double x,y=0.; • double x, a; Declarations go in the first part of a block. ALL VARIABLES MUST BE READ/INITIALISED.

  11. C Arithmetic C variables form expressions using operators: Arithmetic operators: • +, -, *, / • ++, -- for incrementing and decrementing. • % is the remainder of the integer division. Relational operators: • <, >, <=, >= • ==, != Logical: &&=and, ||= or, !=not Assignment: = In addition of those operators there are several math routines in math.h sqrt, sin, cos, asin, acos etc.

  12. Examples int a,b,c; double x,y; a=1;b=2;c=4; a=b+c; // a=6 b=c+2; // b=6 a=a+1; // a increases with 1 ==> a++; x=PI;y=sin(x); x=asin(y/2); a=b=2; // initialise a and b with 2. a==b; // compare a and b.

  13. C tests - if statement. if(test) { statement1; } else { statement 2; } or chooses one alternative if(test) { statement1; } If statement chooses between two alternatives if(test) statement1; else statement 2; or chooses one alternative if(test)statement1; On the branches we have only one statement. If more use a block {} Example: Maximum value of 2 numbers if(a<b) { max=b; } else { max=a; }

  14. Max of two numbers # include <stdio.h> # include <math.h> # include <conio.h> int main(int args, char ** argc) { int a, b, max; printf("a="); scanf("%d", &a); printf("b="); scanf("%d", &b); if(a<b) { max=b; } else { max=a; } printf(”max=%d \n\n", max); return 1; }

  15. To do List • Read more about C types, declarations, operators from the e-tutorial. • Solve the HW problems • Visit the page of AISPC.

More Related