Introduction on C - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Introduction on C

Description:

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 ... – PowerPoint PPT presentation

Number of Views:58
Avg rating:3.0/5.0
Slides: 19
Provided by: Hayk1
Learn more at: http://www.cs.nccu.edu
Category:

less

Transcript and Presenter's Notes

Title: Introduction on C


1
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 .


2
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

3
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
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

5
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
Creating Executable Programs
  • There are several tasks that need to be performed
    before you
  • can run a program coding, compiling and linking.
  • 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
Hello World
Your First Program include ltstdio.hgt int
main() printf("Hello World!\n")
return 0
  • The include Directive
  • Header Files
  • Header files have the extension .h
  • and the full filename follows from
  • The include directive.

8
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.

9
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

10
Do no forget
  • Variable names....
  • CANNOT start with a number
    2times
  • CAN contain a number elsewhere
    times2
  • CANNOT contain any arithmetic operators...
    abc
  • ... or any other pun ctuation marks...
    _at_!!
  • ... 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

11
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

12
Example
  • STATEMENT BLOCKS, on the other hand, can contain
    a
  • group of statements
  • if(x10) / 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
Types of Constants
  • Numbers 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.

14
(No Transcript)
15
HW 4
  • Readig
  • LAB
  • Homework
  • Assigned Due 09/23/02
  • PS. To submit you have to use the fallowing
    command
  • mail melikianlt typescript

16
Life Cycle
17
input
18
compiler
Write a Comment
User Comments (0)
About PowerShow.com