41 A Depth Program #include int main(void) { int inches, feet, fathoms; //declarations fathoms = 7; feet = 6 * fathoms; inches = 12 * feet; printf(“Wreck.

Slides:



Advertisements
Similar presentations
11-2 Identify the parts of the “main” function, which include Preprocessor Directives main function header main function body which includes Declaration.
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
Programming in C– An Overview
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Display a 12-Month Calendar CS-2301 D-term Programming Assignment #2 12-Month Calendar CS-2301 System Programming C-term 2009 (Slides include materials.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
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
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
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 By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
13&14-2 Know the forms of loop statements in C (while,do/while,for). Understanding how data conversion occurs. Read/Write data in files using Unix redirection.
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Chapter 05 (Part III) Control Statements: Part II.
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
Repetition and Iteration ANSI-C. Repetition We need a control instruction to allows us to execute an statement or a set of statements as many times as.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
 Integers and Characters  Character Strings  Input and Output.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
S CCS 200: I NTERACTIVE I NPUT Lect. Napat Amphaiphan.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
CS 1704 Introduction to Data Structures and Software Engineering.
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.
Files A collection of related data treated as a unit. Two types Text
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Arithmetic Expressions
CSCE 206 Structured Programming in C
Formatted Input and Output
Chapter 4 C Program Control Part I
ECE Application Programming
Input/output.
Revision Lecture
File Access (7.5) CSE 2031 Fall July 2018.
Chapter 2 Overview of C.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Introduction to C CSE 2031 Fall /3/ :33 AM.
Programming in C An Overview
Lecture 13 & 14.
Programming in C Input / Output.
Input and Output Lecture 4.
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.
Programming in C Input / Output.
Introduction to CS Your First C Programs
INPUT & OUTPUT scanf & printf.
Variables in C Topics Naming Variables Declaring Variables
A First Book of ANSI C Fourth Edition
Programming in C Input / Output.
Variables in C Topics Naming Variables Declaring Variables
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Variables in C Topics Naming Variables Declaring Variables
Introduction to C Programming
Variables in C Topics Naming Variables Declaring Variables
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

41 A Depth Program #include int main(void) { int inches, feet, fathoms; //declarations fathoms = 7; feet = 6 * fathoms; inches = 12 * feet; printf(“Wreck of the Hesperus:\n”); printf(“Its depth at sea in different units:\n”); printf(“%d fathoms\n”, fathoms); printf(“%d feet\n”, feet); printf(“%d inches\n”, inches); } Wreck of the Hesperus: Its depth at sea in different units: 7 fathoms 42 feet 504 inches

42 Declarations A variable is declared to be of a type. This enables the compiler to: –Set aside the appropriate amount of memory to hold the variable. –Determines what operations are permissible with that variable. –What sort of data is allowed to be stored in that variable. All variables must be declared before they are assigned values and used in statements. Declaration end with a semicolon.

43 Assignment Allows the work and processing of data to achieved. variable; Variable = constant; function call; expression [contains any/all of above joined by operators]; Multiple assignments. Right to Left Association: 1. C = 0 A = B = C = 0;2. B = (C = 0) 3. A = (B = (C = 0))

44 Compound Assignments var operator = expression; var = var (operator)expression; General Form: Variable += Exp; Variable = Variable + Exp; Variable -= Exp Variable = Variable – Exp; Variable *= Exp Variable = Variable * Exp; Variable /= Exp Variable = Variable * Exp; Variable %= Exp Variable = Variable % Exp; x +=10; x = x +10; x -=100; x = x - 100;

45 Data Types: Preliminary info: see more in Chap. 6. Variables -Locations in memory we create which can hold data. Variables have a specific type. Type defines what sort of data is allowed to be stored in the variable. Integer Types:int, short int, long int Real Types:float, double, long double Text Types:Char1 keystroke (1 symbol) Stringmultiple characters

46 Initialization When variables are declared, they may also be initialized. Typically, constants or constant expressions are used to initialize a variable. General Syntax: type variableName = constant; A) Integer Constant: e.g., int x = 100; B) Real:1.63double 1.63Ffloat 1.63Llong double e.g.double x = 1.63;

47 C) Char : Single symbol in single quotes. e.g.Char Letter = ‘A’: D) String: Multiple symbols in double quotes. e.g. Char name[15] = “Mary”;

48 General Syntax: –scanf (“format string”, Arguments); Interactive Input: get data from user entering via keyboard. –fscanf (f, “format string”, Arguments); File Input: get data that is stored in file F. –sscanf (s, “format string”, Arguments); String Input: get data from string S. Return value: If error occurs, return value is EOF (usually -1), Otherwise returns the number of successful assignments of data to the arguments. Input Predefined Functions

49 Unsuccessful Input: –EOF (End of File) is reached before ALL arguments have been assigned data, if so the value EOF (-1) is returned. –Data Mismatch: The input data does not match the control string specifications. (e.g., indicated data would be integer and the data actually entered was REAL).

50 SCANf - Interactive Input –User enters data with keyboard when program interrupt occurs. –Input data treated as an input stream of characters. –Arguments are addresses. scanf(“%d”, &x);

51 #include int main(void) { char str[80]; printf("Enter a string: "); scanf(''%s", str); printf("Here's your string: %s", str); return 0; Why no “&” ? }

52 Output Predefined Functions General Syntax: –printf (“control string”, Arguments); Writes to terminal. –fprintf (f, “control string”, Arguments); Writes to another file. –sprintf (s, “control string”, Arguments); Writes to string s. Return Value: If EOF occurs, return value is - 1, Otherwise returns the number of successful assignments of data to the arguments.

53 Format Strings Contain Conversion Specification Characters. e.g., d - decimal data c - character f - floating point s - string printf("I like %c %s", 'C', "very much!") ; Contain Text: int Sum = 500; printf (“The Sum is %d”, Sum); New Line - printf (“hello, \n world”); Tab - printf (“hello, \t world \n”) The Sum is 500

54 ASIDE: scanffloat use %f doubleuse %lf printffloat or use %f double Format Specifiers (cont.)

55 Field Width Specification printf (“more numbers: %7.1f %7.2f %7.3f”, 4.0, 5.0, 6.0); Field width. Precision # of columns output. # of decimal digits to including dec. pt. right of decimal pt. more numbers:_ _ _ _ 4.0 _ _ _ 5.00 _ _ 6.000

56 Character Data: printf (“%c %5c/n %4c”, ‘x’, ‘y’, ‘z’); x_ _ _ _ y _ _ _z Field Width Specification (cont.)

57 Looping - Iteration Categories: Conditional Loops: while do-while Counting Loop : for While Statement: while (Expression) { Body of Loop } while (scanf(“%d”, &x)==1) { printf(“%d”, x); }

58 How Terminate Loop? Dummy Data Logic: –User enter a value that can’t be successfully converted. End-of-file Logic: –Indicate all data has been entered by typing EOF signal. UNIXctrl + d MS DOSctrl + z break - Exit from Loop immediately. continue - Jump to Bottom of Loop(stops just that single iteration).