Вы находитесь на странице: 1из 16

# C

, 2012

1 / 16

Hello, world!

#include <stdio.h>
int main(void) {
printf("Hello, world!\n");
return 0;
}

2 / 16

## x = x * 2; // this is line comment

y = ++x; // all line after it is ignored
/*
and this is block comment,
note that such comment cannot be nested
*/

3 / 16

Types
bool b; // C99 only
char c;
short s;
int i;
long l;
long long ll;
// all integral types could be unsigned
unsigned int ui;
float f;
double d;
long double ld;
// declare uint32 as alias for unsigned long
typedef unsigned long uint32;

4 / 16

Arithmetic operators

z = +x;
z = -x;
z
z
z
z
z

=
=
=
=
=

x
x
x
x
x

+
*
/
%

y;
y;
y;
y;
y;

z
z
z
z

=
=
=
=

++x;
x++;
--x;
x--;

//
//
//
//

x
z
x
z

+= 1; z =
= x; x +=
-= 1; z =
= x; x -=

x
1
x
1

5 / 16

Comparison operators

z
z
z
z
z
z

=
=
=
=
=
=

(x
(x
(x
(x
(x
(x

== y);
!= y);
>= y);
<= y);
> y);
< y);

6 / 16

## Logical and bitwise operators

z = !x; // not
z = x && y; // and
z = x || y; // or
z
z
z
z

=
=
=
=

~x;
x &
x |
x ^

//
y;
y;
y;

bitwise not
// bitwise and
// bitwise or
// bitwise xor

## z = x << y; // bitwise left shift

z = x >> y; // bitwise right shift

7 / 16

Other operators

z
z
z
z
z

=
=
=
=
=

## x[i]; // i-th element of array

*x; // object pointed to by x
x.f; // member f of object x
x->f; // member f of object pointed to by x

z = a ? x : y; // if (a) { z = x; } else { z = y; }
z = (int)x; // cast

8 / 16

If-then-else
if (some_condition(x)) {
printf("x is cool!\n");
}
if (x % 2 == 0) {
printf("%d is even!\n", x);
} else {
printf("%d is odd!\n", x);
}
if (x < 0) {
printf("%d is
} else if (x ==
printf("%d is
} else { // x >
printf("%d is
}

negative!\n", x);
0) {
zero!\n", x);
0
positive!\n", x);

9 / 16

Switch

switch (apples_count) {
case 0:
printf("no apples!\n");
break;
case 1:
case 2:
case 3:
printf("few apples!\n");
break;
default:
printf("many apples!\n");
break;
}

10 / 16

Loops: while

## // print squared numbers from 100 to 1

x = 100;
while (x > 0) {
printf("%d\n", x * x);
x--;
}
x = get_first();
while (is_good(x)) {
x = get_next(x);
printf("%d\n", x);
}

11 / 16

Loops: do-while

do {
ch = getchar();
} while (ch != \n);
// x could be uninitialized
do {
x = get_next(x);
} while (is_good(x));

12 / 16

Loops: for

## // print squared numbers from 1 to 100

for (i = 1; i <= 100; ++i) {
printf("%d\n", i * i);
}
for (i = get_first();
is_good(i);
i = get_next(i)) {
printf("%d\n", i);
}

13 / 16

## Loops: break & continue

// process values while they are good
for (;;) {
x = get_next();
break; // stop looping
}
process(x);
}
// process values skipping bad ones
for (;;) {
x = get_next();
continue; // start next loop step
}
process(x);
}

14 / 16

Functions

## // Receives nothing, returns nothing.

void work_hard(void) {
printf("Im working hard...\n");
}
// Returns sum of arguments.
int sqr(int x, int y) {
int result = x + y;
return result;
}
// calling...
work_hard();
num = sum(2, sum(3, 4)); // => 9

15 / 16

To be continued

preprocessor;
structs/unions;
extern, static.

16 / 16

## Нижнее меню

### Получите наши бесплатные приложения

Авторское право © 2021 Scribd Inc.