c programming for engineers n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
C Programming for engineers PowerPoint Presentation
Download Presentation
C Programming for engineers

Loading in 2 Seconds...

play fullscreen
1 / 35

C Programming for engineers - PowerPoint PPT Presentation


  • 80 Views
  • Uploaded on

C Programming for engineers. Teaching assistant: Ben Sandbank e-mail: sandban@post.tau.ac.il Home page: http://www.cs.tau.ac.il/~sandban Office hours: Wednesday, 16:00-17:00, room 19 in the basement of the Schreiber building Office phone: 03-6405378. Recommended books.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'C Programming for engineers' - simone-strong


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
c programming for engineers
C Programming for engineers
  • Teaching assistant: Ben Sandbank
  • e-mail:sandban@post.tau.ac.il
  • Home page: http://www.cs.tau.ac.il/~sandban
  • Office hours: Wednesday, 16:00-17:00, room 19 in the basement of the Schreiber building
  • Office phone: 03-6405378
recommended books
Recommended books
  • The C Programming Language (second edition - ANSI C), Brian W. Kernighan and Dennis M. Ritchie, Prentice Hall, Inc., 1988.
  • A Book on C (third or fourth edition), Al Kelley and Ira Pohl, Addison-Wesley, 1997.
  • These and other C programming books can be found in the library under 519.83
c programming for engineers1
C programming for Engineers
  • Lcc compiler – a free c compiler available on the web.
  • http://www.cs.virginia.edu/~lcc-win32/
  • Some instructions
what are variables
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.
example
Example

/* Get a length in cm and convert to inches */

#include <stdio.h>

int main(void)

{

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;

}

declaring variables in c
Declaring variables in C
  • Before using a variable, one must declare it.
  • The declaration first introduces the variable type, then its name.
  • Optionally, you can set some characteristics called qualifiers.
  • When a variable is declared, its value is undefined.
example variable declarations
Example Variable Declarations
  • int i;
  • char c;
  • float f1, f2;
  • float f1=7.0, f2 = 5.2;
  • unsignedint ui = 0;
variable naming rules
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
data types in c
Data types in C
  • char – a single byte character.
  • short int (or just short) – an integer number, usually 2 bytes (rarely used).
  • int - an integer number – usually 4 bytes.
  • long int (or just long) – an integer number, 4 or 8 bytes (rarely used).
  • float – a single precision real number – usually 4 bytes.
  • double – a double precision real number – usually 8 bytes.
  • long double - a double precision real number – usually 8 bytes (rarely used).
  • Signed vs. unsigned
that example again
That example again

/* Get a length in cm and convert to inches */

#include <stdio.h>

int main(void)

{

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;

}

printf and scanf
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.
printf can print variable values
printf can print variable values

printf ("z=%d\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.
scanf gets input from the user
scanf gets input from the user

scanf("%lf", &var);

  • This statement waits for the user to type in a double value, and stores it in the variable named ‘var’.
  • The meaning of ‘&’ will be explained in the future…
  • To get 2 doubles from the user, use –scanf("%lf%lf", &var1, &var2);
prinft scanf conversion codes
prinft/scanf conversion codes
  • A %<conversion code> in the printf/scanf string is replaced by a 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)
one last time
One last time

/* Get a length in cm and convert to inches */

#include <stdio.h>

int main(void)

{

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;

}

exercise
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
solution
Solution

#include <stdio.h>

int main(void)

{

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;

}

char is also a number
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).
text as numbers
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.
more about character encoding
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.
more about character encoding1
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.
example of char as both a character and a small number
Example of char as both a character and a small number

#include <stdio.h>

int main(void)

{

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;

}

another example
Another example

/* Get the position of a letter in the abc */

#include <stdio.h>

int main(void)

{

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;

}

exercise1
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’)

solution1
Solution

/* Convert a letter to uppercase */

#include <stdio.h>

int main(void)

{

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;

}

arithmetic operators
Arithmetic operators
  • An operator is an action (in our case, usually a mathematical one) performed on something (e.g. constants, variables).
  • That “something” is called an operand.
  • Common operators:
    • Braces ()
    • Assignment =
    • Addition +
    • Subtraction -
    • Multiplication *
    • Division /
    • Modulo %
example1

Example

Arithmetic operators on variables - digits.c

operations with different types
Operations with different types
  • When operands of two different types are involved in an operation, the operand of the ‘lesser’ 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.
operations with different types1
Operations with different types

For example -

  • 3 + 4 = 7
  • 3.0 + 4 = 7.0
  • 3 / 4 =
  • 3.0 / 4 = 0.75

0

casting variables
Casting variables
  • 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.
casting variables1
Casting variables

#include <stdio.h>

int main(void)

{

int a=1, b=2;

printf("%d / %d = %d\n", a, b, a/b);

printf("%d / %d = %g\n", a, b, (float)a / b);

return 0;

}

example find what s wrong
Example – find what’s wrong

#include <stdio.h>

int main(void)

{

int a = 10;

int b = 20;

printf("The average of %d and %d is %d\n", a, b, (a + b) * (1 / 2));

return 0;

}

will this work
Will this work?

#include <stdio.h>

int main(void)

{

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;

}

exercise2
Exercise
  • Write a program with such that -
    • Input - a 3 digit number
    • Output – the same number in reverse order
    • For example, if the input is 123, the output should be 321
solution2
Solution
  • Reverse_digits.c