Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 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 learnhttp://download.cnet.com C? Well, because it opens the doors to other languages like C++, Java.

2 H.Melikian Syntax The C code you write is called the SYNTAX. Syntax is a mixture of: v C keywords like int, for and return. v Constants and variable. v Operators like + (arithmetic "addition"), || (logical "or") and & (the "address of" operator). v Note that C is CASE SENSITIVE! v “White space“ in a C program does not affect. Use extra spaces to make your programs more readable

3 H.Melikian Compilers Microsoft's Visual C++ 6.0 (Room 106 & 209) This is a powerful package and is suitable for people who are interested in software engineering. In general, C++ packages are fine for compiling C programs – the compiler executed depends 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 to run the program.

4 H.Melikian Commenting Your Code v You can add comments to your code by enclosing your remarks within /*and */. However, nested comments aren't allowed v 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. v You can use /* and */ to comment out sections of code when it comes to finding errors, instead of deletion

5 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!! */ *

6 H.Melikian Creating Executable Programs There are several tasks that need to be performed before you can run a program: coding, compiling and linking.coding 1. You have to write the source code in C. You declare which header files you want to include within the source code. The source 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.

7 H.Melikian Hello World The #include Directive v Header Files Header files have the extension.h and the full filename follows from The #include directive. Your First Program #include int main(){ printf("Hello World!\n"); return 0; }

8 H.Melikian The main Function v A FUNCTION can be thought of a set of instructions that is carried out when it is CALLED. v All C programs must have a main function. You can only have one, but you can place it anywhere within the code. v 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. v If a function returns nothing, it's return type is of type void – v The main function is special, as it returns an integer by default, which is why you'll see me write return 0.

9 H.Melikian Constants and Variables v Variables are like containers in your computer's memory - you can store values in them and retrieve or modify them when necessary. v Constants are like variables, but once a valued is stored (i.e. the constant is INITIALIZED), its value cannot be change v There are several rules that you must follow when naming variables:

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

11 H.Melikian Some Terminology v 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 */ v 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 */ v STATEMENT BLOCKS, on the other hand, can contain a group of statements

12 H.Melikian Example v 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 */

13 H.Melikian Types of Constants Numbers are considered as LITERAL constants – you can't change the number 20, nor can you assign something else into 20. v On the other hand, SYMBOLIC constants can be assigned a v value during initialization and are represented by a word. const int radius = 5; v 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.

14 H.Melikian

15 HW #4 Readig: LAB: Homework: Assigned Due 09/23/02 PS. To submit you have to use the fallowing command % mail melikian< typescript

16 H.Melikian Life Cycle

17 H.Melikian input

18 H.Melikian compiler


Download ppt "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."

Similar presentations


Ads by Google