C Program. Output. Hi everyone. #include <stdio.h> main () { printf ( Hi everyone\n ); }

Similar documents
Lecture_Module3 Prof Saroj Kaushik, IIT Delhi

Data Types and Variables in C language

Operators and Expressions:

C - Basic Introduction

Lecture 3. More About C

sends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ;

COMP 208 Computers in Engineering

4.1. Structured program development Overview of C language

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Basics of Programming

Computer System and programming in C

UNIT- 3 Introduction to C++

Programming for Engineers Iteration

C: How to Program. Week /Mar/05

Reserved Words and Identifiers

PESIT Bangalore South Campus Hosur Road (1km before Electronic City), Bengaluru Department of Basic Science and Humanities

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Chapter 2 - Introduction to C Programming

CSI33 Data Structures

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Lecture 6. Statements

Fundamental of Programming (C)

UNIT 3 OPERATORS. [Marks- 12]

Computers in Engineering. Moving From Fortran to C Michael A. Hawker

Basic Assignment and Arithmetic Operators

Java Notes. 10th ICSE. Saravanan Ganesh

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

The detail of sea.c [1] #include <stdio.h> The preprocessor inserts the header file stdio.h at the point.

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

DECISION CONTROL AND LOOPING STATEMENTS

A Fast Review of C Essentials Part I

C-LANGUAGE CURRICULAM

Unit 3. Operators. School of Science and Technology INTRODUCTION

Programming Language A

P.E.S. INSTITUTE OF TECHNOLOGY BANGALORE SOUTH CAMPUS 1 ST INTERNAL ASSESMENT TEST (SCEME AND SOLUTIONS)

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Lecture 3 Tao Wang 1

Module 4: Decision-making and forming loops

Decision Making -Branching. Class Incharge: S. Sasirekha

MODULE 2: Branching and Looping

WARM UP LESSONS BARE BASICS

1.3b Type Conversion

Data Type Fall 2014 Jinkyu Jeong

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)

Computer Programming CS F111

C Programming

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme]

Programming and Data Structures

Preview from Notesale.co.uk Page 6 of 52

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

C Programming Multiple. Choice

Unit 7. Functions. Need of User Defined Functions

Course Outline Introduction to C-Programming

LESSON 6 FLOW OF CONTROL

Programming for Engineers Introduction to C

Computers in Engineering. Moving From Fortran to C Part 2 Michael A. Hawker

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

The C language. Introductory course #1

Chapter 7. Basic Types

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

ANSI C Programming Simple Programs

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

Computers in Engineering COMP 208. Roots of a Quadratic in C 10/25/2007. Moving From Fortran to C Part 2 Michael A. Hawker

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Flow Control. CSC215 Lecture

Full file at C How to Program, 6/e Multiple Choice Test Bank

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Unit 5. Decision Making and Looping. School of Science and Technology INTRODUCTION

Conditional Statement

Chapter 2: Overview of C. Problem Solving & Program Design in C

The C Programming Language Part 2. (with material from Dr. Bin Ren, William & Mary Computer Science)

BASIC ELEMENTS OF A COMPUTER PROGRAM

Operators in C. Staff Incharge: S.Sasirekha

C++ Programming: From Problem Analysis to Program Design, Third Edition

Iosif Ignat, Marius Joldoș Laboratory Guide 4. Statements. STATEMENTS in C

n Group of statements that are executed repeatedly while some condition remains true

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Lesson 3 Introduction to Programming in C

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

C Programming Class I

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

Introduction to C. Systems Programming Concepts

Fundamental of C programming. - Ompal Singh

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

Introduction. C provides two styles of flow control:

Transcription:

C Program Output #include <stdio.h> main () { printf ( Hi everyone\n ); Hi everyone

#include <stdio.h> main () { printf ( Hi everyone\n ); #include and main are Keywords (or Reserved Words) Reserved Words have special meaning in Programming Languages Keywords can t be used for any other purpose.

#include <stdio.h> main () { printf ( Hi everyone\n ); Contains Utilities for Input and Output Include information from Standard Library Use utility from Standard Library Not part of C Language but library exists with all C installations

#include <stdio.h> main () { printf ( Hi everyone\n ); Each of H, i, e, etc. is a character String Sequence of characters in... Causes Hi everyone to be printed on output device (monitor) \n means new line ; terminates a statement

C Program #include <stdio.h> main () { printf ( hello\n ); printf ( all\n ); hello all Output

C Program Output #include <stdio.h> main () { printf ( hello everyone\n ); printf ( Best wishes\n ); Identical Hello everyone Best wishes #include <stdio.h> main () { printf ( hello everyone\nbest wishes\n );

C Program #include <stdio.h> main () { printf ( hello ); printf ( everyone\n ); or #include <stdio.h> main () { printf ( hello everyone\n ); Output Hello everyone What happens when you omit the trailing \n?

General Structure of C program /* Include files for input/output functions*/ #include <stdio.h> /* function definitions*/ function definitions /* Main function- each declarations and statements in a function are separated by semi colon */ main() { /* variables; arrays; records; function declarations etc */ declarations /* Each statement is separated by semi colon*/ statements

Comments are enclosed within /* --------- */ Contents within /* and */ are ignored by compiler Used for informal description and documentation for readability explain design decisions Comments do NOT increase size of executable program One line comment can precede // main () { float inc, tax;... tax = (inc - 30) * 0.1; /* according to income tax table */ // Computing Tax Comment

#include statements used to include the header file for input/output stdio.h, the standard mathematics library math.h etc. These files are enclosed within < > #define helps in defining constant symbol. compiler replaces all occurrences of M by 5. Different from variable. # M 5; main () { int x; x= 10+M; printf(x);

Variables and Constants are basic data objects a constant value does not change during program execution variables may change values any number of times Variables are associated with a Type int (integer) float, double (has fractional part) char (character) Type is declared first before variable is used main () { int a, b, c; float d, e;...

Standard: int, float, char, double User defined datatypes: arrays, structures, pointers, enumerated datatype etc.

Form of Declaration: type list of variables; /* each separated by, and finally terminated by ; */ Examples: int x, y, z; float p, q[3][4]; array char name[20]; char ch = A ; /* character is enclosed within */

#include <stdio.h> #define i 6 main() { /* integer declaration */ int x, y; /* Assignment statements */ x=7; y= i + x; /* output statement */ printf("%d\n", y); Format descriptor

An expression is a combination of variables, constants and operators written according to the syntax of C language. Every expression evaluates to a value of a certain type that can be assigned to a variable. Precedence in Arithmetic Operators An arithmetic expression without parenthesis will be evaluated from left to right using the rules of precedence of operators. Each operator in C has a precedence associated with it. There are two distinct priority levels of arithmetic operators in C. High priority * / % Low priority + -

When Parenthesis are used, the expressions within parenthesis assume highest priority. Parenthesized sub expression are evaluated from left to right If parenthesis are nested, the evaluation begins with the innermost sub expression. The operators of higher precedence are evaluated first. The operators of same precedence are evaluated from right to left or from left to right depending on the level. This is known as associativity property of an operator. The associability rule is applied when two or more operators of the same precedence level appear in the sub expression.

x + y * z / 2 + p x + (y * z) / 2 + p x + ((y * z) / 2) + p (x + ((y * z) / 2)) + p ((x + ((y * z) / 2)) + p) x + y - z / 2 * p (x + y) - z / 2 * p (x + y) (z / 2) * p (x + y) ((z / 2) * p) ((x + y) ((z / 2) * p))

i = i + 2 i += 2 i = i +1 i++ or ++i i = i +1 ; x = x + i x +=(++i); x = x + i ; i = i +1 x += (i++); i = i -1 ; x = x + i x +=(--i); x = x + i ; i = i -1 x += (i--); i = i -2 ; x = x + i i -= 2; x += i; x = x + i ; i = i -2 x += i; i -=2 x=x-i; i=i+1 x -= i++

Logical Operators && AND OR! NOT Relational Operators == equality!= Not equal to < less than <= less than equal to > greater than >= greater than equal to

exp? exp1 : exp2 An expression exp is evaluated and if the value is nonzero (or true - represented by 1) then expression exp1 is the final value otherwise exp2 is the final value of entire expression.

Assignment statement x = expression; Compound statement {s1; s2;. ; Collection of statements, each separated by semi colon and enclosed in brackets Multiple lines comments are enclosed within /* comments */ Single line comment can be preceded by //

if (cond) statement; if (cond) s1 else s2; Here cond is a boolean condition which can have non zero value representing true and 0 representing false. Statement may be simple or compound.

for (i = m1; i <= m2; i+=m3) Here { body ; m1 : initial value; m2 : maximum value of I m3 : increment (positive or negative) body sequence of statements.

While statement while (cond) { body ; Do-while statement do {body while cond; In while loop, cond is first checked and if it is true then body is executed. Loop is repeated till condition fails. Do-while loop, first body is executed and then cond is checked. If it is true then loop repeats else exit the loop.

switch (exp) { case v 1 :s 1 ; break; case v 2 :s 2 ; break; case v n :s n ; break; default : s optional If the value of exp is v j then s j is executed and switch statement is exited using break statement. Execution always starts from 1 to last.

Input statement scanf(&x, &y); Input the values of x and y &x means address of x Output statement: Assume x=2; y=5; printf ( %d\n%d\n, x, y); Output 2 5 Format descriptor printf ( x = %d\ny = %d\n, x, y); Output x= 2 y= 5

/* reads single character and stores in character variable x */ x = getchar(); /* prints single character stored in x*/ putchar(x); /* the following functions are in standard file named stdio.h */ scanf(control, v1, v2,..); printf(control, e1,e2,...); Control in input/output control = "seq of format descriptor

Description Meaning %d a decimal integer %o a octal integer %x a hexadecimal integer %c a single character %s a character string %f a decimal number (float or double) \n skip to new line

printf("%4d%7.2f\n%c\n", x, y, z) printf( %c %d %f, ch, i, x); scanf("%4d%8.2f \n", &x, &y) scanf( %c %d %f, &ch, &i, &x); Here & represents memory addresses

Implicit type conversion C permits mixing of constants and variables of different types in an expression. C automatically converts any intermediate values to the proper type so that the expression can be evaluated without loosing any significance. This automatic type conversion is know as implicit type conversion During evaluation it adheres to very strict rules and type conversion. If the operands are of different types the lower type is automatically converted to the higher type before the operation proceeds. The result is of higher type.

1. If one operand is long double, the other will be converted to long double and result will be long double. 2. If one operand is double, the other will be converted to double and result will be double. 3. If one operand is float, the other will be converted to float and result will be float. 4. If one of the operand is unsigned long int, the other will be converted into unsigned long int and result will be unsigned long int. 5. If one of the operand is long int, the other will be converted to long int and the result will be long int. 6. If one operand is unsigned int the other will be converted to unsigned int and the result will be unsigned int.

Many times there may arise a situation where we want to force a type conversion in a way that is different from automatic conversion. Consider for example the calculation of number of female and male students in a class female_students Ratio = ------------------- male_students Since if female_students and male_students are declared as integers, the decimal part will be rounded off and its ratio will represent a wrong figure.

This problem can be solved by converting locally one of the variables to the floating point as shown below. Ratio = (float) female_students / male_students The operator float converts the female_students to floating point for the purpose of evaluation of the expression. Then using the rule of automatic conversion, the division is performed by floating point mode, thus retaining the fractional part of the result. The process of such a local conversion is known as explicit conversion or casting a value. The general form is (type_name) expression

Formula: x1 = [(-b) + (b 2 4ac)]/2a x2 = [(-b) - (b 2 4ac)]/2a Algorithm: input a, b, c; d = b*b 4*a*c; if (d 0) then { e = sqrt(d); r1 = (-b + e) / (2*a); r2 = (-b - e) / (2*a); i1=0; i2 =0 else { e = sqrt(-d); r1 = (-b) / (2*a); r2 = r1; i1= e / (2*a); i2 = -e/(2*a); output r1,r2,i1,i2

C Program (Roots for quadratic equations) #include<stdio.h> #include<math.h> main () { float a, b, c, d, e, r1, r2, i1, i2; printf("input the values of a, b and c\n"); scanf("%f%f%f", &a, &b, &c); d = b*b-4*a*c; printf("d=%f\n", d); if (d >= 0) { e = sqrt(d); printf("e=%f\n", e); r1 = (-b + e) / (2*a); r2 = (-b - e) / (2*a); i1=0; i2 =0; else { e = sqrt(-d); printf("e=%f\n", e); r1 = (-b)/ (2*a); r2 = r1; i1 = e / (2*a); i2 = -e/(2*a); printf("r1= %f\nr2= %f\ni1= %f\ni2= %f\n", r1,r2,i1,i2);

Formula: fact = n * n-1 * * 2 * 1 Algorithm: input n; i = 0; initialization fact = 1; looping while (i < n) { i = i + 1; fact = i * fact; output fact Execution Let n = 4; i = 0; fact = 1; while loop 0 < 4; i = i +1=1; fact = 1 * 1; 1 < 4; i = i +1=2; fact = 1 * 2; 2 < 4; i = i +1=3; fact = 2 * 3; 3 < 4; i = i +1=4; fact = 6 * 4 4 < 4 is false so exit the while loop; output fact = 24 Prof Saroj Kaushik, IIT Delhi 38

C Program (factorial using while) #include<stdio.h> main () { int n, i, fact; i = 0; fact = 1; printf("input the value of n\n"); scanf("%d", &n); while (i < n) { i = i+1; fact = i * fact; ; printf("\nthe Output of fact program is as follows:\n"); printf("n = %d\nfact = %d\n", n, fact);

C Program (factorial using do-while) #include<stdio.h> main () { int n, i, fact; i = 1; fact = 1; printf("input the value of n\n"); scanf("%d", &n); do { fact = fact * i; i = i+1; while (i <= n); printf("\nthe Output of fact program is as follows:\n"); printf("n = %d\nfact = %d\n", n, fact);

Algorithm: input num; rev = 0; while (num > 0) { rev = rev*10 + num mod 10; num = num div 10; output rev Execution num = 245; rev = 0; while loop rev = 0 * 10 + 5 = 5; num = 24; rev = 5 * 10 + 4 = 54; num = 2; rev = 54 * 10 + 2 = 542; num = 0; exit while output rev as 542 Prof Saroj Kaushik, IIT Delhi 41

C Program (reversal of integer number) #include<stdio.h> main () { int num, rev, q; printf("input the value of num\n"); scanf("%d", &num); printf("\nnum = %d", num); rev = 0; while (num > 0) { rev = rev * 10 + num % 10; num = num / 10; ; printf("\nrev= %d\n", rev); Remainder operator Integer division

Algorithm: Here number is read in the loop. At the end of loop, read elements are not available. max = 0; i=0; while (i < 100) { i = i+1; input num; if (num > max) then max = num; output max Prof Saroj Kaushik, IIT Delhi 43

C program (Max with fixed 100 numbers) #include<stdio.h> main() { int max, num, i=0; max= 0; while (i < 100) { i = i+1; printf("\ninput the number \n"); scanf("%d",&num ); if (num > max) max = num; ; printf("\nmax = %d",max);

C program (Max of list with variable numbers) #include<stdio.h> main() { int max, num, i=0; max= 0; printf("\ninput number of elements\n"); scanf("%d",&n ); while (i < n) { i = i+1; printf("\ninput the number \n"); scanf("%d",&num ); if (num > max) max = num; ; printf("\nmax = %d",max);

C program (Max of a list of numbers not known in advance. So terminate the list by -1000) #include<stdio.h> main() { int max, num; max= 0; printf("\ninput the number & -1000 for ending\n"); scanf("%d",&num ); while (num!= -1000) { if (num > max) max = num; scanf("%d",&num ); ; printf("\nmax = %d",max);

Algorithm: input num(i), i = 1,100; max = 0; i = 0; while (i < 100) { i = i+1; if (num(i) > max) then max = num(i); output max, num(i), i = 1,100 Execution Let numbers are: 4, 2,7,1 max = 0; i = 0; while loop i = 1; 4 > max so max = 4; i = 2; 2 < max so no change i = 3; 7 > max so max = 7; i = 4; 1 < max so no change exit of while output max as 7 and numbers as 4, 2, 7, 1 Prof Saroj Kaushik, IIT Delhi 47

Series:f=0!+1!+2!+ +n! (n 0) Algorithm: Here we are given the value of n. Series sum is calculated by finding sum after adding previous sum with a term at each step. input n; f=0;term=1;i=0; while (i < n) { f=f+term; i=i+1; term = term * i; output f

C program (Value of the series containing factorial of number starting from 0 to n) #include<stdio.h> main () { int n, i =0, f = 0, term = 1; printf("input the value of n\n"); scanf("%d", &n); while (i < n) { f = f + term; i = i+1; term = term * i; ; printf("\nthe value of the series is= %d\n", f);