Chapter 2. Introduction to C language. Together Towards A Green Environment

Size: px
Start display at page:

Download "Chapter 2. Introduction to C language. Together Towards A Green Environment"

Transcription

1 Chapter 2 Introduction to C language

2 Aim Aim of this chapter is to provide the fundamentals for basic program construction using C.

3 Objectives To understand the basic information such as keywords, character sets, constants and variables. To familiarize the built in data types provided. To familiarize the arithmetic operators, logical operators and comparison operators supported by C. To familiarize with the control statements. To familiarize how to start with the small programs. To understand the ways in which data input/output can be done in C.

4 History of C program C is a programming language which born at AT & T s Bell Laboratory of USA in Written by Dennis Ritchie. [father of C programming language.] C language was created for a specific purpose i.e. designing the UNIX operating system

5 C program structure The Function Header Void main() The opening Brace The body The closing brace

6 C Programming Introduction : Tips Include header files based on requirements Every C Program Should have exactly one main function C Program Execution Always Starts from main. Execution of C Program begins at Opening brace of function and ends at closing brace of the function Generally all statements in C are written in Lowercase Letters. Uppercase Letters are used for Symbolic names, output strings and messages Every C statement must ends with semicolon All variables must be declared with respective data types before using.

7 Keywords Keywords are those words whose meaning is already defined by Compiler Cannot be used as Variable Name There are 32 Keywords in C Keywords are also called as Reserved words

8 Keywords

9 printf printf() function is inbuilt library function in C We have to include stdio.h file as shown in below C program to make use of printf() library functions. printf() function is used to print the character, string, float, double and integer onto the output screen.

10 Welcome to EC (printf example) //Example 1 #include<stdio.h> void main() printf("welcome to Engineering Computing!"); return 0;

11 Program explanation Line.no Command Explanation 1 #include <stdio.h> 2 int main() This is a preprocessor command that includes standard input output header file(stdio.h) from the C library before compiling a C program This is the main function from where execution of any C program begins. 3 This indicates the beginning of the main function. 4 /*Demo program*/ whatever is given inside the command /* */ in any C program, won t be considered for compilation and execution. 5 printf( Welcome to! ); printf command prints the output onto the screen. 6 return 0; This command terminates C program (main function) 7 This indicates the end of the main function.

12 Data Types Primitive data types char, int, float, double Aggregate data types Arrays come under this category Arrays can contain collection of int, float, char or double data

13 Constant Constants and Variables Constants are the terms that can't be changed during the execution of a program. For example: 1, 2.5, "Programming is easy." etc. In C, constants can be classified as: Integer constants Floating-point constants Character constants

14 Variable Constants and Variables A variable is a named memory location in which data of certain type can be stored. The contents of variable can be changed, thus the name. User-defined variables must be declared before they can be used in the program. It is during the declaration phase that the actual memory for the variable is reserved. All the variables in C must be declared before use.

15 Constant/variable naming rules 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

16 Task: Identify valid/invalid variable names num number 1 Num num 1 1num Num1 _NUM continue NUM_temp2 addition of program int char NAME SUM 1_num 365_days

17 Integer variable Integer variables are used to store whole numbers. Keyword to declare integer variable - int int (2 bytes) can store values from -32,768 to +32,767 We use printf() function with %d format specifier to display the value of an integer variable. If you want to use the integer value that crosses the above limit, you can use modifiers to increase the limit. For example(syntax: datatype variablename;) int count; short no_of_students=30;

18 Float variables Floating variables are used to store floating point numbers. Floating point numbers may contain both a whole number and fractional part. Keyword used to declare a floating number is float. We use printf() function with %f format specifier to display the value of a float variable. For example(syntax: datatype variablename;) float Max_Temp; float Avg = 9.33;

19 Character variables Character variables are used to declare a character with keyword char. Character data type allows a variable to store only one character. We use printf() function with %c format specifier to display the value of a character variable. For example: char status = g ; char feedback = T ; Strings are nothing but array of characters ended with null character ( \0 ). (Will be discussed in another chapter) We use printf() function with %s format specifier to display the value of a string variable. char str[20] = Welcome to C ;

20 Data Types - Example // Example 2-lab2 #include <stdio.h> int main() char ch = 'A'; char str[20] = MUSCAT"; float flt = ; int no = 150; double dbl = ; printf("character is %c \n", ch); printf("string is %s \n", str); printf("float value is %f \n", flt); printf("integer value is %d\n", no); printf("double value is %lf \n", dbl); return 0;

21 Arithmetic Operators

22 Arithmetic operator example :A=20 & B=5 Operator Description Example + Addition of two operands A + B will give 25 - Subtraction of second operand from the first A B will give 15 * Multiply both operands A * B will give 100 / Divide numerator by denominator A / B will give 4 % Modulus Operator and remainder of after an integer division Increment operator increases integer value by one Decrement operator decreases integer value by one A % B will give 0 A++ will give 21 A-- will give 19

23 scanf scanf() function is inbuilt library functions in C scanf() function is used to read character, string, and numeric data from keyboard

24 scanf - Example //Example 4 #include <stdio.h> int main() char ch; int num; float avg; printf("enter any character \n"); scanf("%c", &ch); printf("entered character is %c \n", ch); printf("enter any integer \n"); scanf("%d", &num); printf("entered integer value is %d \n", num); printf("enter any float value \n"); scanf("%f", &avg); printf("entered float number is %f \n", avg); return 0;

25

26 Assignment operator In C programs, values for the variables are assigned using assignment operators. For example, if the value 10 is to be assigned for the variable count, it can be assigned as count= 10;

27 Assignment operator Operators Example Explanation Simple assignment operator = sum = is assigned to variable sum += sum += 10 This is same as sum = sum + 10 Compound assignment operators -= sum -= 10 This is same as sum = sum 10 *= sum *= 10 This is same as sum = sum * 10 /= sum /= 10 This is same as sum = sum / 10 %= sum %= 10 This is same as sum = sum % 10

28 Relational Operators Operator Syntax == Equal to!= Not equal to < Less than > Greater than <= Less than or equal to >= Greater than or equal to Examples will be discussed in control statements

29 Logical Operators Operator Syntax Not!a Logical AND Logical OR a && b a b

30 Control statements in C

31 C provides two styles of flow control Branching Looping Branching is deciding what actions to take and looping is deciding how many times to take a certain action.

32 Branching Branching is so called because the program chooses to follow one branch or another. The C language programs follows a sequential form of execution of statements. Many times it is required to alter the flow of sequence of instructions. C language provides statements that can alter the flow of a sequence of instructions. These statements are called as Control Statements.

33 To jump from one part of the program to another, these statements help. The control transfer may be unconditional or conditional. Branching Statement are of following categories if if else Nested if else Switch case statements

34 if statement The if... statement is used if the programmer wants to execute codes in statement1 when the test expression is true. Syntax if (test expression) statement1 to be executed if test expression is true;

35 Example Program //Example 5 Write a C program to print the number entered by user only if the number entered is negative. #include <stdio.h> int main() int num; printf("enter a number to check.\n"); scanf("%d",&num); if(num<0) printf("number = %d\n",num); return 0;

36 C Program to check whether the number is even or is divisible by 5 #include<stdio.h> void main() int num; scanf("%d", &num); if((num%2=0) (num%5=0)) printf( Number is even or divisible by %d\n,num);

37 if else statement The if...else statement is used if the programmer wants to execute codes in statement1 when the test expression is true and execute codes in statement2 if the test expression is false. Syntax if (test expression) statement1 to be executed if test expression is true; else statement2 to be executed if test expression is false;

38 //Example Program 6 Program to find the largest of two numbers #include<stdio.h> void main() int num1, num2; scanf("%d%d", &num1,&num2); if(num1>num2) printf("first number is largest %d\n,num1); else printf("second number is largest %d\n,num2);

39 Nested if else The nested if...else statement is used when program requires more than one test expression. If the first test expression is true, it executes the codes in statement1 inside the braces just below it. But if the first test expression is false, it checks the second test expression. If the second test expression is true, it executes the codes in statement2 inside the braces just below it. This process continues. If all the test expression are false, statements inside else is executed and the control of program jumps below the nested if...else.

40 Syntax of nested if else if (test expression1) statement/s 1 to be executed if test expression1 is true; else if (test expression2) statement/s 2 to be executed if test expression1 is false and 2 is true; else if (test expression 3) statement/s 3 to be executed if text expression1 and 2 are false and 3 is true; else statements to be executed if all test expressions are false;

41 General Flowchart of nested if else

42 //Example 7 program #include <stdio.h> void main() int num1, num2; printf("enter two integers to check\n"); scanf("%d %d",&num1,&num2); if(num1==num2) //checking whether two integers are equal printf( Both the numbers are equal ); else if(num1>num2) //checking whether num1 is greater than num2 printf(" number 1 is greater than number 2 ); else printf("number 2 is greater than number 1 );

43 Assume you are a owner of a hypermarket which has 100 different items. You have decided to give categorise the customers based on the number of items they purchase in your shop. Based on the below given conditions, write 'C' program using nested if else statement. Offer conditions are as follows: Number of items between 5 to 10, customer type= average buyer Number of items between 15 to 20, customer type= good Number of items between 25 to 30, customer type= valuable Number of items between 35 to 40, customer type= potential Number of items between 41 to 50, customer type= premium otherwise customer type= more potential

44 A commercial bank has introduced an incentive policy of giving bonus to all its deposit holders. The policy is as follows: A bonus of 2 percent of the balance held on 31st December is giving to everyone, irrespective of their balance, and 5 percent is given to female account holders if their balance is more than OMR #include<stdio.h> void main() float balance,bonus; char gender; printf("enter the balance and gender\n"); scanf("%f %c",&balance,&gender); if((balance>5000)&&(gender=='f')) bonus=0.05* balance; else bonus=0.02*balance; balance=balance+bonus; printf("the bonus and balance is %f\t%f",bonus,balance);

45 Program to calculate the Net price of each type of item in the given table based on the price values given in it using nested if else. Formula: Net price=[price-(price*discount/100)] Item code Item name Price Discount % Perfume Soap Noodles Eggs

46 Switch case statement The switch-case statement is a multi-way decision making statement. Unlike the multiple decision statement that can be created using if-else, the switch statement evaluates the conditional expression and tests it against the numerous constant values. During execution, the branch corresponding to the value that the expression matches is taken. The value of the expressions in a switch-case statement must have to be an ordinal type i.e. integer, char, short, long, etc.

47 Syntax and flowchart of switch case statement switch(expression) case Choice1: statement1; break; case Choice2: statement2; break; case Choice-n: statement-n; break; default: default block of statement; break;

48 Switch case program //Example 8 Program to choose the module choice #include<stdio.h> void main() int choice; printf("1: English\n"); printf("2: Maths\n"); printf( 3: Computers\n") ; printf( Enter your choice\n") scanf("%d", &choice); switch(choice) case 1: printf( The module English is selected\n"); break ; case 2: printf("the module Maths is selected\n") ; break ; case 3: printf("the module Computers is selected\n") ; break ; default: printf( Invalid module\n ); break;

49 Loops in C Loops are used to repeat a block of code. Using this the program repeatedly execute a block of code In every programming language, there are circumstances where we want to do the same thing many times. For instance if we want to print the same words ten times. We could type ten printf function, but it is easier to use a loop.

50 for loop The initialization step is executed first, and only once. This step allows to declare and initialize any loop control variables. Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop. After the body of the for loop executes, the flow of control jumps back up to the action-after-each-iteration statement. This statement allows to update any loop control variables. The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then action-after-eachiteration step, and then again condition). After the condition becomes false, the for loop terminates.

51 for Loop syntax for(initialization ; condition; action-after-each-iteration) //loop body; Initial-Action Action-After- Each-Iteration Continuation condition? false true Statement(s) (loop-body) Next Statement

52 for loop program //Example 9 Program to find the sum of numbers from 1 to n #include<stdio.h> void main() int i,n,x,sum=0; printf( Enter the value of n\n ); scanf("%d", &n); for(i=1;i<=n;i++) printf( Enter the value\n ); scanf("%d",&x); sum=sum+x; printf( sum is %d\n", sum);

53 while loop To execute this expression, the compiler first examines the Condition. If the Condition is true, then it executes the Statement. After executing the Statement, the Condition is checked again. AS LONG AS the Condition is true, it will keep executing the Statement. When or once the Condition becomes false, it exits the loop.

54 Syntax of while loop while(tested condition is satisfied) body of loop;

55 while loop program //Example 10 Program to find the sum of numbers from 1 to n. #include<stdio.h> void main() int i,n,sum=0; printf( Enter the value of n\n ); scanf("%d", &n); i=1; while(i<=n) sum=sum+i; i++; printf( sum is %d\n", sum);

56 //Example 11 Program to find the sum of first and last digit of a given number #include<stdio.h> int main() int num,firstdigit,lastdigit; printf("enter the number\n"); scanf("%d",&num); lastdigit=num%10; while(num>=1) firstdigit=num; num=num/10; printf("sum of first and last digit of the number: %d",(firstdigit+lastdigit)); return 0;

57 Program to check whether a number is palindrome or not #include<stdio.h> int main() int num,r,rev=0,temp; printf("enter the number\n"); scanf("%d",&num); temp=num; while(num>0) r=num%10; rev=(rev*10)+r; num=num/10; if(temp==rev) printf("palindrome"); else printf("not palindrome"); return 0;

58 break statement Break statement is used to exit a loop at any time. This is very useful to stop running a loop because a condition has been met other than the loop end condition. In the example in the next section, the while loop will run, as long i is smaller than twenty. In the while loop there is an if statement that states that if i equals ten the while loop must stop (break). The result is that only ten Hello will be printed.

59 //Example 12 Using break in while loop #include <stdio.h> int main() int i = 0; while ( i < 20 ) i++; printf("hello\n ); if ( i == 10) break; return 0;

60 continue statement With continue; it is possible to skip the rest of the commands in the current loop and start from the top again. (the loop variable must still be incremented). In the example in the next section, the printf function is never called because of the continue;.

61 using continue in while loop //Example 13 #include <stdio.h> int main() int i; i = 0; while ( i < 20 ) printf( "Hello\n ); i++; continue; if ( i == 10) break; return 0;

62 #include<stdio.h> int main() int j=10,i; for(i=0;i<j;i++) printf( Welcome to CCE ); break; printf( Oman ); return 0; //once

63 #include<stdio.h> int main() int x=5,i; for(i=0;i<=x;i++) printf( Welcome to CCE\n ); continue; printf( Oman ); return 0; //5 times

64 do while loop The do while condition executes a Statement first. After the first execution of the Statement, it examines the condition. If the Condition is true, then it executes the Statement again. It will keep executing the Statement AS LONG AS the Condition is true. Once the Condition becomes false, the looping (the execution of the Statement) would stop. The Condition being parentheses. checked must be included between The whole do while statement must end with a semicolon.

65 Syntax of do while loop do block of code; while(condition is satisfied);

66 do while loop program //Example 14 #include<stdio.h> void main() int i,n,x,sum=0; printf( Enter the value of n\n ); scanf("%d", &n); i=1; do printf( Enter the value\n ); scanf("%d",&x); sum=sum+x; i++; while(i<=n); printf( sum is %d\n", sum);

67 Go to statements A goto statement provides an unconditional jump from the goto to a labeled statement in the same function. Use of goto statement is highly discouraged in any programming language because it makes difficult to trace the control flow of a program, making the program hard to understand and hard to modify.

68 Syntax of go to

69 //Example 15 #include<stdio.h> void main() printf(" goto x; y: printf("expert"); goto z; x: printf("c Programming"); goto y; z: printf(".com\n"); Example Program for goto

70 References & E-Brary R1. Balaguruswamy (2010), Programming in C,6th Edition, Tata Mc Graw Hill. E1. Vine, Michael, A., C Programming for the Absolute Beginner : Course Technology [Online] Available from : lib/ caledonian/reader.action?docid= [Accesssed on 10 th March 2016] E3. Makay, p., Professional Programmers Guide to C. London:CRC Press.ebrary [Online] Available from : caledonian/docdetail.action?docid= [Accessed on 10 th March 2016]

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 3 Constants, Variables, Data Types, And Operations Department of Computer Engineering

More information

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

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath UNIT - I Introduction to C Programming Introduction to C C was originally developed in the year 1970s by Dennis Ritchie at Bell Laboratories, Inc. C is a general-purpose programming language. It has been

More information

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

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants Data types, variables, constants Outline.1 Introduction. Text.3 Memory Concepts.4 Naming Convention of Variables.5 Arithmetic in C.6 Type Conversion Definition: Computer Program A Computer program is a

More information

Fundamentals of Programming

Fundamentals of Programming Fundamentals of Programming Lecture 3 - Constants, Variables, Data Types, And Operations Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad Outline C Program Data types Variables

More information

Introduction. C provides two styles of flow control:

Introduction. C provides two styles of flow control: Introduction C provides two styles of flow control: Branching Looping Branching is deciding what actions to take and looping is deciding how many times to take a certain action. Branching constructs: if

More information

C: How to Program. Week /Mar/05

C: How to Program. Week /Mar/05 1 C: How to Program Week 2 2007/Mar/05 Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers

More information

Lecture 6. Statements

Lecture 6. Statements Lecture 6 Statements 1 Statements This chapter introduces the various forms of C++ statements for composing programs You will learn about Expressions Composed instructions Decision instructions Loop instructions

More information

Module 4: Decision-making and forming loops

Module 4: Decision-making and forming loops 1 Module 4: Decision-making and forming loops 1. Introduction 2. Decision making 2.1. Simple if statement 2.2. The if else Statement 2.3. Nested if Statement 3. The switch case 4. Forming loops 4.1. The

More information

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

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language 1 History C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC

More information

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University Unit 1 Programming Language and Overview of C 1. State whether the following statements are true or false. a. Every line in a C program should end with a semicolon. b. In C language lowercase letters are

More information

Data types, variables, constants

Data types, variables, constants Data types, variables, constants Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers 2.4 Memory Concepts 2.5 Arithmetic in C 2.6 Decision

More information

Structured programming

Structured programming Exercises 2 Version 1.0, 22 September, 2016 Table of Contents 1. Simple C program structure................................................... 1 2. C Functions..................................................................

More information

Decision Making -Branching. Class Incharge: S. Sasirekha

Decision Making -Branching. Class Incharge: S. Sasirekha Decision Making -Branching Class Incharge: S. Sasirekha Branching The C language programs presented until now follows a sequential form of execution of statements. Many times it is required to alter the

More information

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

Unit 5. Decision Making and Looping. School of Science and Technology INTRODUCTION INTRODUCTION Decision Making and Looping Unit 5 In the previous lessons we have learned about the programming structure, decision making procedure, how to write statements, as well as different types of

More information

Unit 1: Introduction to C Language. Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune

Unit 1: Introduction to C Language. Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune Unit 1: Introduction to C Language Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories

More information

Chapter 2 - Introduction to C Programming

Chapter 2 - Introduction to C Programming Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers 2.4 Memory Concepts 2.5 Arithmetic

More information

Programming and Data Structures

Programming and Data Structures Programming and Data Structures Teacher: Sudeshna Sarkar sudeshna@cse.iitkgp.ernet.in Department of Computer Science and Engineering Indian Institute of Technology Kharagpur #include int main()

More information

C - Basic Introduction

C - Basic Introduction C - Basic Introduction C is a general-purpose high level language that was originally developed by Dennis Ritchie for the UNIX operating system. It was first implemented on the Digital Equipment Corporation

More information

The C language. Introductory course #1

The C language. Introductory course #1 The C language Introductory course #1 History of C Born at AT&T Bell Laboratory of USA in 1972. Written by Dennis Ritchie C language was created for designing the UNIX operating system Quickly adopted

More information

Branching is deciding what actions to take and Looping is deciding how many times to take a certain action.

Branching is deciding what actions to take and Looping is deciding how many times to take a certain action. 3.0 Control Statements in C Statements The statements of a C program control the flow of program execution. A statement is a command given to the computer that instructs the computer to take a specific

More information

UIC. C Programming Primer. Bharathidasan University

UIC. C Programming Primer. Bharathidasan University C Programming Primer UIC C Programming Primer Bharathidasan University Contents Getting Started 02 Basic Concepts. 02 Variables, Data types and Constants...03 Control Statements and Loops 05 Expressions

More information

C Programming Class I

C Programming Class I C Programming Class I Generation of C Language Introduction to C 1. In 1967, Martin Richards developed a language called BCPL (Basic Combined Programming Language) 2. In 1970, Ken Thompson created a language

More information

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

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered )   FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING ( Word to PDF Converter - Unregistered ) http://www.word-to-pdf-converter.net FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING INTRODUCTION TO C UNIT IV Overview of C Constants, Variables and Data Types

More information

DEPARTMENT OF MATHS, MJ COLLEGE

DEPARTMENT OF MATHS, MJ COLLEGE T. Y. B.Sc. Mathematics MTH- 356 (A) : Programming in C Unit 1 : Basic Concepts Syllabus : Introduction, Character set, C token, Keywords, Constants, Variables, Data types, Symbolic constants, Over flow,

More information

Computer Programming : C++

Computer Programming : C++ The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2003 Muath i.alnabris Computer Programming : C++ Experiment #1 Basics Contents Structure of a program

More information

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section :

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section : CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart ANS. Flowchart:- A diagrametic reperesentation of program is known as flowchart Symbols Q-2) Explain basic structure of c language

More information

Flow Control. CSC215 Lecture

Flow Control. CSC215 Lecture Flow Control CSC215 Lecture Outline Blocks and compound statements Conditional statements if - statement if-else - statement switch - statement? : opertator Nested conditional statements Repetitive statements

More information

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

Intro to Computer Programming (ICP) Rab Nawaz Jadoon Intro to Computer Programming (ICP) Rab Nawaz Jadoon DCS COMSATS Institute of Information Technology Assistant Professor COMSATS IIT, Abbottabad Pakistan Introduction to Computer Programming (ICP) What

More information

SELECTION STATEMENTS:

SELECTION STATEMENTS: UNIT-2 STATEMENTS A statement is a part of your program that can be executed. That is, a statement specifies an action. Statements generally contain expressions and end with a semicolon. Statements that

More information

MODULE 2: Branching and Looping

MODULE 2: Branching and Looping MODULE 2: Branching and Looping I. Statements in C are of following types: 1. Simple statements: Statements that ends with semicolon 2. Compound statements: are also called as block. Statements written

More information

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

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance. 2.1 Introduction (No questions.) 2.2 A Simple Program: Printing a Line of Text 2.1 Which of the following must every C program have? (a) main (b) #include (c) /* (d) 2.2 Every statement in C

More information

Chapter 1 & 2 Introduction to C Language

Chapter 1 & 2 Introduction to C Language 1 Chapter 1 & 2 Introduction to C Language Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 1 & 2 - Introduction to C Language 2 Outline 1.1 The History

More information

Subject: PIC Chapter 2.

Subject: PIC Chapter 2. 02 Decision making 2.1 Decision making and branching if statement (if, if-, -if ladder, nested if-) Switch case statement, break statement. (14M) 2.2 Decision making and looping while, do, do-while statements

More information

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

Full file at   C How to Program, 6/e Multiple Choice Test Bank 2.1 Introduction 2.2 A Simple Program: Printing a Line of Text 2.1 Lines beginning with let the computer know that the rest of the line is a comment. (a) /* (b) ** (c) REM (d)

More information

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE Introduction to C programming By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE Classification of Software Computer Software System Software Application Software Growth of Programming Languages History

More information

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

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Introduction to C Programming Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline Printing texts Adding 2 integers Comparing 2 integers C.E.,

More information

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions. Introduction In the programs that we have dealt with so far, all statements inside the main function were executed in sequence as they appeared, one after the other. This type of sequencing is adequate

More information

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah Lecturer Department of Computer Science & IT University of Balochistan 1 Outline p Introduction p Program development p C language and beginning with

More information

Structured programming. Exercises 3

Structured programming. Exercises 3 Exercises 3 Table of Contents 1. Reminder from lectures...................................................... 1 1.1. Relational operators..................................................... 1 1.2. Logical

More information

Basics of Programming

Basics of Programming Unit 2 Basics of Programming Problem Analysis When we are going to develop any solution to the problem, we must fully understand the nature of the problem and what we want the program to do. Without the

More information

Programming for Engineers Introduction to C

Programming for Engineers Introduction to C Programming for Engineers Introduction to C ICEN 200 Spring 2018 Prof. Dola Saha 1 Simple Program 2 Comments // Fig. 2.1: fig02_01.c // A first program in C begin with //, indicating that these two lines

More information

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

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University ITC213: STRUCTURED PROGRAMMING Bhaskar Shrestha National College of Computer Studies Tribhuvan University Lecture 08: Control Statements Readings: Chapter 6 Control Statements and Their Types A control

More information

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

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York CSc 10200! Introduction to Computing Lecture 2-3 Edgardo Molina Fall 2013 City College of New York 1 C++ for Engineers and Scientists Third Edition Chapter 2 Problem Solving Using C++ 2 Objectives In this

More information

Problem Solving and 'C' Programming

Problem Solving and 'C' Programming Problem Solving and 'C' Programming Targeted at: Entry Level Trainees Session 05: Selection and Control Structures 2007, Cognizant Technology Solutions. All Rights Reserved. The information contained herein

More information

Course Outline Introduction to C-Programming

Course Outline Introduction to C-Programming ECE3411 Fall 2015 Lecture 1a. Course Outline Introduction to C-Programming Marten van Dijk, Syed Kamran Haider Department of Electrical & Computer Engineering University of Connecticut Email: {vandijk,

More information

3 The L oop Control Structure

3 The L oop Control Structure 3 The L oop Control Structure Loops The while Loop Tips and Traps More Operators The for Loop Nesting of Loops Multiple Initialisations in the for Loop The Odd Loop The break Statement The continue Statement

More information

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

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities INTERNAL ASSESSMENT TEST 1 SOLUTION PART 1 1 a Define algorithm. Write an algorithm to find sum and average of three numbers. 4 An Algorithm is a step by step procedure to solve a given problem in finite

More information

Lecture 3. More About C

Lecture 3. More About C Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 3-1 Lecture 3. More About C Programming languages have their lingo Programming language Types are categories of values int, float, char Constants

More information

BCA-105 C Language What is C? History of C

BCA-105 C Language What is C? History of C C Language What is C? C is a programming language developed at AT & T s Bell Laboratories of USA in 1972. It was designed and written by a man named Dennis Ritchie. C seems so popular is because it is

More information

COMPUTER SCIENCE HIGHER SECONDARY FIRST YEAR. VOLUME II - CHAPTER 10 PROBLEM SOLVING TECHNIQUES AND C PROGRAMMING 1,2,3 & 5 MARKS

COMPUTER SCIENCE HIGHER SECONDARY FIRST YEAR.  VOLUME II - CHAPTER 10 PROBLEM SOLVING TECHNIQUES AND C PROGRAMMING 1,2,3 & 5 MARKS COMPUTER SCIENCE HIGHER SECONDARY FIRST YEAR VOLUME II - CHAPTER 10 PROBLEM SOLVING TECHNIQUES AND C PROGRAMMING 1,2,3 & 5 MARKS S.LAWRENCE CHRISTOPHER, M.C.A., B.Ed., LECTURER IN COMPUTER SCIENCE PONDICHERRY

More information

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

PESIT Bangalore South Campus Hosur Road (1km before Electronic City), Bengaluru Department of Basic Science and Humanities SOLUTION OF CONTINUOUS INTERNAL EVALUATION TEST -1 Date : 27-02 2018 Marks:60 Subject & Code : Programming in C and Data Structures- 17PCD23 Name of faculty : Dr. J Surya Prasad/Mr. Naushad Basha Saudagar

More information

Lecture 3 Tao Wang 1

Lecture 3 Tao Wang 1 Lecture 3 Tao Wang 1 Objectives In this chapter, you will learn about: Arithmetic operations Variables and declaration statements Program input using the cin object Common programming errors C++ for Engineers

More information

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

C Program. Output. Hi everyone. #include <stdio.h> main () { printf ( Hi everyone\n ); } C Program Output #include main () { printf ( Hi everyone\n ); Hi everyone #include main () { printf ( Hi everyone\n ); #include and main are Keywords (or Reserved Words) Reserved Words

More information

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14 C introduction Variables Variables 1 / 14 Contents Variables Data types Variable I/O Variables 2 / 14 Usage Declaration: t y p e i d e n t i f i e r ; Assignment: i d e n t i f i e r = v a l u e ; Definition

More information

Introduction to C Programming

Introduction to C Programming 1 2 Introduction to C Programming 2.6 Decision Making: Equality and Relational Operators 2 Executable statements Perform actions (calculations, input/output of data) Perform decisions - May want to print

More information

Decision making with if Statement : - Control Statements. Introduction: -

Decision making with if Statement : - Control Statements. Introduction: - Control Statements Introduction: - Any C program if you consider, the set of statements are normally executed sequentially in the order in which they are written, and such programs have sequential structure

More information

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

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme] Programming in C and Data Structures [15PCD13/23] 1 PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme] Course objectives: The objectives of this course is to make students

More information

CS102: Variables and Expressions

CS102: Variables and Expressions CS102: Variables and Expressions The topic of variables is one of the most important in C or any other high-level programming language. We will start with a simple example: int x; printf("the value of

More information

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are: LESSON 1 FUNDAMENTALS OF C The purpose of this lesson is to explain the fundamental elements of the C programming language. C like other languages has all alphabet and rules for putting together words

More information

Use of scanf. scanf("%d", &number);

Use of scanf. scanf(%d, &number); Use of scanf We have now discussed how to print out formatted information to the screen, but this isn't nearly as useful unless we can read in information from the user. (This is one way we can make a

More information

Lecture 2: C Programming Basic

Lecture 2: C Programming Basic ECE342 Introduction to Embedded Systems Lecture 2: C Programming Basic Ying Tang Electrical and Computer Engineering Rowan University 1 Facts about C C was developed in 1972 in order to write the UNIX

More information

DECISION CONTROL AND LOOPING STATEMENTS

DECISION CONTROL AND LOOPING STATEMENTS DECISION CONTROL AND LOOPING STATEMENTS DECISION CONTROL STATEMENTS Decision control statements are used to alter the flow of a sequence of instructions. These statements help to jump from one part of

More information

Objectives. In this chapter, you will:

Objectives. In this chapter, you will: Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates arithmetic expressions Learn about

More information

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C Overview The C Programming Language (with material from Dr. Bin Ren, William & Mary Computer Science) Motivation Hello, world! Basic Data Types Variables Arithmetic Operators Relational Operators Assignments

More information

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1 NAGERCOIL COMPUTER SCIENCE Grade: IX C++ PROGRAMMING 1 C++ 1. Object Oriented Programming OOP is Object Oriented Programming. It was developed to overcome the flaws of the procedural approach to programming.

More information

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

More information

4.1. Structured program development Overview of C language

4.1. Structured program development Overview of C language 4.1. Structured program development 4.2. Data types 4.3. Operators 4.4. Expressions 4.5. Control flow 4.6. Arrays and Pointers 4.7. Functions 4.8. Input output statements 4.9. storage classes. UNIT IV

More information

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers

More information

HISTORY OF C LANGUAGE. Facts about C. Why Use C?

HISTORY OF C LANGUAGE. Facts about C. Why Use C? 1 HISTORY OF C LANGUAGE C is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the UNIX operating

More information

Government Polytechnic Muzaffarpur.

Government Polytechnic Muzaffarpur. Government Polytechnic Muzaffarpur. Name of the Lab: COMPUTER PROGRAMMING LAB (MECH. ENGG. GROUP) Subject Code: 1625408 Experiment: 1 Aim: Programming exercise on executing a C program. If you are looking

More information

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

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science) The C Programming Language (with material from Dr. Bin Ren, William & Mary Computer Science) 1 Overview Motivation Hello, world! Basic Data Types Variables Arithmetic Operators Relational Operators Assignments

More information

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9 Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Uppercase Alphabets Lowercase Alphabets Character Set A, B, C, Y, Z a, b, c, y, z Digits

More information

2. Numbers In, Numbers Out

2. Numbers In, Numbers Out COMP1917: Computing 1 2. Numbers In, Numbers Out Reading: Moffat, Chapter 2. COMP1917 15s2 2. Numbers In, Numbers Out 1 The Art of Programming Think about the problem Write down a proposed solution Break

More information

Course Outline. Introduction to java

Course Outline. Introduction to java Course Outline 1. Introduction to OO programming 2. Language Basics Syntax and Semantics 3. Algorithms, stepwise refinements. 4. Quiz/Assignment ( 5. Repetitions (for loops) 6. Writing simple classes 7.

More information

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below: Q1. Explain the structure of a C program Structure of the C program is shown below: Preprocessor Directives Global Declarations Int main (void) Local Declarations Statements Other functions as required

More information

Lecture 5: C programming

Lecture 5: C programming CSCI-GA.1144-001 PAC II Lecture 5: C programming Mohamed Zahran (aka Z) mzahran@cs.nyu.edu http://www.mzahran.com Brian Kernighan Dennis Ritchie In 1972 Dennis Ritchie at Bell Labs writes C and in 1978

More information

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering 2013/2014 Programming Fundamentals for Engineers Lab Lab Session # 1 Introduction to C Language ALQUDS University Department of Computer Engineering Objective: Our objective for today s lab session is

More information

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

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators Expressions 1 Expressions n Variables and constants linked with operators Arithmetic expressions n Uses arithmetic operators n Can evaluate to any value Logical expressions n Uses relational and logical

More information

Computers Programming Course 5. Iulian Năstac

Computers Programming Course 5. Iulian Năstac Computers Programming Course 5 Iulian Năstac Recap from previous course Classification of the programming languages High level (Ada, Pascal, Fortran, etc.) programming languages with strong abstraction

More information

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

COP 2000 Introduction to Computer Programming Mid-Term Exam Review he exam format will be different from the online quizzes. It will be written on the test paper with questions similar to those shown on the following pages. he exam will be closed book, but students can

More information

Computer Programming. Decision Making (2) Loops

Computer Programming. Decision Making (2) Loops Computer Programming Decision Making (2) Loops Topics The Conditional Execution of C Statements (review) Making a Decision (review) If Statement (review) Switch-case Repeating Statements while loop Examples

More information

Chapter 3 Structured Program Development

Chapter 3 Structured Program Development 1 Chapter 3 Structured Program Development Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 3 - Structured Program Development Outline 3.1 Introduction

More information

Preview from Notesale.co.uk Page 6 of 52

Preview from Notesale.co.uk Page 6 of 52 Binary System: The information, which it is stored or manipulated by the computer memory it will be done in binary mode. RAM: This is also called as real memory, physical memory or simply memory. In order

More information

Decision Making and Branching

Decision Making and Branching INTRODUCTION Decision Making and Branching Unit 4 In the previous lessons we have learned about the programming structure, data types, declaration of variables, tokens, constants, keywords and operators

More information

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence Data and Variables Data Types Expressions Operators Precedence String Concatenation Variables Declaration Assignment Shorthand operators Review class All code in a java file is written in a class public

More information

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Fundamentals of Programming. Lecture 3: Introduction to C Programming Fundamentals of Programming Lecture 3: Introduction to C Programming Instructor: Fatemeh Zamani f_zamani@ce.sharif.edu Sharif University of Technology Computer Engineering Department Outline A Simple C

More information

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar.. .. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar.. A Simple Program. simple.c: Basics of C /* CPE 101 Fall 2008 */ /* Alex Dekhtyar */ /* A simple program */ /* This is a comment!

More information

Chapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops.

Chapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops. Chapter 6 Loops 1 Iteration Statements C s iteration statements are used to set up loops. A loop is a statement whose job is to repeatedly execute some other statement (the loop body). In C, every loop

More information

C Language, Token, Keywords, Constant, variable

C Language, Token, Keywords, Constant, variable C Language, Token, Keywords, Constant, variable A language written by Brian Kernighan and Dennis Ritchie. This was to be the language that UNIX was written in to become the first "portable" language. C

More information

Programming for Electrical and Computer Engineers. Loops

Programming for Electrical and Computer Engineers. Loops Programming for Electrical and Computer Engineers Loops Dr. D. J. Jackson Lecture 6-1 Iteration Statements C s iteration statements are used to set up loops. A loop is a statement whose job is to repeatedly

More information

Fundamentals of Computer Programming Using C

Fundamentals of Computer Programming Using C CHARUTAR VIDYA MANDAL S SEMCOM Vallabh Vidyanagar Faculty Name: Ami D. Trivedi Class: FYBCA Subject: US01CBCA01 (Fundamentals of Computer Programming Using C) *UNIT 3 (Structured Programming, Library Functions

More information

Chapter 1 Introduction to Computers and C++ Programming

Chapter 1 Introduction to Computers and C++ Programming Chapter 1 Introduction to Computers and C++ Programming 1 Outline 1.1 Introduction 1.2 What is a Computer? 1.3 Computer Organization 1.7 History of C and C++ 1.14 Basics of a Typical C++ Environment 1.20

More information

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

These are reserved words of the C language. For example int, float, if, else, for, while etc. Tokens in C Keywords These are reserved words of the C language. For example int, float, if, else, for, while etc. Identifiers An Identifier is a sequence of letters and digits, but must start with a letter.

More information

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1 CSE123 LECTURE 3-1 Program Design and Control Structures Repetitions (Loops) 1-1 The Essentials of Repetition Loop Group of instructions computer executes repeatedly while some condition remains true Counter-controlled

More information

A control structure refers to the way in which the Programmer specifies the order of executing the statements

A control structure refers to the way in which the Programmer specifies the order of executing the statements Control Structures A control structure refers to the way in which the Programmer specifies the order of executing the statements The following approaches can be chosen depending on the problem statement:

More information

Structured Program Development

Structured Program Development Structured Program Development Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline Introduction The selection statement if if.else switch The

More information

Data Types and Variables in C language

Data Types and Variables in C language Data Types and Variables in C language Basic structure of C programming To write a C program, we first create functions and then put them together. A C program may contain one or more sections. They are

More information

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs. I Internal Examination Sept. 2018 Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs. [I]Very short answer questions (Max 40 words). (5 * 2 = 10) 1. What is

More information