Wednesday 18 May 2011

java


basics of c language technology






Technology - Computer science
Basics of C Language


Programming in C
Intro.. Technology - Computer science -

Programming in C
Introduction
A Programing Language should be designed to support certain kind of data, such as numbers,characters,strings etc. To get useful output known as information. A program is a set of statements for a specific task, which will be executed in a sequential form. These statements/instructions are formed using certain words and symbols according to the rules known as syntax rules or grammar of the language. Every program must follow accurately the syntax rules supported by the language.

The C Character set
The characters used to form words,numbers and expressions depend upon the computer on which the program runs. The Characters in C are classified in to four categories.
1. Letters-------------> Ex: A to Z and a to z

2. Digits--------------> Ex: All decimal digits 0 to 9

3. White spaces--------> Ex: Blank space, Horizontal tab, Vertical tab, New line, Form feed

4. Special characters--> Ex:, . ; " ' ! | / \ ~ _ $ ? & ^ * - + < > ( ) [ ] { } % # = @

The C Keywords
The C keywords are reserved words by the compiler. All the C keywords have been assigned fixed meaning. The Keywords cannot be used as variable names because they have been assigned fixed jobs. However, few C compilers allow to construct variable names, which exactly coincides with the keywords. It is suggested not to mix up keywords with variable names.
Keywords:

auto-----break-----case----char----const----continue----default----do

double----else-----enum-----extern-------float--------for-----goto----if
int----long----register---return---short---signed----sizeof---static
struct----switch----typedef----union---unsigned---void---volatile----while
In addition to these standard keywords some more like asm,typeof,huge,interrupt,near,etc.
Identifiers
Identifiers are names of variables,functions, and arrays. They are user-defined names, consisting of sequence of letters and digits, with the letters as the first character. Lower case letters are preferred. However, the upper case letters are also permitted. The (_) under score symbol can be used as an identifier.
Examples:1. #define N 10 2. # define a 15

Here 'N' and 'a' are user-defined identifiers

Constants
The Constants in C are applicable to the values, which do not change during the execution of a program. There are several types of constants in C. They are

1. Numeric Constants

2. Character Constants

Numeric Constants
Numeric Constants Classifieds into several categories.They are

* Integer constants
These are the sequence of numbers from 0 to 9 without decimal points or fractional part or any other symbols. It requires minimum two bytes and maximum four bytes. Integer constants could either be positive or negative or maybe zero. The number without a sign is assumed as positive.

Example: 50,70,+80,-15 etc.

* Real constants
Real constants often as floating point constants. Integer constants are unfit to represent many quantities. Many parameters or quantities are defined not only in integers but also in real numbers. For example length, height, prize, distance etc. are measured in real numbers.

Example: 1.0,2.3450,3.14 etc.

The real constants can be written in exponential notation,which contains a fractional part and an exponential part. For example, the value 2456.123 can be written as 2.4561Xe+3.

The General format of the real number contains mantissa and an exponent. The mantissa is either a real number represented in decimal or an integer.The exponent is an integer number which may be positive or nagative. The letter 'e' separating the mantissa and exponent can be written in lower case or upper case.

Character Constants
* Single character constants:

A characteer constant is a single character. They are also represented with a single digit or a single special symbol or white space enclosed within a pair of single quote marks.

Example: 'a','d','m',etc.

* String constants:

String constants are sequence of characters enclosed within a double quote
marks. The string may be a combination of all kinds of symbols.

Example: "Hello","Sample","Ramana" etc.

Variables
A variable is a data name used for storing a data value. A variable is a name which is used for storing some value in it. Its value may be changed during the program execution of a program. A variable name may be declared based on the meaning of the operation.

Example: height,weight, average, sum, mul etc.

Rules for defining variables
1. They must begin with a character without spaces but underscore is permitted.

2. The length of the variable varies from compiler to compiler. Generally most of the compilers support 8 characters excluding extension. However, the ANSI standard recognizes the maximum length of a variable upto 31 characters.

3. The variable should not be a C keyword.

4. The variable names may be a combination of upper and lower characters. For example Sum and sum are not the same.

5. The variable name should not start with a digit.

Data Types
All C compilers support a variety of data types. This enables the programmer to select the appropriate data type as per the need of the application. Which type of data is storing in a variable is known as data type. Generally data is represented using numbers or characters. The numbers may be integers or real.

A C language programmer has to tell the system before-hand, the type of numbers or characters he is using in his program. These are data types. There are many data types in C language. A C programmer has to use appropriate data type as per his requirement.

C language data types can be broadly classified as

* Primary data type

* Derived data type

* User-defined data type
All C Compilers accept the following fundamental data types

1. Integer -------------------------------------> int

2. Character ----------------------------------> char

3. Floating Point ------------------------------> float

4. Double precision floating point------------> double

5. Void ---------------------------------------> void

The size and range of each data type is given in the below

* char -----------------------------------> -128 to 127

* int -----------------------------------> -32768 to +32767

* float ---------------------------------->3.4 e-38 to 3.4 e+38

* double --------------------------------> 1.7 e-308 to 1.7 e+308

Integer Type :
Integers are whole numbers with a machine dependent range of values. A good programming language as to support the programmer by giving a control on a range of numbers and storage space. C has 3 classes of integer storage namely short int, int and long int. All of these data types have signed and unsigned forms. A short int requires half the space than normal integer values. Unsigned numbers are always positive and consume all the bits for the magnitude of the number. The long and unsigned integers are used to declare a longer range of values.

Floating Point Types :
Floating point number represents a real number with 6 digits precision. Floating point numbers are denoted by the keyword float. When the accuracy of the floating point number is insufficient, we can use the double to define the number. The double is same as float but with longer precision. To extend the precision further we can use long double which consumes 80 bits of memory spaces.

Void Type :
Using void data type, we can specify the type of a function. It is a good practice to avoid functions that does not return any values to the calling function.

Character Type :
A single character can be defined as a defined as a character type of data. Characters are usually stored in 8 bits of internal storage. The qualifier signed or unsigned can be explicitly applied to char. While unsigned characters have values between 0 and 255, signed characters have values from –128 to 127.

Data types and their control strings

Data Type------------------->Size(bytes)--------->Range---------------------------->Control String

Char................................................ 1 ............................... -128 to 127............................................... %c

Unsigned Char............................... 1 ............................... 0 to 255...................................................... %c

Short or int................................... 2 ............................. -32,768 to 32,767 .................................... %i or %d

Unsigned int ................................. 2 .................................. 0 to 655355............................................... %u

Long................................................ 4 ................................. -2147483648 to 2147483647.................. %ld

Unsigned long............................... 4 ..................................... 0 to 4294967295................................. %lu

Float............................................... 4 .................................... 3.4e-38 to 3.4e+38.................. %f or %g

Double.............................................. 8 ................................... 1.7e-308 to 1.7e+308.......................... %lf

Long Double......................................... 10 .......................... 3.4e-4932 to 1.1e+4932.................. %lf



Create your own website!
Basics of C Language (Technology - Computer science)    -    Author : creative - India


20675 visitors since 2008-12-29
last update : 2009-12-29

Blog-City.info >> Technology >> Blog #10275
Find penpals
Students of the World : Penpals for kids

c language basics



C Basics

  Before we embark on a brief tour of C's basic syntax and structure we offer a brief history of C and consider the characteristics of the C language. In the remainder of the Chapter we will look at the basic aspects of C programs such as C program structure, the declaration of variables, data types and operators. We will assume knowledge of a high level language, such as PASCAL.
It is our intention to provide a quick guide through similar C principles to most high level languages. Here the syntax may be slightly different but the concepts exactly the same.
C does have a few surprises:
  • Many High level languages, like PASCAL, are highly disciplined and structured.
  • However beware -- C is much more flexible and free-wheeling. This freedom gives C much more power that experienced users can employ. The above example below (mystery.c) illustrates how bad things could really get.

History of C

The milestones in C's development as a language are listed below:
  • UNIX developed c. 1969 -- DEC PDP-7 Assembly Language
  • BCPL -- a user friendly OS providing powerful development tools developed from BCPL. Assembler tedious long and error prone.
  • A new language ``B'' a second attempt. c. 1970.
  • A totally new language ``C'' a successor to ``B''. c. 1971
  • By 1973 UNIX OS almost totally written in ``C''.

Characteristics of C

We briefly list some of C's characteristics that define the language and also have lead to its popularity as a programming language. Naturally we will be studying many of these aspects throughout the course.
  • Small size
  • Extensive use of function calls
  • Loose typing -- unlike PASCAL
  • Structured language
  • Low level (BitWise) programming readily available
  • Pointer implementation - extensive use of pointers for memory, array, structures and functions.
C has now become a widely used professional language for various reasons.
  • It has high-level constructs.
  • It can handle low-level activities.
  • It produces efficient programs.
  • It can be compiled on a variety of computers.
Its main drawback is that it has poor error detection which can make it off putting to the beginner. However diligence in this matter can pay off handsomely since having learned the rules of C we can break them. Not many languages allow this. This if done properly and carefully leads to the power of C programming.
As an extreme example the following C code (mystery.c) is actually legal C code.
#include <stdio.h>

main(t,_,a)
char *a;
{return!0<t?t<3?main(-79,-13,a+main(-87,1-_,
main(-86, 0, a+1 )+a)):1,t<_?main(t+1, _, a ):3,main ( -94, -27+t, a
)&&t == 2 ?_<13 ?main ( 2, _+1, "%s %d %d\n" ):9:16:t<0?t<-72?main(_,
t,"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+\
,/+#n+,/#;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l q#'+d'K#!/\
+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# ){n\
l]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#\
n'wk nw' iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \
;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;\
#'rdq#w! nr'/ ') }+}{rl#'{n' ')# }'+}##(!!/")
:t<-50?_==*a ?putchar(a[31]):main(-65,_,a+1):main((*a == '/')+t,_,a\
+1 ):0<t?main ( 2, 2 , "%s"):*a=='/'||main(0,main(-61,*a, "!ek;dc \
i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);}
It will compile and run and produce meaningful output. Try this program out. Try to compile and run it yourself. Alternatively you may run it from here and see the output.
Clearly nobody ever writes code like or at least should never. This piece of code actually one an international Obfuscated C Code Contest http://reality.sgi.com/csp/iocc The standard for C programs was originally the features set by Brian Kernighan. In order to make the language more internationally acceptable, an international standard was developed, ANSI C (American National Standards Institute).

C Program Structure

A C program basically has the following form:
  • Preprocessor Commands
  • Type definitions
  • Function prototypes -- declare function types and variables passed to function.
  • Variables
  • Functions
We must have a main() function.


A function has the form:

type function_name (parameters)
  {
     local variables
 
     C Statements
 
   }

If the type definition is omitted C assumes that function returns an integer type. NOTE: This can be a source of problems in a program.
So returning to our first C program:


  /* Sample program */ 
 
  main()
     {
 
     printf( ``I Like C $\backslash$n'' );
     exit ( 0 );
 
     }
NOTE:
  • C requires a semicolon at the end of every statement.
  • printf is a standard C function -- called from main.
  • $\backslash$n signifies newline. Formatted output -- more later.
  • exit() is also a standard function that causes the program to terminate. Strictly speaking it is not needed here as it is the last line of main() and the program will terminate anyway.
Let us look at another printing statement:
printf(``.$\backslash$n.1$\backslash$n..2$\backslash$n...3$\backslash$n'');

The output of this would be:

    .
  .1
  ..2
  ...3

Variables

C has the following simple data types:




The Pascal Equivalents are:




On UNIX systems all ints are long ints unless specified as short int explicitly.

NOTE: There is NO Boolean type in C -- you should use char, int or (better) unsigned char.

Unsigned can be used with all char and int types.

To declare a variable in C, do:
   var_type list variables;

e.g. int i,j,k;
   float x,y,z;
   char ch;

Defining Global Variables

Global variables are defined above main() in the following way:-

          short number,sum;
   int bignumber,bigsum;
   char letter;
 
   main()
     {
 
     }

It is also possible to pre-initialise global variables using the = operator for assignment.

NOTE: The = operator is the same as := is Pascal.

For example:-

          float sum=0.0;
   int bigsum=0;
   char letter=`A';
 
   main()
     {
 
     }

This is the same as:-


          float sum;
   int bigsum;
   char letter;
 
   main()
     {
 
     sum=0.0;
     bigsum=0;
     letter=`A';
 
     }
...but is more efficient.

C also allows multiple assignment statements using =, for example:

          a=b=c=d=3;

...which is the same as, but more efficient than:


          a=3;
  b=3;
  c=3;
  d=3;
This kind of assignment is only possible if all the variable types in the statement are the same.
You can define your own types use typedef. This will have greater relevance later in the course when we learn how to create more complex data structures.
As an example of a simple use let us consider how we may define two new types real and letter. These new types can then be used in the same way as the pre-defined C types:

          typedef real float;
   typedef letter char;
 
Variables declared:
   real sum=0.0;
   letter nextletter;

Printing Out and Inputting Variables

C uses formatted output. The printf function has a special formatting character (%) -- a character following this defines a certain format for a variable:

    %c -- characters
   %d -- integers
   %f -- floats
   e.g. printf(``%c %d %f'',ch,i,x);

NOTE: Format statement enclosed in ``...'', variables follow after. Make sure order of format and variable data types match up.

scanf() is the function for inputting values to a data structure: Its format is similar to printf:

   i.e. scanf(``%c %d %f'',&ch,&i,&x);

NOTE: & before variables. Please accept this for now and remember to include it. It is to do with pointers which we will meet later (Section 17.4.1).

Constants

ANSI C allows you to declare constants. When you declare a constant it is a bit like a variable declaration except the value cannot be changed.
The const keyword is to declare a constant, as shown below:
int const a = 1;
const int a =2;
Note:
  • You can declare the const before or after the type. Choose one an stick to it.
  • It is usual to initialise a const with a value as it cannot get a value any other way.
The preprocessor #define is another more flexible (see Preprocessor Chapters) method to define constants in a program.
You frequently see const declaration in function parameters. This says simply that the function is not going to change the value of the parameter.
The following function definition used concepts we have not met (see chapters on functions, strings, pointers, and standard libraries) but for completenes of this section it is is included here:
void strcpy(char *buffer, char const *string)
The second argiment string is a C string that will not be altered by the string copying standard library function.

Arithmetic Operations

As well as the standard arithmetic operators (+ - * /) found in most languages, C provides some more operators. There are some notable differences with other languages, such as Pascal.
Assignment is = i.e. i = 4; ch = `y';
Increment ++, Decrement -- which are more efficient than their long hand equivalents, for example:-- x++ is faster than x=x+1.
The ++ and -- operators can be either in post-fixed or pre-fixed. With pre-fixed the value is computed before the expression is evaluated whereas with post-fixed the value is computed after the expression is evaluated.
In the example below, ++z is pre-fixed and the w-- is post-fixed:

          int x,y,w;
 
   main()
     {
 
     x=((++z)-(w--)) % 100;
 
     }

This would be equivalent to:


          int x,y,w;
 
   main()
     {
 
    z++;
    x=(z-w) % 100;
    w--;
 
     }
The % (modulus) operator only works with integers.

Division / is for both integer and float division. So be careful.

The answer to: x = 3 / 2 is 1 even if x is declared a float!!

RULE: If both arguments of / are integer then do integer division.

So make sure you do this. The correct (for division) answer to the above is x = 3.0 / 2 or x= 3 / 2.0 or (better) x = 3.0 / 2.0.

There is also a convenient shorthand way to express computations in C.

It is very common to have expressions like: i = i + 3 or x = x*(y + 2)

This can written in C (generally) in a shorthand form like this:
   $\mbox{{\em expr}}_{1} \: \mbox{{\em op}} \: = \:\mbox{{\em expr}}_{2}$

which is equivalent to (but more efficient than):
   $\mbox{{\em expr}}_{1} \: = \: \mbox{{\em expr}}_{1} \: \mbox{{\em
op}} \: \mbox{{\em expr}}_{2}$

So we can rewrite    i = i + 3 as i += 3

and    x = x*(y + 2) as x *= y + 2.

NOTE: that x *= y + 2 means x = x*(y + 2) and NOT x = x*y + 2.

Comparison Operators

To test for equality is ==

A warning:  Beware of using ``='' instead of ``=='', such as writing accidentally

   if ( i = j ) .....

This is a perfectly LEGAL C statement (syntactically speaking) which copies the value in "j" into "i", and delivers this value, which will then be interpreted as TRUE if j is non-zero. This is called assignment by value -- a key feature of C.

Not equals is: !=

Other operators < (less than) , > (grater than), <= (less than or equals), >= (greater than or equals) are as usual.

Logical Operators

Logical operators are usually used with conditional statements which we shall meet in the next Chapter.

The two basic logical operators are:

&& for logical AND, || for logical OR.

Beware & and | have a different meaning for bitwise AND and OR ( more on this later in Chapter 12).

Order of Precedence

It is necessary to be careful of the meaning of such expressions as  a + b * c

We may want the effect as either

   (a + b) * c

or

   a + (b * c)
All operators have a priority, and high priority operators are evaluated before lower priority ones. Operators of the same priority are evaluated from left to right, so that

   a - b - c
is evaluated as

   ( a - b ) - c

as you would expect.

From high priority to low priority the order for all C operators (we have not met all of them yet) is:

     ( )  [  ]  -> .
   !  $\sim$ - * & sizeof cast ++ -
         (these are right->left)
   * / %
   + -
   < <= >= >
   == !=
   &
   $\wedge$   |
   &&
   ||
   ?:        (right->left)
   = += -= (right->left)
   ,   (comma)
Thus
   a < 10 && 2 * b < c

is interpreted as
   ( a < 10 ) && ( ( 2 * b ) < c )
and

   a =
 
   b =
     spokes / spokes_per_wheel
     + spares;

as

   a =
 
   ( b =
     ( spokes / spokes_per_wheel )
     + spares
   );

Exercises

Write C programs to perform the following tasks.
Exercise 12270
Input two numbers and work out their sum, average and sum of the squares of the numbers.
Exercise 12271
Input and output your name, address and age to an appropriate structure.
Exercise 12272
Write a program that works out the largest and smallest values from a set of 10 inputted numbers.
Exercise 12273
Write a program to read a "float" representing a number of degrees Celsius, and print as a "float" the equivalent temperature in degrees Fahrenheit. Print your results in a form such as
100.0 degrees Celsius converts to 212.0 degrees Fahrenheit.
Exercise 12274
Write a program to print several lines (such as your name and address). You may use either several printf instructions, each with a newline character in it, or one printf with several newlines in the string.
Exercise 12275
Write a program to read a positive integer at least equal to 3, and print out all possible permutations of three positive integers less or equal to than this value.
Exercise 12276
Write a program to read a number of units of length (a float) and print out the area of a circle of that radius. Assume that the value of pi is 3.14159 (an appropriate declaration will be given you by ceilidh - select setup).
Your output should take the form: The area of a circle of radius ... units is .... units.
If you want to be clever, and have looked ahead in the notes, print the message Error: Negative values not permitted. if the input value is negative.
Exercise 12277
Given as input a floating (real) number of centimeters, print out the equivalent number of feet (integer) and inches (floating, 1 decimal), with the inches given to an accuracy of one decimal place.
Assume 2.54 centimeters per inch, and 12 inches per foot.
If the input value is 333.3, the output format should be:
333.3 centimeters is 10 feet 11.2 inches.
Exercise 12278
Given as input an integer number of seconds, print as output the equivalent time in hours, minutes and seconds. Recommended output format is something like
7322 seconds is equivalent to 2 hours 2 minutes 2 seconds.
Exercise 12279
Write a program to read two integers with the following significance.
The first integer value represents a time of day on a 24 hour clock, so that 1245 represents quarter to one mid-day, for example.
The second integer represents a time duration in a similar way, so that 345 represents three hours and 45 minutes.
This duration is to be added to the first time, and the result printed out in the same notation, in this case 1630 which is the time 3 hours and 45 minutes after 12.45.
Typical output might be Start time is 1415. Duration is 50. End time is 1505.
There are a few extra marks for spotting.
Start time is 2300. Duration is 200. End time is 100.


Dave Marshall
1/5/1999