Docsity

Introduction to Computer Engineering part 4, Slides of Computer Engineering and Programming

Partial preview of the text

Download Introduction to Computer Engineering part 4 and more Slides Computer Engineering and Programming in PDF only on Docsity!

Lecture 4 Program 1

  • /* A simple program in C */
    • Lines beginning with /* and ending with */ indicate that these two lines are a comment
    • // can also be used
  • #include <stdio.h>
    • This is a directive to the C preprocessor
    • Lines beginning with # are processed by the preprocessor before the program is compiled
    • #include <stdio.h> tells the preprocessor to include the contents of the standard input/output header file (<stdio.h>) in the program
    • <stdio.h> contains information used by the compiler when compiling calls to standard input/output library functions such as printf.

Lecture 4 Program 1

  • int main( void )
    • The parentheses after main indicate that main is a program building block called a function
    • C programs contain one or more functions, one of which must be main.
    • Every program in C begins executing at the function main.
    • Functions can return information. The keyword int to the left of main indicates that main “returns” an integer (whole number) value

Common Escape Sequences

used in printf

Escape sequence Escape sequence Description \n Newline. Position the cursor at the beginning of the next line. \t Horizontal tab. Move the cursor to the next tab stop. \a Alert. Sound the system bell.

\ Backslash. Insert a backslash character in a string.

" Double quote. Insert a double-quote character in a string.

printf("Cape Peninsula University of Technology\n\n Department of Mechanical Engineering\n\n Computer Engineering\n\n CPE150S");

Lecture 4 Program 1

  • return 0;
    • This statement is included at the end of every main function. The keyword return is one of several means used to exit a function.
    • When the return statement is used at the end of main , the value 0 indicates that the program has terminated successfully.

Lecture 4 Program 2

  • The names integer1, integer2 and sum are the names of variables.
  • A variable is a location in memory where a value can be stored for use by a program.
  • These definitions specify that the variables integer1, integer2 and sum are of type int, which means that these variables will hold integer values, i.e., whole numbers such as 7, –11, 0.
  • All variables must be defined with a name and a data type immediately after the left brace that begins the body of main before they can be used in a program.
  • There are other data types besides int in C.
  • The preceding definitions could have been combined into a single definition statement as follows: - int integer1, integer2, sum;

Lecture 4 Program 2

  • A variable name in C is any valid identifier.
    • An identifier is a series of characters consisting of letters, digits and underscores ( _ ) that does not begin with a digit.
    • An identifier can be of any length, but only the first 31 characters are required to be recognized by C compilers according to the C standard.
    • C is case sensitive - uppercase and lowercase letters are different in C, so a1 and A1 are different identifiers.

Lecture 4 Program 2

  • The scanf function
    • reads from the keyboard.
    • this scanf has two arguments, "%d" and &integer1.
  • The first argument, the format control string
    • indicates the type of data that should be input by the user.
    • The %d conversion specifier indicates that the data should be an integer (the letter d stands for “decimal integer”).
    • The % in this context is treated by scanf (and printf as we’ll see) as a special character that begins a conversion specifier.
  • The second argument of scanf begins with an ampersand (&)-called the address operator in C, followed by the variable name - The ampersand, when combined with the variable name, tells scanf the location (or address) in memory at which the variable integer1 is stored. - The computer then stores the value for integer1 at that location.

interge1 23

Lecture 4 Program 2

  • The assignment statement (sum = integer1 + integer2; ) calculates the sum of variables integer1 and integer2 and assigns the result to variable sum using the assignment operator (=).
  • The statement is read as, “sum gets the value of integer1 + integer2.”
  • The (=) operator and the (+) operator are called binary operators because each has two operands. - The + operator’s two operands are integer1 and integer2. - The = operator’s two operands are sum and the value of the expression integer1 + integer2.

interge1 23

interge2 48

sum 71

Lecture 4 Program 2

  • This statement passes the value 0 back to the operating-system environment in which the program is being executed.
  • This value indicates to the operating system that the program executed successfully.

Arithmetic in C

Equality and Relational Operators

Keywords in C