7.1.2001Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.

Slides:



Advertisements
Similar presentations
Chapter 11 Introduction to Programming in C
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Structure of a C program
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
C Programming Lecture 4 : Variables , Data Types
C Tokens Identifiers Keywords Constants Operators Special symbols.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
CSCI 130 Chapter 3. Variables & Names Variable Declarations: –reserve a storage location in memory –identify the name of the variable –identify the type.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Introduction to Programming
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 4.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 2.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
C Building Block Chapter 2. Variables A variable is a space in the computer’s memory set aside for a certain kind of data and given a name for easy reference.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
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.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 1.2 Introduction to C++ Programming
Introduction to the C Language
BASIC ELEMENTS OF A COMPUTER PROGRAM
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
Introduction to the C Language
Chapter 2 - Introduction to C Programming
Chapter 11 Introduction to Programming in C
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
C++ Programming Lecture 3 C++ Basics – Part I
Comments Any string of symbols placed between the delimiters /* and */. Can span multiple lines Can’t be nested! Be careful. /* /* /* Hi */ is an example.
Chapter 2 - Introduction to C Programming
Variables in C Topics Naming Variables Declaring Variables
Introduction to C Programming
Getting Started With Coding
Presentation transcript:

Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3

Sudeshna Sarkar, IIT Kharagpur 2 /* Program Name : countdown Description : This program prompts the user to type in a positive number and counts down from that number to 0, displaying each number */ #include #define STOP 0 main () { int counter ; /* Holds intermediate count value */ int startPoint ; /* Starting point for countdown */ /* Prompt the user for input */ printf ("Enter a positive number : ") ; scanf ("%d", &startPoint) ; for (counter=startPoint; counter >=STOP;counter--) printf ("%d\n", counter) ; }

Sudeshna Sarkar, IIT Kharagpur 3 intro]$ cc t1.c intro]$./a.out Enter a positive number : intro]$

Sudeshna Sarkar, IIT Kharagpur 4 The first C program #include void main () { printf ("Hello, World! \n") ; } All programs run from the main function printf is a function in the library stdio.h To include any library use #include

Sudeshna Sarkar, IIT Kharagpur 5 Second C program #include void main() { int x = 1, y; int sum; y = 3; sum = x + y; /* adds x to y, places value in variable sum */ printf( “%d plus %d is %d\n”, x, y, sum ); }

Sudeshna Sarkar, IIT Kharagpur 6 Comments Any string of symbols placed between the delimiters /* and */. Can span multiple lines Can’not be nested! Be careful. /* /* /* Hi */ is an example of a comment. /* Hi */ */ is going to generate a parse error

Sudeshna Sarkar, IIT Kharagpur 7 Keywords Reserved words that cannot be used as variable names OK within comments... Examples: break, if, else, do, for, while, int, void Exhaustive list in any C book

Sudeshna Sarkar, IIT Kharagpur 8 Identifiers • A token (word) composed of a sequence of letters, digits, and underscore (_) character. (NO spaces.) First character cannot be a digit C is case sensitive, so beware (e.g. printf  Printf) • Identifiers such as printf normally would not be redefined; be careful • Used to give names to variables, functions, etc. • Only the first 31 characters matter

Sudeshna Sarkar, IIT Kharagpur 9 Constants 0, 77, 3.14 examples. Strings: double quotes. “Hello” Characters: single quotes. ‘a’, ‘z’ Have types implicitly associated with them too large for most machines

Sudeshna Sarkar, IIT Kharagpur 10 Simple Data Types Void Integer types (signed or unsigned): char, short int, int, long int char is an 8 bit (=1 byte) number Floating-point types: float, double, long double No boolean types Use 0=False and anything else(usually 1)=True

Sudeshna Sarkar, IIT Kharagpur 11 Input and Output printf : performs output to the standard output device (typically defined to be the monitor) It requires a format string to which we can provide The text to print out Specifications on how to print the values printf ("The number is %d.\n", num) ; The format specification %d causes the value listed after the format string to be embedded in the output as a decimal number in place of %d.

Sudeshna Sarkar, IIT Kharagpur 12 Input scanf : performs input from the standard input device, which is the keyboard by default. It requires a format string and a list of variables into which the value received from the input device will be stored. scanf ("%d", &size) ; scanf ("%c", &nextchar) ; scanf ("%f", &length) ;

Sudeshna Sarkar, IIT Kharagpur 13 Variables Variables hold the values upon which a program acts. They are the symbolic reference to values. The following declares a variable that will contain an integer value. int num_of_students ; The compiler reserves an integer's worth of memory for num_of_students In C, all variables must be declared before they can be used.

Sudeshna Sarkar, IIT Kharagpur 14 A variable declaration conveys three pieces of information the variable's identifier its type its scope - the region of the program in which the variable is accessible. (implicitly specified by the place in the code where the declaration occurs.)

Sudeshna Sarkar, IIT Kharagpur 15 #include main () { int num_of_students ; scanf ("%d", &num_of_students) ; printf ("%d\n", num_of_students) ; } C Program # 3

Sudeshna Sarkar, IIT Kharagpur 16 Sample C program #4 #include #define PI /* Compute the area of a circle */ main() { float radius, area; float myfunc (float radius); scanf (“%f”, &radius); area = myfunc (radius); printf (“\n Area is %f \n”, area); } float myfunc (float r) { float a; a = PI * r * r; /* return result */ return (a); }

Sudeshna Sarkar, IIT Kharagpur 17 Operators Operators are used to manipulate variables. They perform arithmetic logic functions comparisons between values int x = 6 ; int y = 9; int z, w; z = x + y ; w = x * y ;

Sudeshna Sarkar, IIT Kharagpur 18 Expressions and statements Expressions : combine constants and variables with operators x * y Expressions can be grouped to form statements z = x * y ; Semicolons terminate statements One or more simple sentences can be grouped to form a compound sentence or a block by enclosing within { }