article

Learn C

Email
Submitted on: 1/7/2015 8:33:00 AM
By: Raakesh Bharathi (from psc cd)  
Level: Beginner
User Rating: By 3 Users
Compatibility: C, C++ (general), Microsoft Visual C++, Borland C++
Views: 658
 
     Learn the launguage C, with some of my tutorials

 
				Introduction to C Programming 
So you want to learn C? We hope to provide you with an easy step by step guide to programming in C. The course is split up into several sections, or lessons,

which include C example programs for you to demonstrate what has been taught.

Although the ordering of the sections does not have to be strictly followed,

the sections become progressively more involved and assume background knowledge attained from previous sections.

Good Luck!

Before you start:

1.Please read this Introduction.

2.It is a long course and will take you quite a while to complete.

if you have any problems please let us know!.

The Course Section Topics:

1.Overview of C.

a.Why use C?

b.Uses of C

c.A Brief History of C

d.C for Personal Computers

2.Running C Programs.

a.Using Microsoft C.

b.Unix System.

3.Structure of C Programs.

a.C's Character Set

b.The form of a C Program

c.The layout of C Programs

d.Preprocessor Directives

4.Your First Program.

a.Commenting Programs.

5.Data Types - Part I.

a.Integer Number Variables.

b.Decimal Number Variables.

c.Character Variables.

d.Assignment Statement.

e.Arithmetic Ordering.

f.Something To Declare.

6.Input and Output

a.printf.

b.The % Format Specifiers.

c.Formatting Your Output.

d.scanf.

7.Control Loops

a.The while and do while Loops.

b.Conditions, or Logical Expression.

c.The for Loop.

8.Conditional Execution

a.Program Control - if , if else etc..

b.Using break and continue Within Loops.

c.Select Paths with switch.

9.Structure and Nesting

10.Functions and Prototypes

a.Functions - C's Building Blocks.

b.Functions and Local Variables.

c.Getting the Value of Variables into Functions.

d.Functions and Prototypes.

e.What is ANSI C?.

f.Standard Library Functions.

11.Data Types - Part II

a.Global Variables.

b.Constant Data Types.

12.Arrays

13.Pointers

a.Point To Point.

b.Swap Shop.

c.Pointers Linked To Arrays.

14.Strings

a.Stringing Along.

b.As easy as... B or C?.

c.A Sort OF Bubble Program.

15.Structures

a.Defining A New Type.

b.Structures and Functions.

c.Pointers To Structures.

d.Malloc.

e.Structures and Linked Lists.

f.Structures and C++.

g.Header Files.

16.File Handling

a.Stream Files.

b.Text File Functions.

c.Binary File Functions.

d.File System Functions.

e.Command Line Parameters.

17.Recommended Books

18.Appendix: C's functions

You've now reached the end of this tutorial.

We have covered a lot of ground - but this has been a first course in C and there is still plenty to learn.

However, as long as you keep in mind that C is an essentially simple language and how new features are built from this simplicity you shouldn't have many problems.

You also need to be aware of the fact that C is a very low-level language and as a result allows programmers

to confuse data types and muck around with the bit

patterns of the data in a way that higher level languages would disown!

You probably need to make sure that you understand binary and the way that values are

represented to get the best from C. Overview of C

Objectives:

This section is designed to give you a general overview of the C programming language.

Although much of this section will be expanded in later sections it gives you a taste of what is to come.

Why use C?:

C has been used successfully for every type of programming problem imaginable from operating systems to spreadsheets to expert systems - and efficient compilers are available for machines ranging in power from the Apple Macintosh to the Cray supercomputers.

The largest measure of C's success seems to be based on purely practical considerations:

the portability of the compiler;

the standard library concept;

a powerful and varied repertoire of operators;

an elegant syntax;

ready access to the hardware when needed;

and the ease with which applications can be optimized by hand-coding isolated procedures C is often called a "Middle Level" programming language.

This is not a reflection on its lack of programming power but more a reflection on its capability to access the system's low level functions.

Most high-level languages (e.g. Fortran) provides everything the programmer might want to do already built into the language.

A low level language (e.g. assembler) provides nothing other than access to the machines basic instruction set.

A middle level language, such as C, probably doesn't supply all the constructs found in high-languages -

but it provides you with all the building blocks that you will need to produce the results you want!

Uses of C C was initially used for system development work, in particular the programs that make-up the operating system. Why use C? Mainly because it produces code that runs nearly as fast as code written in assembly language.

Some examples of the use of C might be:

Operating Systems

Language Compilers

Assemblers

Text Editors

Print Spoolers

Network Drivers

Modern Programs

Data Bases

Language Interpreters

Utilities

In recent years C has been used as a general-purpose language because of its popularity with programmers.

It is not the world's easiest language to learn and you will certainly benifit if you are not learning C as your first programming language!

C is trendy (I nearly said sexy) - many well established programmers are switching to C for all sorts of reasons,

but mainly because of the portability that writing standard C programs can offer.

A Brief History of C:

C is a general-purpose language which has been closely associated with the UNIX operating system for which it was developed - since the system and most of the programs that run it are written in C.

Many of the important ideas of C stem from the language BCPL, developed by Martin Richards.

The influence of BCPL on C proceeded indirectly through the language B, which was written by Ken Thompson in 1970 at Bell Labs, for the first UNIX system on a DEC PDP-7.

BCPL and B are "typeless" languages whereas C provides a variety of data types.

In 1972 Dennis Ritchie at Bell Labs writes C and in 1978 the publication of

The C Programming Language by Kernighan & Ritchie caused a revolution in the computing world.

In 1983, the American National Standards Institute (ANSI) established a committee to provide a modern, comprehensive definition of C. The resulting definition, the ANSI standard, or "ANSI C", was completed late 1988. A Rough Guide to Programming Languages is available on-line for those of you that are interested.

C for Personal Computers:

With regards to personal computers Microsoft C for IBM (or clones) PC's.

and Borlands C are seen to be the two most commonly used systems. However, the latest version of Microsoft C is now considered to be the most powerful and efficient C compiler for personal computers.

Running C Programs

Objectives:

Having read this section you should be able to:

1.Edit, link and run your C programs

This section is primarily aimed at the beginner who as no or little experience of using compiled languages.

We cover the various stages of program development.

The basic principles of this section will apply to what ever C compiler you choose to use, the stages are nearly always the same

The Edit-Compile-Link-Execute Process:

Developing a program in a compiled language such as C requires at least four steps:

1.editing (or writing) the program 2.compiling it 3.linking it 4.executing it We will now cover each step separately.

Editing:

You write a computer program with words and symbols that are understandable to human beings.

This is the edit part of the development cycle.

You type the program directly into a window on the screen and save the resulting text as a separate file.

This is often referred to as the source file (you can read it with the TYPE command in DOS or the cat command in unix).

The custom is that the text of a C program is stored in a f ile with the extension .

c for C programming language

Compiling:

You cannot directly execute the source file.

To run on any computer system,

the source file must be translated into binary numbers understandable to the computer's Central Procesing Unit

(for example, the 80*87 microprocessor).

This process produces an intermediate object file - with the extension .obj, the .obj stands for Object.

Linking:

The first question that comes to most peoples minds is Why is linking necessary?

The main reason is that many compiled languages come with library rountines which can be added to your program.

Theses routines are written by the manufacturer of the compiler to perform a variety of tasks, from input/output to complicated mathematical functions.

In the case of C the standard input and output functions are contained in a library (stdio.h) so even the most basic program will require a lib rary function.

After linking the file extension is .exe which are executable files.

Executable files:

Thus the text editor produces .

c source files, which go to the compiler, which produces .obj object files, which go to the linker, which produces .exe executable file.

You can then run .exe files as you can other applications, simply by typing their names at the DOS prompt or run using windows menu.

Using Microsoft C:

Edit stage:

Type program in using one of the Microsoft Windows editing packages.

Compile and link:

Select Building from Make menu.

Building option allows you to both compile and link in the same option.

Execute:

Use the Run menu and select Go option.

Errors:

First error highlighted.

Use Next Error from Search menu for further errors if applicable.

If you get an error message, or you find that the program doesn't work when you finally run it (at least not in the way you anticipated) you will have to go back to the source file - the .c file - to make changes and go through the whole development process again!

Structure of C Programs

Objectives:

Having completed this section you should know about:

1.C's character set

2.C's keywords

3.the general structure of a C program

4.that all C statement must end in a ;

5.that C is a free format language

6.all C programs us header files that contain standard library functions.

C's Character Set:

C does not use, nor requires the use of, every character found on a modern computer keyboard.

The only characters required by the C Programming Language are as follows:

A - Z

a -z

0 - 9

space . , : ; ' $ "

# % & ! _ {} [] () < > |

+ - / * =

The use of most of this set of characters will be dicussed throughout the course.

The form of a C Program:

All C programs will consist of at least one function, but it is usual (when your experience grows) to write a C program that comprises several functions.

The only function that has to be present is the function called main.

For more advanced programs the main function will act as a controling function calling other functions in their turn to do the dirty work! The main function is the first function that is called when your program executes.

C makes use of only 32 keywords which combine with the formal syntax to the form the C programming language.

Note that all keywords are written in lower case - C, like UNIX, uses upper and lowercase text to mean different things.

If you are not sure what to use then always use lowercase text in writing your C programs.

A keyword may not be used for any other purposes.

For example, you cannot have a variable called auto.

The layout of C Programs:

The general form of a C program is as follows

(don't worry about what everything means at the moment - things will be explained later):

preprocessor directives

global declarations

main()

{

local variables to function main ;

statements associated with function main ;

}

f1()

{

local variables to function 1 ;

statements associated with function 1 ;

}

f2()

{

local variables to function f2 ;

statements associated with function 2 ;

}

.

.

.

etc

Note the use of the bracket set () and {}.

() are used in conjunction with function names whereas {} are used as to delimit the C statements that are associated with that function.

Also note the semicolon - yes it is there, but you might have missed it! a semicolon (;) is used to terminate C statements.

C is a free format language and long statements can be continued, without truncation, onto the next line.

The semicolon informs the C compiler that the end of the statement has been reached.

Free format also means that you can add as many spaces as you like to improve the look of your programs.

A very common mistake made by everyone, who is new to the C programming language, is to miss off the semicolon.

The C compiler will concatinate the various lines of the program together and then tries to understand them - which it will not be able to do.

The error message produced by the compiler will relate to a line of you program which could be some distance from the initial mistake.

Preprocessor Directives:

C is a small language but provides the programmer with all the tools to be able to write powerful programs.

Some people don't like C because it is too primitive! Look again at the set of keywords that comprises the C language and see if you can find a command that allows you to print to the computer's screen the result of, say, a simple calculation.

Don't look too hard because it dosen't exist.

It would be very tedious, for all of us, if everytime we wanted to communicate with the computer we all had to write our own output functions.

Fortunately, we do not have to. C uses libraries of standard functions which are included when we build our programs.

For the novice C programmer one of the many questions always asked is does a function already exist for what I want to do? Only experience will help here but we do include a function listing as part of this course.

All programs you will write will need to communicate to the outside world - I don't think I can think of a program that doesn't need to tell someone an answer.

So all our C programs will need at least one of C's standard libraries which deals with standard inputting and outputting of data. This library is called stdio.h and it is declared in our programs before the main function.

The .h extension indicates that this is a header file.

I have already mentioned that C is a free format language and that you can layout your programs how you want to using as much white space as you like.

The only exception are statements associated with the preprocessor.

All preprocessor directives begin with a # and the must start in the first column.

The commonest directive to all C programs is: #include

Note the use of the angle brackets (< and >)

around the header's name. These indicate that the header file is to be looked for on the system disk which stores the rest of the C program application.

Some text books will show the above statement as follows:

#include "stdio.h"

The double quotes indicate that the current working directory should be searched for the required header file.

This will be true when you write your own header files but the standard header files should always have the angle brackets around them.

NOTE: just to keep you on your toes - preprocessor statements, such as include, DO NOT use semi-colons as delimiters! But don't forget the # must be in the first column.

Thats enough background to C programs - lets get on with our first program which will start to bring together some of the ideas outlined above.

Your First Program

Objectives:

Yes - it's the ubiquitous

"Hello World" program.

All your first program is going to do is print the message "Hello World" on the screen. The program is a short one, to say the least.

Here it is:

#include

int main()

{

printf("Hello World\n");

return 0;

}

*******THERES A LOT MORE LOOK FOR TEM IN MY NEXT TUTORIAL********


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.