CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.

Slides:



Advertisements
Similar presentations
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Advertisements

Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2.
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.
 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
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.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
Basic Input - Output. Output functions  printf() – is a library function that displays information on-screen. The statement can display a simple text.
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 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Input, Output, and Processing
© 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.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
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.
Khalid Rasheed Shaikh Computer Programming Theory 1.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
E-1 University of Washington Computer Programming I Lecture 5: Input and Output (I/O) © 2000 UW CSE.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Program Development Cycle 1.Edit program 2.Compile program - translates it from C to machine language 3. Run/execute your program. 4. If not satisfied,
 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
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
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 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
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.
+ Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
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.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
C Formatted Input/Output
CSCE 206 Structured Programming in C
Chapter Topics The Basics of a C++ Program Data Types
Chapter 2 - Introduction to C Programming
Lecture 7: Repeating a Known Number of Times
Getting Started with C.
Chapter 2 - Introduction to C Programming
By: Syed Shahrukh Haider
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.
Arrays, For loop While loop Do while loop
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.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C EECS May 2019.
Introduction to C Programming
Getting Started With Coding
Introduction to C Programming
Presentation transcript:

CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6

Do not Confuse between C and C++ C is a structured language C++ is an object oriented language C++ supports most of the features of structure language hence you may say C is a subset of C++ (with few exceptions that you need not to worry at this stage) CSC141 Introduction to Computer Programming

1.Declaration, definition and initialization of variables 2.User input/output functions scanf() and printf() 3.printf () Function 4.Format specifier 5.Field width specifier 6.Escape sequence 7.Scanf( ) function 8.Address operator 9.For loop Structure of the for loop Initialization, test, increment expression Body of the for loop Operation of the for loop Multiple statements in for loop Multiple initialization in a for loop CSC141 Introduction to Computer Programming

Defining and Declaring variables A variable declaration only specifies the name and type of the variable without allocating memory to it Declarations are important in multi file programs where a variable defined in one file must be referred to in a second file. Variable definition on the other hand specifies the name and type of the variable and also sets aside memory for it. When a data is assigned first time to the variable it is defined. (We will discuss it in detail when study functions) CSC141 Introduction to Computer Programming

Initializing variables Examples int num=2; int char=‘d’, float length= ; By initializing a variable we provide: Declaration of variable Definition of a variable Initial value loaded in the variable CSC141 Introduction to Computer Programming

User Input and output functions The printf() function is used to display output on the screen The scanf() function is used to read input from the keyboard printf() and scanf() both are defined in stdio.h called header file which we include using: # include CSC141 Introduction to Computer Programming

The printf() function Printf() is a function like main() It caused the string written within the quotes “ ” inside printf() function on the screen The string which is phrase in quotes is the function argument passed to printf() “Hello World” is a string of characters. C compiler recognizes a string when it is enclosed within quotes. CSC141 Introduction to Computer Programming

The definition of printf() and scanf() functions We have called the function printf() but have not defined it anywhere The declaration is done in the header file called stdio.h Its definition is is provided in a standard Library which we will study later on. The linker links our program with the ***.lib file at the time of linking. This happens for all C library functions Remember the name of a function is like variable name also called an identifier. CSC141 Introduction to Computer Programming

Exploring the printf() function Printing numbers (integers) void main (void) { printf (“Printing number: %d”, 10); } Here printf took two arguments: Printing number --- argument 1 10 ( what is that ? )--- argument 2 what is %d ---- Format specifier CSC141 Introduction to Computer Programming

Format Specifiers Tells the compiler where to put the value in string and what format to use for printing the value %d tells the compiler to print 10 as a decimal integer Why not write 10 directly inside the string? Possible !! However, to give printf() more capability of handling different values, we use format specifiers and variables CSC141 Introduction to Computer Programming

Different Format Specifiers Format specifiersused for %csingle character %sstring %dsigned decimal integer %ffloating point (decimal notation) %eexponential notation %g floating point (%f or %e whichever is shorter) %uunsigned decimal integer %xunsigned hexadecimal %ounsigned octal integer lprefix used with %d, %u, %x %o to specify long integer e.g. %ld CSC141 Introduction to Computer Programming

Field width Specifiers Format specifier: %(-)w.nf f is a format specifier used for float data type e.g %f.n means number of digits after decimal point e.g %.2f & %.3f means The digit (w) preceding the decimal point specifies the amount of space that number will use to get displayed: e.g printf (“Age is % 2d” 33);33 printf (“Age is % 4d” 33); printf (“Age is % 6d” 33); CSC141 Introduction to Computer Programming

Field width Specifiers void main (void) { printf(“8.1f %8.1f %8.1f\n”, 3.0, 12.5, 523.3); printf(“8.1f %8.1f %8.1f\n”, 300.0, , ); } ‘ ‘ ‘ ‘ ‘ ‘ CSC141 Introduction to Computer Programming

Field width Specifiers void main (void) { printf(“-8.1f %-8.1f %-8.1f\n”, 3.0, 12.5, 523.3); printf(“-8.1f %-8.1f %-8.1f\n”, 300.0, , ); } 3.0’‘12.5 ‘ ‘ ‘ ‘ ‘ ‘ CSC141 Introduction to Computer Programming

Escape Sequence \n prints carriage return and linefeed combination \t tab \b backspace \r carriage return \’ single quote \ “ double quote \ \ backslash CSC141 Introduction to Computer Programming

Printing Strings using printf () function void main (void) { printf(“%s is %d years old”,”Ahmed”,22); } CSC141 Introduction to Computer Programming

Printing characters using printf () function void main (void) { printf(“%c is pronounced as %s”, ’j’, ”jay”); } Single quotes are for character whereas the %c is format specifier Double quotes are for strings whereas the %s is format specifier CSC141 Introduction to Computer Programming

Scanf() function void main (void) { int userAge; printf(“Enter the the age: ”); scanf(“ %d”, &userAge); } scanf() reads in data and stores it in one or more variables. The first argument (the control string), contains a series of placeholders The remaining arguments to scanf() is a variable name proceed with & sign called address operator CSC141 Introduction to Computer Programming

scanf() can read data into multiple variables int a, b, float c; char d; scanf(“ %d %d %f %c”, &a, &b, &c, &d); Input is stored in these variables. Each variable name is preceded by &, &a means “the memory address associated with variable a” It uses any whitespace (space, newline or tab) character to delimit inputs in case of multiple inputs Format specifiers are like the ones that printf() uses e.g. %d = int, %f = float, %c = char, etc. CSC141 Introduction to Computer Programming

Comments /* */ multiline comments // single line comments CSC141 Introduction to Computer Programming

Loops Computer has the ability to perform set of instructions repeatedly This repetitive operation is performed using loops It involves repeating some portion of the program either a specified number of times or until a particular condition is being satisfied CSC141 Introduction to Computer Programming

Loops There are three types of loops in C –For loop –While loop –Do while loop For loop is the most popular looping instruction used by the programmers CSC141 Introduction to Computer Programming

For loop It specifies three things about a loop in one line –Setting a loop counter to an initial value –Testing the loop counter to determine whether its value has reached the number of desired repetitions –Increasing the value of loop counter each time the program segment within the loop has been executed CSC141 Introduction to Computer Programming

Flowchart of the for loop CSC141 Introduction to Computer Programming

For loop … continued The general form of for loop is: for (initialize counter; test counter; increment/decrement counter) { Do this; And this; } CSC141 Introduction to Computer Programming

Example: Print an integer number from -4 to 0 CSC141 Introduction to Computer Programming

Sample Program void main() { int j; for(j = -4; j <= 0 ; j = j + 1) printf("%d\n", j); } CSC141 Introduction to Computer Programming

print numbers from 1 to 10 void main( ) { int i ; for ( i = 1 ; i <= 10 ; i = i + 1 ) printf ( "%d\n", i ) ; } Instead of i = i + 1, the statements i++ or i += 1 can also be used. CSC141 Introduction to Computer Programming

void main( ) { int i ; for ( i = 1 ; i <= 10 ; ) { printf ( "%d\n", i ) ; i = i + 1 ; } Here, the increment is done within the body of the for loop and not in the for statement. Note that despite of this the semicolon after the condition is necessary. Print numbers from 1 to 10 CSC141 Introduction to Computer Programming

print numbers from 1 to 10 void main( ) { int i = 1 ; for ( ; i <= 10 ; i = i + 1 ) printf ( "%d\n", i ) ; } Here the initialisation is done in the declaration statement itself, but still the semicolon before the condition is necessary. CSC141 Introduction to Computer Programming

print numbers from 1 to 10 void main( ) { int i = 1 ; for ( ; i <= 10 ; ) { printf ( "%d\n", i ) ; i = i + 1 ; } Here, neither the initialisation, nor the incrementation is done in the for statement, but still the two semicolons are necessary. CSC141 Introduction to Computer Programming

print numbers from 1 to 10 void main( ) { int i ; for ( i = 0 ; i++ < 10 ; ) printf ( "%d\n", i ) ; } Here, the comparison as well as the incrementation is done through the same statement, i++ < 10. Since the ++ operator comes after i firstly comparison is done, followed by increment. Note that it is necessary to initialize i to 0. CSC141 Introduction to Computer Programming

void main( ) { int i ; for ( i = 0 ; ++i <= 10 ; ) printf ( "%d\n", i ) ; } Here, both, the comparison and the incrementation is done through the same statement, ++i <= 10. Since ++ precedes i firstly incrementation is done, followed by comparison. Note that it is necessary to initialize i to 0. print numbers from 1 to 10 CSC141 Introduction to Computer Programming

Nesting of Loops void r = 1 c = 1 sum = 2 r = 1 c = 2 sum = 3 r = 2 c = 1 sum = 3 r = 2 c = 2 sum = 4 r = 3 c = 1 sum = 4 r = 3 c = 2 sum = 5 CSC141 Introduction to Computer Programming

Multiple Initialisations in the for Loop The initialisation expression of the for loop can contain more than one statement separated by a comma. For example, for ( i = 1, j = 2 ; j <= 10 ; j++ ) Multiple statements can also be used in the incrementation expression of for loop; i.e., you can increment (or decrement) two or more variables at the same time. However, only one expression is allowed in the test expression. This expression may contain several conditions linked together using logical operators. CSC141 Introduction to Computer Programming

Solution to the last Practice Quiz Question -1: Solve the following expressions a) / 4b). 3 * / 2 void main( ) { printf ( “The result of the expression is %d \n", / 4) ; printf ( “The result of the expression is %d \n", (3 * / 2)); } CSC141 Introduction to Computer Programming

Solution to the last Practice Quiz Question - 2: If a=2, b=4, c=6, d=8, then what will be the result of the following expression? x = a * b + c * d / 4; void main( ) { int a =2, b=4, c=6, d=8; printf ( “The result of the expression is %d \n", x = a * b + c * d / 4) ; } CSC141 Introduction to Computer Programming