CS111 Computer Programming

Slides:



Advertisements
Similar presentations
1 C++ Syntax and Semantics The Development Process.
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to C Programming
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Structure of a C program
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.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
CS150 Introduction to Computer Science 1
CSCI/CMPE 4341 Topic: Programming in Python Chapter 3: Control Structures (Part 1) – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg,
Introduction to C Programming
Basic Elements of C++ Chapter 2.
A First Book of ANSI C Fourth Edition
 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.
C Tokens Identifiers Keywords Constants Operators Special symbols.
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 Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 CSC103: Introduction to Computer and Programming Lecture No 6.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
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.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Constants, Variables and Data types in C The C character Set A character denotes any alphabet, digit or special symbol used to represent information.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Numbers in ‘C’ Two general categories: Integers Floats
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Tokens in C Keywords Identifiers Constants
ITEC113 Algorithms and Programming Techniques
Basic Elements of C++.
Data Types, Identifiers, and Expressions
Revision Lecture
ICS103 Programming in C Lecture 3: Introduction to C (2)
Java Programming: From Problem Analysis to Program Design, 4e
CS1101 Computational Engineering
Introduction to C Programming
INTRODUCTION c is a general purpose language which is very closely associated with UNIX for which it was developed in Bell Laboratories. Most of the programs.
Basic Elements of C++ Chapter 2.
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 CS Your First C Programs
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Lectures on Numerical Methods
Chapter 2 - Introduction to C Programming
WEEK-2.
Chapter 2 - Introduction to C Programming
Lexical Elements & Operators
Variables in C Topics Naming Variables Declaring Variables
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Presentation transcript:

CS111 Computer Programming C Variables and instructions

A tiny program /* A first program in C */ #include <stdio.h> A comment /* A first program in C */ #include <stdio.h> main( )‏ { printf(“Hello, World! \n”); } Library of standard input output functions Every C program starts execution with this function. Statement & terminator Escape sequence - newline Body of the function - enclosed in braces printf - a function from C Standard library stdio.h - prints a char string on the standard output 2

Programming Basics A variable – changes value during the execution of a program. A variable has a name, e.g. – name, value, speed, revsPerSec etc. Always referred to by its name Note: physical address changes from one run of the program to another. 3

Variables and Constants Names - made up of letters, digits and ‘_’ case sensitive: classSize and classsize are different maximum size: 31 chars - first character must be a letter - choose meaningful and self-documenting names MAX_PILLAR_RADIUS a constant pillarRadius a variable keywords are reserved: if, for, else, float, … 4

Assignments and variables The value of a variable is modified due to an assignment. The LHS is the variable to be modified and the RHS is the value to be assigned. So RHS is evaluated first and then assignment performed. a = 1 a = c a = MAX_PILLAR_RADIUS a = a*b + d/e 5

Variable Declaration Need to declare variables. A declaration: type variablename; Types: int, float, char int x; contents of the location corresponding to x is treated as an integer. Number of bytes assigned to a variable depends on its type. Assigning types helps write more correct programs. Automatic type checking can catch errors like integer = char +char; 6

Valid Variable declaraation Characters Allowed : Underscore(_) Capital Letters ( A – Z ) Small Letters ( a – z ) Digits ( 0 – 9 ) Blanks & Commas are not allowed No Special Symbols other than underscore(_) are allowed First Character should be alphabet or Underscore Variable name Should not be Reserved Word

Variables need Declaration Another simple C program #include<stdio.h> main() { int int_size; int chr_size; int flt_size; int_size = sizeof(int); chr_size =sizeof(char); flt_size = sizeof(float); printf(“int, char, and float use %d %d and %d bytes\n”, int_size, chr_size, flt_size); } 1 2 3 4 5 6 7 8 9 An operator in C 8

a + ((( b * c ) * d ) % e ) – (f / g ) Order of evaluation (operator precedence)‏ First parenthesized sub-expessions - innermost first Second *, / and % - left to right Third + and – - left to right a + b * c * d % e – f / g a + ((( b * c ) * d ) % e ) – (f / g ) Good practice -- use parentheses rather than rely on precedence rules -- better readability 9

Value = a * (b+c) % 5 + x / (3 + p) – r - j Precedence – another example Value = a * (b+c) % 5 + x / (3 + p) – r - j Evaluation order – (b+c) and (3+p) : due to parenthesis * and % and / have same precedence: a(b+c) is evaluated first, then mod 5. Also, x/(3+p). Then, the additions and subtractions are done from the left to right. Finally, the assignment of the RHS to LHS is done. 10

Primary Data Type Integral Type Integer Character Signed Unsigned int, short int, long int -- Signed Unsigned Floating Point Type float double long Double

Real Constant Fractional (234.456) Decimal Fractional Exponent (2.34456E5) Mantissa Exponent

Temperature Conversion Problem Algorithm Start Prompt user to enter temperature in F Store Fahrenheit temperature in [a variable] F Calculate Centigrade C = (F-32) * 5 / 9 Print C Stop 1. Start 2. Input Temperature in Fahrenheit in F variable. 3. Calculate Centigrade C = (F-32) x 5 / 9 4. Print temperature in centigrade 5. Stop

C Expressions Algebraic Expression C Expression a*b –c*d 3*x*x+2*x+5 (a+b+c)/(d+e)