EPSII
59:006
Spring 2004
Introduction to C
More Administrative Details The C Programming Language How a computer processes programs Your first C program Anatomy of a C program
More Administrative Details
Evening Exams Will try to have T.A.’s in lab by Monday
(1245 SC) Accessing Unix systems
Course web page
http://www.engineering.uiowa.edu/~eng006
Note: use webCT4.1
Exams: concurrent exams at nightUnified HW assignmentsGrading uniform across all sections.
Introduction to the C Programming Language C is a powerful high-level programming
language that also maintains the ability to do some low-level stuff
With the power of C comes both freedom and responsibilityThe language’s rules allow for many different
programming techniquesThis freedom can get you into trouble!
Writing C Programs
In this course you will learn one consistent approach to writing C programs (and pick up much information common to any way of writing these programs)
If you try hard to conform to the guidelines presented, you will be up and running with a minimum of difficulty
How does a computer process a user program?
A simple C program
Compiling, linking, and running
Next, compile, link, and run your program:% gcc simple.c –o simple
% simple
I am a simple computer.My favorite number is 1 because it is first.
%
Anatomy of a C Program
Anatomy of a C Program
Fundamental Aspect of a C Program: Everything is a function! Functions are blocks, and curly braces { and } delimit blocks So,
int main(void){ …….
.…… return 0;
}
Is a function Well learn more about functions in a moment
Anatomy of a C Program The main () function
Tells the compiler to start with this function Calls other functions in your program Example:
int f1(void){ return 1;}
int f2(void) {
return1;}
int main(void){ f1(); f2(); return 0;}
Anatomy of a C Program
#include<stdio.h>
This is actually a pre-processor statement that includes a header file (note the .h extension) which describes the standard I/O library functions (needed for printf() in this program).
Anytime you use functions from external libraries you should include a header file to tell the compiler about the function you are using.
Anatomy of a C Program
Variables are symbolic names referring to storage in computer memory
All variables must be declared in Cvariable declaration:
int num; All variables must be defined before use:
variable definition:num = 1;
Anatomy of a C Program
Variables Must refer to a specific amount of computer storage
(in bytes), but you don’t have to worry about the details – for now you can just use a pre-defined type.
Some Predefined Variable Types int – integer (positive or negative whole number) char – character variable (holds a code describing a character) double – double-precision floating point, a real number float – single-precision floating point number
Anatomy of a C Program
Where to Declare Variables In C, you must declare all variables first in a
function (this includes main()) Where to Define Variables
Although you can define (initialize) variables anywhere in a function, you usually do it at the top for readability
Anatomy of a C Program
Each of these lines of code are called statements
Statements always are terminated with a semicolon ‘;’
Functions can return a value:int f1(void);
Or, functions can return nothing:void f1(void);
Anatomy of a C Program
Functions also take 0 or more arguments, separated by commas:Examples:
int main (void); /* no arguments */int f1(int); /* a single integer argument */
int f2(int, int); /* two integer arguments */
Example program: Adding two numbers
Let’s say you want to add two numbers:
int main(void){ int num1,num2,sum;
num1 = 1; num2 = 2; sum = num1 + num2; printf("The sum of %d and %d is %d\n", num1,
num2, sum); return 0;}
Writing to the screen
Use the printf() function:”
#include <stdio.h>
printf(”This goes to the screen without a new line”);
printf(”This gets a new line\n”);
Writing to the screen
Escape Sequences
\n new line
\a alert (rings terminal bell)
\t tab
\\ Prints a backslash
\*
*/
Start of comment
End of comment
Top Related