C programming

  • View
    11

  • Download
    0

Embed Size (px)

Text of C programming

C FOR ENGINEERS

R.G.Salokhe

FOR OTHER BOOKS WRITTEN BY ME PLEASE VISIT http://www.geocities.com/arutacomp/ or mail me on arutacomp@rediffmail.com The books available are 1] AutoCAD 2007 The third dimension 2] Visual Basic 6 from bottom to top 3] MS-ACCESS Thank you.Rajendra Salokhe

C FOR ENGINEERS

R.G.Salokhe

The C language was created, designed and written by Dennis Ritchie in 1972 at Bell Laboratories. C was designed to run under UNIX operating system on the DEC, PDP11 computer.

Features of C :a) Portability :- The best feature for which C gained popularity is the portability of code. The C language programs could be run on variety of computers with a little or no change in the source code. Which means the C language code can be used under various operating systems. b) Efficiency :- The C language is efficient in two ways I) The source code is very compact ii) Memory Management through C is very efficient. c) Modularity :- C allows separately compiled modules which can be linked together. The programs can be written in well structured manner. C is a language of functions. Various modules are written as functions. d) Pointer Operations :- C is very powerful in pointer operations. Pointers can be set to various data types as well as to funtions, structures etc. Arrays can be manipulated with the help of pointers. e) Flexible level :- C programs can be written with the features of high level languages as well as that of low level languages. C thus fits in between the two. C is not a strongly typed language. There are no bounds to number of array elements. f) Case Sensitivity :- C is case sensitive. Which means the upper case and lower case characters are treated differently in variable names, function names etc.

ANSI C

:- ANSI stands for American National Standard Institute. The ANSI set a standard for C language in 1990. This was for the various compiler developers to use this standard. However many compilers are not strictly adhering to the ANSI standard.

Structure of C Programs :- Since C is a language of functions, let us understand

what a function means; A function is an entity which accepts some input, processes the same and gives the output. The type of function is determined by its output. The function can be used as its output. >>> >>> input Function output

C FOR ENGINEERS

R.G.Salokhe

4 Numeric Input 4 input to function value 2. e.g. 6 X 4

2 Numeric Output gives 2 as output. The function 4 can be used as

= 12. Here

4 is used as 2.

30 degrees

SINE

1/2 number

Let us look at the structure of a C program; /*PROG1.C */ # include < stdio.h > main ( ) { ; }

Comment Header Function Main Body of Main starts Null statement Main ( ) ends

We have written the program name as comments. The comments, spaces, tabs, newline characters are ignored by the C Compiler. At this stage we will keep aside the discussion on header file. just remember that stdio stands for standard input output.

C FOR ENGINEERS

R.G.Salokhe

Every set of C programs should have one main ( ) function. This is called the driving function. The program execution starts at { , the opening brace of the main function body and ends at the } closing brace.

We have included a null statement, a statement which does not mean anything in the body of the main ( ) function. Every statement in C ends with a ; ( semicolon ). The above prgram will run but would not produce any results. Let us slightly modify the program; /* PROG2.C */ # include < stdio.h > main ( ) { printf ( "Hello friends! " ) ; } Output Hello friends! printf ( ) is another function available in the C library and declared in the stdio.h file. Thus whichever functions we use, we have to include corresponding header files into our programs. /* PROG3.C */ # include < stdio.h > main ( ) { printf ( " Hello friends ! \ n " ) printf ( " How are you ? \ n " ) ; } Output Hello friends! How are you?

The \ ( backslash ) causes escape from the normal printing process and \n causes the computer to skip one line while printing. Other escape sequences are ; \b backspace \t tab

C FOR ENGINEERS \f form feed \r carriage return

R.G.Salokhe \ single quote \0 null

C character set :C uses the following character set; a) Alphabets upper case ( A to Z ) b) Alphabets lower case ( a to z ) c) Decimal digits ( 0 to 9 ) d) Other characters; comma ( , ), semicolon ( ; ), period ( . ) querry ( ? ), exclaimation ( ! ), colon ( : ), plus ( + ), minus ( - ), asterisk ( * ), slash ( / ), backslash ( \ ), vertical bar ( | ), single quote ( ), double quote ( ),tild ( ~ ) , right parantheses ( ( ), left paranthese ( ) ), right bracket ( [ ),left bracket ( ] ), right brace ( { ), left brace ( } ), right angle bracket ( < ), left angle bracket ( > ), equal sign ( = ), ampersand ( & ), percent sign ( % ), pound sign ( # ) caret ( ^ ), under score ( _ ).

Variable Names :As a very basic concept we can say that a variable is an entity whose value changes. We use a lot of variables in Algebra. When we say a = 5; a is a variable and we are assigning the value of 5 to a. Coming to a better concept a variable is the name assigned to the address of a cell in the memory. Memory cell Name Address Value a 2529 5

When you say

C FOR ENGINEERS

R.G.Salokhe

a =6; Then the value 5 vanishes and the value of 6 is put into the cell. a = a+6; is a wrong algebric equation but it is a valid computer equation as it means add 6 to the present value of a and assign the resulting value to a. Thus a becomes 12. a + 5 = 6 ; / * wrong * / The above equation is wrong for the computer as you can not add anything to the name. We have used a as a variable name. We can use any name for the variable within the following rules. 1) The variable name can not have a space, comma or characters like ( #,?,&,$,!,| ) - why use funny characters? 2) The name uses underscore, alphabet and numeric characters only. e.g. _ total BASIC D_A etc. 3) The language reserved words or key words can not be used as variable names. e.g. double, int,auto etc. 4) The first character of name can not be a digit. The variable name can be 31 characters long.

Data Types :-

Larger the value, larger is the space required in memory to store the number. If you are using a variable whose value varies between 0 and 255 then there is no need to allocate more space in memory for storing this number. The data types are created for efficient use of the memory. How much memory is alloted for the various types depends on the machine.

Character Data Types :-

C FOR ENGINEERS

R.G.Salokhe

The basic data types is the character variable. Character variable occupies 1 byte of the memory. 1 byte comprises of 8 bits and the maximum value of 8 bit binary number is 255. 1 byte = 8 bits 27 26 25 24 23 22 21 20

128+ 64 +32 +16 + 8 + 4 + 2 + 0 = 255 If the character is unsigned then it can store values from 0 to 255. In other words we can say that total values that can be stored in 8 bits is 28 or 256. / * PROG4.C * / # include < stdio.h > main ( ) { char a ; a=A; printf ( " A = %c ", a ) ; } output A=A Variable a is declared as character type and then assigned a value 'A' which is a character constant. A character enclosed in single quotes makes a character constant. Type char and unsigned char are equivalent. signed char can store values from -128 to 127 ( 128 + 127 + 1 for zero = 256 ) C has a unique feature by which numeric values can be mixed with characters or characters can be treated as numbers.

a is declared to be of character type.

/ * PROG5.C * /

C FOR ENGINEERS

R.G.Salokhe

# include < stdio.h > main ( ) { char a ; a is declared as character. a = 65 ; a is assigned a numeric value printf ( " Decimal value of a is %d \n ,"a ) ; printf ( " Character value of a is %c \n,"a) ; } output Decimal value of a is 65 Character value of a is A The ASCII value of A is 65. B is 66 etc. Thus A can mean 65 or B can mean 66. ASCII value of lower case a is 97 and that of b is 98 etc. This feature can be used to convert upper case letters into lower case and vice versa.

/ * PROG6.C * / # include < stdio.h > main ( ) { char a, b ; a = 'A' print f ( " value of a is % c\ n", a ) ; a = a +32; Adding 32 to character converts upper case to lower case. printf ( " now a is %c " , a ) ; } output value of a is A now a is a

Initially we have assigned upper case A to variable a. Later we have added 32 to variable a. Now the value stored in variable a is lower case a. We have used char type in above examples. However the char type can be declared in three ways;

C FOR ENGINEERS

R.G.Salokhe char signed char unsigned char

Simply mentioning char declares the variable to be of signed char type ( However for some compilers it is unsigned char.) The range for char varies from -128 to 127 and for unsigned char it is 0 to 255, total 256 numbers. The characters have a space of 1 byte or 8 bits available. The first bit is taken up by the sign

sign

maximum number you can store in 7 bits is 127.

and the maximum number that can be stored in the remaining 7 bits is 127. So how can you store the number -128 ? It is something called as 2S compliment method ; 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 -128 -127 -126

Integer Type :Integer variables store a value which are whole numbers i.e. a number with or without sign but no fractional part and hence are declared as; int i; Integers have the following range ;

C FOR ENGINEERS

R.G.Salokhe

Declaration short int signed short int unsigned short int int signed int unsigned