Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 105 Structured Programming Language (C)

Similar presentations

Presentation on theme: "CSE 105 Structured Programming Language (C)"— Presentation transcript:

1 CSE 105 Structured Programming Language (C)
Introduction CSE 105 Structured Programming Language (C)

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

3 C: History This requires low-level access to the underlying hardware:
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 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 C: Dangers C is not object oriented! C has portability issues
Can’t “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 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 Libraries Linker Executable code a.out

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 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 Header File Section #include “header file name”
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 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 constant’s value

11 Definition of Constants
It is good programming practice that constant’s 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 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 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 Compilation Process Executable code a.exe Source code file1.c
Preprocessor Intermediate C code Object code file1.obj Compiler Your header file2.h Header files stdio.h Source code file2.c Intermediate C code Object code file1.obj Preprocessor Compiler Libraries (e.g. stdio.lib) Linker Executable code a.exe

15 Execution Environment
CPU Input Devices Registers ALU CU Output Devices RAM OS memomry Disk drives Stack Heap Your program Variables / data Executable code Other Programs

16 References “The C Programming Language” , 2nd Edition, Brian W. Kernighan & Dennis M.Ritchie. Web page …

Download ppt "CSE 105 Structured Programming Language (C)"

Similar presentations

Ads by Google