editorscommunity
Would you like to react to this message? Create an account in a few clicks or log in to continue.



 
HomeportalSearchLatest imagesRegisterLog in

 

 Programming /*code*/

Go down 
AuthorMessage
emz[12]
Moderator
emz[12]


Posts : 546
Join date : 2009-06-06
Location : cagayan de oro city

Programming /*code*/ Empty
PostSubject: Programming /*code*/   Programming /*code*/ EmptyThu Aug 20, 2009 11:41 pm



A C program is made by running a compiler which takes the typed source program and converts it into an object file that the computer can execute. A compiler usually operates in two or more phases (and each phase may have stages within it). These phases must be executed one after the other. As we shall see later, this approach provides a flexible way of compiling programs which are split into many files.

Declarations

Compiler languages require us to make a list of the names and types of all variables which are going to be used in a program and provide information about where they are going to be used. This is called declaring variables. It serves two purposes: firstly, it provides the compiler with a definitive list of the variables, enabling it to cross check for errors, and secondly, it informs the compiler how much space must be reserved for each variable when the program is run. C supports a variety of variable types (variables which hold different kinds of data) and allows one type to be converted into another. Consequently, the type of a variable is of great importance to the compiler. If you fail to declare a variable, or declare it to be the wrong type, you will see a compilation error.

The printf() function

One invaluable function provided by the standard input/output library is called printf or `print-formatted'. It provides an superbly versatile way of printing text. The simplest way to use it is to print out a literal string:

printf ("..some string...");

Text is easy, but we also want to be able to print out the contents of variables. These can be inserted into a text string by using a `control sequence' inside the quotes and listing the variables after the string which get inserted into the string in place of the control sequence. To print out an integer, the control sequence %d is used:

printf ("Integer = %d",someinteger);

The variable someinteger is printed instead of %d. The printf function is described in full detail in the relevant chapter, but we'll need it in many places before that. The example program below is a complete program. If you are reading this in Info, you can copy this to a file, compile and execute it.

Variables, Types and Declarations

Storing data. Descriminating types. Declaring data.

A variable is a seqeuence of program code with a name (also called its identifier). A name or identifier in C can be anything from a single letter to a word. The name of a variable must begin with an alphabetic letter or the underscore _ character but the other characters in the name can be chosen from the following groups:

a .. z
(any letter from a to z)
A .. Z
(any letter from A to Z)
0 .. 9
(any digit from 0 to 9)
_
(the underscore character)

Some examples of valid variable names are:

a total Out_of_Memory VAR integer etc...


In C variables do not only have names: they also have types. The type of a variable conveys to the the compiler what sort of data will be stored in it. In BASIC and in some older, largely obsolete languages, like PL/1, a special naming convention is used to determine the sort of data which can be held in particular variables. e.g. the dollar symbol $ is commonly used in BASIC to mean that a variable is a string and the percentage % symbol is used to indicate an integer. No such convention exists in C. Instead we specify the types of variables in their declarations. This serves two purposes:

* It gives a compiler precise information about the amount of memory that will have to be given over to a variable when a program is finally run and what sort of arithmetic will have to be used on it (e.g. integer only or floating point or none).
* It provides the compiler with a list of the variables in a convenient place so that it can cross check names and types for any errors.

There is a lot of different possible types in C. In fact it is possible for us to define our own, but there is no need to do this right away: there are some basic types which are provided by C ready for use. The names of these types are all reserved words in C and they are summarized as follows:

char
A single ASCII character
short
A short integer (usually 16-bits)
short int
A short integer
int
A standard integer (usually 32-bits)
long
A long integer
long int
A long integer (usually 32-bits, but increasingly 64 bits)
float
A floating point or real number (short)
long float
a long floating point number
double
A long floating point number
void
Discussed in a later chapter.
enum
Discussed in a later chapter.
volatile

Discussed in a later chapter.
Listing

/***************************************************/
/* */
/* Special Characters */
/* */
/***************************************************/

#include

main ()

{
printf ("Beep! \7 \n");
printf ("ch = \'a\' \n");
printf (" <- Start of this line!! \r");
}


The output of this program is:

Beep! (and the BELL sound )
ch = 'a'
<- Start of this line!!




[/font]


YEAH. hope this could
help in your programming lesson
Back to top Go down
 
Programming /*code*/
Back to top 
Page 1 of 1
 Similar topics
-
» no ryt click. code.
» Code Geass Panatik:D

Permissions in this forum:You cannot reply to topics in this forum
editorscommunity :: Hobbies and Entairtaments :: Tech and Internet-
Jump to: