Programming Concepts C Language

  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Programming Concepts C Language as PDF for free.

More details

  • Words: 2,204
  • Pages: 42
PESIT – Dept. D off MCA

Subject Code: MCA 14

Programming Concepts & C Language.

Faculty: Mr. N. Guruprasad Head – Dept. Dept of MCA http://www.pes.edu

PESIT – Dept. of MCA

Overview of flow of Subject

• Computer Concepts and Programming Concepts • Introduction of C • Fundamental Of C • Preprocessor • Decision Control Structures • Loop Control Structures • Functions Storage g Classes • Arrays and Pointers • Strings • Structure and Unions • Bitwise Operators • File Handling g http://www.pes.edu

PESIT – Dept. Dept of MCA

CORE COMPETENCE

• • • • • • •

Introduction to Computers. C Programming g g Language. g g Data Structures and Algo. OS Concepts. UNIX OS. Object Oriented Programming C++ Programming Language From Here?

S/W Engg. Graphics

Network

http://www.pes.edu

PESIT – Dept. Dept of MCA

Problem gy Solution Methodology ( Algorithm) Programming Language ( C, C++, etc.,)

Flowchart to go through problems Example

Program + Data Solve Result

http://www.pes.edu

PESIT – Dept. Dept of MCA

ALGOL 60 (International Committee -1960) CPL (Cambridge & Univ. Of London-1963)

DEVELOPMENT OF C

BCPL (Martin Richards-cambridge-1967) B (Ken Thompson,Bell Labs.1970) C (Dennis Ritchie,bell Labs.1972)

http://www.pes.edu

PESIT – Dept. Dept of MCA

• H H.L.L: L L: Better program efficiency. Ex: Fortran etc., • L.L.L.: Better machine efficiency. Ex: Assembly etc., Where does

C stands ?

LANGUAGES

PROGRAMMING LANGUAGES

HIGH LEVEL

C

( Problem Oriented )

LOW LEVEL ( Machine Oriented )

• Has relatively good programming efficiency, efficiency compared to L.L.L. • Has relatively good machine efficiency, compared to H.L.L. http://www.pes.edu

PESIT – Dept. of MCA

EDITOR C SOURCE CODE C PREPROCESSOR

STAGES IN COMPILATION OF C PROGRAM

C COMPILER ASSEMBLY LANG. CODE ASSEMBLER OBJ CODE & LIB FILES LINKER EXECUTABLE CODE LOADER http://www.pes.edu

PESIT – Dept. Dept of MCA

• •

CHARACTERISTICS OF C

• • • •

No input output operations. It is a language of functions data types functions, types, assignments and flow controls. C iss noted oted for o its ts ab ability ty to perform pointer arithmetic. C functions are recursive by default. The code in C is grouped into blocks. C allows you to develop a program in i multiple lti l source files that are independently compiled.

http://www.pes.edu

PESIT – Dept. Dept of MCA

A fundamental does not differ from language to language. It still remains same. What differs is the syntax.

FUNDAMENTALS OF C

C Character set: • • • •

Character set A-Z Character set a-z Digits 0-9 Special p Characters like *,, +,, , ( , ) , < , > etc.,

http://www.pes.edu

PESIT – Dept. Dept of MCA

FUNDAMENTALS OF C

Identifiers: name given to various program elements. Ex: variables, functions, arrays. First character must always be a letter or underscore. Ex: area, tax_rate. Keywords: Words which are standard and have predefined meaning in C.Ex: C Ex: break, break printf, switch. Constant: C constant are also referred as data types. Two types of constant: primary and secondary. http://www.pes.edu

PESIT – Dept. Dept of MCA

TYPES OF C CONSTANTS

Integer Constant: • Must have at least one digit. • Should not contain decimal point. • Could be either +ve or _ve. Real Constants: Two forms 1. Exponential form: mantissa & exponent , they should be separated by letter e. 2. Fractional form Character constant: single character/digit. Ex. ‘A’ max length is one. http://www.pes.edu

PESIT – Dept. Dept of MCA

Values varies during program execution.

VARIABLES OF C

Integer variables Real variable Ch Character variable i bl

int n; float n; char h n;

Rules: • Length 1 to 8 char (depends) • First char should be alphabet. • No commas, no blank spaces. • No special symbols, only underscore.

http://www.pes.edu

PESIT – Dept. Dept of MCA

Achieved with the help of library Functions.

I/O / OPERATIONS IN C

scanf() : Allows you to input data from standard d d input i device. d i Ex: scanf(“%d”, &var); printf(): Allows you to output data on standard output device. Ex: printf(“%d”, var);

http://www.pes.edu

PESIT – Dept. Dept of MCA

How does C program look like? /* Sample Program */ main(){ i (){ printf(“\n Welcome to C”); }

STRUCTURE OF C PROGRAM

main(): Where C prg starts executing. \n: New line character. { }: Analogous to begin & end in PASCAL. /* */: Comment statement Spacing should be provided from the point of readability. http://www.pes.edu

PESIT – Dept. of MCA

OPERATORS AND ITS HIERARCHY

Operators: p Operations Addition Subtraction Multiplication Division Remainder

C + * / %

PASCAL + * DIV MOD

Ex: on=ink on=ink*act/2+3/2*act+2+t; act/2+3/2 act+2+t; ink=3, act=2, t=2.2; O/P: on=9.2 Ex: s=qui*add/4-6/2+2/3*6/go; qui=2, add=4, go=3; O/p: s=-1 s= 1 http://www.pes.edu

PESIT – Dept. of MCA

Ex: a=7//2*(3.14+2)*3/5

OPERATORS AND ITS HIERARCHY

http://www.pes.edu

PESIT – Dept. Dept of MCA

OPERATIONS

C

PASCAL

greater than

>

>

>=

>=

<

<

less than or equal to

<=

<=

equal to

==

=

not equal to

!=

<>

greater than or equal to

RELATIONAL OPERATORS

less than

http://www.pes.edu

PESIT – Dept. Dept of MCA

LOGICAL OPERATORS

OPERATIONS

C

PASCAL

Negation L i l AND Logical Logical OR

! && ||

NOT AND OR

S Synopsis i off LLogical i l AND & OR. OR T– True

F– False

LOGICAL AND T && T = T T && F = F F && T = F F && F = F

LOGICAL OR T || T = T T || F = T F || T = T F || F = F

http://www.pes.edu

PESIT – Dept. Dept of MCA

INCREMENT OPERATOR AND FORMAT SPECIFICATION

Increment operators: a = a + 1 is same as a+=1 OR a++ (post ( t iincrement) t) or ++a (pre increment) Similarly decrement operator. C be Can b used d for f prefix fi or suffix. ffi Format Specification: %d integer %f real, float %c character %s string %o octal %x hexadecimal %u unsigned integer http://www.pes.edu

PESIT – Dept. Dept of MCA

? VALIDITY OF CODE

Problem P bl 1: 1 main(){ int x; x=-3+4*5-6; printf(“\n x=%d”, xx=3+4%5-6; 3+4%5 6; printf(“\n x=%d”, x=-3*4%-6/5; printf(“\n i tf(“\ x=%d”, %d” x=(7+6)%5/2; printf(“\n x=%d”, }

x); x); x); ) x);

Sol: x=11, x 11, x x=1, 1, x x=0, 0, x x=1 1 http://www.pes.edu

PESIT – Dept. of MCA

?

Problem 2: main(){ printf(“\n %d %o %x”,72,72,72); %x 72 72 72); } Sol: 72, 110(Octal),48(Hex).

VALIDITY OF CODE Problem 3: main(){ int a=5.99999, b=5.00001; printf(“ a=%d b=%d printf( b=%d”, a,b); a b); } Sol: a=5 b=5. http://www.pes.edu

PESIT – Dept. of MCA

Problem 4:

? VALIDITY OF CODE

main(){ printf(“\n printf(“\n printf(“\n printf(“\n }

%d”, %d”, %d”, %d”,

4/3); 4/-3); -4/3); -4/-3);

Sol: 1, -1, -1, 1; Note: One of them is –ve it will give –ve result.

http://www.pes.edu

PESIT – Dept. Dept of MCA

? VALIDITY OF CODE

P bl Problem 5: 5 main(){ printf(“\n printf(“\n printf(“\n printf( \n printf(“\n }

%d”, %d”, %d %d”,, %d”,

4%3); 4%-3); -4%3); 4%3); -4%-3);

Sol: 1, 1, -1, -1 Note: one or both –ve result takes sign of the numerator.

http://www.pes.edu

PESIT – Dept. Dept of MCA

? VALIDITY OF CODE

Problem P bl 6: 6 main(){ printf(“ Hello, world “ ); } Sol: Compilation error Unterminated String / Char Const. Problem 7: main(){ printf(“ Hello world” ) ; } Sol: Hello world http://www.pes.edu

PESIT – Dept. Dept of MCA

? VALIDITY OF CODE

Problem P bl 8: 8 main(){ int i = 5; printf (“%d“, i = i == 6 ); } Sol: 0 Problem 9: Will there th be b any error? ? main(){ { { } } } Sol: No http://www.pes.edu

PESIT – Dept. Dept of MCA

? VALIDITY OF CODE

Problem P bl 10 : main(){ int a,b=2,c; a=2*(b++); c=2*(++b); printf (“ %d %d %d“, a,b,c); } Sol: 4 4 8

http://www.pes.edu

PESIT – Dept. Dept of MCA

? VALIDITY OF CODE

P bl Problem 11 : main(){ i (){ int k=35; printf(“\n %d %d %d “, k==35,k=50,k>40); }

Sol: 0 50 0

http://www.pes.edu

PESIT – Dept. Dept of MCA

HOW ABOUT ASSIGNMENTS

1) How should I decide which integer type to use 2) Under my compiler this code prints 49 int i=7; printf(“%d”,i++ p ( , * i++); );

Regardless of the order of evaluation, should’nt it print 56. 3) Is it safe to assume that the right hand side of the && and || operators won won’tt be evaluated if the left hand side determines the outcome. http://www.pes.edu

PESIT – Dept. Dept of MCA

HOW ABOUT ASSIGNMENTS ( cont )

4) P People l keep k saying i that the behavior of i = i++ is undefined, but I just tried it on an ANSI conforming compiler and got the results I expected. 5) If I’m not using the value of the expression, should I use i++ or ++I to increment a variable. 6) Y does’nt does nt this code work : int a=1000,b=1000; long int c c=a*b; a b; http://www.pes.edu

PESIT – Dept. Dept of MCA

HOW ABOUT ASSIGNMENTS (cont)

7) H How do d I round d off ff th the numbers. 8) Y does’nt C have an exponentiation ti ti operator. 9) What is top down d i design methodology. th d l What R its advantages. 10) How does operation of an interpreter i t t differ diff from compiler.

http://www.pes.edu

PESIT – Dept. Dept of MCA

PREPROCESSOR

“ It is a program that preprocesses the source code, before it passes through compiler. Operates under what is called as P Preprocessor C Command d lines li or Directives ” Begins with #(hash) symbol and do not requires semicolon at the end.

http://www.pes.edu

PESIT – Dept. of MCA

PREPROCESSOR

Grouped under three categories: • File Inclusion Directives : Achieved by # include. Syntax: #include Ex: #include<stdio.h> • Macro Substitution : Identifier in a program is replaced by some predefined strings composed of tokens. Ex: #define MAX 100 • Compiler Control : Selectivelyy removing g the code sections or choosing form two sections. Such process called as Conditional Compilation. http://www.pes.edu

PESIT – Dept. of MCA

PREPROCESSOR

Ex: - - main(){ --#ifdef IBM-PC { ----/* code for IBMPC *// } #else{ ----/* code for HP */ } #endif } To run prg on IBM-PC we include: #define IBM IBM-PC. PC. http://www.pes.edu

P bl Problem 1:

PESIT – Dept. of MCA

? VALIDITY OF CODE

#define PRODUCT(x) (x*x) (x x) main(){ int i=3,j; j PRODUCT(i+1); j=PRODUCT(i+1); printf(“\n %d “,j); }

Sol : 7

http://www.pes.edu

P bl Problem 2:

PESIT – Dept. of MCA

? VALIDITY OF CODE

#define PRODUCT(x) (x*x) (x x) main(){ int i=3,j,k; j PRODUCT(i++); j=PRODUCT(i++); k=PRODUCT(++i); printf(“\n %d %d“,j,k); }

Sol : 9 49

http://www.pes.edu

PESIT – Dept. of MCA

Q Questions ti

What do U mean by a platform. How do U classify computer according to logic use. How do U classify computer according to size. Diff. b/w : RAM & ROM PROM & EPROM Different generations Batch & Transaction processing Hardware Software & its classification

http://www.pes.edu

PESIT – Dept. of MCA

Internet What it is Who made it WWW / Web server etc Internet & Intranet

Q Questions ti ( contt )

http://www.pes.edu

PESIT – Dept. of MCA

A i Assignments t :

Explain the organization of the digital computer. What is the need for secondary storage devices. Write note on hard disk. Explain the working principle of different d e e t types o of p printers. te s What is an O.S. Explain its various functions. Diff b/w : Diff. Assemble & HLL What is Computer N/w. Explain briefly different types of nw nw.

http://www.pes.edu

PESIT – Dept. of MCA

A i Assignments t ( contt ) :

Differentiate between primary memory and secondary memory. Explain input and output devices along with examples. Define : OS O.S Compiler Interpreter Editor Edit Explain features of UNIX O.S Note on Internet & E-mail.

http://www.pes.edu

PESIT – Dept. of MCA

Logic is very important in programming. Consider the following procedure of washing clothes using a washing machine.

Fl Flowchart h t Example E l :

1) Put P the h clothes l h in i the h washtub. 2) Pour water. 3) Pour detergent powder. powder 4) Switch on the washing machine. 5) Set the timer and wait f a few for f minutes. i t 6) Drain the water out. 7) End.

http://www.pes.edu

PESIT – Dept. of MCA

This procedure gets the work k done. d Now, suppose the same steps are performed in a slightly different order.

Fl Flowchart h t Example E l ( cont ) :

1) Put the clothes in the washtub. h b 2) Switch on the washing machine. 3) Set the timer and wait for a few minutes. 4) Pour water. 5) Pour detergent powder. 6) Drain the water out. 7) End.

http://www.pes.edu

PESIT – Dept. of MCA

In this case, case your clothes may tear off, since you have switched on the washing machine before pouring water into it.

Fl Flowchart h t Example E l

If you apply the above analogy to programming, you should carefully think about the order in which the instructions are carried out by your computer. This is precisely what logic is all about.

( cont ) :

http://www.pes.edu

Related Documents