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

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

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

Introduction to C Language

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

Computers Programming Course 5. Iulian Năstac

Preview from Notesale.co.uk Page 6 of 52

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

EL2310 Scientific Programming

The C language. Introductory course #1

Room 3P16 Telephone: extension ~irjohnson/uqc146s1.html

C: How to Program. Week /Mar/05

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019

CSCI 171 Chapter Outlines

Chapter 2 - Introduction to C Programming


EL2310 Scientific Programming

Chapter 1 & 2 Introduction to C Language

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

edunepal_info

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

Work relative to other classes

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

Introduction to C An overview of the programming language C, syntax, data types and input/output

Reserved Words and Identifiers

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science

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:

Programming. Elementary Concepts

Differentiate Between Keywords and Identifiers

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

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

6.096 Introduction to C++ January (IAP) 2009

CS313D: ADVANCED PROGRAMMING LANGUAGE

EC 413 Computer Organization

Course Outline Introduction to C-Programming

Computers Programming Course 6. Iulian Năstac

Introduction to the C Programming Language

BLM2031 Structured Programming. Zeyneb KURT

Lecture 3. More About C

UNIT- 3 Introduction to C++

Operators and Expressions:

Programming in C Quick Start! Biostatistics 615 Lecture 4

Fundamentals of Programming

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

A Fast Review of C Essentials Part I

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

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

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

C - Basic Introduction

Presented By : Gaurav Juneja

Computer Systems Lecture 9

1.1 Introduction to C Language. Department of CSE

SEQUENTIAL STRUCTURE. Erkut ERDEM Hacettepe University October 2010

Fundamental of C programming. - Ompal Singh

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

3. Types of Algorithmic and Program Instructions

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Informatica e Sistemi in Tempo Reale

CSCE 110 PROGRAMMING FUNDAMENTALS

OBJECT ORIENTED PROGRAMMING

Computer Programming

Review for Test 1 (Chapter 1-5)

Computers in Engineering. Moving From Fortran to C Michael A. Hawker

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

CS 61C: Great Ideas in Computer Architecture Introduction to C

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 1

EL2310 Scientific Programming

Functions. Arash Rafiey. September 26, 2017

Motivation was to facilitate development of systems software, especially OS development.

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Week 2 Introduction to Computer and Algorithm (Part 2)

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

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

CS16 Exam #1 7/17/ Minutes 100 Points total

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

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

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

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

C Programming

Lecture 02 C FUNDAMENTALS

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

KARMAYOGI ENGINEERING COLLEGE, Shelve Pandharpur

Final CSE 131B Spring 2004

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Topic 6: A Quick Intro To C

Lesson 3 Introduction to Programming in C

Department of Computer Applications

P.E.S. INSTITUTE OF TECHNOLOGY BANGALORE SOUTH CAMPUS 1 ST INTERNAL ASSESMENT TEST (SCEME AND SOLUTIONS)

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

High Performance Programming Programming in C part 1

Variables and literals

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

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

ANSI C Programming Simple Programs

Motivation was to facilitate development of systems software, especially OS development.

Basics of Programming

bphanikrishna.wordpress.com

Transcription:

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 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 program System Libraries and Preprocessor Directives void 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 100 int 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];

#include When 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, etc 2. Mathematical computations Sine, Cosine, etc 3. Input/output : printf, scanf, etc Location of Header File : Windows: Linux: c:/tc/include /usr/local/include or /usr/include/i386-linux-gnu or /usr/include

Main Function The main function is generally the first programmerwritten 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; operator // Variable initialization using assignment All what previous statement does + Stores 10 to that memory location.

Comment line statements Single line ------ // Multiple line ---- /* this is multiple comment line */

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.234000 Integer value is 150 Double value is 20.123456 Octal value is 226 Hexadecimal 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 : 20 Multiplication of a, b is : 800 Division of a, b is : 2 Modulus 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 = 60 OR_opr value = 60

Homework a =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