h.melikian introduction on c c is a high-level programming language that forms the basis to other...

18
H.Melikian Introduction on C C is a high-level programming language that forms the basis to other programming languages such as C++, Perl and Java. It was developed by a guy called Dennis Ritchie in 1972. He named it C because there was an existing programming language called B. You should also get a C compiler and write and test your own programs I learnt best from my (many) careless mistakes. There are many free compilers available on

Upload: darren-knight

Post on 12-Jan-2016

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: H.Melikian Introduction on C C is a high-level programming language that forms the basis to other programming languages such as C++, Perl and Java. It

H.Melikian

Introduction on C

C is a high-level programming language that forms the basis to

other programming languages such as C++, Perl and Java. It

was developed by a guy called Dennis Ritchie in 1972. He

named it C because there was an existing programming

language called B.

You should also get a C compiler and write and test your own

programs I learnt best from my (many) careless mistakes.

There are many free compilers available on the net. For

freeware, shareware and demo programs, try performing a

search at: http://download.cnet.com. So why should you learn

C? Well, because it opens the doors to other languages like C++, Java .

Page 2: H.Melikian Introduction on C C is a high-level programming language that forms the basis to other programming languages such as C++, Perl and Java. It

H.Melikian

Syntax

The C code you write is called the SYNTAX. Syntax is a mixture of: C keywords like int, for and return.  Constants and variable. Operators like + (arithmetic "addition"), || (logical "or") and & (the "address of" operator).

Note that C is CASE SENSITIVE! “White space“ in a C program does not affect. Use extra spaces to make your programs more readable

Page 3: H.Melikian Introduction on C C is a high-level programming language that forms the basis to other programming languages such as C++, Perl and Java. It

H.Melikian

Compilers

Microsoft's Visual C++ 6.0 (Room 106 & 209) This is apowerful package and is suitable for people who areinterested in software engineering. In general, C++ packagesare fine for compiling C programs – the compiler executeddepends on the extension of your source file(s). UNIX-based compiler (gcc available on laplace ). The command for compiling is of the form: % gcc world.c -o world . The filename after cc is the file to be compiled, which is"world.c" in this case, the filename after -o is the output file,which is "world". The output filename is the word you type torun the program.

Page 4: H.Melikian Introduction on C C is a high-level programming language that forms the basis to other programming languages such as C++, Perl and Java. It

H.Melikian

Commenting Your Code

You can add comments to your code by enclosing your

remarks within /*and */. However, nested comments aren't

allowed A few properties of comments:    

They can be used to inform the person viewing the code what the code does. This is helpful when you revisit the code at a later date.   

The compiler ignores all the comments. Hence, commenting does not affect the efficiency of the program.   

You can use /* and */ to comment out sections of code when it comes to finding errors, instead of deletion

Page 5: H.Melikian Introduction on C C is a high-level programming language that forms the basis to other programming languages such as C++, Perl and Java. It

H.Melikian

Examples

/* Comments spanning several */

/* lines can be commented*/

/* out like this!*/

/* But this is a simpler way of doing it! */

// These are C++

/* /* NESTED COMMENTS ARE ILLEGAL!! */ *

Page 6: H.Melikian Introduction on C C is a high-level programming language that forms the basis to other programming languages such as C++, Perl and Java. It

H.Melikian

Creating Executable Programs

There are several tasks that need to be performed before youcan run a program: coding, compiling and linking.1.  You have to write the source code in C. You declare whichheader files you want to include within the source code. Thesource code must be saved with the extension .c 2.  Then you run the compiler, which translates the source code

into machine, or binary code, which the computer can understand. Computers do NOT understand C !

3.  Sometimes the source code is still lacking some parts, so after going through the compiler, the code is passed through a LINKER. This basically "links" the source code to other library or object files so that the final binary code is produced.

Page 7: H.Melikian Introduction on C C is a high-level programming language that forms the basis to other programming languages such as C++, Perl and Java. It

H.Melikian

Hello World

The #include Directive Header Files

Header files have the extension .h

and the full filename follows from

The #include directive.

Your First Program

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

Page 8: H.Melikian Introduction on C C is a high-level programming language that forms the basis to other programming languages such as C++, Perl and Java. It

H.Melikian

The main Function A FUNCTION can be thought of a set of instructions that is

carried out when it is CALLED.

All C programs must have a main function. You can only have one, but you can place it anywhere within the code.

The program always start with the main function and ends when the end of main is reached. Functions return a value too - this will be explained later.

If a function returns nothing, it's return type is of type void –

The main function is special, as it returns an integer by default, which is why you'll see me write return 0.

Page 9: H.Melikian Introduction on C C is a high-level programming language that forms the basis to other programming languages such as C++, Perl and Java. It

H.Melikian

Constants and Variables

Variables are like containers in your computer's memory - you can store values in them and retrieve or modify them when necessary.

Constants are like variables, but once a valued is stored (i.e. the constant is INITIALIZED), its value cannot be change

There are several rules that you must follow when naming variables:

Page 10: H.Melikian Introduction on C C is a high-level programming language that forms the basis to other programming languages such as C++, Perl and Java. It

H.Melikian

Do no forget

Variable names....

CANNOT start with a number 2times CAN contain a number elsewhere times2 CANNOT contain any arithmetic operators... a*b+c ... or any other pun ctuation marks... #@%£!! ... but may contain or begin with an underscore _height CANNOT be a C keyword while CANNOT contain a space stupid me CAN be of mixed cases HaykMelik

Page 11: H.Melikian Introduction on C C is a high-level programming language that forms the basis to other programming languages such as C++, Perl and Java. It

H.Melikian

Some Terminology EXPRESSIONS consist of a mixture of constants, variables and

operators (more about operators later). They return values.

17 /* a constant */

x /* a variable */

x + 17 /* a variable plus a constant */ STATEMENTS are instructions and are terminated with a

semicolon,;.

x = 1 + 8;

printf("We will learn printf soon!\n");

int x, y, z; /* more on "int" later */ STATEMENT BLOCKS, on the other hand, can contain a

group of statements

Page 12: H.Melikian Introduction on C C is a high-level programming language that forms the basis to other programming languages such as C++, Perl and Java. It

H.Melikian

Example STATEMENT BLOCKS, on the other hand, can contain a

group of statements

if(x==10) { /* block 1 */

printf("x equals 10\n");

x = 11; printf("Now x equals 11\n");

x = x + 1; printf("Now x equals 12\n");

} /* end of block 1 */

else { /* block 2 */

printf("x not equal to 10\n");

printf("Good bye!\n");

} /* end of block 2 */

Page 13: H.Melikian Introduction on C C is a high-level programming language that forms the basis to other programming languages such as C++, Perl and Java. It

H.Melikian

Types of ConstantsNumbers are considered as LITERAL constants –

you can't change the number 20, nor can you assign something

else into 20.

On the other hand, SYMBOLIC constants can be assigned a value during initialization and are represented by a word.

const int radius = 5;

There are several ways to define constants in C. Later, we will meet the #define directive and the enum keyword –

two more ways of defining constants.

Page 14: H.Melikian Introduction on C C is a high-level programming language that forms the basis to other programming languages such as C++, Perl and Java. It

H.Melikian

Page 15: H.Melikian Introduction on C C is a high-level programming language that forms the basis to other programming languages such as C++, Perl and Java. It

H.Melikian

HW #4Readig:

LAB:Homework:Assigned Due 09/23/02

PS. To submit you have to use the fallowing command

% mail melikian< typescript

Page 16: H.Melikian Introduction on C C is a high-level programming language that forms the basis to other programming languages such as C++, Perl and Java. It

H.Melikian

Life Cycle

Page 17: H.Melikian Introduction on C C is a high-level programming language that forms the basis to other programming languages such as C++, Perl and Java. It

H.Melikian

input

Page 18: H.Melikian Introduction on C C is a high-level programming language that forms the basis to other programming languages such as C++, Perl and Java. It

H.Melikian

compiler