first lesson in c - washington state university · 2016. 6. 6. · first lesson in c history, basic...
TRANSCRIPT
![Page 1: First Lesson in C - Washington State University · 2016. 6. 6. · First Lesson in C History, Basic Data Types, Language Elements CptS 121 –Summer 2016 –Armen Abnousi Lecture](https://reader036.vdocuments.site/reader036/viewer/2022071010/5fc7dd40f18ec0360f66a646/html5/thumbnails/1.jpg)
First Lesson in C
History, Basic Data Types, Language Elements
CptS 121 – Summer 2016 – Armen Abnousi
Lecture 2
![Page 2: First Lesson in C - Washington State University · 2016. 6. 6. · First Lesson in C History, Basic Data Types, Language Elements CptS 121 –Summer 2016 –Armen Abnousi Lecture](https://reader036.vdocuments.site/reader036/viewer/2022071010/5fc7dd40f18ec0360f66a646/html5/thumbnails/2.jpg)
History
• 1972 by Dennis Ritchie at AT&T Bell Labs
• CPL -> BCPL -> B -> C
• Designed to implement UNIX operating system with it
• Many other languages somehow derived from C: C++, Java, PHP, Python
• Is an imperative procedural language
• Largely used for embedded programming but also for “low level” and general
purpose programming
![Page 3: First Lesson in C - Washington State University · 2016. 6. 6. · First Lesson in C History, Basic Data Types, Language Elements CptS 121 –Summer 2016 –Armen Abnousi Lecture](https://reader036.vdocuments.site/reader036/viewer/2022071010/5fc7dd40f18ec0360f66a646/html5/thumbnails/3.jpg)
Let’s write our first C Program!
• HelloWorld Program:
/* This program prints “Hello World!” on the screen! */
/* This is my first program */
#include <stdio.h> /* needed for call to printf */
int main()
{
printf(“Hello World!\n”);
return 0;
}
![Page 4: First Lesson in C - Washington State University · 2016. 6. 6. · First Lesson in C History, Basic Data Types, Language Elements CptS 121 –Summer 2016 –Armen Abnousi Lecture](https://reader036.vdocuments.site/reader036/viewer/2022071010/5fc7dd40f18ec0360f66a646/html5/thumbnails/4.jpg)
Analyzing HelloWorld…
• main is where your program starts the execution
• Every C program must have a main
• main should always be preceded by int
• Returning 0 at the indicates normal (without errors)
exit
• printf refers to some operation not defined in basic C,
hence we need to #include the file that contains
instructions about printf.
• Comments make your code more readable and
maintainable!
![Page 5: First Lesson in C - Washington State University · 2016. 6. 6. · First Lesson in C History, Basic Data Types, Language Elements CptS 121 –Summer 2016 –Armen Abnousi Lecture](https://reader036.vdocuments.site/reader036/viewer/2022071010/5fc7dd40f18ec0360f66a646/html5/thumbnails/5.jpg)
More programming!
• Time to home calculator: /* this computes the time required to walk to home*/
#include <stdio.h> /* This is required for printf statement */
#define average_walking_speed 3.31
int main() {
/* variable declaration */
double distance;
double required_time;
/*Get the distance from home */
printf(“Enter distance from home”);
scanf(“%lf”, &distance);
/* Compute the time required to walk home */
required_time = distance / average_walking_speed;
/* output the results */
printf(“It will take an estimated %f hours to get home”, required_time);
return 0;
}
![Page 6: First Lesson in C - Washington State University · 2016. 6. 6. · First Lesson in C History, Basic Data Types, Language Elements CptS 121 –Summer 2016 –Armen Abnousi Lecture](https://reader036.vdocuments.site/reader036/viewer/2022071010/5fc7dd40f18ec0360f66a646/html5/thumbnails/6.jpg)
Build process in C
C
preprocessor
Source
file (.c)C compiler
( + assembler)
Machine
lang. file (.o)C linkerExecutable
file (.exe)
• Replaces macros in code
• Finds the called functions
in header files
• Translates the code to
machine language
• Brings the .o files required in your code
(e.g. for called functions) and attaches the
instruction for them to your code
![Page 7: First Lesson in C - Washington State University · 2016. 6. 6. · First Lesson in C History, Basic Data Types, Language Elements CptS 121 –Summer 2016 –Armen Abnousi Lecture](https://reader036.vdocuments.site/reader036/viewer/2022071010/5fc7dd40f18ec0360f66a646/html5/thumbnails/7.jpg)
Build process in C
C
preprocessor
Source
file (.c)C compiler
( + assembler)
Machine
lang. file (.o)C linkerExecutable
file (.exe)
• Replaces macros in code
• Finds the called functions
in header files
• Translates the code to
machine language
• Brings the .o files required in your code
(e.g. for called functions) and attaches the
instruction for them to your code
#define avg_wlk_spd 3.31#include <stdio.h>
![Page 8: First Lesson in C - Washington State University · 2016. 6. 6. · First Lesson in C History, Basic Data Types, Language Elements CptS 121 –Summer 2016 –Armen Abnousi Lecture](https://reader036.vdocuments.site/reader036/viewer/2022071010/5fc7dd40f18ec0360f66a646/html5/thumbnails/8.jpg)
More programming!
• Time to home calculator: /* this computes the time required to walk to home*/
#include <stdio.h> /* This is required for printf statement */
#define average_walking_speed 3.31
int main() {
/* variable declaration */
double distance;
double required_time;
/*Get the distance from home */
printf(“Enter distance from home”);
scanf(“%lf”, &distance);
/* Compute the time required to walk home */
required_time = distance / average_walking_speed;
/* output the results */
printf(“It will take an estimated %f hours to get home”, required_time);
return 0;
}
![Page 9: First Lesson in C - Washington State University · 2016. 6. 6. · First Lesson in C History, Basic Data Types, Language Elements CptS 121 –Summer 2016 –Armen Abnousi Lecture](https://reader036.vdocuments.site/reader036/viewer/2022071010/5fc7dd40f18ec0360f66a646/html5/thumbnails/9.jpg)
Memory (Remember from yesterday?)
Address: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Content: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
• When we write a program, the OS assigns a section of those cells to our program:
a = 3;
b = 5;
c = a + b;
Name:
Address: 1 2 3 4 5 6 7
b
8
c
9
a
10 11 12 13 14 15 16 17 18
Content: ? ? ? ? ? ? ? 5 8 3 ? ? ? ? ? ? ? ?
My program’s scope assigned by the OS
![Page 10: First Lesson in C - Washington State University · 2016. 6. 6. · First Lesson in C History, Basic Data Types, Language Elements CptS 121 –Summer 2016 –Armen Abnousi Lecture](https://reader036.vdocuments.site/reader036/viewer/2022071010/5fc7dd40f18ec0360f66a646/html5/thumbnails/10.jpg)
Variable declaration
Address: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Content: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
double distance; /* line 6 */
double required_time; /* line 7 */
• Our program needs to store values for “distance” and “required_time”:
Name:
Address: 1 2 3 4 5 6 7
distance
8 9
required_
time
10 11 12 13 14 15 16 17 18
Content: ? ? ? ? ? ? ? 5 8 3 ? ? ? ? ? ? ? ?
My program’s scope assigned by the OS
![Page 11: First Lesson in C - Washington State University · 2016. 6. 6. · First Lesson in C History, Basic Data Types, Language Elements CptS 121 –Summer 2016 –Armen Abnousi Lecture](https://reader036.vdocuments.site/reader036/viewer/2022071010/5fc7dd40f18ec0360f66a646/html5/thumbnails/11.jpg)
Variable Declaration
double distance; /* line 6 */
double required_time; /* line 7 */
• Declaring a variable reserves space for a value in the memory
• Associates a name with that space/value, so the programmer can call the
space/value using that name
• To declare a variable we mention it’s data type and the name we want to call it!
• Basic data types:
• int (for integer numbers) => int my_integer_value;
• double (for real numbers) => double required_time;
• char (for characters) => char your_first_initial;
![Page 12: First Lesson in C - Washington State University · 2016. 6. 6. · First Lesson in C History, Basic Data Types, Language Elements CptS 121 –Summer 2016 –Armen Abnousi Lecture](https://reader036.vdocuments.site/reader036/viewer/2022071010/5fc7dd40f18ec0360f66a646/html5/thumbnails/12.jpg)
Variable Declaration
• Define variables by: variable_type variable_name;
• We need to identify the type of data because (1) there are specific operators for
each data type, (2) each data type requires a different amount of space in memory.
• Variables must be declared before executable statements.
• Variable names can include letters, numbers and underscore. But MUST start with a
letter. (use lower case letters and separate name parts with _ for a good style!)
• int
• integers (at least -32767 to 32767)
• Operations: +, -, *, /, % (mod), >, <, <=, >=, ==, !=
![Page 13: First Lesson in C - Washington State University · 2016. 6. 6. · First Lesson in C History, Basic Data Types, Language Elements CptS 121 –Summer 2016 –Armen Abnousi Lecture](https://reader036.vdocuments.site/reader036/viewer/2022071010/5fc7dd40f18ec0360f66a646/html5/thumbnails/13.jpg)
Basic Data Types
• double
• real numbers (must include a decimal point) (at least ~10e-307 – 10e308)
• not all real numbers can be modeled because of space limitations (64 bits)
• Operations: +, -, *, /, >, <, <=, >=, ==, !=
• char
• Characters (ASCII codes)
• Each char is a single character.
• Operations: >, <, <=, >=, ==, !=
• ASCII includes all lower and upper case letters, white spaces (new line, space, tab,
etc., some special characters) (refer to http://www.asciitable.com/)
• Chars are identified by ‘’. You need to enter a character in between single quotes.
• float
• unsigned int
• …
![Page 14: First Lesson in C - Washington State University · 2016. 6. 6. · First Lesson in C History, Basic Data Types, Language Elements CptS 121 –Summer 2016 –Armen Abnousi Lecture](https://reader036.vdocuments.site/reader036/viewer/2022071010/5fc7dd40f18ec0360f66a646/html5/thumbnails/14.jpg)
More programming!
• Time to home calculator: /* this computes the time required to walk to home*/
#include <stdio.h> /* This is required for printf statement */
#define average_walking_speed 3.31
int main() {
/* variable declaration */
double distance;
double required_time;
/*Get the distance from home */
printf(“Enter distance from home”);
scanf(“%lf”, &distance);
/* Compute the time required to walk home */
required_time = distance / average_walking_speed;
/* output the results */
printf(“It will take an estimated %f hours to get home”, required_time);
return 0;
}
![Page 15: First Lesson in C - Washington State University · 2016. 6. 6. · First Lesson in C History, Basic Data Types, Language Elements CptS 121 –Summer 2016 –Armen Abnousi Lecture](https://reader036.vdocuments.site/reader036/viewer/2022071010/5fc7dd40f18ec0360f66a646/html5/thumbnails/15.jpg)
Executable Statements
• Executable statements follow variable declaration
• These include statements that do some computation, assignment,
input/output statements and other “function calls”.
![Page 16: First Lesson in C - Washington State University · 2016. 6. 6. · First Lesson in C History, Basic Data Types, Language Elements CptS 121 –Summer 2016 –Armen Abnousi Lecture](https://reader036.vdocuments.site/reader036/viewer/2022071010/5fc7dd40f18ec0360f66a646/html5/thumbnails/16.jpg)
Assignment Statements
• Variable_name = expression;
Variable_name is the name of a variable defined earlier
Expression can be a combination of variable names and operators
required_time = distance / average_walking_speed;
• C computes the left hand side of an assignment statement and stores
the result in the memory space reserved for the variable on the right
hand side.
double a, b, average, average_copy;
average = (a + b) / 2;
average_copy = average;
![Page 17: First Lesson in C - Washington State University · 2016. 6. 6. · First Lesson in C History, Basic Data Types, Language Elements CptS 121 –Summer 2016 –Armen Abnousi Lecture](https://reader036.vdocuments.site/reader036/viewer/2022071010/5fc7dd40f18ec0360f66a646/html5/thumbnails/17.jpg)
Input/Output Statements
• Used in interactive programming
• scanf and printf : standard console input I/O functions
• Scanf and printf come from stdio.h (needs to be included)
printf(“ “, … ) scanf(“ “,…)
printf(“estimated %f hours to get home”, required_time);
placeholder Print list
![Page 18: First Lesson in C - Washington State University · 2016. 6. 6. · First Lesson in C History, Basic Data Types, Language Elements CptS 121 –Summer 2016 –Armen Abnousi Lecture](https://reader036.vdocuments.site/reader036/viewer/2022071010/5fc7dd40f18ec0360f66a646/html5/thumbnails/18.jpg)
Input/Output Statements
• For basic data types (int, char, double, float, etc) use ‘&’
before variable names in scanf
scanf(“%lf”, &distance);
Placeholders*:
*Hanly & Koffman book, Problem solving and program design in C, 8th ed, Pearson pub.
![Page 19: First Lesson in C - Washington State University · 2016. 6. 6. · First Lesson in C History, Basic Data Types, Language Elements CptS 121 –Summer 2016 –Armen Abnousi Lecture](https://reader036.vdocuments.site/reader036/viewer/2022071010/5fc7dd40f18ec0360f66a646/html5/thumbnails/19.jpg)
Input/Output Statements
• Make sure every scanf is following a printf, asking the user to
input data:
printf(“please enter your age:>”);
scanf(“%d”, &age);
• When there is a scanf program pauses until values are input
• Numeric values can be separated by spaces or enter.
• When scanf looks for char (%c), whitespaces are also treated
as input values
• If there are more valus entered than expected, they will be
stored in computer buffer and used in the next scanf
statement! (or can cause errors later!)
![Page 20: First Lesson in C - Washington State University · 2016. 6. 6. · First Lesson in C History, Basic Data Types, Language Elements CptS 121 –Summer 2016 –Armen Abnousi Lecture](https://reader036.vdocuments.site/reader036/viewer/2022071010/5fc7dd40f18ec0360f66a646/html5/thumbnails/20.jpg)
Parts of Speech in C
• HelloWorld Program:
/* This program prints “Hello World!” on the screen! */
/* This is my first program */
#include <stdio.h> /* needed for call to printf */
int main()
{
printf(“Hello World!\n”);
return 0;
}
comment
Preprocessor directive
Standard header file
Reserved
words
Standard identifier
![Page 21: First Lesson in C - Washington State University · 2016. 6. 6. · First Lesson in C History, Basic Data Types, Language Elements CptS 121 –Summer 2016 –Armen Abnousi Lecture](https://reader036.vdocuments.site/reader036/viewer/2022071010/5fc7dd40f18ec0360f66a646/html5/thumbnails/21.jpg)
Parts of Speech in C
• Time to home calculator:
#include <stdio.h> /* This is required for printf statement */
#define average_walking_speed 3.31
int main() {
double distance;
double required_time;
printf(“Enter distance from home”);
scanf(“%lf”, &distance);
required_time = distance / average_walking_speed;
printf(“It will take an estimated %f hours to get home”, required_time);
return 0;
}
Reserved word
Preprocessor
Directive
Comment
Variable
Standard identifiers
Constant macro
Special symbol / operator
![Page 22: First Lesson in C - Washington State University · 2016. 6. 6. · First Lesson in C History, Basic Data Types, Language Elements CptS 121 –Summer 2016 –Armen Abnousi Lecture](https://reader036.vdocuments.site/reader036/viewer/2022071010/5fc7dd40f18ec0360f66a646/html5/thumbnails/22.jpg)
Parts of Speech in C
• Preprocessor direvctive start with # and are replaced by
C preprocessor during the build process
• Reserved words cannot be used for user-defined
identifiers (variables, constants or user functions).
• Standard identifiers are the names used in standard C
but are not reserved. Programmer can redefine them,
but is discouraged!
![Page 23: First Lesson in C - Washington State University · 2016. 6. 6. · First Lesson in C History, Basic Data Types, Language Elements CptS 121 –Summer 2016 –Armen Abnousi Lecture](https://reader036.vdocuments.site/reader036/viewer/2022071010/5fc7dd40f18ec0360f66a646/html5/thumbnails/23.jpg)
Programming Style - comments
• Always start your code with a comment box, including
your name, date (of version), purpose and a description
of the program.
/*
* Programmer:
* (Class: CptS 121, Summer 2016)
* (Programming Assignment #)
* Date:
*
* Description:
*/
• Use comments to document your code.
![Page 24: First Lesson in C - Washington State University · 2016. 6. 6. · First Lesson in C History, Basic Data Types, Language Elements CptS 121 –Summer 2016 –Armen Abnousi Lecture](https://reader036.vdocuments.site/reader036/viewer/2022071010/5fc7dd40f18ec0360f66a646/html5/thumbnails/24.jpg)
Programming Style
• Use meaningful names for your variables
• Use underscore if there are multiple words in the
variable name
• Use lower case letters for variable and function names
• Use indentation appropriately
• Use spaces in between operators (+, /, *, etc.) and
operands and after commas.
![Page 25: First Lesson in C - Washington State University · 2016. 6. 6. · First Lesson in C History, Basic Data Types, Language Elements CptS 121 –Summer 2016 –Armen Abnousi Lecture](https://reader036.vdocuments.site/reader036/viewer/2022071010/5fc7dd40f18ec0360f66a646/html5/thumbnails/25.jpg)
Programming Style
• C ignores white spaces
• You can write multiple statements in one line
• Or can extend a statement over multiple lines
• But having one statement per line makes it more
readable and more professional!
• Using good indentation also helps the programmer
himself in locating what he needs when the code is long
enough for him to be lost!
![Page 26: First Lesson in C - Washington State University · 2016. 6. 6. · First Lesson in C History, Basic Data Types, Language Elements CptS 121 –Summer 2016 –Armen Abnousi Lecture](https://reader036.vdocuments.site/reader036/viewer/2022071010/5fc7dd40f18ec0360f66a646/html5/thumbnails/26.jpg)
References
• J.R. Hanly & E.B. Koffman, Problem Solving and Program
Design in C (8thed.), Pearson, 2016
• Andy O’Fallon’s lecture notes for CptS121
(http://eecs.wsu.edu/~aofallon/cpts121)