MENU

« 1 2 ... 28 29 30 31 »
Category: Education | Views: 929 | Added by: farrel | Date: 2013-10-22 | Comments (0)

Category: Education | Views: 1001 | Added by: farrel | Date: 2013-10-22 | Comments (0)


In this drawing the three variables i, j and p have been declared, but none of the three has been initialized.

Pointer Basics

To understand pointers, it helps to compare them to normal variables.

A "normal variable" is a location in memory that can hold a value. For example, when you declare a variable i as an integer, four bytes of memory are set aside for it. In your program, you refer to that location in memory by the name i. At the machine level that location has a memory address. The four bytes at that address are known to you, the programmer, as i, and the four byt ... Read more »

Category: Education | Views: 8278 | Added by: farrel | Date: 2013-04-22 | Comments (0)

Functions

Most languages allow you to create functions of some sort. Functions let you chop up a long program into named sections so that the sections can be reused throughout the program. Functions accept parameters and return a result. C functions can accept an unlimited number of parameters. In general, C does not care in what order you put your functions in the program, so long as a the function name is known to the compiler before it is called.

We have already talked a little about functions. The rand function seen previously is about as simple as a function can get. It accepts no parameters and returns an integer result:

int rand()
/* from K&R
 - produces a random number between 0 and 32767.*/
{
 rand_seed = rand_seed * 1103515245 +12345;
 return (unsigned int)(rand_seed / 65536) % 32768;
}

The int rand() line declares the function rand to the rest of the program and specifies that rand will accept no parameters and return an integer result. This function has no local variables, but if it needed locals, they would go right below the opening { (C allows you to declare variables after any { -- they exist until the program reaches the matching } and then they disappear. A function's local variables therefore vanish as soon as the ma ... Read more »

Category: Education | Views: 1552 | Added by: farrel | Date: 2013-04-22 | Comments (0)

Branching and Looping

In C, both if statements and while loops rely on the idea of Boolean expressions. Here is a simple C program demonstrating an if statement:

#include int main() { int b; printf("Enter a value:"); scanf("%d", &b); if (b < 0) printf("The value is negativen"); return 0; }

This program accepts a number from the user. It then tests the number using an if statement to see if it is less than 0. If it is, the program prints a message. Otherwise, the program is silent. The (b < 0) portion of the program is the Boolean expression. C evaluates this expressio ... Read more »

Category: Education | Views: 2221 | Added by: farrel | Date: 2013-04-22 | Comments (0)