Top Banner
INFERO ‘C’ LECTURE SERIES SESSION II “THE REAL C LANGUAGE”
33

INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts

Sep 01, 2018

Download

Documents

dodat
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts

INFERO ‘C’ LECTURE SERIES

SESSION II“THE REAL C LANGUAGE”

Page 2: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts

WHAT YOU ARE EXPECTED TO KNOW

BEFORE THIS SESSION ??

Page 3: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts

•Basic Programming Terminology i.e.

•Algorithms/Flowcharts etc.

•“HelloWorld” Program. i.e. using the ‘printf’ statement in C.

•Writing a program in ‘gedit’ and compiling it using the ‘gcc’ command in Ubuntu.

•Have attempted the Programming Assignment sent after the previous session !!

Page 4: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts

WHAT YOUWILL LEARN IN

THIS SESSION ??

Page 5: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts

• How to name C Program Files !!

• C Program Memory Map

• Few techniques used during compilation.

• Using the ‘scanf’ statement to accept user input.

• Variables

• Keywords

• Data Types

• Operators

Page 6: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts

RECAP

Page 7: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts
Page 8: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts

LET US BEGIN !!

Page 9: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts

“HISTORY OF C”Developed by “Dennis Ritchie”

“Procedural” Programming Language

Actually developed for OS Programming.

Most Operating Systems(OS) are written in C and C++. eg. Windows, UNIX, Linux Kernel, etc.

Page 10: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts
Page 11: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts
Page 12: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts

THIS WAS JUST A GLIMPSE INTO THE

HISTORY OF C.

NOW LETS ACTUALLY JUMP

INTO THE ‘C’

Page 13: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts

HOW TO NAME C PROGRAM FILES

Most Important ---- extension ‘.c’

Few Rules/Conventions

All alphabets and numbers can be used.

Names should be in lowercase.

Can include underscores(_) or dashes(-).

Page 14: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts

C PROGRAM MEMORY

MAP

Page 15: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts

C PROGRAM MEMORY MAP

Dynamic Memory

Page 16: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts

• Program Code -- part of the memory where the written source code is stored, on execution.

• Stack -- linear type of memory, (“Last In First Out”) where local variables are stored.

• Heap -- unordered memory, i.e chunks of memory can be allocated without any order, where dynamic memory is allocated.

Page 17: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts

COMPILATION

Page 18: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts

✦ Converting the source code (i.e code written in the programming language) into the target code (i.e. code understood by the computer)

✦ Using command gcc <filename>.c in the terminal.

Page 19: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts

SOME USEFUL COMMANDS

USED DURING COMPILATION

Page 20: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts

-Wall optionThis command is used to enable and display all compiler warnings, in the source code.

These warnings are actually common programming errors, which do not affect the result of the program but are syntactically incorrect or unused memory blocks.

Very useful for beginners.

Page 21: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts

-o OptionThis command is used to specify the name of the output file; in case you wish to name it, rather than using the default name.After using this command, the program can run, simply by using this output filename. (i.e ./<filename>)

Page 22: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts

VARIABLES

Page 23: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts

Data storage location in the computer’s memory.

When a program is loaded into the computer memory, we do not know which part of the memory is used to store our data.

Variables are used to refer to these parts, rather than remembering the actual memory address.

Page 24: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts

This data stored can be integers, real numbers, alphabets or special characters.

Hence comes the need for specifying “data-types”.

Page 25: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts

IDENTIFIERS

❖Variables have a “Name” ---- Identifier

❖To simplify, consider Variables as boxes and Identifiers as their labels.

Page 26: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts

RULES/CONVENTIONS IN NAMING VARIABLES

Variable Name can have alphabets, digits and underscores; but the first letter has to be an alphabet or underscore.

Length of Name can be up to 247 characters long.

Some reserved words (known as “Keywords”) cannot be used as Variable Names.

Page 27: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts

KEYWORDSReserved Words that convey special meaning to the compiler.Cannot be used as Variable Names.Typically, there are 32 keywords in C Programming Language.

Page 28: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts

• auto

• break

• case

• char

• const

• continue• default

• do

• double

• else

• enum

• extern

• float

• for

• goto

• if

• int

• long

• register

• return

• short

• signed

• sizeof

• static

• struct

• switch

• typedef

• union

• unsigned• void

• volatile

• while

LIST OF ALL KEYWORDS IN ‘C’

Page 29: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts

DATA TYPESAs mentioned earlier, data is of different forms, hence to differentiate between them, we need “Data Types”.

Five Basic Data Types in C :

int

char

float

double

void

Page 30: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts

Size of Data Types varies across Compilers and Processors.

However typically, char is 1-byte, int is 2-bytes, float is 4-bytes and double is 8-bytes.

Page 31: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts

HOW TO DECLARE VARIABLES

<data-type> <variable-name>;

Eg.

int rollno=23;

float percentage;

char grade= ‘A’;

Is there any difference between

eg. 1,2 and 3}

Page 32: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts

VARIABLE ASSIGNMENT STATEMENTS

• variable = expression/value;

• L-value or Location value appears on the LHS of the = sign. Denotes the address which is stored into.

• R-value or Read value appears on the THS of the = sign. Expressions produce the R-value.

• “All L-values are R-values, but not all R-values are L-values”.

Page 33: INFERO ÔCÕ LECTURE SERIES - gymkhana.iith.ac.ingymkhana.iith.ac.in/sci-tech/infero/documents/cseries/Infero C... · •Basic Programming Terminology i.e. •Algorithms/Flowcharts

EXERCISEint a,b,c,d,e,f;

a=10;

b=20;

c=a+b;

d+5=30;

e+c=e;

50=f;

Is there any error in this code snip ??