You are on page 1of 22

Basic Programming for Electrical Engineers

Introduction to C Programming
A Brief History of the C Language
• C evolved from two previous languages, BCPL and B by
Dennis Ritchie at Bell Laboratories.
• C became widely known as the development language of
the UNIX operating system.
• Different organizations began utilizing their own versions
of C which started to cause a serious problem for
programmers who needed to develop a code that would
run on several platforms.
• American National Standards Institute (ANSI) formed a
committee in 1983 to establish a standard definition of C,
which became known as ANSI Standard C.
Why Use C?
• C is a powerful computer programming language. It
allows direct access to memory and many low-level
computer operations. It is used for projects as diverse as
operation systems (OS), word processors, and even
compilers for other languages.
• C is a portable language. C programs, once compiled into
executables, can be transferred to other similar machines
• Most of the things you learn with C will be directly
transferable to future programming languages.
• C has a syntax very close to MATLAB, making the
transistion easy.
Computers: Hardware and Software
• Computer - is a device the can perform computations and
make logical decisions billions of times faster than human
beings can. The process data under the control of sets of
instructions called computer programs.

Basic Computer
Organization
Typical C Program Development Environment

• Phase 1: Creating a Program


• Phase 2 and 3: Preprocessing and Compiling a C
Program
• Phase 4: Linking
• Phase 5: Loading
• Phase 6: Execution
Preparing to Program

When creating a program in C or any computer program,


you should follow a similar sequence of steps:
1. Determine the objective(s) of the program
2. Determine the methods you want to use in the writing the
program.
3. Create the program to solve the problem.
4. Run the program to see the results.
Introduction to C Programming

• The C language facilitates a structured and disciplined


approach to computer program design.
Common Escape Sequences
Introduction to C Programming

• Edit the program to have the following output:


“Welcome”
“to”
“C!”
_
Another Simple C Program: Adding Two Integers
Memory Concepts

• Variables such as integer1, integer2, and sum actually


correspond to locations in the computer's memory. Every
variable has a name, a type, and a value.

• when the above statement is executed, the value typed by


the user is placed into a memory location to which the
name integer1 has been assigned.
Arithmetic in C
Rules of Operator Precedence
1. Operators in expressions contained within pairs of
parentheses are evaluated first. Thus, parentheses may be
used to force the order of evaluation to occur in any
sequence you desire. Parentheses are said to be at the
“highest level of precedence”. In cases of nested, or
embedded, parentheses, the operators in the innermost pair
are applied first.
2. Multiplication, Division and Remainder operations are
applied first. If an expression contains several of these
operations, evaluation proceeds from left to right. These
operations are of the same level of precedence.
3. Addition and Subtraction operations are evaluated next.
Precendence of Arithmetic Operators
Precedence of Arithmetic Operators

• Examples:
• a = 2, b = 3, c = 7, x = 5
Decision Making: Equality and Relational Operators

• Executable C statements either perform actions or make


decisions. We might make a decision in a program, for
example, to determine if a person's grade on an exam is
greater than or equal to 60 and it it is to print the message
“Congrats! You passed.” C allows this with the use of the
if statement. This allows the program to make a decision
based on the truth or falsity of a statement called a
condition.
Decision Making: Equality and Relational Operators

• Conditions are formed by using the equality operators


and relational operators
Activity
• Write a program that gets two integers from the user,
compares and prints the relationship they satisfy using 6 if
statements (two equality operators and four relational
operators).
• Sample Output:

You might also like