C programming for Engineers - PowerPoint PPT Presentation

1 / 54
About This Presentation
Title:

C programming for Engineers

Description:

They contain the data your program works with. ... Example. Declaring variables in C. Before using a variable, one must declare it. ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 55
Provided by: csta3
Category:

less

Transcript and Presenter's Notes

Title: C programming for Engineers


1
C programming for Engineers
  • Lcc compiler a free C compiler available on the
    web.
  • http//www.cs.virginia.edu/lcc-win32/
  • Also available visual Studio C,
  • Express edition
  • http//msdn.microsoft.com/vstudio/express/visualc/
    download/
  • Note that you have to register (free)

2
What are variables?
  • A named area in the computer memory, intended to
    contain values of a certain kind (integers, real
    numbers, etc.)
  • They contain the data your program works with.
  • They can be used to store data to be used
    elsewhere in the program.
  • In short they are the only way to manipulate
    data.

3
Variables in memory
  • int my_int 5
  • double my_double 3.5

5
my_int
3.5
my_double
4
Variables in memory
  • Whenever we write the variable name (my_int), we
    ask to read the value of that variable
  • If we write variable_name, we ask for the
    address of that variable

5
my_int
3.5
my_double
5
Example
  • / Get a length in cm and convert to inches /
  • include ltstdio.hgt
  • int main()
  • double cm, inches
  • printf("Please enter length in centimeters ")
  • scanf("lf", cm)
  • inches cm / 2.54
  • printf("This is equal to g inches\n", inches)
  • return 0

6
Declaring variables in C
double cm, inches
  • Before using a variable, one must declare it.
  • The declaration first introduces the variable
    type, then its name.
  • When a variable is declared, its value is
    undefined.

7
Example variable declarations
  • int i
  • char c
  • float f1, f2
  • float f17.0, f2 5.2
  • unsigned int ui 0

8
Variable naming rules
  • Letters, digits, underscores
  • i
  • CSE_5a
  • a_very_long_name_that_isnt_very_useful
  • fahrenheit
  • First character cannot be a digit
  • 5a_CSE is not valid!
  • Case sensitive
  • CSE_5a is different from cse_5a

9
Data types in C
  • char a single byte character.
  • int an integer number usually 4 bytes.
  • float a single precision real number usually
    4 bytes.
  • double a double precision real number usually
    8 bytes.

char
float
int
double
10
Data types in C
  • short int (or just short) an integer number,
    usually 2 bytes (rarely used).
  • long int (or just long) an integer number, 4
    or 8 bytes (rarely used).
  • long double - a double precision real number
    usually 8 bytes (rarely used).
  • unsigned vs. signed

11
That example again
  • / Get a length in cm and convert to inches /
  • include ltstdio.hgt
  • int main()
  • double cm, inches
  • printf("Please enter length in centimeters ")
  • scanf("lf", cm)
  • inches cm / 2.54
  • printf("This is equal to g inches\n", inches)
  • return 0

12
printf and scanf
  • printf prints to the screen.
  • Can also accept variables and print their values.
  • scanf gets values from the standard input and
    assigns them to variables.

13
printf can print variable values
  • printf("zd\n", z)
  • The sequence d is a special sequence and is not
    printed!
  • It indicates to printf to print the value of an
    integer variable written after the printed string.

14
scanf gets input from the user
  • scanf("lf", cm)
  • This statement waits for the user to type in a
    double value, and stores it in the variable named
    cm.
  • To get 2 doubles from the user, use
    scanf("lflf", var1, var2)

15
prinft/scanf conversion codes
  • A ltconversion codegt in the printf/scanf string
    is replaced by the respective variable.
  • c a character
  • d an integer, u an unsigned integer.
  • f a float
  • lf a double
  • g a nicer way to show a double (in printf)
  • - the character (in printf)

16
One last time
  • / Get a length in cm and convert to inches /
  • include ltstdio.hgt
  • int main()
  • double cm, inches
  • printf("Please enter length in centimeters ")
  • scanf("lf",cm)
  • inches cm / 2.54
  • printf("This is equal to g inches\n", inches)
  • return 0

17
Exercise
  • Write a program that accepts as input -
  • The Dollar-Shekel exchange rate
  • An integer amount of dollars
  • and outputs -
  • The equivalent amount in Shekels

18
Solution
  • include ltstdio.hgt
  • int main()
  • double shekels, xchange
  • int dollars
  • printf("Enter the US-NIS exchange rate ")
  • scanf("lf", xchange)
  • printf("Enter the amount of dollars ")
  • scanf("d", dollars)
  • shekels dollars xchange
  • printf("d dollars g shekels\n", dollars,
    shekels)
  • return 0

19
The int type
  • The most common and efficient integer type.
  • Ranges from -2,147,483,648 to 2,147,483,647.
  • When you need to save memory and you are sure to
    use small numbers, use char or short.
  • When you need very big numbers, use long.
  • Otherwise, better use int.

20
The double type
  • The most efficient floating point type.
  • Range is
    2.2250710-308 1.7976910308
  • When you need to save memory/time, use float
    (whose range is 1.1754910-38
    3.402821038)
  • Otherwise, better use double.

21
Char is also a number!
  • A char variable is used to store a text
    character
  • Letters.
  • Digits.
  • Keyboard signs.
  • Non-printable characters.
  • But also small numbers (0 to 255 or -128 to 127).

22
Text as numbers
  • Every character is assigned a numeric code.
  • There are different sets of codes
  • ASCII (American Standard Code for Information
    Interchange) most common.
  • EBCDIC ancient, hardly used today.
  • Maybe others.
  • We will use ASCII.
  • The ASCII table.

23
The ASCII Table
24
More about character encoding
  • Most of the time, you don't care what the
    particular numbers are.
  • The table above shows only 128 characters (7
    bits). Some are non-printable.
  • Extended ASCII code contains 256 characters.

25
More about character encoding
  • ASCII code 0 (NULL character) is important we
    will see it again.
  • Note contiguous sets of numbers, upper case and
    lower case characters.

26
Example of char as both a character and a small
number
  • include ltstdio.hgt
  • int main()
  • char i 'b'
  • printf("i as a character is c\n", i)
  • printf("i as an integer is d\n", i)
  • printf("The character after c is c\n",
    i, i 1)
  • return 0

27
Another example
  • / Get the position of a letter in the abc /
  • include ltstdio.hgt
  • int main()
  • char letter
  • printf("Please enter a lowercase letter\n")
  • scanf("c", letter)
  • printf("The position of this letter in the abc
    is d\n", letter - 'a' 1)
  • return 0

28
Exercise
  • Write a program that accepts as input
  • A lowercase letter
  • and outputs
  • The same letter in uppercase
  • (e.g., if the input is g, the output should be
    G)

29
Solution
  • / Convert a letter to uppercase /
  • include ltstdio.hgt
  • int main()
  • char letter
  • printf("Please enter a lowercase letter\n")
  • scanf("c", letter)
  • printf("This letter in uppercase is c\n",
    letter - 'a' 'A')
  • return 0

30
Arithmetic operators
  • An operator is an action performed on something
    (e.g. constants, variables).
  • That something is called an operand.
  • Common operators
  • Assignment
  • Addition
  • Subtraction -
  • Multiplication
  • Division /
  • Modulo

31
Example
  • Arithmetic operators on variables - digits.c

32
Lets see how it works
num
sum
  • int main()
  • int sum, num
  • sum 0
  • printf("Enter 3-digits number\n")
  • scanf("d", num)
  • /extract the first digit/
  • sum sum num 10
  • num num / 10

1350
5198
Arbitrary numbers (garbage)
33
Lets see how it works
num
sum
  • int main()
  • int sum, num
  • sum 0
  • printf("Enter 3-digits number\n")
  • scanf("d", num)
  • /extract the first digit/
  • sum sum num 10
  • num num / 10

1350
5198
34
Lets see how it works
num
sum
  • int main()
  • int sum, num
  • sum 0
  • printf("Enter 3-digits number\n")
  • scanf("d", num)
  • /extract the first digit/
  • sum sum num 10
  • num num / 10

1350
0
35
Lets see how it works
num
sum
  • int main()
  • int sum, num
  • sum 0
  • printf("Enter 3-digits number\n")
  • scanf("d", num)
  • /extract the first digit/
  • sum sum num 10
  • num num / 10

1350
0
36
Lets see how it works
num
sum
  • int main()
  • int sum, num
  • sum 0
  • printf("Enter 3-digits number\n")
  • scanf("d", num)
  • /extract the first digit/
  • sum sum num 10
  • num num / 10

369
0
37
Lets see how it works
num
sum
  • int main()
  • int sum, num
  • sum 0
  • printf("Enter 3-digits number\n")
  • scanf("d", num)
  • /extract the first digit/
  • sum sum num 10
  • num num / 10

369
0
38
Lets see how it works
num
sum
  • int main()
  • int sum, num
  • sum 0
  • printf("Enter 3-digits number\n")
  • scanf("d", num)
  • /extract the first digit/
  • sum sum num 10
  • num num / 10

369
9
39
Lets see how it works
num
sum
  • int main()
  • int sum, num
  • sum 0
  • printf("Enter 3-digits number\n")
  • scanf("d", num)
  • /extract the first digit/
  • sum sum num 10
  • num num / 10

369
9
40
Lets see how it works
num
sum
  • int main()
  • int sum, num
  • sum 0
  • printf("Enter 3-digits number\n")
  • scanf("d", num)
  • /extract the first digit/
  • sum sum num 10
  • num num / 10

36
9
41
Lets see how it works
  • /extract the second digit/
  • sum sum num 10
  • num num / 10
  • /extract the third digit/
  • sum sum num 10
  • printf("The digits sum is d\n", sum)
  • return 0

num
sum
36
9
42
Lets see how it works
  • /extract the second digit/
  • sum sum num 10
  • num num / 10
  • /extract the third digit/
  • sum sum num 10
  • printf("The digits sum is d\n", sum)
  • return 0

num
sum
36
15
43
Lets see how it works
  • /extract the second digit/
  • sum sum num 10
  • num num / 10
  • /extract the third digit/
  • sum sum num 10
  • printf("The digits sum is d\n", sum)
  • return 0

num
sum
36
15
44
Lets see how it works
  • /extract the second digit/
  • sum sum num 10
  • num num / 10
  • /extract the third digit/
  • sum sum num 10
  • printf("The digits sum is d\n", sum)
  • return 0

num
sum
3
15
45
Lets see how it works
  • /extract the second digit/
  • sum sum num 10
  • num num / 10
  • /extract the third digit/
  • sum sum num 10
  • printf("The digits sum is d\n", sum)
  • return 0

num
sum
3
15
46
Lets see how it works
  • /extract the second digit/
  • sum sum num 10
  • num num / 10
  • /extract the third digit/
  • sum sum num 10
  • printf("The digits sum is d\n", sum)
  • return 0

num
sum
3
18
47
Operations with different types
  • When operands of two different types are involved
    in an operation, the operand of the weaker type
    is promoted to the other type (int ? float ?
    double).
  • The result of the operation is of the higher
    type.
  • When the operands are of the same type, the
    result is of that type as well.

48
Operations with different types
  • For example -
  • 3 4 7
  • 3.0 4 7.0
  • 3 / 4 0 !!!
  • 3.0 / 4 0.75

49
Operations with different types
  • Sometimes it is desirable for a variable of one
    type to be considered as belonging to another in
    an operation
  • We say the variable is cast to the new type.
  • The casting operator is of the form (type)
  • For example, (float)i casts the variable i to a
    float.

50
Casting variables
  • include ltstdio.hgt
  • int main()
  • int a1, b2
  • printf("d / d d\n", a, b, a/b)
  • printf("d / d g\n", a, b, (float)a / b)

51
Example find whats wrong
  • include ltstdio.hgt
  • int main()
  • int a 10
  • int b 20
  • printf("The average of d and d is d\n",
    a, b, (a b) (1 / 2))
  • return 0

52
Will this work?
  • include ltstdio.hgt
  • int main()
  • int a 10
  • int b 20
  • printf ("The average of d and d is d\n",
    a, b, (a b)(1.0 / 2))
  • return 0

53
The unsigned qualifier
  • Normally, the last bit of a variable serves as a
    sign bit.
  • In unsigned variables, it has the same role as an
    ordinary bit.
  • Because of the extra bit, the range of possible
    values is doubled but only for positive
    numbers.
  • For example, unsigned chars can range from 0 to
    255 while (signed) chars range from -128 to 127.
  • To declare a variable as unsigned, add the
    unsigned keyword before its type. For example
  • unsigned int ui

54
Overflow
  • Happens when a variable gets assigned a value
    that is outside of its range
  • This is equivalent to saying that the number of
    bits required to encode the value exceeds the
    number of bits in the variable
  • The value of the variable will usually be
    non-sense

55
Overflow An example
  • include ltstdio.hgt
  • int main()
  • int iA 1000
  • int iB 1000000
  • int iC 3000000
  • int iD 5000000
  • printf ("d d d\n", iA, iB, iAiB)
  • printf ("d d d\n", iA, iC, iAiC)
  • printf ("d d d\n", iA, iD, iAiD)
  • return 0

56
Thank you
  • See you next time!
Write a Comment
User Comments (0)
About PowerShow.com