The basic elements of a C program are the data declarations, functions, and comments.
int a = 5;
char *str = "I have seen the fnords";
//library function call
printf("%d %s\n", a, str);
The main() function is special, as it is the first function called in a program. All other functions in a program are called directly or indirectly from main().
The return statement is used to inform the operating system that the program exited normally. Returning a nonzero value indicates an error.
Note that in C, an end-of-line does not indicate the end of a statement; instead, statements are delineated with semicolons.
As we have seen earlier, the standard printf() function is used to output data to standard output.
int a = 7;
int b = 9;
double c = 1.618;
* output the results
* of some simple mathematical expressions
printf("a + b = %d\n", a + b);
printf("b * c = %f\n", b * c);
printf("a / c = %f\n", a / c);
Computers are of course here to do computations. A computer can quickly resolve complex mathematical expressions. The C programming language has five simple mathematical operators: multiply, divide, add, subtract, and modulus. Of these, the only one that is not commonly used in everyday arithmetic is the modulus operator, represented by a percentage sign.
* lets use the modulus operator
* to get the remainder value
printf("%d %% %d = %d\n",9, 3, 9 % 3);
printf("%d %% %d = %d\n", 10, 3, 10 % 3);
printf("%d %% %d = %d\n", 15, 4, 15 % 4);
With C we can store values in variables. Each variable has a string identifier, a name, basically. In addition to the name, each variable also has a variable type. The type defines what sort of data the variable can hold.
Before we can use a variable, we must define it in a declaration statement. A variable declaration defines the name of the variable and specifies the type of data the variable is meant to store.
* declare four variables
long long int d;
//see what values they store
printf("a = %d\n", a);
printf("b = %c\n", b);
printf("c = %f\n", c);
printf("d = %lld\n", d);
//return zero to the
One common variable type is int, or integer. Integer numbers have no fractional part or decimal point. Instead, decimal numbers are stored in the double type. Letters are stored in the char variable type.
When a variable is created, it is given a garbage value of whatever data happened to be in memory. We can have a variable store more meaningful data via an assignment statement.
//declare an int variable
//assign the variable a value
a = 73;
We use the standard library function printf() to display data. The printf() function uses special characters called conversion specifiers to output the value stored in a variable. The %d characters make up the integer conversion specifier, and the %f characters are the float or double conversion specifier. The variables that we want displayed are listed after the format string; these variables and the format string make up the parameter list for the printf() function.
If you are interested in learning more about C and you have an Amazon Kindle or a Kindle app for your smartphone or tablet, take a look at my book http://www.amazon.com/Big-Als-C-Standard-ebook/dp/B00A4JGE0M