Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to CS Your First C Programs

Similar presentations


Presentation on theme: "Introduction to CS Your First C Programs"— Presentation transcript:

1 Introduction to CS Your First C Programs

2 Technical details Visual Studio 6.0 Development environment
If you prefer to work at home, you can use Visual Studio C Express Edition (download installation from cours site)

3 Creating a program in C Write a program – a set of instructions
Compile and link Use a C compiler The compiler will do one of two things: print error messages and abort (most probably…) produce an executable program When compiler finished successfully – Run the program

4 Compilation and Linkage
Machine language with “holes” Compilation Source1.c Source1.obj Executable Linker Machine language with “holes” myprog.exe Compilation Source2.c Source2.obj

5 Our first C program /* HelloWorld – An example program */
#include <stdio.h> int main( ) { printf(“Hello world!\n”); return 0; } This is an instruction to the compiler to insert the contents of the file stdio.h to the program prior to compilation. This file contains information about the printf command. This tells the compiler we are about to define a block of commands called main. main is a special block – it is where the program starts running. This is a C statement. This statement executes the command printf, which causes text to be printed on the screen. Note that all C statements end with a semicolon (;). Yet another C statement. This one terminates the program and informs the operating system that it has ended successfully. This is a comment – starts with a /* and ends with a */. Comments are used to explain the program to human readers, and are ignored by the compiler. Curly braces indicate the beginning and end of a block of instructions. In this case we give the block the name main.

6 printf command Write formatted strings to the console
Escape sequences: \n - move to the next line on the console \\ - output a single \ \" - outputs a single "

7 Now, let’s get to work!!!

8 In-class assignment Write, compile and run the “Hello World” program.
Modify your program so that it prints: Your first name on one line. Your last name on the next line. Your age on the third line.

9 Solution - a name and age printing program
/* * This program prints my name on the * screen in two lines, and my age in a * third line */ #include <stdio.h> int main( ) { printf(“Dani\nCohen\n25\n”); return 0; }

10 Variables That’s how our program tells the computer that it needs memory. The memory is given a name. The command: int a; means give me enough memory to hold an integer number and name it a. If we want a to hold the number 3 we write: a=3;

11 How to print variables? printf is a command that can also print variables. How? We tell it what kind of variable to print Then we give the variable name If we want to print: a=<the value of variable a> we write: Tells the computer which integer variable to print Tells the computer to print an integer printf(“a=%d”,a); printf(“a=%d”,a); printf(“a=%d”,a); printf(“a=%d”,a); prints “a=“

12 Another Assignment Write a program that defines three integer variables day, month, and year Assign your birth day to the day variable, birth month to the month variable etc. Use the variables to print your birth date in the following format: dd-mm-yyyy

13 Solution - /* * This program prints my name on the * screen in two lines. */ #include <stdio.h> int main( ) { int year,month,day,sum; year=1981; month=1; day=31; sum=year*365+month*30+day; printf(“My Birthdate: %d/%d/%d\n”,day,month,year); printf(“Total of %d days from \n”,sum); return 0; }

14 What are variables? Our way of asking the operating system to allocate memory for our C program Each piece of allocated memory receives a name Our program uses this memory to save values that it wants to use later in the program

15 Variables in memory int my_int = 5; double my_double = 3.5; Memory
32 152 4 8

16 Variables in memory Whenever we write the variable name (e.g. 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 3.5 my_int my_double Memory 32 152 4 8 my_int=5 &my_int=32 my_double=3.5 &my_double=152

17 Program with variables
/* Get a length in cm and convert to inches */ #include <stdio.h> 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; }

18 Declaring variables in C
double cm, inches; By declaring a variable, we tell the computer that our program needs memory We may declare variables only at the beginning of a block The declaration first introduces the variable type, then its name When a variable is declared, its value is undefined

19 Example: variable declarations
int i; char c; float f1, f2; float f1=7.0, f2 = 5.2; unsigned int ui = 0;

20 Variable naming rules Letters, digits, underscores
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

21 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

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

23 That example again /* Get a length in cm and convert to inches */
#include <stdio.h> 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; }

24 printf and scanf printf – prints formatted data to the screen
scanf – reads data from the user stores the data in variables

25 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.

26 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("%lf%lf", &var1, &var2);

27 prinft/scanf conversion codes
A %<conversion code> in the printf/scanf string is replaced by the respective variable. %c – a character %d – an integer, %u – an unsigned integer. %f – a float %e – a float in different representation %g – chooses between %f and %e %lf – a real number – for scanf %% - the ‘%’ character (printf) Only for printf!

28 One last time /* Get a length in cm and convert to inches */
#include <stdio.h> 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; }

29 Exercise Write a program that accepts as input - and outputs -
The Dollar-Shekel exchange rate (real number) An integer amount of dollars and outputs - The equivalent amount in Shekels

30 For Example

31 Solution #include <stdio.h> int main() {
double shekels, /* (OUT) Result of conversion */ xrate; /* (IN) US$-NIS exchange rate */ int dollars; /* (IN) number of US$ to convert */ printf("Enter the US$-NIS exchange rate: "); scanf("%lf", &xrate); printf("Enter the amount of dollars to convert: "); scanf("%d", &dollars); shekels = dollars * xrate; printf("%d dollars equals %g shekels\n", dollars, shekels); return 0; }

32 See you next time


Download ppt "Introduction to CS Your First C Programs"

Similar presentations


Ads by Google