INFERO ÔCÕ LECTURE SERIES -...

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

Transcript of INFERO ÔCÕ LECTURE SERIES -...

INFERO ‘C’ LECTURE SERIES

SESSION II“THE REAL C LANGUAGE”

WHAT YOU ARE EXPECTED TO KNOW

BEFORE THIS SESSION ??

•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 !!

WHAT YOUWILL LEARN IN

THIS SESSION ??

• 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

RECAP

LET US BEGIN !!

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

THIS WAS JUST A GLIMPSE INTO THE

HISTORY OF C.

NOW LETS ACTUALLY JUMP

INTO THE ‘C’

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

C PROGRAM MEMORY

MAP

C PROGRAM MEMORY MAP

Dynamic Memory

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

COMPILATION

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

SOME USEFUL COMMANDS

USED DURING COMPILATION

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

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

VARIABLES

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.

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

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

IDENTIFIERS

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

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

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.

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

• 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’

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

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.

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}

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

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