C
programming (2)
WHILE
COMMANDwhile
(expression) statementwhile ( number != 0)
{
printf("%3d\n", number);
printf("Input an integer number - terminate with 0\n");
scanf("%d", &number);
}
double number;
number = 2.0;
while (number <= 10.0)
{......
number=number+0.5;
}
DO WHILE
COMMANDdo
statement while (expression);
do
{
printf("Input an integer number");
scanf("%2d", &number);
}
while (number != 0);
L
VALUES
counter++
increases counter by onecounter- -
decreases counter by oneprintf("%2d", counter++);
prints the old value of counterprintf("%2d", ++counter);
prints the new value of counter
FOR
COMMAND
for
(expression-1 ; expression-2 ; expression-3 ) statement
Expression 1:
initialisation of loop control variableExpression
2: conditional expressionExpression 3:
changes value of loop control variableAnyone of the three expressions can be optional
int counter;
for (counter = 1; counter < 4; counter ++)
printf("%2d", counter);
counter = 1;
for (; counter < 4; counter ++)
printf("%2d", counter);
for (counter = 1 ; ; counter++)
{
printf("%2d", counter);
if (counter == 3) break;
}
for(counter = 1; counter < 4;)
printf(%2d", counter++);
THE
DEFINE COMMAND
#define pi 3.14159
#define magic "ambracadambra"
MATHEMATICS LIBRARY <math.h>
Sin(x) |
Cos(x) |
Tan(x) |
Sinh(x) |
Cosh(x) |
Tanh(x) |
Asin(x) |
Acos(x) |
Atan(x) |
Atan2(x,y) Returns the arctangent of y/x |
Log(x) Returns the natural logarithm of x |
Log10(x) Returns the base 10 logarithm of x |
Exp(x) |
Pow(x,y) Raises x to the power of y |
Sqrt(x) |
Fabs(x) Returns the absolute value of x |
Int abs(int num) Returns the absolute value of an integer number |
Int rand(void) Returns pseudo-random number in the range 0-rand_max |
#include <stdio.h>
#include<math.h>
#
define pi 3.14159#define tangent(x) (float)tan(x*pi/180.0)
for (angle = 0; angle<=20; angle++)
printf("%d\t %6.4f\n", angle, tangent(angle));
#include <stdio.h>
#include<math.h>
#define logarithm(b,n) (log(n) / log(b))
for (base=2; base<=10; base=base+2)
printf("8.4f", logarithm(base , number));
ARRAYS
The contents of all cells in an array MUST be of the same data type, e.g all real numbers
ONE DIMENSIONAL ARRAYS
int numbers[7] ;
one dimensional array of 7 elements, starting from 0 and ending at 6.int numbers[5] = {54,26,99,-25,13};
int numbers[ ] = {54,26,99,-25,13};
numbers[0]=54;
numbers[1]=26;
numbers[2]=99;
numbers[3]=-25;
numbers[4]=13;
for (index=0; index < 5; index++)
scanf(%2d", &numbers[index])
THE STATIC COMMAND
The command static gives initial values to the matrices. For example:
static int numbers[7];
All the elements of matrix numbers have the initial value of zero.
static int numbers[7]={1,2,3};
The three first elements of the matrix get the values 1, 2,3 and then the rest elements take the value of zero.
TWO-DIMENSIONAL ARRAYS
int matrix[3][4];
int matrix[3][4] = { {1,2,3}, {2,1,6}, {3,9,0} } ;
for (row=0; row <3; row++)
{
for (column=0; column < 6; column++)
printf("%2d\t", matrix[row][column]) ;
}
STRUCTURES
main()
{
typedef struct {
int day;
int month;
int year;
} date_of_birth;
date_of_birth birthday;
scanf("%d%d%d" , &birthday.day, &birthday.month, &birthday.year);
printf("day %d\n" , birthday.day);
}
main()
{
typedef struct {
int day;
int month;
int year;
} date_of_bearth;
date_of_bearth birthdays[5] = { { 11, 10, 1953},
{ 18 , 3, 1948},
{14, 6, 1920} } ;
int index;
for (index=0; index < 5; index++)
printf("%-3d%-3d%4d\n", birthday[index].day,
birthday[index].month,
birthday[index].year);
}
FUNCTIONS
int square(num)
int num
{
int answer ;
answer=num*num;
return answer;
}
main()
{
extern int square()
int solution, input_val;
printf("Enter integer value");
scanf("%2d", &input_val);
solution=square(input_val);
printf("The square of %d is %d\n" , input_val, solution);
exit(0)
}
void incr(val)
int val;
{
val++
}
main()
{
extern void incr();
int a=3;
printf("Value of variable a before calling incr: %d\n", a);
incr(a);
printf("Value of variable a after calling incr: %d\n", a);
exit(0);
}
Output:
prints number 3 two times.void incr(val)
int *val;
{
(*val)++
}
main()
{
extern void incr();
int a=3;
printf("Value of variable a before calling incr: %d\n", a);
incr(&a);
printf("Value of variable a after calling incr: %d\n", a);
exit(0);
}
Output:
prints numbers 3 and 4.
POINTERS
int *P;
The pointer is defined.Arrays
static int table[ ] ={1,2,3,4,5};
int *p;
p= &table[0];
The pointer p points to the first element of the matrix table.
*(table+0)=table[0]
*(table+1)=table[1]
...
*(table+i)=table[i]
#include <stdio.h>
#include <stdlib.h>
main()
{
int *P, *Q
P = malloc(sizeof(int));
printf("Input a single integer "); scanf("%2d",P);
Q = P;
printf("Value of the integer being pointed at is %d\n" , *Q);
free(P)
}
Output:
Input a single integer 12345Value of integer being pointed at is 12345
malloc(b) |
Returns a pointer in a consecutive series of b bytes. If the wanted (zitoumenos) place is available , it returns \0. |
calloc(n,t) |
Returns a pointer in a consecutive series of as many bites as they are required for storing n numbers (stoixeia) of type t. If the wanted place is not available, it returns \0. |
realloc(p,b) |
Changes the dimensions of the object at which the pointer p points, from what it was, to b. |
free(p) |
Apodesmeuei to xoro pou dexnei o deiktis p kai o opoios prepei na exei demseuthei me mia apo tis sinastiseis malloc or calloc. |
FILES
REDIRECTION
Program called : exa.c
Input files with data: numbers.txt
Output file to write data: results.txt
Redirections of results:
A) For DOS:
exa <a:\numbers.txt > a:\results.txt
B) For UNIX
cc exa.c -o run
run <numbers.txt>results.txt