Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to C Programming CE00312-1 Lecture 1 Introduction to C.

Similar presentations


Presentation on theme: "Introduction to C Programming CE00312-1 Lecture 1 Introduction to C."— Presentation transcript:

1 Introduction to C Programming CE00312-1 Lecture 1 Introduction to C

2 Module Structure 2 lectures, 1 Tutorial, 1 Practical per week Module Handbook contains details teaching team class contact, reading list Assessment Web site:-www.soc.staffs.ac.uk/rgh1/ lecture notes and practical exercises AFTER delivery Module handbook to download

3 Origins of C 1971 Ritchie and Thompson develop Unix written in assembly language This Unix wasn’t portable, a suitable HLL with low level facilities was required CPL had been developed in the 60’s CPL was a large language, later pared down -> BCPL Thompson further reduced BCPL -> B Ritchie enhanced B -> C. Unix was rewritten in C Unix + C became widely available Unix is now portable; any machine with a C compiler can run Unix

4 C Standards and Variants 1978; Kernighan & Ritchie publish “The C Programming Language” (Traditional C) or K&R C 1990 ANSI Standard (ANSI C) Anything written in traditional C will run on an ANSI C compiler Also available: Borland C, Microsoft C GNU C invoked by the gcc command is a version of ANSI C that runs on UNIX or windows platforms

5 Characteristics of C Suitable for systems programming Suitable for applications programming C is not a visual language C is not object oriented C supports structured programming functions

6 The C language C commands are close to machine code Cryptic syntax – programs can be written quickly but can be difficult to read small overheads - fast few safeguards - flexible but dangerous permissive data typing – gives flexibility but reduces the number of errors detected at compilation time can access memory directly using pointers can overflow arrays

7 Why study the C language? Most widely used programming language. Many application areas real time systems Lots of employment prospects. Other languages have developed from C: C++, Visual C++, Java, C#……. writing C code forces you to think about programming fundamentals Most common example of procedural programming paradigm.

8 Developing a C Program Fully understand the problem Design a solution Test Implement in C suitable programming language Test Release Maintain

9 Steps in implementation of C programs Editing: using an editor program, write the code Preprocessing and Compiling translate the C source code into a form that can be executed the output produced by the compiler is called the object program Linking: combine all of the intrinsic functions and individual object files into a single program ready for execution Running run the object program

10 Overall Structure of a C program # include DEFINITION OF ANY CONSTANTS int main(void) /* some explanatory comment */ { DECLARATIONS STATEMENTS return 0; }

11 A Simple C program #include /* A simple C program*/ main () { printf(“Hello, world.\n”); }

12 Expressions and Statements A section of code such as n1+n2 is called an expression A statement is a step in a program An expression becomes a statement when it is terminated by a semicolon

13 Comments /* An example of a C comment */ A multiline comment can also be used: /* This is a multiline comment. Indentation is not compulsory but it makes the code more readable */ // In some versions this comment can be used

14 The preprocessor The C preprocessor replaces the line #include with the contents of the header file stdio.h C header files end with the suffix “.h” The C preprocessor also removes the line /* A simple C program*/ because it is a comment On many systems the preprocessor is built into the compiler All preprocessor commands or compiler directives start with #

15 Statements All C programs must contain a main() function definition. main() is the entry point for the program. Like all function definitions, main() is delimited by curly braces { } should contain one or more statements. Statements are what do things (“perform actions”) in C. printf prints something on the standard output.

16 C is a compiled language!!! Once a program is written in C, it cannot be executed without further translation on the computer. source program=>machine language this is unlike interpreted languages where each statement in the source code is translated individually and executed immediately in compiled languages such as C all the statements are translated before being executed

17 The Pre-Processor CPure C Object Code C Compiler Pre- processor Object code is executed when the program runs

18 A simple C program #include #define PI 3.14 int main(void) /*calculates the area and the */ /*circumference of a circle of radius 3 */ { int radius; float circum, area; radius=3; circum=2*PI*radius; area=PI*radius*radius; printf(“circumference %f\n”, circum); printf(“area %f\n”, area); return 0; }

19 Output (printf statements) The printf statement consists of any text to be printed plus a format specifier for each argument Format Specifiers: %d, %6d, %f, %6.2f, %ld, %lf, %c, %s Special Character Constants for use in control strings; \nnewline \ttab \\backslash \’single quote


Download ppt "Introduction to C Programming CE00312-1 Lecture 1 Introduction to C."

Similar presentations


Ads by Google