Академический Документы
Профессиональный Документы
Культура Документы
Armed with the knowledge about the types of variables, constants & keywords the next logical step is to combine them to form instructions. However, instead of this, we would write our first C program now. Once we have done that we would see in detail the instructions that it made use of. (a) Each instruction in a C program is written as a separate statement. Therefore a complete C program would comprise of a series of statements. (b)The statements in a program must appear in the same order in which we wish them to be executed; unless of course the logic of the problem demands a deliberate jump or transfer of control to a statement, which is out of sequence.
(C) Blank spaces may be inserted between two words to improve the readability of the statement. However, no blank spaces are allowed within a variable, constant or keyword. (d) All statements are entered in small case letters.
(e)C has no specific rules for the position at which a statement is to be written. Thats why it is often called a freeform language. (d) Every C statement must end with a ;. Thus ; acts as a statement terminator.
A Simple C Program. 1: /* Hello.c: This is my first C program */ 2: #include <stdio.h> 3: 4: main() 5: { 6: printf (Hello World! This is my first C program.\n); 7: return 0; 8: } Execution of First C program On Windows Hello.c
// More arithmetic expressions #include <stdio.h> int main (void) { int a = 25; int b = 2; float c = 25.0; float d = 2.0; printf ("6 + a / 5 * b = %i\n", 6 + a / 5 * b); printf ("a / b * b = %i\n", a / b * b); printf ("c / d * d = %f\n", c / d * d); printf ("-a = %i\n", -a); return 0; } Program 4.3 Output 6 + a / 5 * b = 16 a / b * b = 24 c / d * d = 25.000000 -a = -25
Program 4.4 Illustrating the Modulus Operator // The modulus operator #include <stdio.h> int main (void) { int a = 25, b = 5, c = 10, d = 7; printf ("a %% b = %i\n", a % b); printf ("a %% c = %i\n", a % c); printf ("a %% d = %i\n", a % d); printf ("a / d * d + a %% d = %i\n", a / d * d + a % d); return 0; } Program 4.4 Output a%b=0 a%c=5 a%d=4 a / d * d + a % d = 25
/* Operators Demo # 2 */ #include <stdio.h> main () { int i; printf ("Assignment Operators\n\n"); i = 10; /* Assignment */ printf("i = 10 : %d\n",i); i++; /* i = i + 1 */ printf ("i++ : %d\n",i); i += 5; /* i = i + 5 */ printf ("i += 5 : %d\n",i); i--; /* i = i = 1 */ printf ("i-- : %d\n",i); i -= 2; /* i = i - 2 */ printf ("i -= 2 : %d\n",i); i *= 5; /* i = i * 5 */ printf ("i *= 5 :%d\n",i); i /= 2; /* i = i / 2 */ printf ("i /= 2 : %d\n",i);i %= 3; /* i = i % 3 */