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

Post on 16-May-2018

223 views 6 download

transcript

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

Rohit SehgalNishit Majithia

Association of Computing Activities,Indian Institute of Technology,Kanpur

rsehgal@cse.iitk.ac.innishitm@cse.iitk.ac.in

May 23, 2016

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

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

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

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

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

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

Suggestions?Questions?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

# 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

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

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

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

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

The End

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