Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE-105 – Structured Programming CSE, BUET Introduction CSE 105 Structured Programming Language (C)

Similar presentations


Presentation on theme: "CSE-105 – Structured Programming CSE, BUET Introduction CSE 105 Structured Programming Language (C)"— Presentation transcript:

1 CSE-105 – Structured Programming CSE, BUET Introduction CSE 105 Structured Programming Language (C)

2 CSE-105 – Structured Programming CSE, BUET 2 Programming Languages Machine Language Assembly Language Structured Programming Language (Fortran, Basic) Modular Programming Language (C, Pascal ) Object Oriented Programming Language (C++, Java ) Scripting Languages (Python, Awk) Logic Programming (Prolog ) Low level Mid level high level Binary code Pneumonic per binary code Program is a set of procedures Application is set of Modules – consisting of related procedures World is composed of entities objects – consisting of data and behavior English like construction Focus on task not on programming Application-specific

3 CSE-105 – Structured Programming CSE, BUET 3 C: History Developed in the 1970s – in conjunction with development of UNIX operating system When writing an OS kernel, efficiency is crucial This requires low-level access to the underlying hardware: e.g. programmer can leverage knowledge of how data is laid out in memory, to enable faster data access UNIX originally written in low-level assembly language – but there were problems: No structured programming (e.g. encapsulating routines as functions, methods, etc.) – code hard to maintain Code worked only for particular hardware – not portable

4 CSE-105 – Structured Programming CSE, BUET 4 C: Characteristics C takes a middle path between low-level assembly language… Direct access to memory layout through pointer manipulation Concise syntax, small set of keywords … and a high-level programming language like Java: Block structure Some encapsulation of code, via functions Type checking (pretty weak)

5 CSE-105 – Structured Programming CSE, BUET 5 C: Dangers C is not object oriented! Cant hide data as private or protected fields You can follow standards to write C code that looks object- oriented, but you have to be disciplined – will the other people working on your code also be disciplined? C has portability issues Low-level tricks may make your C code run well on one platform – but the tricks might not work elsewhere The compiler and runtime system will rarely stop your C program from doing stupid/bad things Compile-time type checking is weak No run-time checks for array bounds errors, etc. like in Java

6 CSE-105 – Structured Programming CSE, BUET 6 Separate compilation A C program consists of source code in one or more files Each source file is run through the preprocessor and compiler, resulting in a file containing object code Object files are tied together by the linker to form a single executable program Source code file1.c Preprocessor/ Compiler Object code file1.obj Source code file2.c Preprocessor/ Compiler Object code file2.oobj Linker Libraries Executable code a.out

7 CSE-105 – Structured Programming CSE, BUET 7 Separate compilation Advantage: Quicker compilation When modifying a program, a programmer typically edits only a few source code files at a time. With separate compilation, only the files that have been edited since the last compilation need to be recompiled when re- building the program. For very large programs, this can save a lot of time.

8 CSE-105 – Structured Programming CSE, BUET 8 Structure of a Simple C Program #include stdio.h #include stdlib.h #define CONSTANT_NAME 4 void main(void) { float y=0.1; int x[100]; char name[50]; printf(\nProgram start); ………..……. } Header files to be included Definition of any constants used in program Main C Program Section Variable declaration and (optional) initialisation C Program Code

9 CSE-105 – Structured Programming CSE, BUET 9 Header File Section All C programs will contain this section Header files are used by the computer when it is verifying the syntax of your program In your C programs, you will use some standard C commands (functions) that someone else has written the code to implement Examples are the functions to display text on the screen (printf) and to read information typed in from the keyboard (scanf) The contents of the header files listed in your program must contain a description of the valid syntax for such C functions At some point, you may write your own C functions which can be used by any C program in which case you will also have to create an associated header file The syntax for including a header file is #include header file name

10 CSE-105 – Structured Programming CSE, BUET 10 Definition of Constants This is an optional section in your C file It is highly recommended that if you have to use a constant value in your program that you define it in this section It could save you a lot of time if you ever need to change its value (particularly if it occurs in many places in a long program) The syntax for the definition is #define constant name constants value

11 CSE-105 – Structured Programming CSE, BUET 11 Definition of Constants It is good programming practice that constants names should only contain CAPITAL LETTERS and the _ (underscore) character You can define integer, real or character constants #define INTEGER_CONSTANT 101 #define REAL_CONTANT #define CHARACTER_CONSTANT hello

12 CSE-105 – Structured Programming CSE, BUET 12 Main C Program Section In simple C programs, this section is always started with the line void main(void) The beginning and end of the main section are delimited by chain brackets - { and } It is good programming practice to indent your code in this section If you have further sub-blocks of code then all the lines of code in these sub-sections should be further indented

13 CSE-105 – Structured Programming CSE, BUET 13 Main C Program Section It is good practice to include comments in you C file One comment at the top of the program should provide a general overview of what the C file does Many comments within the file should explain the various steps in the program Comments can be placed either Within the bounds of /* and */ On a single line starting with //

14 CSE-105 – Structured Programming CSE, BUET 14 Compilation Process Source code file1.c Preprocessor Source code file2.c Linker Libraries (e.g. stdio.lib) Executable code a.exe Header files stdio.h Intermediate C code Compiler Object code file1.obj Preprocessor Intermediate C code Compiler Object code file1.obj Your header file2.h

15 CSE-105 – Structured Programming CSE, BUET 15 RAM Execution Environment StackHeap OS memomry Executable code Variables / data Your program Other Programs CPU ALUCU Registers Input Devices Output Devices Disk drives

16 CSE-105 – Structured Programming CSE, BUET 16 References The C Programming Language, 2 nd Edition, Brian W. Kernighan & Dennis M.Ritchie. Web page … …


Download ppt "CSE-105 – Structured Programming CSE, BUET Introduction CSE 105 Structured Programming Language (C)"

Similar presentations


Ads by Google