* c programming for the absolute beginner */ // by michael vine #include main() { printf(“\nc you...

34

Upload: vernon-williams

Post on 03-Jan-2016

241 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Page 2: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }

/* C Programming for the Absolute Beginner */

// by Michael Vine#include <stdio.h>#include <stdlib.h>main(){

printf(“\nC you later\n”);system(“pause”);

}

Page 3: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Page 4: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }

Serve to control program execution and functionality. Must end with a semicolon(;) with the exception of:

Comments: /* */ Preprocessor Directives: #include or

#define Begin and end program identifiers:

{ } Function definition beginnings: main()

Page 5: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }

Functions allow you to group program statements under one name

C is case-sensitive so main(), MAIN(), and Main() are all different

The main function is special because the values it returns are returned to the operating system

Most main functions in this course do not take or pass information to the operating system

Page 6: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }

Definition: Escape sequences are specially sequenced characters used to format output

\” Ex: printf(“ \ “This is quoted text \ “ “)

\’ Ex: printf(“ \n A single quote looks like \’ \n”);

\* *\ Comment Block

Page 7: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }

#include <stdio.h> Using a directive to include a header file

stdio.h = standard input output header file

stdlib.h = ‘system’ commands

Page 8: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }

A computer’s long-term memory is called nonvolatile memory and is generally associated with mass storage devices, such as hard drives.

A computer’s short term memory is called volatile memory. It loses is data when power is removed from the computer

Page 9: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }

To declare a constant (read only) value:const int x = 20const float PI = 3.14

Page 10: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }

TYPE SIZE VALUES

bool 1 byte true (1) or false (0)

char 1 byte ‘a’ to‘z’ , ‘A’ to ‘Z’, ‘0’ to ‘9’, space, tab, and so on

int 4 bytes -2,147,483,648 to 2,147,483,647

short 2 bytes -32,768 to 32,767

long 4 bytes -2,147,483,648 to 2,147,483,647

float 4 bytes + - (1.2 x 10^-38 to 3.4 x 10^38)

double 8 bytes +- (2.3 x 10^-308 to -1.7 x 10^308)

Page 11: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }

Can you explain what the code is doing?

Page 12: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }

Character - %c Integer - %d Float (decimal)- %f String - %s Printf Format Tags:

%[flags][width][.precision][length]specifier

%[.precision]specifer -> %.2f

Page 13: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }

int main(){ printf (“%c %c \n", 'a', 65); printf (" %10d \n", 1977); printf ("%010d \n", 1977); printf ("floats: %4.2f \n", 3.1416); printf ("%s \n", "A string"); printf(“%f \n”, 55.55);return 0; }

}

Page 14: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }

printf (“%c %c \n", 'a', 65); aAprintf (" %10d \n", 1977);

1977printf ("%010d \n", 1977);

0000001977printf ("floats: %4.2f \n", 3.1416); 3.14

printf ("%s \n", "A string"); A stringprintf(“%f \n”, 55.55); 55.550000

Page 15: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Page 16: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }

Syntaxscanf(“conversion specifier”, variable);

Page 17: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }

#include <stdio.h>

main(){

int iOperand1 = 0;int iOperand2 = 0;

printf(“\n Enter first operand: “);scanf(“%d”, &iOperand1);printf(“\n Enter second operand: “);scanf(“%d”, &iOperand2);

printf(“The result is %d \n”, 24/(iOperand1 * iOperand2)+6/3);

}

Page 18: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Page 19: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }

#include <stdio.h>

main(){

int x = 4;int y = 9;

int result1, result2;

result1 = y/x;result2 = y%x;

printf(“The result is %d.%d \n”, result1, 25*result2);

}

Page 20: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Page 21: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }

Do you know the answers to these?A. !( 1 || 0 ) B. !( 1 || 1 && 0 ) C. !( ( 1 || 0 ) && 0 )

Page 22: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }

A. !( 1 || 0 ) ANSWER: 0

B. !( 1 || 1 && 0 ) ANSWER: 0 (AND is evaluated before OR)

C. !( ( 1 || 0 ) && 0 ) ANSWER: 1 (Parenthesis are useful)

Page 23: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Page 24: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }

Can you write code that will ask a user to enter a number 1 , 2 , or 3 and print out the following:

Page 25: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }

#include <stdio.h>

int main(){ int a; printf (“Enter one of the following: %d, %d, or %d\n”, 1, 2, 3); scanf(“%d”, &a);if(a==1|| a==2|| a ==3){

if(a==1){printf(“\n %d is the loneliest number \n“, 1); }

if(a==2){printf(“\n%d is better than %d \n”,2,1);

}if(a==3){

printf(“\n%d \’ s a crowd \n”,3); }

elseprintf(“Sorry, you entered an invalid value\n”);

return 0; }

Page 26: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Page 27: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Page 28: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }

while ( condition ) { Code to execute while the condition is true }

Quiz: Can you write a program that prints xwhile x increments from 0 to 10?

Page 29: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Page 30: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }

x++; Tells the function to use the current value of x and increment it by 1.++x; Increment the value of x by 1 and use the new value for calculations.

x--; Tells the function to use the current value of x and decrease its value by 1.--x; Decrease the value of x by 1 and use the new value for calculations.

x=0;printf(“The Value of x is: %d”, x++);printf(“\n The Value of x is: %d”,++x);

Would results in: The Value of x is: 0The Value of x is: 2

Page 31: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }

Often used when the # of iterations is already known.

Contains 3 separate expressions:1. Variable initialization2. Conditional expression3. Increment/Decrement

Try writing a program with a for loop that counts down from 10 seconds.

Page 32: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }

#include <stdio.h>

main(){ int x=0; for(x=10; x>=0; x--) { printf("%d \n", x); } system("pause");}

Page 33: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }

break;Used to exit a loop. Once this statement is executed the program will execute the statement immediately following the end of the loop.

continue;Used to manipulate program flow in a loop. When executed, any remaining statements in the loop will be skipped and the next iteration of the loop will begin

Page 34: * C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }