Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2 Overview of C.

Similar presentations


Presentation on theme: "Chapter 2 Overview of C."— Presentation transcript:

1 Chapter 2 Overview of C

2 A Simple C program #include <stdio.h> int main (void) { int num; printf(“Enter a number: “); scanf(“%d”, &num); num = num + 5; printf(“Your number plus five is: %d”, num); return 0; }

3 Preprocessor Directives (aka #include)
#include <stdio.h> Gives information to the compiler This code does not “run” Includes information about input/output functions printf scanf All of your programs will begin with this We will learn more about functions next week

4 The main function int main (void)
All your programs will start with this When you run your program, it always starts in main

5 Variable Declaration int num;
Sets aside a “box” in memory where your program can store and integer value Every variable declaration has a type, and a name Type is short for data type The name of our “box” is num, its type is int Every variable must be declared before it is used

6 Variables Types Different types require different space in memory int
Whole numbers (positive and negative) double Decimal values char Represents a single character Is actually a small number

7 Variables (continued)
Variable names Letters, digits, and underscores Cannot begin with a digit Case sensitive (miles, Miles) Some invalid identifiers 10x,

8 Output (Prompt) printf(“Enter a number: “); printf is a function call
Outputs data to the screen

9 Input scanf(“%d”, &num); Gets user input from the keyboard
“Wait for the user to press the return key, then read what they have typed in, interpret the keystrokes as an integer, then store the value in the box named num.” %d means “read in an integer” What about the “&”? Just use it for now, I’ll explain more in 6 weeks If you forget it, program will crash!!!

10 Computation num = num + 5;
“Take the value in the box named “num”, add 5 to that value, then put the new value back in the box.” = in C is not the same as in math Before a value is assigned to a variable, it contains junk Value on the left must be a variable, not an expression

11 Output (Results) printf(“Your number plus five is: %d”, num);

12 return 0; Signifies the end of “main” Compiler may warn you if you forget, but nothing bad will happen

13 printf printf(“Format String”, arg1, arg2, …);
Arguments are mapped to placeholders in the format string Placeholders are called format specifiers and tell the function how to interpret the data %c for char %d for int %f for double printf(“%c, %d, %f”, myChar, myDouble, myInt); What if there aren’t enough arguments passed? Incorrect format specifier? Junk data will be printed

14 scanf scanf(“Format String”, arg1, arg2, …);
Arguments are mapped to placeholders in the format string Placeholders tell the function how to interpret the data %c for char %d for int %lf for double scanf(“%c %d %lf”, &char1, &int1, &dbl1); What if there aren’t enough arguments passed or no &? Data put in random location in memory Will likely crash your program What about the &? Address of operator Tells the function to use the location, not the value

15 Another Simple Program
#include <stdio.h> int main(void) { double fCels = 0.0; double fFahr = 0.0; printf(“Welcome to Temmperature Conversion\n”); printf(“This converts Celsius to Fahrenheit\n”); printf(“Enter a temperature in degrees C:\n”); scanf(“%lf”, &fCels); /* The next line will compute the temp to F */ fFahr = ((9.0/5.0) * fCels) ; printf (“%f degrees C is %f degrees F\n”, fCels, fFahr); return (0); }

16 Things to Note Whitespace Semicolons Braces Comment /* This is a comment */ Format specifiers:


Download ppt "Chapter 2 Overview of C."

Similar presentations


Ads by Google