+ All Categories
Home > Documents > Introduction to C/C++ Lecture 1 - Introduction to Programming · Lecture 1 - Introduction to...

Introduction to C/C++ Lecture 1 - Introduction to Programming · Lecture 1 - Introduction to...

Date post: 16-May-2018
Category:
Upload: nguyenque
View: 223 times
Download: 6 times
Share this document with a friend
28
Introduction to C/C++ Lecture 1 - Introduction to Programming Rohit Sehgal Nishit Majithia Association of Computing Activities, Indian Institute of Technology,Kanpur [email protected] [email protected] May 23, 2016 Rohit Sehgal Nishit Majithia (IITK) ACA May 23, 2016 1 / 15
Transcript
Page 1: Introduction to C/C++ Lecture 1 - Introduction to Programming · Lecture 1 - Introduction to Programming Rohit Sehgal Nishit Majithia ... Binary codes provides way to interact to

Introduction to C/C++Lecture 1 - Introduction to Programming

Rohit SehgalNishit Majithia

Association of Computing Activities,Indian Institute of Technology,Kanpur

[email protected]@cse.iitk.ac.in

May 23, 2016

Rohit Sehgal Nishit Majithia (IITK) ACA May 23, 2016 1 / 15

Page 2: Introduction to C/C++ Lecture 1 - Introduction to Programming · Lecture 1 - Introduction to Programming Rohit Sehgal Nishit Majithia ... Binary codes provides way to interact to

Overview

Content Coverage of the Course:

Elements of C/C++ programming languages

Data types

Sequential and conditional execution,Loops

Arrays,pointers

Strings and their Manipulation

Functions

User Defined Data Types in C

Migrating to C++,User defined Data Types Classes

Generic Programming Using Templates and STL

Rohit Sehgal Nishit Majithia (IITK) ACA May 23, 2016 2 / 15

Page 3: Introduction to C/C++ Lecture 1 - Introduction to Programming · Lecture 1 - Introduction to Programming Rohit Sehgal Nishit Majithia ... Binary codes provides way to interact to

Course Scheme

Course Websitehttp://www.cse.iitk.ac.in/users/rsehgal/intro2c/

Evaluation

One Quiz After first weekTwo programming AssignmentFinal Exam at the end of the coursePractice problems(Ungraded) will uploaded after every lecture oncourse webpage

Course Timings

18.00 to 19.30 - week daysExtra Classes will be announced.

Rohit Sehgal Nishit Majithia (IITK) ACA May 23, 2016 3 / 15

Page 4: Introduction to C/C++ Lecture 1 - Introduction to Programming · Lecture 1 - Introduction to Programming Rohit Sehgal Nishit Majithia ... Binary codes provides way to interact to

Course Scheme

Course Websitehttp://www.cse.iitk.ac.in/users/rsehgal/intro2c/

Evaluation

One Quiz After first weekTwo programming AssignmentFinal Exam at the end of the coursePractice problems(Ungraded) will uploaded after every lecture oncourse webpage

Course Timings

18.00 to 19.30 - week daysExtra Classes will be announced.

Rohit Sehgal Nishit Majithia (IITK) ACA May 23, 2016 3 / 15

Page 5: Introduction to C/C++ Lecture 1 - Introduction to Programming · Lecture 1 - Introduction to Programming Rohit Sehgal Nishit Majithia ... Binary codes provides way to interact to

Course Scheme

Course Websitehttp://www.cse.iitk.ac.in/users/rsehgal/intro2c/

Evaluation

One Quiz After first weekTwo programming AssignmentFinal Exam at the end of the coursePractice problems(Ungraded) will uploaded after every lecture oncourse webpage

Course Timings

18.00 to 19.30 - week daysExtra Classes will be announced.

Rohit Sehgal Nishit Majithia (IITK) ACA May 23, 2016 3 / 15

Page 6: Introduction to C/C++ Lecture 1 - Introduction to Programming · Lecture 1 - Introduction to Programming Rohit Sehgal Nishit Majithia ... Binary codes provides way to interact to

Code of conduct

strictly obey the rules of the CSE Department.Any destruction (specially writing stuff on benches) will result in thecancellation of registration.

Since every registered student is given an ID (badge) mentioning thecode of the courses he/she has registered. You are supposed to wearit in the class.

You can use Ground floor labs to access PC(possibly try to bringlaptops), you may ask Ankita(CSE Office) to provide you withuserid/passwd to access PCs and wifi.

Please Don’t hog the PCs for long.

Rohit Sehgal Nishit Majithia (IITK) ACA May 23, 2016 4 / 15

Page 7: Introduction to C/C++ Lecture 1 - Introduction to Programming · Lecture 1 - Introduction to Programming Rohit Sehgal Nishit Majithia ... Binary codes provides way to interact to

Suggestions?Questions?

Rohit Sehgal Nishit Majithia (IITK) ACA May 23, 2016 5 / 15

Page 8: Introduction to C/C++ Lecture 1 - Introduction to Programming · Lecture 1 - Introduction to Programming Rohit Sehgal Nishit Majithia ... Binary codes provides way to interact to

Computer Languages ?

Computer being an electronic device understand ON/OFF Signals.

Series of ON/OFF Instructions analogous to 1/0(binary Instructions).

Binary codes provides way to interact to Computers,MachineLanguage

Computer can do intended task,if provided with meaningful string of0,1(s) as these ON/OFF signals controls different components ofcomputers viz ALU,CU etc

Cumbersome to write a binary string even to add two nos.

Solution Assembly language ?

Rohit Sehgal Nishit Majithia (IITK) ACA May 23, 2016 6 / 15

Page 9: Introduction to C/C++ Lecture 1 - Introduction to Programming · Lecture 1 - Introduction to Programming Rohit Sehgal Nishit Majithia ... Binary codes provides way to interact to

Computer Languages ?

Computer being an electronic device understand ON/OFF Signals.

Series of ON/OFF Instructions analogous to 1/0(binary Instructions).

Binary codes provides way to interact to Computers,MachineLanguage

Computer can do intended task,if provided with meaningful string of0,1(s) as these ON/OFF signals controls different components ofcomputers viz ALU,CU etc

Cumbersome to write a binary string even to add two nos.

Solution Assembly language ?

Rohit Sehgal Nishit Majithia (IITK) ACA May 23, 2016 6 / 15

Page 10: Introduction to C/C++ Lecture 1 - Introduction to Programming · Lecture 1 - Introduction to Programming Rohit Sehgal Nishit Majithia ... Binary codes provides way to interact to

Computer Languages ?

Computer being an electronic device understand ON/OFF Signals.

Series of ON/OFF Instructions analogous to 1/0(binary Instructions).

Binary codes provides way to interact to Computers,MachineLanguage

Computer can do intended task,if provided with meaningful string of0,1(s) as these ON/OFF signals controls different components ofcomputers viz ALU,CU etc

Cumbersome to write a binary string even to add two nos.

Solution Assembly language ?

Rohit Sehgal Nishit Majithia (IITK) ACA May 23, 2016 6 / 15

Page 11: Introduction to C/C++ Lecture 1 - Introduction to Programming · Lecture 1 - Introduction to Programming Rohit Sehgal Nishit Majithia ... Binary codes provides way to interact to

Computer Languages ?

Computer being an electronic device understand ON/OFF Signals.

Series of ON/OFF Instructions analogous to 1/0(binary Instructions).

Binary codes provides way to interact to Computers,MachineLanguage

Computer can do intended task,if provided with meaningful string of0,1(s) as these ON/OFF signals controls different components ofcomputers viz ALU,CU etc

Cumbersome to write a binary string even to add two nos.

Solution Assembly language ?

Rohit Sehgal Nishit Majithia (IITK) ACA May 23, 2016 6 / 15

Page 12: Introduction to C/C++ Lecture 1 - Introduction to Programming · Lecture 1 - Introduction to Programming Rohit Sehgal Nishit Majithia ... Binary codes provides way to interact to

Computer Languages ?

Computer being an electronic device understand ON/OFF Signals.

Series of ON/OFF Instructions analogous to 1/0(binary Instructions).

Binary codes provides way to interact to Computers,MachineLanguage

Computer can do intended task,if provided with meaningful string of0,1(s) as these ON/OFF signals controls different components ofcomputers viz ALU,CU etc

Cumbersome to write a binary string even to add two nos.

Solution Assembly language ?

Rohit Sehgal Nishit Majithia (IITK) ACA May 23, 2016 6 / 15

Page 13: Introduction to C/C++ Lecture 1 - Introduction to Programming · Lecture 1 - Introduction to Programming Rohit Sehgal Nishit Majithia ... Binary codes provides way to interact to

Computer Languages ?

Computer being an electronic device understand ON/OFF Signals.

Series of ON/OFF Instructions analogous to 1/0(binary Instructions).

Binary codes provides way to interact to Computers,MachineLanguage

Computer can do intended task,if provided with meaningful string of0,1(s) as these ON/OFF signals controls different components ofcomputers viz ALU,CU etc

Cumbersome to write a binary string even to add two nos.

Solution Assembly language ?

Rohit Sehgal Nishit Majithia (IITK) ACA May 23, 2016 6 / 15

Page 14: Introduction to C/C++ Lecture 1 - Introduction to Programming · Lecture 1 - Introduction to Programming Rohit Sehgal Nishit Majithia ... Binary codes provides way to interact to

Languages Above Machine Language

Assembly Language provides Mnemonics.

E.g ADD,LOAD,STORE, need not to remember binary codes(s) forthese Operations.

Assembler,converts Assembly Language to Machine.

Programming in Assembly, Why not something similar to NaturalLanguages like English.

High level Languages C,C++,Java,python

Rohit Sehgal Nishit Majithia (IITK) ACA May 23, 2016 7 / 15

Page 15: Introduction to C/C++ Lecture 1 - Introduction to Programming · Lecture 1 - Introduction to Programming Rohit Sehgal Nishit Majithia ... Binary codes provides way to interact to

Languages Above Machine Language

Assembly Language provides Mnemonics.

E.g ADD,LOAD,STORE, need not to remember binary codes(s) forthese Operations.

Assembler,converts Assembly Language to Machine.

Programming in Assembly, Why not something similar to NaturalLanguages like English.

High level Languages C,C++,Java,python

Rohit Sehgal Nishit Majithia (IITK) ACA May 23, 2016 7 / 15

Page 16: Introduction to C/C++ Lecture 1 - Introduction to Programming · Lecture 1 - Introduction to Programming Rohit Sehgal Nishit Majithia ... Binary codes provides way to interact to

Languages Above Machine Language

Assembly Language provides Mnemonics.

E.g ADD,LOAD,STORE, need not to remember binary codes(s) forthese Operations.

Assembler,converts Assembly Language to Machine.

Programming in Assembly, Why not something similar to NaturalLanguages like English.

High level Languages C,C++,Java,python

Rohit Sehgal Nishit Majithia (IITK) ACA May 23, 2016 7 / 15

Page 17: Introduction to C/C++ Lecture 1 - Introduction to Programming · Lecture 1 - Introduction to Programming Rohit Sehgal Nishit Majithia ... Binary codes provides way to interact to

Languages Above Machine Language

Assembly Language provides Mnemonics.

E.g ADD,LOAD,STORE, need not to remember binary codes(s) forthese Operations.

Assembler,converts Assembly Language to Machine.

Programming in Assembly, Why not something similar to NaturalLanguages like English.

High level Languages C,C++,Java,python

Rohit Sehgal Nishit Majithia (IITK) ACA May 23, 2016 7 / 15

Page 18: Introduction to C/C++ Lecture 1 - Introduction to Programming · Lecture 1 - Introduction to Programming Rohit Sehgal Nishit Majithia ... Binary codes provides way to interact to

Languages Above Machine Language

Assembly Language provides Mnemonics.

E.g ADD,LOAD,STORE, need not to remember binary codes(s) forthese Operations.

Assembler,converts Assembly Language to Machine.

Programming in Assembly, Why not something similar to NaturalLanguages like English.

High level Languages C,C++,Java,python

Rohit Sehgal Nishit Majithia (IITK) ACA May 23, 2016 7 / 15

Page 19: Introduction to C/C++ Lecture 1 - Introduction to Programming · Lecture 1 - Introduction to Programming Rohit Sehgal Nishit Majithia ... Binary codes provides way to interact to

C/C++

Compiler Convert HLL to Assembly.

Programming Computers is now easier.

Example (Compilers for C/C++)

$ gcc <filename.c> -o <outputfilename>

$ g++ <filename.cpp> -o <outputfilename>

.c,.cpp are the file extensions for C,C++ source file respectively.

Rohit Sehgal Nishit Majithia (IITK) ACA May 23, 2016 8 / 15

Page 20: Introduction to C/C++ Lecture 1 - Introduction to Programming · Lecture 1 - Introduction to Programming Rohit Sehgal Nishit Majithia ... Binary codes provides way to interact to

C/C++

Compiler Convert HLL to Assembly.

Programming Computers is now easier.

Example (Compilers for C/C++)

$ gcc <filename.c> -o <outputfilename>

$ g++ <filename.cpp> -o <outputfilename>

.c,.cpp are the file extensions for C,C++ source file respectively.

Rohit Sehgal Nishit Majithia (IITK) ACA May 23, 2016 8 / 15

Page 21: Introduction to C/C++ Lecture 1 - Introduction to Programming · Lecture 1 - Introduction to Programming Rohit Sehgal Nishit Majithia ... Binary codes provides way to interact to

Compiling and Running

Linux users gcc,g++$ sudo apt-get install gcc g++

Windows users:

Install Virtual Machine with Linux system.https://www.virtualbox.org/wiki/Downloads

Ubuntu 16.04 Linux System vmdk @https://drive.google.com/file/d/0B78zyZuoC7SAbE00UEplWDEwQzQ/view

User-name - intro2cPassword - summerschool

OR,Install Mingw,http://www.mingw.org/

In class, we will be working with Linux System

Rohit Sehgal Nishit Majithia (IITK) ACA May 23, 2016 9 / 15

Page 22: Introduction to C/C++ Lecture 1 - Introduction to Programming · Lecture 1 - Introduction to Programming Rohit Sehgal Nishit Majithia ... Binary codes provides way to interact to

Our First Program

Example (C)

#include<stdio.h>

int main()

{

printf("Hello World");

return 0;

}

Example (C++)

#include<iostream>

using namespace std;

int main()

{

cout<<"Hello World";

return 0;

}

Now on-wards we will follow C’s syntax.

Rohit Sehgal Nishit Majithia (IITK) ACA May 23, 2016 10 / 15

Page 23: Introduction to C/C++ Lecture 1 - Introduction to Programming · Lecture 1 - Introduction to Programming Rohit Sehgal Nishit Majithia ... Binary codes provides way to interact to

# pre-processor Directives

Replaced before compilation starts.$ gcc -E demo.c > preprocessed file.dat

#include<>,#include””, includes the content of file in source file.

these file stores declarations of functionsfunctions are part of program that does some computations details infunctions lecture

others #define,#ifdef,#ifndef etc.

#define - text replacement blindly.e.g

Rohit Sehgal Nishit Majithia (IITK) ACA May 23, 2016 11 / 15

Page 24: Introduction to C/C++ Lecture 1 - Introduction to Programming · Lecture 1 - Introduction to Programming Rohit Sehgal Nishit Majithia ... Binary codes provides way to interact to

The Main function

Entry point of code execution as program may contain severalfunctions.

body of any function is enclosed with { }return 0 ??, exit status, we will see later.

Rohit Sehgal Nishit Majithia (IITK) ACA May 23, 2016 12 / 15

Page 25: Introduction to C/C++ Lecture 1 - Introduction to Programming · Lecture 1 - Introduction to Programming Rohit Sehgal Nishit Majithia ... Binary codes provides way to interact to

printf

writes to standard output(monitor).

first argument is always a format specifier-how the output is printedto screen.

Rohit Sehgal Nishit Majithia (IITK) ACA May 23, 2016 13 / 15

Page 26: Introduction to C/C++ Lecture 1 - Introduction to Programming · Lecture 1 - Introduction to Programming Rohit Sehgal Nishit Majithia ... Binary codes provides way to interact to

Comments

Compiler do not read comments.

// this is a single line comment.

/* this comment spansmultiple lines */

Rohit Sehgal Nishit Majithia (IITK) ACA May 23, 2016 14 / 15

Page 27: Introduction to C/C++ Lecture 1 - Introduction to Programming · Lecture 1 - Introduction to Programming Rohit Sehgal Nishit Majithia ... Binary codes provides way to interact to

1 #i n c l u d e <s t d i o . h>2 #de f i n e N 103 /∗ Block4 ∗ comment ∗/5

6 i n t main ( )7 {8 i n t i ;9

10 // L ine comment .11 printf ( ” He l l o World %d” ,N ) ; // p r i n t s to s td output12 r e t u r n 0 ;13 }

Rohit Sehgal Nishit Majithia (IITK) ACA May 23, 2016 15 / 15

Page 28: Introduction to C/C++ Lecture 1 - Introduction to Programming · Lecture 1 - Introduction to Programming Rohit Sehgal Nishit Majithia ... Binary codes provides way to interact to

The End

Rohit Sehgal Nishit Majithia (IITK) ACA May 23, 2016 15 / 15


Recommended