Showing posts with label Programming with C language. Show all posts
Showing posts with label Programming with C language. Show all posts

Saturday, 17 November 2012

Programming with C - Pointers as Function Arguments

Programming with C -
Pointers as Function Arguments
Pointers as Function Arguments Earlier, we learned that functions in C receive copies of their arguments. (This means that C uses call by value; it means that a function can modify its arguments without modifying the value in the caller.) Consider the following function to swap two integers void swap(int x, int y) {     int temp;     temp=x;     x=y;     y=temp;     return; } The problem with this function is that since C uses call by value technique of parameter passing, the caller can not get the changes done in the function. This can be achieved using pointers as illustrated in the following program. Program 1.4: Program to swap two integers using pointers #include<stdio.h> main() {     int a, b;     void swap(int *a, int *b);     printf(" Read the integers:");     scanf("%d%d", &a, &b);     swap(&a, &b);        /* call by reference or call by address*/     printf(" \nAfter swapping:a=%d    b=%d", a, b); } void swap(int *x, int *y) {     int temp;     temp=*x;     *x=*y;     *y=temp;     return; } Execute this program and observe the result. Because of the use of call by reference, the changes made in the function swap() are also available in the main().

Programming with C Language: Basic Pointer Operations

Programming with C Language:
Basic Pointer Operations
Basic Pointer Operations The first things to do with pointers are to declare a pointer variable, set it to point somewhere, and finally manipulate the value that it points to. A simple pointer declaration has the following general format:     datatype *variablename where datatype represents the type of the data to which the pointer variablename points to. In simple terms, the variablename holds the address of the value of type datatype. For example,        int *ip; This declaration looks like our earlier declarations, with one obvious difference: that is the asterisk. The asterisk means that ip, the variable we're declaring, is not of type int, but rather of type pointer-to-int. (Another way of looking at it is that *ip, which as we'll see is the value pointed to by ip, will be an int.) We may think of setting a pointer variable to point to another variable as a two-step process: first we generate a pointer to that other variable, and then we assign this new pointer to the pointer variable. We can say (but we have to be careful when we're saying it) that a pointer variable has a value, and that its value is "pointer to that other variable''. This will make more sense when we see how to generate pointer values. Pointers (that is, pointer values) are generated with the "address-of'' operator &, which we can also think of as the "pointer-to'' operator. We demonstrate this by declaring (and initializing) an int variable i, and then setting ip to point to it:     int i = 5;     ip = &i; The assignment expression ip = &i; contains both parts of the "two-step process'': &i generates a pointer to i, and the assignment operator assigns the new pointer to (that is, places it "in'') the variable ip. Now ip ``points to'' i, which we can illustrate with this picture: i is a variable of type int, so the value in its box is a number, 5. ip is a variable of type pointer-to-int, so the "value'' in its box is an arrow pointing at another box. Referring once again back to the "two-step process'' for setting a pointer variable: the & operator draws us the arrowhead pointing at i's box, and the assignment operator =, with the pointer variable ip on its left, anchors the other end of the arrow in ip's box. We discover the value pointed to by a pointer using the "contents-of'' operator, *. Placed in front of a pointer, the * operator accesses the value pointed to by that pointer. In other words, if ip is a pointer, then the expression *ip gives us whatever it is that's in the variable or location pointed to by ip. For example, we could write something like     printf("%d\n", *ip); which would print 5, since ip points to i, and i is (at the moment) 5. (You may wonder how the asterisk * can be the pointer contents-of operator when it is also the multiplication operator. There is no ambiguity here: it is the multiplication operator when it sits between two variables, and it is the contents-of operator when it sits in front of a single variable. The situation is analogous to the minus sign: between two variables or expressions it's the subtraction operator, but in front of a single operator or expression it's the negation operator. Technical terms you may hear for these distinct roles are unary and binary: a binary operator applies to two operands, usually on either side of it, while a unary operator applies to a single operand.) The contents-of operator * does not merely fetch values through pointers; it can also set values through pointers. We can write something like     *ip = 7; which means "set whatever ip points to 7.'' Again, the * tells us to go to the location pointed to by ip, but this time, the location isn't the one to fetch from--we're on the left-hand side of an assignment operator, so *ip tells us the location to store to. (The situation is no different from array subscripting expressions such as a[3] which we've already seen appearing on both sides of assignments.) The result of the assignment *ip = 7 is that i's value is changed to 7, and the picture changes to: If we called printf("%d\n", *ip) again, it would now print 7. At this point, you may be wonder, if we wanted to set i to 7, why didn't we do it directly? We'll begin to explore that next, but first let's notice the difference between changing a pointer (that is, changing what variable it points to) and changing the value at the location it points to. When we wrote *ip = 7, we changed the value pointed to by ip, but if we declare another variable j:     int j = 3; and write     ip = &j;   we've changed ip itself. The picture now looks like this: We have to be careful when we say that a pointer assignment changes ``what the pointer points to.'' Our earlier assignment     *ip = 7; changed the value pointed to by ip, but this more recent assignment     ip = &j; has changed what variable ip points to. It's true that "what ip points to'' has changed, but this time, it has changed for a different reason. Neither i (which is still 7) nor j (which is still 3) has changed. (What has changed is ip's value.) If we again call     printf("%d\n", *ip); this time it will print 3. We can also assign pointer values to other pointer variables. If we declare a second pointer variable:     int *ip2; then we can say     ip2 = ip; Now ip2 points where ip does; we've essentially made a "copy'' of the arrow: Now, if we set ip to point back to i again:     ip = &i; the two arrows point to different places: We can now see that the two assignments     ip2 = ip; and     *ip2 = *ip; do two very different things. The first would make ip2 again point to where ip points (in other words, back to i again). The second would store, at the location pointed to by ip2, a copy of the value pointed to by ip; in other words (if ip and ip2 still point to i and j respectively) it would set j to i's value, or 7. It's important to keep very clear in your mind the distinction between a pointer and what it points to. You can't mix them. You can't "set ip to 5'' by writing something like     ip = 5;            /* WRONG */ 5 is an integer, but ip is a pointer. You probably wanted to "set the value pointed to by ip to 5,'' which you express by writing     *ip = 5; Similarly, you can't "see what ip is'' by writing     printf("%d\n", ip);    /* WRONG */ Again, ip is a pointer-to-int, but %d expects an int. To print what ip points to, use     printf("%d\n", *ip); Finally, a few more notes about pointer declarations. The * in a pointer declaration is related to, but different from, the contents-of operator *. After we declare a pointer variable     int *ip; the expression     ip = &i sets what ip points to (that is, which location it points to), while the expression     *ip = 5 sets the value of the location pointed to by ip. On the other hand, if we declare a pointer variable and include an initializer:     int *ip3 = &i; we're setting the initial value for ip3, which is where ip3 will point, so that initial value is a pointer. (In other words, the * in the declaration int *ip3 = &i; is not the contents-of operator, it's the indicator that ip3 is a pointer.) If you have a pointer declaration containing an initialization, and you ever have occasion to break it up into a simple declaration and a conventional assignment, do it like this:     int *ip3;     ip3 = &i; Don't write     int *ip3;     *ip3 = &i; or you'll be trying to mix pointer and the value to which it points Also, when we write     int *ip; although the asterisk affects ip's type, it goes with the identifier name ip, not with the type int on the left. To declare two pointers at once, the declaration looks like     int *ip1, *ip2; Some people write pointer declarations like this:     int* ip; This works for one pointer, because C essentially ignores whitespace. But if you ever write     int* ip1, ip2;        /* PROBABLY WRONG */ it will declare one pointer-to-int ip1 and one plain int ip2, which is probably not what you meant. What is all of this good for? If it was just for changing variables like i from 5 to 7, it would not be good for much. What it's good for, among other things, is when for various reasons we don't know exactly which variable we want to change. Program 1.1: A simple program to illustrate the relationship between two integer variables, their corresponding addresses and their associated pointers #include<stdio.h> main() { int x=5; int y; int *px;    /* pointer to an integer */ int *py;    /* pointer to an integer */   px=&x;    /* assign address of x to px */ y=*px;        /* assign value of x to y */ py=&y;    /* assign address of y to py */      printf("\nx=%d &x=%u    px=%u        *px=%d", x, &x, px, *px); printf("\ny=%d &y=%u    py=%u        *py=%d", y, &y, py, *py); } Execute this program and observe the result. Self Assessment Questions i.     What is an indirection operator? ii.    State true or false     Pointer is a variable containing address of another variable iii.    State whether the following statements are correct     int a, b;     b=&a;

Programming with C Language - Exercises

Programming with C1.9 Exercises
Exercises
  1. Explain the history of C language.
  2. What are the advantages of C language?
  3. Explain the basic structure of a C program with an example.
  4. What are the different steps in executing a C program ?
  5. Write a C program to convert Celsius to Fahrenheit and vice versa.

Programming with C Language - More simple C programs

Programming with C1.4 More simple C programs
More simple C programs Program 1.1 Area of a circle Here is an elementary C program that reads in the radius of a circle, calculates the area and then writes the calculated result. #include <stdio.h>    /* Library file access */ /* program to calculate the area of a circle */    /* Title (Comment) */ main()    /* Function heading */ { float radius, area;    /*Variable declarations */ printf("Radius=?");    /* Output statement(prompt) */ scanf("%f", &radius);    /* Input statement */ area=3.14159*radius*radius;    /* Assignment statement */ printf("Area=%f",area);    /* Output statement */ } Program 1.2 Print a few numbers Here is a program to illustrate a simple loop #include <stdio.h> /* print a few numbers, to illustrate a simple loop */ main() { int i; for(i = 0; i < 10; i = i + 1)                /* Looping statement */     printf("i is %d\n", i); return 0; } Program 1.3: Program to add two numbers #include <stdio.h> main() { int i,j,k; // Defining variables i = 6; // Assign values j = 8; k = i + j; printf("sum of two numbers is %d \n",k); // Printing results }

Programming with C Language - A simple C Program

Programming with C1.3 A simple C Program
A simple C Program #include <stdio.h> main() { printf("Hello, world!\n"); return 0; } If you have a C compiler, the first thing to do is figure out how to type this program in and compile it and run it and see where its output went. The first line is practically boilerplate; it will appear in almost all programs we write. It asks that some definitions having to do with the "Standard I/O Library'' be included in our program; these definitions are needed if we are to call the library function printf correctly. The second line says that we are defining a function named main. Most of the time, we can name our functions anything we want, but the function name main is special: it is the function that will be "called'' first when our program starts running. The empty pair of parentheses indicates that our main function accepts no arguments, that is, there isn't any information which needs to be passed in when the function is called. The braces { and } surround a list of statements in C. Here, they surround the list of statements making up the function main. The line     printf("Hello, world!\n"); is the first statement in the program. It asks that the function printf be called; printf is a library function which prints formatted output. The parentheses surround printf 's argument list: the information which is handed to it which it should act on. The semicolon at the end of the line terminates the statement. printf 's first (and, in this case, only) argument is the string which it should print. The string, enclosed in double quotes (""), consists of the words "Hello, world!'' followed by a special sequence: \n. In strings, any two-character sequence beginning with the backslash \ represents a single special character. The sequence \n represents the "`new line'' character, which prints a carriage return or line feed or whatever it takes to end one line of output and move down to the next. (This program only prints one line of output, but it's still important to terminate it.) The second line in the main function is     return 0; In general, a function may return a value to its caller, and main is no exception. When main returns (that is, reaches its end and stops functioning), the program is at its end, and the return value from main tells the operating system (or whatever invoked the program that main is the main function of) whether it succeeded or not. By convention, a return value of 0 indicates success.   Self Assessment Questions i) The information that needs to be passed in when a function is called is ______ ii) State true or false The main() function doesn't return any value.

Programming with C Language - Basic structure of C Programs

Programming with C1.2 Basic structure of C Programs
 
Basic structure of C Programs   A C program can be viewed as a group of building blocks called functions. A function is a subroutine that may include one or more statements designed to perform a specific task. To write a C program we first create functions and then put them together. A C program may contain one or more sections shown in Fig. 1.1.
  Fig. 1.1 The documentation section consists of a set of comment(remarks) lines giving the name of the program, the author and other details which the programmer would like to use later. Comments may appear anywhere within a program, as long as they are placed within the delimiters /* and */ (e.g., /*this is a comment*/). Such comments are helpful in identifying the program's principal features or in explaining the underlying logic of various program features. The link section provides instructions to the compiler to link functions from the system library. The definition section defines all symbolic constants. There are some variables that are used in more than one function. Such variables are called global variables and are declared in the global declaration section that is outside of all the functions. Every C program must have one main function section. This section contains two parts, declaration part and executable part. The declaration part declares all the variables used in the executable part. There is at least one statement in the executable part. These two parts must appear between opening and closing braces({ and }). The program execution begins at the opening brace and ends at the closing brace. The closing brace of the main function section is the logical end of the program. All statements in the declaration and executable parts end with a semicolon(;). The subprogram section contains all the user-defined functions that are called in the main function. User-defined functions are generally placed immediately after the main function, although they may appear in any order. All sections, except the main function section may be absent when they are not required. Self Assessment Questions i) The documentation section contains a set of __________ lines. Ii) State true or false Every C program must have one main() function. iii) What are global variables?

Programming with C Language - Features of C

Programming with C1.1 Features of C
Features of C C is characterized by the ability to write very concise source programs, due in part to the large number of operators included within the language. It has a relatively small instruction set, though actual implementations include extensive library functions which enhance the basic instructions. The language encourages users to write additional library functions of their own. Thus, the features and capabilities of the language can easily be extended by the user. C compilers are commonly available for computers of all sizes. The compilers are usually compact, and they generate object programs that are small and highly efficient when compared with programs compiled from other high-level languages. Another important characteristic of C is that its programs are highly portable, even more so than with other high-level languages. The reason for this is that C relegates most computer dependent features to its library functions. Thus, every version of C is accompanied by its own set of library functions, which are written for the particular characteristics of the host computer.
 Self Assessment Questions i) State true or false Using C language programmers can write their own library functions ii) C is a ________ level programming language