Lecture 02
Lecture 02
(C Programming Language)
• Comments:
▪ Text surrounded by /*...*/ is ignored by C compiler
▪ Used to described program or give a note/guidance for later.
#include <stdio.h>
▪ Preprocessor directive to tell C that the propgram needs to load contents of a certain file.
▪ <stdio.h> allows standard input/output operations.
• Errors:
▪ Forgetting to terminate a comment with */
▪ Starting a comment with */ and ending a comment with /*
▪ Another solution – using // so each line of comment
Simple C: Print a line of text
• int main()
▪ C++ programs contain one or more functions, exactly one of which must be main
▪ Parenthesis used to indicate a function
▪ Int means that the main returns an integer value
▪ Braces {...} indicate a block. The bodies of all functions must be contained in braces.
• Tips:
▪ Every function should have a comment describing the purpose
Simple C: Print a line of text
• Escape character ()
▪ Indicates that the function printf should do something out of the ordinary
▪ ╲n is the newline character
Simple C: Print a line of text
return 0;
▪ A way to exit a function
▪ return 0, in this case, means that the program terminated normally
• Right brace }
▪ Indicates end of main has been reached
• Linker
▪ When a function is called, linker locates it in the library
▪ Inserts it into object program
▪ If function name is misspelled, the linker will produce an error because it will not be able to find
function in the library
Simple C: Print a line of text
• Tips:
▪ Add a comment to the line containing the right brace, }, that closes every function, including
main.
▪ The last character printed by a function that displays output should be a newline (╲n).
▪ Indent the entire body of each function one level of indentation
▪ Tab key may be used to create indents.
Assignment 1 (1-1)
• Write a simple program to print “Welcome to C!” in a single line using multiple
printf functions.
▪ Output will be like as follows:
Welcome to C!
• Write a simple program to print “Welcome to C!” in multiple lines with each line
containing one words using a single printf function
▪ Output will be like as follows:
Welcome
to
C!
Assignment 2 (1-2)
Definitions of variables
• As before
▪ Comments, #include <stdio.h> and main
• Errors:
▪ Typing Main instead of main.
▪ Placing variable definitions among executable statements causes syntax errors.
• Tips:
▪ Multiple-word variable names can help make a program more readable. total_commissions
or totalCommissions.
▪ Separate the definitions and executable statements in a function
Simple C: Adding two integers
• Tips:
▪ Place a space after each comma (,) to make programs more readable.
Simple C: Adding two integers
• Errors:
▪ A calculation in an assignment statement must be on the right side of the = operator. It is a
syntax error to place a calculation on the left side of an assignment operator.
▪ Forgetting to precede a variable in a scanf statement with an ampersand &
▪ Forgetting one or both of the double quotes surrounding the format control string in a printf or
scanf.
▪ Forgetting the % in a conversion specification in the format control string of a printf or scanf.
▪ Placing an escape sequence such as ╲n outside the format control string of a printf or scanf.
Memory Concepts
• Variables
▪ Variable names correspond to locations in the computer's memory
▪ Every variable has a name, a type, a size and a value
▪ Whenever a new value is placed into a variable (through scanf, for example), it replaces (and
destroys) the previous value
▪ Reading variables from memory does not change them
Memory Concepts
• Arithmetic calculations
▪ Use * for multiplication and / for division
▪ Integer division truncates remainder
− 7 / 5 evaluates to 1
▪ Modulus operator % returns the remainder
− 7 % 5 evaluates to 2
• Operator precedence
▪ Some arithmetic operators act before others (i.e., multiplication before addition)
− Use parenthesis when needed
▪ Example: Find the average of three variables a, b and c
− Do not use: a + b + c / 3
− Use: (a + b + c ) / 3
Arithmetic in C
• Errors:
▪ An attempt to divide by zero is normally undefined on computer systems and generally results
in a fatal error
▪ Nonfatal errors allow programs to run to completion, often producing incorrect results.
Arithmetic in C
• Tips:
▪ Using redundant parentheses in complex arithmetic expressions can make the expressions
clearer.
Arithmetic in C
Floating Point Arithmetic
• Ex1-3: Write a program to calculate (a+b)3 with a and b are inputted by user.
• Ex1-4: Write a program to calculate the volume of a cylinder with radius and
height inputted by user
Equality and Relational Operators
• Executable statements
▪ Perform actions (calculations, input/output of data)
▪ Perform decisions
− May want to print "pass" or "fail" given the value of a test grade
• if control statement
▪ Simple version in this section, more detail later
▪ If a condition is true, then the body of the if statement executed
− 0 is false, non-zero is true
▪ Control always resumes after the if structure
• Keywords
▪ Special words reserved for C
▪ Cannot be used as identifiers or variable names
Equality and Relational Operators
Equality and Relational Operators
• Errors:
▪ If the two symbols in any of the operators ==, !=, >= and <= are separated by spaces.
▪ If the two symbols in any of the operators !=, >= and <= are reversed as in =!, => and =<,
respectively.
▪ Confusing the equality operator == with the assignment operator =.
▪ Placing a semicolon immediately to the right of the right parenthesis after the condition in an if
statement.
▪ Placing commas (when none are needed) between conversion specifiers in the format control
string of a scanf statement.
Equality and Relational Operators
• Tips:
▪ Indent the statement(s) in the body of an if statement.
▪ Place a blank line before and after every if statement in a program for readability.
▪ Although it is allowed, there should be no more than one statement per line in a program.
Equality and Relational Operators
• Tips:
▪ No more than one statement per line.
▪ Choose breaking points that make sense
▪ Confirm that the operators in the expression are applied in the proper order.
Assignment 4 (1-5)
• Write a program to print the tax order and tax percentage with income entered by
user using if statement.
2 From 60 up to 120 10
E3: Using the statements you wrote in E1, write a complete program that calculates
the product of three integers.
E4: Identify and correct the errors in each of the following statements:
a) printf( "The value is %d╲n", &number );
b) scanf( "%d%d", &number1, number2 );
c) if ( c < 7 );{
printf( "C is less than 7╲n" );
}
d) if ( c => 7 ) {
printf( "C is greater than or equal to 7╲n" );
}