based upon Practical C Programming by Steve Oualline CS550 Operating Systems About C C is a structural also called imperative programming language Each program is viewed as a group of operations or actions ID: 448839
Download Presentation The PPT/PDF document "C Programming Day 1" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
C Programming Day 1 based upon Practical C Programming by Steve Oualline
CS550
Operating SystemsSlide2
About C C is a structural (also called imperative) programming languageEach program is viewed as a group of operations or actions
Programs may be modularized
statements
--------
------
----Slide3
Programming processSpecify a task (design)Find an algorithm to complete the task
Write code
Test your code
Repeat from appropriate pointSlide4
Preprocessor Directives
Like import statements in Java these allow you to include code that already exists
#include <filename>
#include <
stdio.h
> //Standard input/output
#include "
myheader.h
" //homemade header file
/
/The
printf
function comes from
stdio.h
printf
("Hi my name is...\n");Slide5
Preprocessor DirectivesYou can also use preprocessor directives to create constants or macros
#define // <-- use to create constants or a macro
#define PI 3.14
//Never use PI 3.14; <-- semicolons should not be used when defining
//a constantSlide6
CompilationCompiler Translates C code to machine code
Linker
takes object code (intermediate code) and joins it togetherSlide7
Compilation At $ prompt
gcc
prog1.c
–
std
=c99
-o prog1
.c file - source code
-
std
=
c99
– Use
C99 standard
-o
- output to a specific file
prog1
- executable file
Without an executable specified with –o,
a.out
is the executableSlide8
Comments and a Simple C Program
/* a multiline comment */
//a one line comment
#include <
stdio.h
>
int
main(
int
argc
, char **
argv
)
{
printf
("Hello, world!\n"):
return 0;
}Slide9
Primitive Variable DeclarationsVariable declarations similar to Java
i
nt
centimeters, meters, kilometers;
d
ouble watts;
c
har
y_or_n
= ‘y’;Slide10
BlocksBlocks similar to Java
//A BLOCK
{
declarations
statements
}Slide11
AssignmentAssignment statements comparable to Java
int
a;
a = 1 + 2 * 5;
result --> a = 11
Results of RHS stored in LHSSlide12
printfPrint statements may be performed using
printf
similar to, but not exactly the same as, Java
Format:
printf
(control string, variable list);
variable list -> variable | variable listSlide13
Example Program#include <
stdio.h
>
int
main(
int
argc
, char **
argv
)
{
int
meters = 8;
printf
(“%d meters\n”, meters);
}
Result:
8 metersSlide14
Another Example#include <
stdio.h
>
int
main(
int
argc
, char **
argv
)
{
int
kilometers = 10, meters = 7, centimeters = 8;
printf
(“%d %d %d\n”, kilometers, meters, centimeters);
}
Result:
10 7 8
What if we reorder the variable list?Slide15
Common Conversion Specifications%d
-
int
(often 4 bytes)
%lf
-
double
(often 8 bytes)
%c
- ASCII character or
char
(often 1 byte)
%f
-
float
(often 4 bytes)
%s
- string (
NULL
terminated)Slide16
Escape CodesUsed to represent characters that aren't readily visible in strings
\n
new line (in
L
inux this represents return)
\r
return (in Windows
\n\r
represents return)
\t
tab
\'
single quote
\"
double quote
\\
backslashSlide17
Reserved wordssigned, unsigned,
struct
union, char,
int
, float, double
while, for, if, do, short
long, static, auto, extern
register
Remember that C is case sensitive!Slide18
Arithmetic Expressions in C* multiplication
/ division
% modulus (remainder)
+ addition
- subtractionSlide19
Order of Operations1. ()
2. unary negation
3. * / %
4. + -
Example: 2 + 3 % 7
3 % 7 = 3
2 + 3 = 5Slide20
Example Program
#include <
stdio.h
>
int
main(
int
argc
, char **
argv
)
{
int
kilometers, meters, centimeters;
kilometers = 4;
meters = 1000*kilometers;
centimeters = 100*meters;
printf
("The length of a bridge :\n");
printf
("%
s%s
\n", "The length", " of a bridge");
printf
(”The length in different units:\n");
printf
("%d kilometers\n", kilometers);
printf
("%d meters\n", meters);
printf
("%d centimeters\n", centimeters);
return 0;
}