Week 3 C Programming
Week 3 C Programming
Computer Programming I
Week 3
Intro to C Programming
• OUTPUT:
– Welcome to C!
Comments
• Begin with //
• Insert comments to document programs and improve program readability
• Comments do not cause the computer to perform actions
• Help other people read and understand your program
• Can also use /*…*/ multi-line comments
– Everything between /* and */ is a comment
Escape Sequences
• In a string, backslash (\) is an escape character
• Compiler combines a backslash with the next character to form an escape
sequence
• \n means newline
• When printf encounters a newline in a string, it positions the output
cursor to the beginning of the next line
\a Produces a sound or visible alert without changing the current cursor position.
Indentation Conventions
• Indent the entire body of each function one level of indentation within the
braces that define the function’s body
• Emphasizes a program’s functional structure and helps make them easier to
read
• Set an indentation convention and uniformly apply that convention
• Style guides often recommend using spaces rather than tabs
• OUTPUT:
– Welcome to C!
Copyright © 2021 Pearson Education, Inc. All Rights Reserved
2.2 A Simple C Program: Printing a Line of Text (14 of 15)
Displaying Multiple Lines with a Single printf
• One printf can display several lines
• Each \n moves the output cursor to the beginning of the next line
• OUTPUT:
Welcome
to
C!
• scanf standard library function obtains information from the user at the
keyboard
• Next program obtains two integers, then computes their sum and displays the
result
1. // fig02_04.c
2. // Addition program.
3. #include <stdio.h>
14.
17.
• OUTPUT:
Prompting Messages
• Line 10 displays "Enter first integer: "
• This message is called a prompt
– tells user to take a specific action
Assignment Statement
• The assignment statement in line 17 calculates the total of integer1 and
integer2, then assigns the result to variable sum using the assignment
operator (=)
• Read as, “sum gets the value of the expression integer1 + integer2.”
• Most calculations are performed in assignments
Binary Operators
• The = operator and the + operator are binary operators—each has
two operands
• Place spaces on either side of a binary operator to make the operator
stand out and make the program more readable
Subtraction
Minus
p cc
p minus p - c
p minus c
Multiplication * bm
b m b m
b asterisk m
x
Division / x /y or
x slash y or start fraction x over y end fraction
x / y
x forward slash y
y
Remainder % rr mod
modss r s
r percent sign s
– 17 % 5 yields 2
• An attempt to divide by zero usually is undefined
– Generally, a fatal error
– Nonfatal errors allow programs to run to completion, often with incorrect results
Higest
Lowest
Copyright © 2021 Pearson Education, Inc. All Rights Reserved
2.5 Arithmetic in C (4 of 9)
abc d e
Algebra: m
5
C: m =(a + b +c + d + e)/5;
C: y = m x + b;
C:
– y = a x x + b x + c;
greater than or equal to, right angle
bracket with underscore
less t han or equal t o, lef t angle
bracket with underscore
Equality operators
Algebraic equality of C equality of Sample C
Meaning of C condition
relational operator relational operator condition
= ==
equals equals
x == y
x equals equals y
x is equal to y
does not equal, equal sign with a slash t hrough
!=
exclamation point equals
x!= y
x exclamation point equals
x is not equal to y
1. // fig02_05.c
2. // Using if statements, relational
3. // operators, and equality operators.
4. #include <stdio.h>
5.
6. // function main begins program execution
7. int main(void) {
8. printf("Enter two integers, and I will tell you\n");
9. printf("the relationships they satisfy: ");
10.
11. int number1 = 0; // first number to be read from user
12. int number2 = 0; // second number to be read from user
13.
14. scanf("%d %d", &number1, &number2); // read two integers
15.
• OUTPUT 1:
• OUTPUT 2:
• OUTPUT 3:
Comparing Numbers
• Lines 16–18 compare number1’s and number2’s values for equality
• If the values are equal, line 17 displays a line of text indicating that
• Indenting each if statement’s body and placing blank lines above and below each
if statement enhances program readability
• { begins the body of each if statement
• } ends each if statement’s body
• Any number of statements can be placed in an if statement’s body.
• Placing a semicolon immediately to the right of the right parenthesis after an if
statement’s condition is a common error
– The semicolon is treated as an empty statement that does not perform a task
Keywords
• Some words, such as int, if and void, are keywords or reserved words
of the language and have special meaning to the compiler
• The following table contains the C keywords
• Do not use them as identifiers