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

Post on 16-Oct-2020

26 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Saurabh Khatri Lecturer

Department of Computer TechnologyVIT, Pune

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 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.

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

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.

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

variables……statements…..

}

Functions{}

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

void main(){

printf(“Hello World\n”);

}

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

#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];

#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

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

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()

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

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;

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;

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

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.

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

Multiple line ---- /*this is

multiple commentline */

Operators : Arithmetic + _ * / % ^

Operators : Logical AND NOT OR

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

Operators : Bitwise

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

Post Decrement a-- Pre Decrement --a

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

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

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

result = c+d/a;

result = (c+d)/a;

result = c+d/b;

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

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

Precedence

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.

#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);

}

Output Character is A

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

# 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);

}

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

#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"); } }

Output m and n are not equal

#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"); } }

Output m is greater than n and not equal to 0

#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 ); }

Output AND_opr value = 0

XOR_opr value = 60OR_opr value = 60

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

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

top related