4

Click here to load reader

Ete105 fall2014 lec2 (East West University)

Embed Size (px)

DESCRIPTION

East West University ETE lecture for all.

Citation preview

Page 1: Ete105 fall2014 lec2 (East West University)

EAST WEST UNIVERSITY

Department of Electronics & Communications Engineering

Semester: Fall 2014

ETE 105: Computer Fundamentals and Programming Language

Lecture 2: Introduction to C Programming Language

WHAT IS A PROGRAMMING?

Programming is telling a computer to do something. Computers know very little and have no

common sense what-so-ever, so your instructions have to be broken down to small, simple parts.

Computer will do exactly what you tell them.

BASIC PROGRAMMING

Programming at its core is about two things:

Defining problems

Solving problems

These two aspects are tightly integrated. To solve a problem you first must clearly know what it

is. Clearly and completely defining a problem gets you a long way towards a solution.

WHAT IS A PROGRAMMING LANGUAGE?

A programming language is a set of rules that provides a way of telling a computer what

operations to perform. It provides a linguistic framework for describing computations.

English is a natural language. It has words, symbols and grammatical rules. A programming

language also has words, symbols and rules of grammar. The grammatical rules are called

syntax. Each programming language has a different set of syntax rules.

TYPES OF PROGRAMMING LANGUAGE

Machine Language:

The fundamental language of the computer’s processor, also called Low Level

Language.

All programs are converted into machine language before they can be executed. This is

known as compilation or interpretation.

Consists of combination of 0’s and 1’s that represent high and low electrical voltage.

Assembly Language:

Symbolic operation codes replaced binary operation codes.

Assembly language programs needed to be “assembled” for execution by the computer.

Each assembly language instruction is translated into one machine language instruction.

Very efficient code and easier to write.

High level language:

Instruction set is more compatible with human language and human thought process.

Uses English like statements.

Computer (programming) languages those are easier to learn.

Examples are C ++, Visual Basic, Pascal, FORTRAN, etc.

Page 2: Ete105 fall2014 lec2 (East West University)

EAST WEST UNIVERSITY

Department of Electronics & Communications Engineering

Figure 1

INTRODUCTION TO C

C is a general-purpose, structured programming language. Its instructions consists of

terms that resemble algebraic expressions, augmented by certain English keywords such

as if, else, for, do, while.

It has the flexibility to use at a lower level thus bridging the gap between machine

language and the more conventional high-level language.

C is used for system programming, operating systems, microcontrollers, embedded

processors, DSP processors, applications programming.

C has a relatively small instruction set, though actual implementations include extensive

library functions which enhance the basic instructions. Furthermore, the language

encourages users to write additional library functions of their own.

LEARNING C

There is a close analogy between learning English language and learning C language.

Figure 2

Page 3: Ete105 fall2014 lec2 (East West University)

EAST WEST UNIVERSITY

Department of Electronics & Communications Engineering

STRUCTURE OF A C PROGRAM

Every C program consists of one or more modules called functions. One of the functions must be

called main. The program will always begin by executing the main function, which may access

other functions. Any other function definitions must be defined separately, either ahead of or

after main.

Each function must contain:

1. A function heading, which consists of the function name, followed by an optional list of

arguments, enclosed in parentheses.

2. A list of argument declarations, if arguments are included in the heading.

3. A compound statements, which comprises the reminder of the function.

Arguments:

Symbols that represent information being passed between the function and other parts of

the program

Also referred to as parameters.

Compound Statements:

Enclosed within a pair of brace, i.e. {}

The braces may contain one or more elementary statements called expression statements.

Compound statements may be nested, one within another.

Each expression statement must end with a semicolon (;)

Comments:

Comments may appear anywhere within a program, as long as they are placed within the

delimiters /* and */

For example, /* this is a comment */

Such comments are helpful in identifying the program’s principle features or in

explaining the underlying logic of various program features.

Example 1

#include <stdio.h> /* LIBRARY FILE ACCESS*/

#include <stdlib.h> /* LIBRARY FILE ACCESS*/

main() /* FUNCTION HEADING*/

{

printf("I am a novice programmer."); /* OUTPUT STATEMENT*/

return 0;

}

Page 4: Ete105 fall2014 lec2 (East West University)

EAST WEST UNIVERSITY

Department of Electronics & Communications Engineering

Example 2: Write a C program that reads in the radius of a circle, calculates its area and

then writes the calculated result.

#include<stdio.h> /*LIBRARY FILE ACCESS*/

main() /*FUNCTION HEADING*/

{

float radius, area; /*VARIABLE DECLARATIONS*/

printf("Radius=?"); /*OUTPUT STATEMENT (PROMPT)*/

scanf("%f", &radius); /*INPUT STATEMENT*/

area=3.1416*radius*radius; /*ASSIGNMENT STATEMENT*/

printf("Area=%f", area); /*OUTPUT STATEMENT*/

}

GETTING SET UP

The very first thing, before starting out C, is to make sure that you have a compiler. A compiler

turns the program that you write into an executable that your computer can actually understand

and run. You may use Code::Blocks with MinGW.

Download and install Code::Blocks.

Run your first program that prints “Hello World!” following the instructions given in the

link below:

http://www.cprogramming.com/code_blocks/