unit 1: introduction to c language€¦ · introduction to c language the c programming language...

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

Upload: others

Post on 16-Oct-2020

19 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

Saurabh Khatri Lecturer

Department of Computer TechnologyVIT, Pune

Unit 1: Introduction to C Language

Page 2: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

Introduction to C Language The C programming language was designed by Dennis

Ritchie at Bell Laboratories in the early 1970s Traditionally used for systems programming. In fact C Language was part of Unix OS development. Standardized in 1989 by ANSI (American National Standards

Institute) known as ANSI C.

Page 3: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

Introduction to C Language A C development environment includes System libraries and headers: a set of standard libraries and their

header files. Compiler: converts source to object code for a specific platform Linker: resolves external references and produces the

executable module

Page 4: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

C as Middle Level Language Allows Inline assembly code. Allows to access system registers. Allows to access memory directly. C has the efficiency as close to the assembly language.

Page 5: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

Basic Structure of C programSystem Libraries and Preprocessor Directivesvoid main(){

variables……statements…..

}

Functions{}

Page 6: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

My First C Program#include <stdio.h>#include <stdlib.h>

void main(){

printf(“Hello World\n”);

}

Page 7: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

Preprocessor Directives Preprocessor directives are the statements that is not part of

C code. However these statements are processed by a pre processor. These lines are always preceded by a hash sign (#) Eg: #include, #define

Page 8: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

#define #define identifier replacement

When the preprocessor encounters this directive, it replaces any occurrence of identifier in the rest of the code by replacement.

#define TABLE_SIZE 100int table1[TABLE_SIZE]; int table2[TABLE_SIZE];

After the preprocessor has replaced TABLE_SIZE, the code becomes equivalent to:

int table1[100]; int table2[100];

Page 9: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

#includeWhen the preprocessor finds an #include directive it replaces it by the entire content of the specified file.

#include <file> Ex: #include<stdio.h> contains the functions printf, scanf, etc

Page 10: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

System libraries and Header Files A header file is a file containing C declarations and prototypes for

standard functions. There are some predefined operations like:

1. String handling – concatenate, length, etc2. Mathematical computations – Sine, Cosine, etc3. Input/output : printf, scanf, etc

Location of Header File : Windows: c:/TC/includeLinux: /usr/local/include or

/usr/include/i386-linux-gnu or /usr/include

Page 11: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

Main Function The main function is generally the first programmer-

written function. It runs when a program starts It is invoked directly from the system-specific initialization. Syntax : void main()

Page 12: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

Syntax and Semantics Syntax: The form of writing the statements in a programming language. These are the rules to be followed to write correct code in a

Programming language. Semantics is the meaning of those statements and thus the

after effect. For example, the semantics for the syntax for (int i=0; i<10; i++) x += i; ----------in C

and FOR i IN 0..9 LOOP x := x+i; END LOOP; ------in Ada

Page 13: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

Variables and Constants Reincarnation from Mathematics. A variable is a data name that can be used to store a data

value. int p = 10;

Constants: Its value cannot be changed by the program during its execution. Literal constant : “Hello”, 7, 1.345

----- Literals are used to initialize the variables. Named constant : const int p = 3;

Page 14: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

Data Types Data types refers to an extensive system for declaring

variables of different types. Declaring a data type of a variable speaks of the range and

type of the values can be stored in the variable. Example: int n ; float x; char c; Boolean false;

Page 15: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

Data Types

Type Storage size Value range

char 1 byte -128 to 127 or 0 to 255

Int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647

float 4 byte 1.2E-38 to 3.4E+38

Page 16: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

Variable declaration int n ; Tells the compiler that n will be used to store an integer

value. n is a memory location. n corresponds to some memory address.

int n = 10; // Variable initialization using assignment operator

All what previous statement does + Stores 10 to that memory location.

Page 17: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

Comment line statements Single line ------ //

Multiple line ---- /*this is

multiple commentline */

Page 18: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

Operators : Arithmetic + _ * / % ^

Page 19: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

Operators : Logical AND NOT OR

Page 20: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

Operators : Relational == != > > >= <=

Page 21: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

Operators : Bitwise

Page 22: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

Increment and Decrement Operator Post Increment a++ Pre Increment ++a

Post Decrement a-- Pre Decrement --a

Page 23: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

Expressions A Mathematical equation Ex: a = 5*3/(3+4) ;

b = (c-d)/(4*d) ;

Page 24: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

a =5; b = 7; c = 10; d = 15;

result = c+d/a;

result = (c+d)/a;

result = c+d/b;

Page 25: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

A = 0011 1100 B = 0000 1101 ----------------- A&B 0000 1100 A|B 0011 1101 ~A 1100 0011

Page 26: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

Points to Note What is the difference ! A and ~A What is the difference A&B and A&&B A|B and A||B

Page 27: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

Precedence

Page 28: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

Versions of Printf Statement The printf() function is used to print the character, string,

float, integer, octal and hexadecimal values onto the output screen.

To display the value of an integer variable, we use printf statement with the %d format specifier.

Similarly %c for character, %f for float variable,%s for string variable, %lf for double, %x for hexadecimal variable.

To generate a newline, we use \n in C printf statement.

Page 29: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

#include <stdio.h>

int main(){

char ch = 'A';float flt = 10.234;int no = 150; double dbl = 20.123456;

printf("Character is %c \n", ch);printf("Float value is %f \n", flt);printf("Integer value is %d\n" , no);printf("Double value is %lf \n", dbl);printf("Octal value is %o \n", no);printf("Hexadecimal value is %x \n", no);

}

Page 30: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

Output Character is A

Float value is 10.234000Integer value is 150Double value is 20.123456Octal value is 226Hexadecimal value is 96

Page 31: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

# include <stdio.h>int main(){

int a=40,b=20, add,sub,mul,div,mod;add = a+b;sub = a-b;mul = a*b;div = a/b;mod = a%b;printf("Addition of a, b is : %d\n", add);printf("Subtraction of a, b is : %d\n", sub);printf("Multiplication of a, b is : %d\n", mul);printf("Division of a, b is : %d\n", div);printf("Modulus of a, b is : %d\n", mod);

}

Page 32: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

Output Addition of a, b is : 60

Subtraction of a, b is : 20Multiplication of a, b is : 800Division of a, b is : 2Modulus of a, b is : 0

Page 33: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

#include <stdio.h> int main() { int m=40,n=20; if (m == n) { printf("m and n are equal"); } else { printf("m and n are not equal"); } }

Page 34: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

Output m and n are not equal

Page 35: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

#include <stdio.h> int main() { int m=40,n=20; if (m>n && m !=0) { printf("m is greater than n and not equal to 0"); } }

Page 36: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

Output m is greater than n and not equal to 0

Page 37: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

#include <stdio.h> int main() { int m=40,n=20,AND_opr,OR_opr,XOR_opr ;

AND_opr = (m&n); OR_opr = (m|n); XOR_opr = (m^n); printf("AND_opr value = %d\n",AND_opr ); printf("XOR_opr value = %d\n",XOR_opr ); printf("OR_opr value = %d\n",OR_opr ); }

Page 38: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

Output AND_opr value = 0

XOR_opr value = 60OR_opr value = 60

Page 39: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

Homeworka =5 , b = 10, x = 5; (a/b)*b + a%b – a x = (x << 1) | 1; y = x++ + --x; a /= b * 5;

0 11 8 0

Page 40: Unit 1: Introduction to C Language€¦ · Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally

Input Statement scanf() function scanf(" format string ", arguments ); %c" - Reads a single character "%d" - Reads a signed integer (int) numbers ( white space is

used to separate multiple numbers ) %f for float values