Sie sind auf Seite 1von 19

FACULTY OF COMPUTER SCIENCE AND ENGINEERING

Introduction
part 3 (C programming language)

Structured Programming

Prof. Dejan Gjorgjevikj, PhD


FCSE 2016

C programming language

FACULTY OF
COMPUTER SCIENCE
AND ENGINEERING

Characteristics

C (/si/, as in the letter C) is a general-purpose


programming language
It enables for the programmer to communicate
with the computer in an efficient way
Basic C characteristics:
language of relatively low level
FAST
appropriate for system programming
very portable
can be cryptic

STRUCTURED PROGRAMMING

C programming language

FACULTY OF
COMPUTER SCIENCE
AND ENGINEERING

Widespread C

C is one of the most widely used programming


languages of all time, and C compilers are available
for the majority of available computer
architectures and operating systems
Many later languages have borrowed directly or
indirectly from C, including C#, D, Go, Rust, Java,
JavaScript, Limbo, LPC, Objective-C, Perl, PHP,
Python, Verilog (hardware description language)
and Unix's C shell. C++ and Objective-C started as
preprocessors for C; C++ is currently nearly a
superset of C, while Objective-C is a strict superset
of C
STRUCTURED PROGRAMMING

C programming language

FACULTY OF
COMPUTER SCIENCE
AND ENGINEERING

What is C used for?

IEEE Spectrums Top Ten Languages for 2016

STRUCTURED PROGRAMMING

C programming language

FACULTY OF
COMPUTER SCIENCE
AND ENGINEERING

Why C?

STRUCTURED PROGRAMMING

C programming language

FACULTY OF
COMPUTER SCIENCE
AND ENGINEERING

The Most Popular Programming


Languages of 2015

What will get me a great job?

STRUCTURED PROGRAMMING

C programming language

FACULTY OF
COMPUTER SCIENCE
AND ENGINEERING

C history

Dennis Ritchie

Brian Kernighan

Created by Dennis Ritchie


(1941-2011) and Brian
Kernighan of (AT&T Bell Labs)
1972.
Its predecessor is the
programming language B,
developed by Ken Thompson
in 1970.
ALGOL 60
CPL
BCPL
B
C
STRUCTURED PROGRAMMING

Ken Thompson
7

C programming language

FACULTY OF
COMPUTER SCIENCE
AND ENGINEERING

Elements of the language (1)

Alphabet of the language: lowercase and


uppercase letters of the English alphabet,
digits, special symbols and interpunction
signs
Letters: az,

AZ, _

Digits: 09
Punctuation: ~

! @ # % ^ & * ( ) - + = :
; " ' < > , . ? | / \ { } [ ]
Whitespace characters: space, horizontal tab,
vertical tab, form feed, newline
STRUCTURED PROGRAMMING

C programming language

FACULTY OF
COMPUTER SCIENCE
AND ENGINEERING

Elements of the language (1)

Reserved words of the language: 32 (very


small number)
element with strictly defined meaning and usage
can not be used differently than defined
can not be redefined

Reserved word (aka reserved identifier,


keyword) is a word that cannot be used as
an identifier, such as a name of a variable or
function or a label.
STRUCTURED PROGRAMMING

C programming language

FACULTY OF
COMPUTER SCIENCE
AND ENGINEERING

C is exceptionally small language

C keywords according to the ANSI standard:


auto

double

int

struct

break

else

long

switch

case

enum

register

typedef

char

extern

return

union

float

short

unsigned

continue

for

signed

default

goto

sizeof

volatile *

do

if

static

while

const

void

* appear for the first time in the ANSI standard


STRUCTURED PROGRAMMING

10

C programming language

FACULTY OF
COMPUTER SCIENCE
AND ENGINEERING

Elements of the language (2)

Names of programming elements identifiers


each name can contain only letters, digits and the _

(underscore) character
each name must begin with a letter
the name can not contain white spaces and
interpunction symbols
the name can not be equal to a keyword (reserved
word)
identifier length
case sensitive

STRUCTURED PROGRAMMING

11

C programming language

FACULTY OF
COMPUTER SCIENCE
AND ENGINEERING

C standardization history
K&R C (1978)
ANSI ISO C (1989 / 1990 aka C89 or C90)
C99 (5 new keywords)

inline, _Imaginary, _Complex, _Bool, restrict

C11 (+ concurrency, security, optimization,


)

STRUCTURED PROGRAMMING

12

FACULTY OF
COMPUTER SCIENCE
AND ENGINEERING

In C++
Recall from high scholl...
#include <iostream>
using namespace std;
int main() {
cout << "Hello!";
return (0);
}

STRUCTURED PROGRAMMING

13

C programming language

FACULTY OF
COMPUTER SCIENCE
AND ENGINEERING

A structure of a C program
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.

/* header Program name, author, version */


/* INCLUDE section holds #include directives */
/* definition of constants and data types definition of types and #define directives */
/* GLOBAL variables */
/* function declarations / definitions */
int main()
{
/* variable declarations */
/* executable expressions */
return 0;
}
/* possibly some more function definitions */

STRUCTURED PROGRAMMING

14

C programming language

FACULTY OF
COMPUTER SCIENCE
AND ENGINEERING

1
2
3
4
5
6
7
8
9
10

/* Fig. 2.1: fig02_01.c


A first program in C */
#include <stdio.h>

Typical C program

int main()
{
printf( "Hello to everyone!\n" );
return 0;
}

Hello to everyone!

Comments

the text embedded in /* and */ is being ignored by the compiler

used to explain the program to the human reader

#include <stdio.h>

preprocessor directive tells the compiler to insert the content of a certain file at
the specified place

<stdio.h> contains the library functions for input/output operations

every preprocessor directive must appear alone in a separate line

does not end with ;


STRUCTURED PROGRAMMING

15

C programming language

FACULTY OF
COMPUTER SCIENCE
AND ENGINEERING

Structure of typical C program (1)


int main()
every C program can have one or more functions, but

exactly one can be named main


the main function is the first to start execution when the C
program is executed
the parentheses behind main () represent a function

empty () means that this function does not require additional parameters

int denotes that the function returns an integer result


the curly braces represent the body of the function
every statement ends with semicolon ;
no strict rules for source code formatting (newlines have no

meaning to the compiler)

STRUCTURED PROGRAMMING

16

C programming language

FACULTY OF
COMPUTER SCIENCE
AND ENGINEERING

Structure of typical C program (2)

printf("Hello to everyone!\n" );

Issues instruction to the computer to perform some action, to present


the string between the quotes on the computer screen
The whole line is called an expression
Expressions in C end with ;
\ - special character (escape character)
Represents that printf()should perform something different
than usual
\n represents new line

return 0;

By convention return 0, represents that the program has finished


regularly without errors

STRUCTURED PROGRAMMING

17

C programming language

FACULTY OF
COMPUTER SCIENCE
AND ENGINEERING

To take home

The Development of the C Language


http://cm.bell-

labs.com/cm/cs/who/dmr/chist.html
http://www.livinginternet.com/i/iw_unix_c.htm

The C Language keywords


http://tigcc.ticalc.org/doc/keywords.html

STRUCTURED PROGRAMMING

18

C programming language

FACULTY OF
COMPUTER SCIENCE
AND ENGINEERING

STRUCTURED PROGRAMMING

19

Das könnte Ihnen auch gefallen