01 introduction to c, Lecture notes of C programming

Introduction to C

Typology: Lecture notes

2013/2014

Uploaded on 12/29/2014

wsabren1..
wsabren1.. 🇮🇶

4

(5)

11 documents

1 / 48

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to
C Language
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30

Partial preview of the text

Download 01 introduction to c and more Lecture notes C programming in PDF only on Docsity!

Introduction to

C Language

A look at C



We are going

(^) to learn a language called C.



C is a very flexible and powerful

the early 1970s.programming language originally designed in



specially designed for this.operating system was written in, and wasIt is famous as the language the UNIX



that field and it is currently very popular.However its use has now spread way beyond

C is a professional language



In programming

terms this means is that C

programming languages.lacks some safety features provided by other



This makes the language much more flexible.



However, if

I do something stupid C will not

I do with a safer language.crashing the computer with a C program thanstop me, so I have a much greater chance of

Making C Run



You actually write

the program using

part of the compiling and linking system.some form of text editor - which may be



C is usually

a compiled programming

language.



language directly,The computer cannot understand the



so a program called a

compiler

instructions which do the job. converts the C into the machine code

LinkingLinking LinkingLinking



Linking is

when all the various portions of

something which can be run.your program are brought together to form



pieces of code from a library.process is that it allows us to reuse standardThe reason that we have the additional linking



common to lots of other programs.Many things your program will do are



Once the

linker has finished you are left with

you run this your program gets control!a free standing file which is your program. If

LinkingLinking LinkingLinking



a compiled version of this code into a library.program, a much more efficient way is to putdoes this every time you compile yourRather than compile the program code which



when it creates your program.then loads the relevant part from the libraryparticular function you want to use, the linkerYour program just contains a reference to the



get it going.you will then spend hours fiddling with tosomething which almost works, whichYou will almost certainly end up with



If you had

sat down with a pencil and

around half the time.probably get to a working system inworked out the solution first you would

What Comprises

a C Program?



A program

is the thing that you write to

perform a particular task.



It will

(^) actually be a file of text, often called a

A source file contains three things :source file. This is what the compiler acts on.

Instructions to the compiler

hold the data to be stored and manipulated.Information about the structures, which will

Instructions which manipulate the data.

Storing the Data



Programs work

by processing data. The data

the program processes it.has to be stored within the computer whilst



All computer languages support variables of

one form or another. A

variable

is simply a

the program runs.named location in which a value is held whilst



a particular bank customer.structure could hold all the information abouthold more than one item, for example a singleC also lets you build up structures which can

Describing the Solution



The actual

instructions, which describe

be part of your program.your solution to the problem, must also



a function.which does one particular thing, is calledIn the case of C a lump of program,



Experienced

programmers break down

and make a function for each.a problem into a number of smaller ones

FunctionsFunctions FunctionsFunctions



A

statement

is

an

instruction

to

perform

one

particular

operation,

for

example

add

two

numbers together and store the result.



You

give

a

name

to

each

function

that

you

save_file.function fit what it does, for example menu orcreate, and you try to make the name of the



The

C

language

actually

runs

program

by

looking for a function with a special name,

main



endsrunning, and when main finishes, your programThis function is called when your program starts

The Advantages of C



The good news

(^) about C is that you can write

quite easily.access low-level facilities in your computervery "close to the hardware“, so that you cancode, which runs quickly, and your program is



The use of

compiler directives to the pre-

several different types of computer.version of a program which can be compiled onprocessor make it possible to produce a single



systems.versions of C - they can be used on allThe function libraries are standard for all

Another Disadvantage



statements.express exactly what you want to do in very fewC allows you to write very terse code. You can



harder to understand.But it has the side effect of making them much



what each part is supposed to do.At the time you write the code you know exactly



Several months you will need time to

"get back

inside it."



at all.Other people may not be able to understand it

A First C Program



Perhaps the

(^) best way to start looking at C is to jump

{ void main ( void )#include <stdio.h>straight in with our first ever C program, so here it is:

printf ( "The area of glass is : %f metres.\n",area )wood_length = 2 * ( height + width ) * 3.25 ;area = 2 * height * width ;scanf ( "%f", &width ) ;scanf ( "%f", &height ) ;float height, width, area, wood_length ;

;

printf ( "The length of wood is : %f

} feet.\n",wood_length ) ;