Title: Your First Steps in C Programming
1Your First Steps in C Programming
2Comments
- Arbitrary strings placed between the delimiters
/ and /. - /
- A comment can be written in this fashion
- to set it off from the surrounding code.
- /
- The compiler changes each comment into a single
blank character.
Use comments in your code! It makes coding a lot
simpler!
3An Example
- / This program does not do much ... its an
example / - include ltstdio.hgt
- int main(void)
-
- int a, b, c
- float x 0.0f, y 3.3f, z -7.7f
- printf( Input two integers )
- scanf( dd, b, c )
- a b c
- x y - z
- printf( d d d\n, b, c, a )
- printf( f - f f\n, y, z, x )
- return 0
4VariablesandData Types
5What is a Variable?
- A space in the computers memory which
- Belongs to my program.
- Has a name.
- Can store stuff for me.
- Lets me read the stuff that I stored there.
- This is how we store data in an application.
6Fundamental data types
- int
- Short for integer.
- Stores integer numbers.
- float / double
- Store real numbers (numbers with decimal points).
- double is more accurate than float.
- char
- Short for character.
- Stores characters.
7Fundamental Data Types
- Common ones
- int float double char
- Less common
- unsigned int
- Rarely used
- short long ...
8An Example
- / This program does not do much ... its an
example / - include ltstdio.hgt
- int main(void)
-
- int a, b, c
- float x 0.0f, y 3.3f, z -7.7f
- printf( Input two integers )
- scanf( dd, b, c )
- a b c
- x y - z
- printf( d d d\n, b, c, a )
- printf( f - f f\n, y, z, x )
- return 0
Defining variables
9Variables
- A variable is a space in memory reserved by your
application. - You can access the variable through its name.
- You can retrieve the value of the variable and
you can change its value through assignment. - int main()
-
- int a, b, c
- float x 0.0f, y 3.3f, z -7.7f
- ...
Memory
???
a
3.3
y
Application
10Variables
- Before you can use a variable, you must declare
it. - Give the variable a name.
- Define its data type.
- int main()
-
- int a, b, c
- float x 0.0f, y 3.3f, z -7.7f
- ...
11Variables
- Variables
- Defined at the beginning of a scope.
- Recognized anywhere within the scope.
- Once outside the scope the variable dies.
- void main()
-
- ...
- ...
- ...
- ...
A scope begin with and ends with
12Variable Names
- Variables names can include
- English Letters (lowercase and uppercase)
- Numbers
- An underscore (_).
- Rules
- First character cannot be a number!
- Case sensitive.
- Some names are already taken keywords.
2temp is illegal
temp2 is different from Temp2
13Keywords
- Reserved words with strict meanings.
- C does a lot with relatively few keywords.
14Always Initialize Variables
Always initialize variables!!! Variables always
have a value, even if you dont initialize them.
The initial value is usually meaningless
(garbage). People, for some reason, assume that
this value is zero ... its not!!!
15Give Meaningful Names
Give variables meaningful names!!! What do the
varibles a, i, x, t store? What do the variables
angle, time, ctr store?
16Things You Can Do With Variables
x is assigned the value 5
a is assigned the value of b c
The value of y is increased by 1
z is assigned the remainder of y / 5
17Things You Can Do With Variables
- / This program does not do much ... its an
example / - include ltstdio.hgt
- int main(void)
-
- int a, b, c
- float x 0.0f, y 3.3f, z -7.7f
- printf( Input two integers )
- scanf( dd, b, c )
- a b c
- x y - z
- printf( d d d\n, b, c, a )
- printf( f - f f\n, y, z, x )
- return 0
Assignment during declaration
Assignment and math operators
Printing variable values
18Fahrenheit to Celsius
- include ltstdio.hgt
- int main()
-
- double cels0.0, fahr0.0
- printf("Please enter a fahrenheit
temperature\n") - scanf("lf",fahr)
- cels 5(fahr-32)/9
- printf("Is equal to lf degrees celsius\n",
cels) - return 0
Definition with initialization
Computation and assignment
19Char
- for some reason this requires
- additional explanation
20Char
- char data type
- Represents a character.
- One byte in size.
- Stores the characters as a number.
- Ascii Table
- Converts a number to a character (and
vice-versa). - char c A ? char c 65
21Char
The character \0 has a value of 0
22Char
- char cVal 'a'
- printf("c", cVal)
- printf("d", cVal)
- printf("ccc", cVal, cVal1, cVal 2)
a is printed
97 is printed
abc is printed
23Variable Limits
24compute the size of some fundamental types
- run on PC Pentium 3"
- The size of some fundamental types is computed.
- char 1 byte
- short 2 bytes
- int 4 bytes
- long 4 bytes
- unsigned 4 bytes
- float 4 bytes
- double 8 bytes
- long double 8 bytes
25Going over the Limit
- include ltstdio.hgt
- int main(void)
-
- int i 0
- unsigned u UINT_MAX
- for ( i 0 i lt 5 i )
- printf( u d u\n, u, i, u i )
- return 0
Typically equal to 4294967295
26Going over the Limit
- 4294967295 0 4294967295
- 4294967295 1 0
- 4294967295 2 1
- 4294967295 3 2
- 4294967295 4 3
The variable restarts itself
27Float Representation
- include ltstdio.hgt
- int main()
-
- int i 0
- float f 0
- for ( i 0 i lt 100 i )
- f 0.01f
- printf( f\n, f )
- return 0
Due to the way a float / double are stored in
memory, there are precision errors.
Output 0.999999
28Special Float Values
- NaN Not a Number - represents an illegal value.
- printf(f\n, sqrt(-1))
- will print -1.IND00 or NAN
- INF infinity - will print 1.INF00
29Printf Scanf
- Youre going to use them a lot!
30Printf
- Writes data to the standard output (stdout)
- Arguments are filled with corresponding values
requested in the format string. - d ? int
- c ? char
- f ? float
- lf ? double
31Scanf
- Reads data from the standard input (stdin)
- Arguments are filled with corresponding values
requested in the format string - Return Value The number of items successfully
read. If EOF is returned an error has occurred
before the first assignment could be done - Whitespace tab, space, newline (CR) (isspace())
32A Simple scanf Example
- include ltstdio.hgt
- int main()
-
- int n
- char c
-
- while (scanf("d , c",n, c) 2)
- printf ("d, c\n",n,c)
-
- return 0
Dont forget the '' Its very important!! Well
talk about it later in the course.
SimpleScanf.c
33A Simple scanf Example
- SAMPLE INPUT SAMPLE OUTPUT
- 123,a 123, a
- 123 , a 123, a
- 123 123, a
- ,
- a
- 123a lterrorgt
34Math
35Something to Remember
- When doing a math on variables of different
types - The computer converts the variables to the more
accurate type. - 7 / 2.0 ? 3.5
- When doing math on variable of the same type
- All computation remain within the type
- 7 / 2 ? 3
36Casting
Treating a value of one type as if it were of
another type.
- double d 3.3
- int i 0
- unsigned ui 0
- i (int)d
- d (double)i / 2
- ui (unsigned)i
i 3
d 1.5
37Mathematical Functions
- sqrt()
- pow()
- exp()
- log()
- sin()
- cos()
- tan()
The functions are declared in ltmath.hgt
All the functions use doubles
38Square Root and Power
- include ltstdio.hgt
- include ltmath.hgt
- int main()
-
- double x 0
- printf( "Input x \n" )
- scanf( "lf", x )
- if ( x gt 0.0 )
-
- printf("x lf\n", x)
- printf("sqrt(x) lf\n", sqrt(x))
- printf("pow(x, x) lf\n", pow(x, x))
-
- else
-
- printf( "Sorry, your number must be
nonnegative.\nBye Bye\n" ) -
- return 0
This is the interesing part
SqrtPow.c
39The Result of the Program
- The square root of x and x raised to the x power
will be computed. - Input x 2
- x 2.000000
- sqrt(x) 1.41421
- pow(x, x) 4.000000