Introduction to CS Your First C Programs

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
C Programming for engineers Teaching assistant: Ben Sandbank Home page:
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
Programming The Development Environment and Your First C Program.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
Introduction to C Programming
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
Basic Input/Output and Variables Ethan Cerami New York
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables.
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Week 1 Algorithmization and Programming Languages.
CSCI 171 Presentation 2. Program Components main() #include Variable Definition Function Prototype Program Statements Function Call Function Definition.
1 C Programming Week 2 Variables, flow control and the Debugger.
E-1 University of Washington Computer Programming I Lecture 5: Input and Output (I/O) © 2000 UW CSE.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Computer Programming A simple example /* HelloWorld: A simple C program */ #include int main (void) { printf (“Hello world!\n”); return.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Lecture2.
Chapter 1.2 Introduction to C++ Programming
CSCE 206 Structured Programming in C
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Computing Fundamentals
Revision Lecture
Chapter 2 Overview of C.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Getting Started with C.
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Administrative things
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Computer Science 210 Computer Organization
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
פרטים נוספים בסילבוס של הקורס
Chapter 2 - Introduction to C Programming
פרטים נוספים בסילבוס של הקורס
CS111 Computer Programming
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
Lecture3.
Chapter 2: Introduction to C++.
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
Unit 3: Variables in Java
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
Getting Started With Coding
Presentation transcript:

Introduction to CS Your First C Programs http://www.cs.tau.ac.il/courses/cs4math/10b

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

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

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

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.

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 "

Now, let’s get to work!!!

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.

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; }

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;

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=“

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

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 00-00-0000\n”,sum); return 0; }

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

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

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

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; }

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

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

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

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

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

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; }

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

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("%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);

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!

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; }

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

For Example

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; }

See you next time