Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tarik Booker CS 290 California State University, Los Angeles.

Similar presentations


Presentation on theme: "Tarik Booker CS 290 California State University, Los Angeles."— Presentation transcript:

1 Tarik Booker CS 290 California State University, Los Angeles

2  This class is taught in the C language  The book is written in the C++ language ◦ Not (exactly) the same language ◦ Very similar  Look at lecture first before looking at book.

3  Introduction to C  Variables  Data Types  Intro to C

4  You should now have: ◦ Code::Blocks working and accessible  (Code:Blocks or Xcode for Mac users) ◦ The basic “Hello World” sample program that came with the Code::Blocks

5  Lets look at that main.c program.

6 File Headers Main Function C Code

7  The simplest program in C: int main() { }  Doesn’t do anything!!!

8 int main() { }  What does this mean? ◦ Main = most basic function ◦ Function = piece of written code  “Our” function  Doesn’t do anything

9  Functions: ◦ Pieces of code that run ◦ Main  Special function that runs first!  All C programs must have a main function  The type that is before specifies a return type (the type of value the function should result in)  Normally main should return some value (using the return keyword), but C lets you omit it in main

10 int main() { }  Curly Braces ◦ {} ◦ Signals the beginning and end of functions ◦ Also other code blocks  Note: Nothing in between curly braces here, so nothing happens (executes)

11  Back to main.c ◦ “Hello World” is the most basic program  Main function  Now there’s code  File Headers File Headers Main Function C Code

12  Include statements  “Header” files ◦ Compiler copies and pastes everything in the header file to your code ◦ Access to basic functions  Stdio  Standard input/output functions  Stdlib  Standard library functions ◦ Can even create your own header files!

13  Using a file header: ◦ Use #include  Put the header name between the <>  These are really “.h” files  Built in files omit.h  Non-standard files are different:  #include “math.h”

14  Now we know: ◦ Main Function ◦ File Headers  Printf? File Headers Main Function C Code

15  Print formatted data ◦ To Standard Out (stdio) ◦ Prints a formatted String (characters and text) to the screen  printf(“This should display in my results.”)  The backslash-n (\n) will print a new line  \ - called an escape character  “Escape” from the current mode

16  What do these display?  printf(“Hello there!”);  printf(“Hi! \n My\n Name\n Is…”);

17  Can store values to be used (later) in a program ◦ Values can be changed ◦ Remember algebra:  x + y = 34  y = 5  x =? ◦ Similar Concept in Programming

18  You must declare a variable in a program before you use it ◦ Format:  datatype variableName;  A variable can be of any name  Follow identifier rules (later…)  Descriptive names are best

19  Datatype variableName;  Datatype variableName1, variableName2, …,variableName3;  Datatype  Specific classification of variables  Different types / different memory sizes  int, double (main ones)  Discuss later

20  To name variables or functions ◦ Called identifiers  Should obey rules:  Consists of letters (upper/lower), digits, underscores  Must start with a letter (alphabetic character)  Must NOT start with a digit!!!  Cannot be a reserved word (int, return, main, etc.)  Can be up to 31 characters

21  Which are legal?  helloworld  aadzxvcna343546390thjke456hftg3sd  HappyHappy  34joy56  $Dollarsand  return  mains  #love  Why / Why not?

22  After declaring a variable, you must assign it a value. ◦ Use the assignment operator (=)  int myNumber;  myNumber = 1;  Assigns the value 1 to variable myNumber  Note: You can assign a value to a variable in the variable declaration statement!!!  Ex:  int myNumber = 1;  double volume = 3.0;

23  Assigning a value to a variable results in an assignment statement ◦ double volume = 5.0;  You can also assign the results of an expression to a variable ◦ double volume = 5.0 * 3.0; ◦ int x = 5 * (3 / 2);  Either method initializes the variable ◦ Should initialize variable after (during) you declare it!

24  You can use other variables inside of expressions! ◦ Can also use the same variable! ◦ int x = 1; ◦ x = x + 1;  Executes Right-Hand Side (RHS) first, then assigns the result to the Left-Hand Side (LHS)  What is x after the code?

25  Variable values can change during a program  (Named) Constants are permanent (throughout the life of the program) ◦ You can define constants ◦ Use the const keyword (either before or after the datatype)  const datatype constantname = value;  datatype const constantname = value;  Ex:  const double PI = 3.14159;  int const x = 5;

26  (Numeric) Data Types represent different ways to store numeric values in a variable ◦ Variables are stored in (primary) memory ◦ Different data types take up different spaces in memory

27  Integer Data Types ◦ Used to store integers  …, -10, -9, …, -3, -2, -1, 0, 1, 2, 3, …, 9, 10, …  int  long ◦ Many more (will discuss later…)  Floating-Point Data Types ◦ Written in Scientific Notation  Mantissa Exponent  double  float  (Will discuss later…)

28  C includes standard arithmetic operators  Addition+  Subtraction-  Multiplication*  Division/

29  Also has modulus (%)  Also called the remainder operator  Gives the remainder after division  6%2 (Say 6 “mod” 2)  What is 6%2?  3%1  1%3  10%5  Any even number % 2?  Any odd number % 2?  Can use this to check evens or odds

30  Use the pow(a,b) function ◦ Requires double type ◦ Returns a b ◦ Must include math.h  #include ◦ Ex:  double y=1.0;  y = pow(2.0,3.0);

31  Literal – Constant values that appear in a program ◦ Ex:int numberOfYears = 34; ◦ Or:double weight = 0.305  34 and 0.305 are literals ◦ Different types of literals  Integer  Floating-Point

32  Integer Literals: ◦ Can be assigned to an integer variable ◦ Works so long as it is within the range of the variable type ◦ Assumed to be type int (32-bit range) ◦ For type long, append the letter L to it  Ex: 2145063967L

33  Can also use binary integer literals  Start with 0B or 0b (zero b)  Ex: 0B1111results in 15  Also can use octal (base 8) and hexadecimal (base 16) integer literals  Octal: Start with 0  Ex: 07777results in 4095  Hex: Start with 0x or 0X (zero x)  Ex: 0XFFFFresults in 65535

34  Floating-Point Literals ◦ Written with a decimal point  Ex: 5.0 ◦ Default is double  Make literal float by adding an f  Ex: 100.2f  Note: double has more significant digits (15-18)than float (7-8)

35  You must use the percent sign (%) when printing a variable ◦ Use the percent sign and a code for the type  %d  integer  %lf  double  %f  float ◦ Use the code within the String, then after, use a comma, and the variable name

36  Ex: ◦ int x = 1; ◦ printf(“The result is: %d”,x); ◦ double y = 3.0; ◦ printf(“The result is: %lf”,y);

37

38  What’s the order of operations? ◦ PEMDAS ◦ Please Excuse my Dear Aunt Sally ◦ Parentheses, Exponent, Multiplication, Division, Addition, Subtraction ◦ Equal operators (M, D, and A,S) go from left to right

39  You can combine arithmetic operators and the assignment operator to perform augmented operators!!  Ex: x = x + 1;  1 is added to x, and the result is put back into itself  Will happen often (later)  Can shorten this to:  x += 1;  This is translated as x = x + 1;  Works for other operators

40  Addition ◦ x += 1;  Result:x = x + 1;  Subtraction ◦ x -= 5;  Result:x = x – 5;  Multiplication ◦ y *= 3;  Result: y = y * 3;  Division ◦ volume /= 7;  Result:volume = volume / 7;  Remainder ◦ i %= 5;  Result:i = i % 5;

41  Shorthands for incrementing and decrementing variables; ◦ ++, - - ◦ Adds (++) or Subtracts (--) by one  Ex: int i = 3;  i++;  Result?  Ex:int z = 4;  z--; ◦ Can put before (++i) or after variable (z--)

42  If used alone (i++, or --z) direction doesn’t matter. ◦ Ex:++j; ◦ or:j++;// Doesn’t matter  If used within an expression (or statement), direction does matter

43  Preincrement / Predecrement ◦ Ex: ++var--count ◦ If used within an expression, the variable is incremented (or decremented) first, then used in the statement ◦ Ex:int j = ++i;  If i is 2, what is j?

44  Postincrement / Postdecrement ◦ Ex: var++ count-- ◦ If used within an expression, the variable is used in the statement first, then incremented (or decremented) afterward ◦ Ex:int j = i++;  If i is 2, what is j?  What if i is 6?


Download ppt "Tarik Booker CS 290 California State University, Los Angeles."

Similar presentations


Ads by Google