The C language facilitates a structured and disciplined approach to computer-program design. This chapter introduces C programming and presents several examples illustrating many fundamental C features. We analyze each example one statement at a time. In Chapters 3 and 4, we introduce structured programming—a methodology that will help you produce clear, easy-to-maintain programs. We then use the structured approach throughout the remainder of the text. This chapter concludes with the first of our “Secure C Programming” sections.

A Simple C Program: Printing a Line of Text

We begin with a simple C program that prints a line of text.

// fig02_01.c
// A first program in C.
#include <stdio.h>

// function main begins program execution
int main(void) {
    printf("Welcome to C!\n"); 
    return 0; 
} // end function main

Output:

Welcome to C!

Another Simple C Program: Adding Two Integers

Our next program uses the scanf standard library function to obtain two integers typed by a user at the keyboard, then computes their sum and displays the result using printf. The program and sample output are shown in Fig. 2.4. In the input/output dialog box of Fig. 2.4, we emphasize the numbers entered by the user in bold.

// fig02_04.c
// Addition program.
#include <stdio.h>

// function main begins program execution 
int main(void) {
    int integer1 = 0; // will hold first number user enters 
    int integer2 = 0; // will hold second number user enters

    printf("Enter first integer: "); // prompt
    scanf("%d", &integer1); // read an integer

    printf("Enter second integer: "); // prompt
    scanf("%d", &integer2); // read an integer

    int sum = 0; // variable in which sum will be stored
    sum = integer1 + integer2; // assign total to sum

    printf("Sum is %d\n", sum); // print sum
    return 0;
} // end function main

Sample Output:

Enter first integer: 45
Enter second integer: 72
Sum is 117
Calculations in printf Statement

Actually, we do not need the variable sum, because we can perform the calculation in the printf statement. So, lines 16-19 can be replaced with:

printf("Sum is %d\n", integer1 + integer2);

Memory Concepts

Understanding Memory Allocation and Values
Every variable has a name, a type, a value, and a location in the computer's memory.

Consider Line 11 from the addition program:

scanf("%d", &integer1); // read an integer

When a value is placed in a memory location (like integer1), it replaces the location's previous value, which is lost. So, this process is said to be destructive.

Similarly, for:

scanf("%d", &integer2); // read an integer

The value entered by the user destructively overwrites whatever was previously in integer2.

Once we have values for integer1 and integer2, the statement:

sum = integer1 + integer2; // assign total to sum

adds these values and places the total into variable sum, replacing its previous value (which was 0 from its initialization).

The integer1 and integer2 values are unchanged by the calculation (integer1 + integer2). The calculation uses their values but does not destroy them. Thus, reading a value from a memory location is nondestructive.

Arithmetic in C

Most C programs perform calculations using the following binary arithmetic operators:

  • + (addition)
  • - (subtraction)
  • * (multiplication)
  • / (division)
  • % (remainder)

Note the use of various special symbols not used in algebra. The asterisk (*) indicates multiplication, and the percent sign (%) denotes the remainder operator (introduced below).

Rules of Operator Precedence

C applies the operations in arithmetic expressions in a precise sequence determined by the following rules of operator precedence, which are generally the same as those in algebra:

  1. Parentheses (): Expressions grouped in parentheses evaluate first. If parentheses are nested, the innermost pair evaluates first. This is the "Highest level of precedence."
  2. Multiplication *, Division /, Remainder %: These are applied next. If an expression contains several such operations, evaluation proceeds from left to right. They share the "Same level of precedence."
  3. Addition +, Subtraction -: These are evaluated next. If an expression contains several such operations, evaluation proceeds from left to right. These two operators have the same level of precedence, which is lower than multiplication/division/remainder.
  4. Assignment =: The assignment operator is evaluated last.

Decision Making: Equality and Relational Operators

Executable statements can also make decisions. For example, a program might determine whether a person's grade on an exam is greater than or equal to 60, so it can decide whether to print a message.

A condition is an expression that can be true or false. This section introduces the if statement, which allows a program to make a decision based on a condition's value. If the condition is true, the statement (or block of statements in curly braces {}) in the if's body executes; otherwise, it does not.

// fig02_05.c 
// Using if statements, relational operators, and equality operators.
#include <stdio.h>

int main(void) {
    printf("Enter two integers, and I will tell you\n");
    printf("the relationship they satisfy: ");
    int number1 = 0;
    int number2 = 0;

    scanf("%d %d", &number1, &number2); // read two integers

    if (number1 == number2) {
        printf("%d is equal to %d\n", number1, number2);
    } // end if

    if (number1 != number2) {
        printf("%d is not equal to %d\n", number1, number2);
    } // end if

    if (number1 > number2) {
        printf("%d is greater than %d\n", number1, number2);
    } // end if

    if (number1 < number2) {
        printf("%d is less than %d\n", number1, number2);
    } // end if

    if (number1 >= number2) {
        printf("%d is greater than or equal to %d\n", number1, number2);
    } // end if

    if (number1 <= number2) {
        printf("%d is less than or equal to %d\n", number1, number2);
    } // end if
    return 0;
} // end function main

Secure C Programming