c basics

27
BASICS OF C PROGRAMMING

Upload: abhijit-pattnaik

Post on 23-Dec-2015

214 views

Category:

Documents


0 download

DESCRIPTION

kiui

TRANSCRIPT

BASICS OF C PROGRAMMING

COMPILER• A compiler is a piece of code that translates the high level language into machine

language. When a user writes a code in a high level language such as Java and wants it to execute, a specific compiler which is designed for Java is used before it will be executed. The compiler scans the entire program first and then translates it into machine code which will be executed by the computer processor and the corresponding tasks will be performed.

Interpreter• Interpreters are not much different than compilers. They also convert the

high level language into machine readable binary equivalents. Each time when an interpreter gets a high level language code to be executed, it converts the code into an intermediate code before converting it into the machine code. Each part of the code is interpreted and then execute separately in a sequence and an error is found in a part of the code it will stop the interpretation of the code without translating the next set of the codes.

differences between compiler and interpreter are listed below

• The interpreter takes one statement then translates it and executes it and then takes another statement. While the compiler translates the entire program in one go and then executes it.

• Compiler generates the error report after the translation of the entire page while an interpreter will stop the translation after it gets the first error.

• Compiler takes a larger amount of time in analyzing and processing the high level language code comparatively interpreter takes lesser time in the same process.

• Besides the processing and analyzing time the overall execution time of a code is faster for compiler relative to the interpreter.

STAGES OF COMPILATION

• PREPROCESSING : expand macros, removing comment lines, included header files

• COMPILATION : source code to assembly language

• ASSEMBLY : assembly to machine code• LINKING : create final executable

DATA TYPES

• Char – 8bit /1byte• Int – 2 byte/4byte• Float – 4 byte• Double – 8 byte

MODIFIERS

• Signed – signed (+/-)• Unsigned – only +• Short – 2byte• Long – 4 byte

Big-endian and Little-endian

• Big-endian system address are always in ABCD and data is stored in ABCD order.

• Little-endian system address are always in ABCD and data is stored in DCBA order.

ASCII TABLE

Properties of variable

• Scope of the variable (local and global)• Life of the variable (initialization and free)• Default initial value of a variable• Storage of a variable (Memory / register)• Lvalue and Rvalue of the variable• Qualifier of the variable (const and volatile)

Variable :

A variable is a named object that resides in the RAM and is capable of being examined and modified.

Types of variable

(a)Variables which can store only one data at time. Example: integer variables, char variables, pointer variables etc.

(b)Variables which can store more than one data of similar type at a time. Example: array variables

(c) Variables, which can store more than one value of dissimilar type at a time. Example: structure or union variables.

Rules for naming variables

• Variable name must be within 32 char in 16bit compiler such as turbo C and 4096 characters in 32 bit compiler like GCC.– Int abcdefghijklmnopqrstuvwxyz123456=200;

• The First char must be an alphabet or an underscore and the rest can be alphabets ,digits and underscore.

• No space or special character allowed.• No keyword can be a variable name.

Lvalue• LValue: Lvalue stands for left value. In any assignment statement LValue must be a

container i.e. which have ability to hold the data. In c has only one type only container and which is variable. Hence LValue must be any variable in c it cannot be a constant, function or any other data type of c.

//10=5; LValue cannot be a integer constant

//max=20; //Lvalue cannot be a micro constant

//b=11; Lvalue cannot be a constant variable

//float=3.3f; Lvalue cannot be a data type

//abc={“sachin”,5}; Lvalue cannot be a data type

//BLUE =2; Lvalue cannot be a enum constant

Rvalue

• RValue: In any assignment statement RValue must be anything which can return and constant value or it is itself a constant. RValue can be any c constants, variables, function which is returning a value etc.

//RValue can be a constant.//RValue can be a constant variable//RValue can be a variable.//RValue can be a enum constant.//RValue can be a function.

Declaration of variables in c

Declaration of variables means to acknowledge the compiler only about variable name and its data type with its modifiers but compiler doesn’t reserve any memory for the variables.

(1) Since declaration variable doesn’t get any memory space so we cannot assign any value to variable.

(2) We cannot use any operator with a variable which has only declared.

(3) We can declare any variable either globally or locally.(4) A same variable can be declared many times.

Definition of variables in cA c statement in which a variable gets a memory is known as definition of

variable. All auto, register, static and initialized extern variable are example of definition of variables.

(1)If any variable has not declared then it declaration occurs at the time of definition.

(2)We can use any operator after the definition of variable.

(3)Definition of variables can be globally or locally.

(4)A register variable gets CPU instead of memory in definition.

(5)A static or extern variable gets memory at the compile time while auto and register variables get memory or CPU at the run time.

Few questions

• Swap two variables without using third variable.

• Write a c program without using any semicolon which output is: Hello world.

Operators

What is Operator? C consists of 45 operators.Operators specify the basic operations that are to be performed with

the basic data objects. expression 4 + 5 is equal to 9. Here 4 and 5 are called operands and +

is called operator. C language supports following type of operators.

Arithmetic OperatorsLogical (or Relational) OperatorsBitwise OperatorsAssignment OperatorsMisc Operators

Arithmetic Operators:Assume variable A holds 10 and variable B holds 20

Logical (or Relational) Operators:

Bitwise Operators:

Bitwise operator works on bits and perform bit by bit operation.

Assume if A = 60; and B = 13; Now in binary format they will be as follows:

A = 0011 1100B = 0000 1101-----------------A&B = 0000 1100A|B = 0011 1101A^B = 0011 0001~A = 1100 0011

Assignment Operators:

Misc Operators

Operator precedence table