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.
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,
Rules:
- User names use letters, digits, _. - C is case sensitive
# include <stdio.h> # include <math.h> int main(int args, char ** argc) { // nothing in the program return 1; }
2.
-
Another program.
Three variables a, b, mean.
They are declared. The inputs a, b are read. The output mean is calculated and written.
# 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; }
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);
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
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;
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.
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.
C tests - if statement.
If statement chooses between two alternatives
if(test)statement1; else statement 2;
On the branches we have only one statement. If more use a block {} Example:
if(a<b) max=b; else max=a; if(a<b){max=b;min=a;} else {max=a;min=b;}
To do List
1. Read more about C types, declarations, operators from the e-tutorial. Solve the HW problems Visit the page of AISPC.
2. 3.