These are the comments. Anything between /* .... */ or after // will be ignored
by the compiler.
Comments in a program are extremely important. One
reason is that they allow other people to be able to follow the "flow"
of your program.
#include <stdio.h>
#include <math.h>
These are preprocessor directives. Preprocessing occurs before a program is compiled. This line tells the preprocessor to include the contents of the header files stdio.h and math.h. These header files contain information about the functions used in the program like printf() and exp(). The #include preprocessor directive is normally used to include standard library header files such as stdio.h and stdlib.h .
int main()
{ .... }
All C programs need a main function where excution begins. The parentheses after main indicate that it is a function. It defines a function main that takes no arguments and returns an integer value. The left brace { must begin the body of every function and the right brace } must end each function. The program between the braces is called a block.
C programs stop when
These are variable declarations. These two lines define two different types of variables: reals (float) and integers (int). Some simple data types are listed below.
These are two basic IO functions. (Remeber that they are functions becuase they have ()). The function definitions for printf and scanf are kept in the header file stdio.h. The printf function prints what is ever between the qoutation marks to the screen. The scanf function takes input from the keyboard. Note that scanf takes two arguments: "%d" and &n . The first argument is called a format control string, which contains the conversion specifer %d, and it tells scanf what to expect to receive, which in this case is an integer. The second argument tells scanf to store the value received into the memory location where the variable n is located.
Example:
if (grade >= 60)
printf("Passed\n");
The if/else allows an action to be performed when the condition is true and an alternate statement to be performed when the condition is false.
Example:
if
(grade >= 60)
printf("Passed\n");
else
printf("Failed\n");
To test for multiple cases you can use nested if/else structures. (Also look at the switch/case structure)
Example:
if
(grade >= 90)
printf("A\n");
else if (grade >= 80)
printf("B\n");
else if (grade >= 70)
printf("C\n");
else
if (grade >= 60)
printf("D\n");
else
printf("F\n");
You can also include multiple statements within the if/else structure with the left and right brace {}.
Example: Consider a program segment designed to find the first power of 2 larger than 1000:
product = 2;
while (product <= 1000)
product = 2 *
product;
The condition in this example is product <= 1000 and the statement is product = 2 * product;. The program will not even consider the statements in a while if the condition if initially false.
Examples:
1)
if (gender == 0 && age >= 65)
seniorMales++;
2)
if (semesterAverage >= 90 || finalExam
>= 90)
printf("Student grade is A\n");
3)
if (!(grade == sentinelValue))
printf("The next grade if %f\n");
(Note: Don't confuse the assignment operator = with the equality operator ==.)
Examples:
1)
for (x = 1; x<=10; x++) {
if (x == 5)
break;
printf("%d ",x);
}
2)
for (x = 1; x <= 10; x++) {
if (x == 5)
continue;
printf("%d ",x);
}