/* Simple program to add two integers together. */#include <stdio.h>
int main ( )
{
int number1, number2; /* variables to hold the two integers */
int sum; /* variable to hold the sum */
printf("Enter the first number: "); /* print a prompt */
scanf("%d", &number1); /* read a number from the keyboard */
printf("Enter the second number: ");
scanf("%d", &number2);
sum = number1 + number2;
printf("The sum of %d and %d is %d.\n", number1, number2, sum);
return 0; /* 0 indicates program ended successfully */
} /* end main */
Compiling and running the program under Linux would look like:
$ emacs ex1.c
$ gcc -o ex1 ex1.c
$ ./ex1
Enter the first number: 5
Enter the second number: 10
The sum of 5 and 10 is 15.
NOTES about the program:
a) "#include <stdio.h> is a directive to the C preprocessor to include the standard input/output header file.
b) A functions in C returns a value. "main" is a special function where C programs start execution. The value returned by the main function indicates the status of execution.
c) When a function starts execution, a chuck of memory (called a "call frame") is allocated to hold its local variable (e.g., number1, number2, sum are named locations in memory).
d) C programming instructions/statements are terminated by a semicolon (";").
e) The "printf" module is sent ("passed") parameters/arguments to be printed. Double quotes("") indicate a character string to be printed with formatting information (%d) where variables values should go. The actual variables corresponding to the formatting information are listed after the character string. The "\n" is the escape sequence for the non-printable "newline" character.
f) The "scanf" module reads a specified type of data (%d for integer). Additionally, scanf needs the memory address of the variable where the datum is to be placed -- hence the "&" address-of operator.
/* Simple program to print a grade bases on 10 points off the top score. */
#include <stdio.h>
int main ( )
{
int highScore, minA, minB, minC, minD;
int scoreToAssign;
printf("Enter the highest score: "); /* print a prompt */
scanf("%d", &highScore); /* read highest score */
/* Calculate grade cutoffs */
minA = highScore - 10 + 1;
minB = minA - 10;
minC = minB - 10;
minD = minC - 10;
printf("Enter the score to be assigned: "); /* print a prompt */
scanf("%d", &scoreToAssign); /* read score to be assigned*/
if (scoreToAssign >= minA) {
printf("The score of %d receives an A.\n", scoreToAssign);
} /* end if */
if (scoreToAssign >= minB && scoreToAssign < minA) {
printf("The score of %d receives a B.\n", scoreToAssign);
} /* end if */
if (scoreToAssign >= minC && scoreToAssign < minB) {
printf("The score of %d receives the grade of C.\n", scoreToAssign);
} /* end if */
if (scoreToAssign >= minD && scoreToAssign < minC) {
printf("The score of %d receives the grade of D.\n", scoreToAssign);
} /* end if */
if (scoreToAssign < minD) {
printf("The score of %d receives the grade of F.\n", scoreToAssign);
} /* end if */
return 0; /* 0 indicates program ended successfully */
} /* end main */
/* Simple program to print a grade bases on 10 points off the top score. */
#include <stdio.h>
int main ( )
{
int highScore, minA, minB, minC, minD;
int scoreToAssign;
printf("Enter the highest score: "); /* print a prompt */
scanf("%d", &highScore); /* read highest score */
/* Calculate grade cutoffs */
minA = highScore - 10 + 1;
minB = minA - 10;
minC = minB - 10;
minD = minC - 10;
printf("Enter the score to be assigned: "); /* print a prompt */
scanf("%d", &scoreToAssign); /* read score to be assigned*/
if (scoreToAssign >= minA) {
printf("The score of %d receives an A.\n", scoreToAssign);
} else {
if (scoreToAssign >= minB) {
printf("The score of %d receives a B.\n", scoreToAssign);
} else {
if (scoreToAssign >= minC) {
printf("The score of %d receives the grade of C.\n", scoreToAssign);
} else {
if (scoreToAssign >= minD) {
printf("The score of %d receives the grade of D.\n", scoreToAssign);
} else {
printf("The score of %d receives the grade of F.\n", scoreToAssign);
} /* end if minD */
}/* end if minC */
}/* end if minB */
}/* end if minA */
return 0; /* 0 indicates program ended successfully */
} /* end main */