fundamentals of computer programming by dr. a. charan kumari

35
Fundamentals of Computer Programming - I (CSL106) Dr. A. Charan Kumari Department of CSE & IT The NorthCap University Gurugram

Upload: the-northcap-university

Post on 20-Mar-2017

40 views

Category:

Education


2 download

TRANSCRIPT

Fundamentals of Computer Programming - I (CSL106)

Fundamentals of Computer Programming - I (CSL106)Dr. A. Charan KumariDepartment of CSE & ITThe NorthCap UniversityGurugram

ContentsIntroduction to CC FundamentalsData types and operatorsI/O OperationsDecision Control StructureCase Control StructureLoopsArraysFunctions

1. Introduction to CC is a general-purpose structured programming languageC was originally developed in the 1970s by Dennis Ritchie at Bell Telephone Laboratories, Inc.It is an outgrowth of two earlier languages BCPL and BC can be used for systems programming as well as application programming

2. C FundamentalsThe C Character set:C uses upper case letters A to Z, the lower case letters a to z, the digits 0 t0 9 and certain special character s as building blocks to from basic program elements2. Identifiers:Identifiers are names that are given to various program elements, such as variables, functions and arrays3. Keywords:Keywords are the reserved words that have standard, pre-defined meaning and are used for their intended purpose

A simple C Program#includemain(){printf(welcome to C Programming\n);}The first statement uses # as a compiler directive and instructs the compiler to take necessary actions to handle input/output operationsSecond line uses the keyword main() , which denotes the starting point for execution of the programprintf is an output statement used to display any message on the screen

3. Data types and OperatorsData typeDescriptionMemory requirementintInteger quantity2 bytescharsingle character1 bytefloatfloating-point number4 bytesdoubledouble-precision floating-point number8 bytes

Types of operators

Operators1. Arithmetic operatorsOperatorPurpose+ addition-subtraction*multiplication/division%remainder after integer division

Operators contd.2. Unary operatorsOperatorPurpose-unary minus++increment--decrementsizeofreturns sizeof its operand in bytes

Operators contd.3. Relational operatorsOperatorpurpose=greater than or equal to==equal to!=not equal to

Operators contd.4. Logical operators

OperatorPurpose&&and||or!not

Operators contd.5. Assignment operators

OperatorExample=a = b+=a += b equivalent to a = a + b-=a -= b equivalent to a = a b*=a *= b equivalent to a = a * b/=a /= b equivalent to a = a / b%=a %= b equivalent to a = a %b

Operators contd.6. Conditional operatorC offers a conditional operator (?:) that stores a value depending upon a condition.The operator is ternary operator as it requires three operands.Syntax: Expression1 ? Expression2: Expression3If expression1 evaluates to true i.e.1, then the value of whole expression is the value of expression2, otherwise, the value of the whole expression is the value o0f the exspression3.

Operator Precedence GroupsOperator CategoryOperatorsAssociativityUnary-, ++, --, !, sizeofR to LArithmetic *, /, %*, /, %L to RArithmetic +, -+, -L to RRelational< , , >=L to REquality==, !=L to RLogical and&&L to RLogical or||L to RConditional operator? :R to LAssignment =, +=, -=, *=, /= , %=R to L

4. I/O operations

I/O operations contd.1. Single Character I/O

getchar() is used to input a single character syntax : ch = getchar();

2. putchar() is used to display a single character syntax : putchar(ch);

I/O operations contd.2. Formatted I/Oscanf(): syntax : scanf(control string, arg1, arg2, .argn); Example : scanf(%d %f %c, &no, &value, &ch);

2. printf(): syntax : printf(control string, arg1, arg2, ., argn); Example : printf(%d %f %c, no, value, ch);

I/O operations contd.3. String I/Ogets(): syntax : gets(string_variable); Example : char str[20]; gets(str);

2. puts(): syntax : puts(string_variable); Example : char str[20] = Welcome 2 C; puts(str);

5. Decision Control structure - The if StatementSyntax: if (condition) statement; else statement;

An example of one alternative:if ( x != 0)product = product * x;An example of two alternatives:if ( x%2 == 0) printf(x is even); else printf(x is odd);

Nested if StatementsNested if statement is an if statement with another if statement as its true task or false task.

Example :if (experience >= 20)if (salary > 1000000)increment = 0.08 * salary;elseincrement = 0.06 * salary;elsesalary = 0.05 * salary;

6. Case control structure - The switch StatementThe switch statement is used to select one of several alternatives when the selection is based on the value of a single variable or an expression.

Syntax :switch (controlling expression) {case label1:statement1;break;case label2:statement2;break;case labeln:statementn;break;default:statementd;}

Example#includemain(){int day;scanf(%d, &day);switch(day){case 1: printf(Monday\n);break;case 2: printf(Tuesday\n);break; default : printf(Invalid input\n);}}

7. LoopsRepetitive execution of a set of statements in a program is known as a LoopTypes of loop constructs

for loopSyntax :for (initialization; test condition; step value){ // body of the loop}Example: (to find 10!)int factorial = 1;for (i = 1; i < =10; i++) factorial = factorial * i;

Initialize the control variablecheck the conditionExecute the body of the loopIncrement/Decrement the control variableTrueFalse

while loopSyntax :while (test condition){ // body of the loop}Example: (to find 10!)int factorial = 1, i = 1;while (i