Beruflich Dokumente
Kultur Dokumente
C Programming Basics
Lecture 5
http://feh.eng.ohio-state.edu
C Program Basics
• C vs. C++
Program Structure in C
– Comment statements
– Pre-processor directives
– Declaration statements
– One or more functions
– Executable statements
Program Structure in C
• EACH complete C program is composed of:
– Comment statements
– Pre-processor directives
– Comment statements
– Declaration statements
– Comment statements
– One or more functions
– Comment statements
– Executable statements
– Comment statements
Winter Quarter The Ohio State University Lect 5 P. 7
Gateway Engineering Education Coalition
Engineering H192 - Computer Programming
Comment Statements
• Formal Comments:
/* Comment ….. */
– Used for detailed description of functions or
operations (for our benefit, not compiler’s).
– Can take multiple lines in source file.
• Informal Comments (only in C++, not C):
// Comment ….. Ends at the end of line
– Used for quick comments like:
int temp; // temporary variable for storing
// the input value
Winter Quarter The Ohio State University Lect 5 P. 8
Gateway Engineering Education Coalition
Engineering H192 - Computer Programming
Pre-Processor Directives
Declarations
• Example declarations:
int a, b, c ;
float r, p, q ;
double x, y, z ;
char m, n ;
Data Types
Executable Statements
• Simple
Declaring variables
int temp ;
char a ;
Assigning Values
temp = 5 ; temp is assigned the value of 5
• Complex, i.e., Calling Functions
plotxy (x, y) ;
• Calculations
x = (5. / 2 + 6) * 7 ;
Arithmetic Operators
* multiply + add
/ divide - subtract
% remainder, where:
x = 13 % 5 ; /* x will be equal to 3 */
For example:
x = 2 * 3 - (4 + 5) + 8 % 7;
x = 2 * 3 - 9 + 8 % 7;
x = 6 - 9 + 8 % 7;
x = 6 - 9 + 1;
x = -3 + 1;
x = -2;
Another example:
x = 6 / 2 + 1 - 3 + 8 * 4;
x = 33;
x = 6 / (2 + 1) - (3 + 8) * 4;
x = -42;
Assignment Operators
Operator: Example: Meaning:
= x=5; x=5;
+= x += 5 ; x=x+5;
–= x –= 5 ; x=x–5;
/= x /= 5 ; x=x/5;
*= x *= 5 ; x=x*5;
%= x %= 5; x= x%5;
Assignment Operators
int a = 4, b = 2, c = 36 ;
a += b ; /* This adds b to a, a = ? */
Assignment Operators
int a = 4, b = 2, c = 36 ;
a += b ; /* This adds b to a, a = ? */
[ Answer: a = a + b, so a = 4 + 2 or a = 6 ]
c /= a + b ; /* What is value of c now? */
Assignment Operators
int a = 4, b = 2, c = 36 ;
a += b ; /* This adds b to a, a = ? */
[ Answer: a = a + b, so a = 4 + 2 or a = 6 ]
c /= a + b ; /* What is value of c now? */
[ Answer: c = c / (a + b), and a = 6 now,
so c = 36 / (6 + 2), so c = 36 / 8 or c = 4 ]
Increment/Decrement Operators
Increment/Decrement Operators
c = b-- - ++a ;
/* What are the values of a, b, c now? */
Increment/Decrement Operators
Increment/Decrement Operators
Examples of increment and decrement operators:
int a = 4, b = 2, c;
c = ++a + b-- ;
/* What are the values of a, b, c now? */
(Answers: a = 5, b = 1, c = 7)
c = b-- - ++a ;
/* What are the values of a, b, c now? */
(Answers: a = 6, b = 0, c = -5)
Relational Operators
Operator: Meaning:
Relational Operators
• Used for asking questions like:
Is x bigger than 10?
Logical Operators
! (not)
Ex: a != b is true if a and b are not equal
&& (and)
Ex: 5<6 && 7>4 is true, but
5>6 && 7>4 is not true (i.e., false)
|| (or)
Ex: 5>6 || 7>4 is true
5<6 || 7<4 is also true
Exponentiation Operations
Skeleton Program
/*****************************************/
/* Name: Brutus Buckeye */
/* Seat No. 0, Instr: W. Hayes */
/* Program progname/assignment */
/*****************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main ( )
{
statements ;
}
Winter Quarter The Ohio State University Lect 5 P. 32
Gateway Engineering Education Coalition
Engineering H192 - Computer Programming
Today’s Assignment
• Today’s Assignment is G04
• G03 has a C program that calculates values and
assigns them to variables.
• You are to do the calculations by hand (quiz and
midterm material!!) and print the answers on the
problem sheet.
• Then, copy the program into your account and
run it to get the correct answers. Compare your
hand calculations to the computer’s calculations.
• What needs to be added to the code so that you
know what the computer calculated?