Sunteți pe pagina 1din 22

Eof defined in stdio.h 32 keywords in C.

-1

what is a data type? data type is a mechanism thru which we can tell we tell what a variable contains + what operations can be performed on it. header files are for abstraction abstraction is an amazing thing in design becuase it provides loose coupling. typedef long mint; mint i; mint j; By default return type of function is int ,also the arguments r by default int Memory allocation is being done in order of chunk of 4bytes,in VC++ Structure :It is an aggregated type. examples process is an aggreate of processid and priority which could be of int types. Sprintf(str,abc %d,54);

what does char indicate in C? it act like a char data type, ok, but more important it indicates a byte. 1. function declaration and function definition. 2. variable declaration and variable definition if you associate a type with a variable it is called declaration, if a particular memory region is associated with a variable it is called definition. what ever variables you find inside the structure is only declarations.

pass by value : 1. we pass an argument to the function and in the function we get a new copy of the variable being passed.

What is a Pointer? 1. pointer is a mechanism thru which we can do pass by address. 2. We can also do dynamic allocation. pointer has got some special capabilities, example is it can store address of some variable and then we can refer to that variable. pointer type tells the pointer how many bytes to read. int *p; we mention a type to the pointer, becuase when we operate on the pointer, pointer needs to know how bytes to read. int *p; if we assign any address ot p. and then we put *p=?, *P, the pointer will start from the address and use the 4 bytes char *x; x= ? *x , the pointer will read only one byte and not 4 bytes. Because pointer type is a character. int *x =NULL; x = &some variable name; *x , now we are operating on the variable the pointer is pointing to. Differencing the pointer. main() { int *p ; int j; p = &j; f1(p); // this is ok, we need to pass pointer f1(*p); // we are not passing pointer to f1, we are passing the value what the pointer is pointing to. } what is the drawback of pointer. 1. drawback of pointer is we can freak around in the memory ie we do high jmp and long jump in memory when we do it os gets upset and it will slap our program and our program will die.

Array

1. cont... allocation of memory with a common name ,content of array accessed


with subscript. group of contigous elements defined with a common name.

WHAT IS PASS BY ADDRESS. pass by address is a mechansim thru whcih we pass variables to conserve memory space. you can use it as long as you are in the same process.

CONST POINTER. POINTERS ADDRESS CANNOT BE CHANGED., VALUE CAN BE. char *const x; POINTER TO A CONSTANT VALUE CANNOT BE CHANGED, ADDRESS CAN BE INT CONST *P; const int *y; pc. if you want yoiur function not to change the value of the pointer then you should use pc. Eg. we are writing a function which reads deviceId, that function should not change deviceId any way. that means pass by deviceid as a pc.

what is void pointer?? void pointer is a mechanism in C. to indicate that pointer can hold any type of address. ie can hold address of any type of variable. Void ptr we told can take anything and return anything. 1. Void ptr is a thing thru which we can assign any kind of variable address to it. 2. We cannot deference the void pointer.

void *ptr = ? ? can be an address of an int variable or a struct variable. void f1(int i) {

} f1(3); struct Device d; f1(d); //not work. you want to make f1 take anything f1(void *ptr) { } int i; struct Device d; f1(&i); f1(&d); void *ptr = &i; *ptr ='A'; //not possible. it wil give us compilation error. i have a void pointer, inside it there is some address ,I dont which type of address is there, then how i can access the contents of the address preset in void ptr. forget it, you cannot access it.

Regions of memory : in the system, we have different memory regions let bother about only two as of now. 1. stack : where system will allocate memory and system will take care of deallcoation of memory 2.heap: - we need to allocate and we need to deallocate memory. Memory can b allocated using calloc, malloc, relloc. Memory leak: we have things only on heap, nothing on stack.

memory leak is like a water tank leak. we dont get a immediate problem, but problem will come in an amplified way.

Dangling pointer: something in stack, but nothing in heap.


dangling pointer, we have a pointer it is pointing to some invalid region in memory and we are

operating on it. what will happen it depends where the pointer is pointing to. one thing is for sure we get unpredictable results.

Function Pointers. used to achieve callbacks. example of callback usage is events what is event. change in state and somebody has to know about the change. explain callbacks. what is callbacks, callbacks are functions primarily. YOU CAN NEVER UNDERSTAND FP, UNTIL YOU KNOW THERE ARE TWO PEOPLE WHEN YOU USE FUNCTION POINTER AND NOT ONE. syntax of function pointers. 1.creating the function pointer 2. assigning the functoin pointer 3. deferencing the functon pointer ( this will result in calling of the function) How to show I am very stupid, do 1 to 3 all by myself. what happens in real life is. 1 and 3 is done by person A 2 is done by person B. because of this only person A will be able to call the function without kwowing the function name. 1.creating the function pointer do you know the signature of the function which you want to point to. in case you dont know then we cannot create a function pointer. signature here, it means input arguments, output arguments name of the function does not matter. void (*x)()=NULL; X IS A FUNCTION POINTER, IT CAN POINT TO ANY FUNCTION WHOSE RETURN TYPE IS VOID AND INPUT ARGUMENTS IS NOT MENTIONED.

Eg of function ptr. atexit() A registerforMouseClick(int (*y)(int) ) { HE WILL DETECT MOUSE CLICK AND THEN TELL (*Y)(3);

} b INT F3(INT X) { if( x == 3) printf("right clicked the mouse"); else printf("normal click"); } main() { registerforMouseClick(f3); } ---------------------------------------------------------------------------------------------#include <stdio.h> void f1() { } void f2() { } printf("f2 ");

printf("f1 ");

int main() { void (*x)()=NULL; // FUNCTION POINTER CREATION. // X IS VARIABLE , IT IS A FUNCTION POINTER, IT CAN HOLD //THE ADDRESS OF ANY FUNCTION WHOSE RETURN TYPE IS VOID //INPUT ARGUMENT IS NOT SPECIFIED. x = f2; // function pointer assignment. step2 (*x)(); // this results in the function call step 3 }

Array name and function name are both const pointers.

Storage classes. 1. 2. 3. 4. storage class talks about how long a variable is available in memory . storage class applies to variables present in stack only. there are 4 storage classes in c and a variable can use only one of the 4 storage classes. scope and storage class are not the same things.

scope talks about where you can access. storage class talks about how a long a variable is in memory. 4 storage classes are 1. 2. 3. 4. auto register static extern.

static storage class, nice thing to know. when a vairalbe is of static storage class, it is avaialble in memory when the process starts all the way till the process ends. when we make a global variable static, what happens is that variable gets a file scope.

Linkages. 1. internal linkage -- resource cannot be accessed outside the file -- all static resources are that kind of linkage. 2. external linkage. -- resources can be accesed otuside the file example non static resources. 3. no linkage

Union is an exclusive type, if we have union called Travel union Travel { int shipId; char flightId; }; Union Travel t; t.shipId=3; t.flightId ='a'; shipId disppaers

In data structure what we do is we decide the operations and then decide to store data to aid the operations example in a queue, we have decided that we want whichever is put first, to be removed first. Since we decided this operation we have decided to go for a queue.

what is IO? movement of info from place A to place B. what can A and B be. 1. file 2. memory.

correct techical meaning. 1. binary files recognize data types 2 in binary format, it may take 1 byte or 2 byte or 4 bytes or 8 bytes. if Iwrite 2 in text mode it takes only 1 byte. 222 in text mode. 3 bytes. 2222 in text mode 4 bytes. 222 in binary mode 3, 2,8 or 4. SCANF IS USED FOR READING -- reading -- input streams print is used for writing -- outputstreams.

what is difference between C and C++, tell me just one difference? 1. C is loosely typed 2. C++ is relatively tightly typed. int *x = malloc(sizeof(int)); //wont compile in C++ will compile in C. C we have no function overloading C++ we have function overloading. function overloading

1. we have the same function name. 2. but different input argument types. f1(int x) f1(int x, int y) In C function are called just because of function name. In C++, functions are called because of function name as well as input arguments, because overloading exists.

char *s ="Hello"; what is the implication of it. s is a immutable string, this string cannot be changed in any way.

difference between macro and a function. 1. macro results in text substitution -- macro is a preprocessor directive. 2. function results in a call ie it has got a runtime context 3. macros cannot enforce types, functions can.

Input Output
#include <stdio.h> #include <stdlib.h> int main() { FILE *fp; char ch;

if((fp=fopen("a.exe", "w"))==NULL) { printf("Cannot open file.\n"); return 1; } do { ch = getchar(); putc(ch, fp); } while (ch != '$'); fclose(fp); return 0; }

#include <stdlib.h> #include <string.h> #include <stdio.h> int main(void) { char str[80];

FILE *fp; if((fp = fopen("TEST", "w"))==NULL) { printf("Cannot open file.\n"); return 1; } do { printf("Enter a string (CR to quit):\n"); gets(str); strcat(str, "\n"); /* add a newline */ fputs(str, fp); } while(*str!='\n'); } return 0;

#include <stdio.h> #include <stdlib.h> int main(void) { FILE *fp; double d = 12.23; int i = 101; long l = 123023L; if((fp=fopen("test.txt", "wb+"))==NULL) { printf("Cannot open file.\n"); exit(1); } fwrite(&d, sizeof(double), 1, fp); fwrite(&i, sizeof(int), 1, fp); fwrite(&l, sizeof(long), 1, fp); fclose(fp); printf("program is working"); } return 0;

malloc and calloc both allocate memory on the heap. malloc does not initalize memory , calloc does intialize memory to zero. realloc -- you have already allocated memory, and you find that it is not sufficient, so you want a new chunk of memory to be allocated and whatever is there in the existing memory region should be copied to the new region. then we can use realloc. if realloc fails. old details exist in the old region as it is.

What is Enum??

enum is a mechanism thru which we can put some predefined range of values into the variable .

Differences between C and C++ 1. references in C++. 2. what is diffdrence pass by refrence, pass by address when to use pointers in C++,if you want to do dynamic allocation. NO I want to do pass by address, STOP, DONT DO IT IN c++ use pass by reference. that what i said, pass by referencemeans pass by address, NO in C++, pass by reference is different from pass by address.

we are all talking about C++ 1. strongly typed and loose typed nature 2. function calling differerence between c and c++ 3. reference introduced. concept is over, reference needs an a variable. 4. dont use pass by address in C++. use pass by reference. and both are not same in C++. 5. pass by address, pass by reference, pass by value. pass by address, pass by value have something in common, because in both case new variables get created in the function. pass by ref no new variable gets created in the function.
#include <stdio.h> #include <stdlib.h> void passByValue(int x) { printf("pbv"); x =0; } void passByAddress(int *const x) { printf("pba"); *x =0; } void passByReference(int &x) { printf("pbr"); x =0; } int main() { int k =4; //passByValue(k); //passByAddress(&k); passByReference(k); printf("%d",k); return 0; }

Object oriented ness is a mechanism thru which we can get maintainable software because it enforces two important things Abstraction and Encapsulation. what is abstraction, example for header file, but in OO world, interface. interfaces in OO helping abstraction. what is encapsulation encapsulation is mechanism thru which we access data in an organized way.

what is an object. object is something which has got uniqueness. class is a generic way of looking at an object, class is unit for achieving encapsulation in OO.

what do you mean by static and non static variables.? Are you talking about storage classes or, are you talking about variables present inside the class. class A { }

int i; static int j; // dont repeat the C story, it is fully wrong.

non static varibales contribute to the identity of the object of the class. example if we have a class called A, we have a non static variable called i inside A. when we create three objects of class A, o1,o2 and o3 o1 , o2 and o3 get separate copies of i. static variable. static variables exist without hte object also and they r shared across the objects. if we have a class called B, we have a static variable called k. we have not created any object of class B. even k will be active. ok we created 2 ojects of B , o5 and o6. o5 and 06 share a same copy of k. Non static variables come into life only when we create object. Static variables when we define it.

why did we write static functions??

To deal with static variables outside the class. static functions can be accessed without an object also static functions cannot access non static parts of the class.

write non static functions?? to deal with non static variables outside the class. non static functions compulsorily need an object.

how to call a static function and how to call a non static function?? non static function: 1. create an object. 2. put objectname.functionanme. static function: 1. classname::functionanme() ( most preferable ) 2. stupid thing is to do objectname.functionname, this will work but is not preferable -------------------------------------------------------------------------------------------What is Constructors and Destructors. 1. Constructors and destructors are specials functions present inside the class. 2. special because, it gets called only once on any object by the system automatically. 3. Constructors get called only during object creation. Destructors get called when objects are being removed from memory. (objects die) Constructors, what we do initialize non static variable values, or if need be we may allocate memory for the non static variables in case the variable is pointer type.

constructor syntax. 1. functioname and classname is same. 2. no return type is mentioned.

Destructors 1. Destructors are required, when we are doing dynamic allocation for our variables in the constructor. Destructor gets called just before the object is about to go out of memory.

destructor is one per class and it cannot be overloaded. destructor used to clean up memory for the non static variables of the class in case they have been dynamically allocated. purpose of destructor is not to deal with the object. purpose of destructor is to deal with the instance variables of the object. -----------------------------------------------------------------------------------------what is new and delete?? new is an operator which is used to allocate memory on heap. delete is an operator which is used to deallocate memory on heap. what is free and malloc ?? malloc is a function which is used to allocate memory on heap. free is a function which is used to deallocate memory on heap. when we are using malloc or new with a class type new will always call the constructor. malloc will not call the constructor. Similarly delete will call the destructor, free will not. ------------------------------------------------------------------------------------------copy constructor copy constructor is used when there is a need to create a new object(constructor needs to be called) based on an existing object.( already constructor has got called) copy constructor to work new object has to be created, but it should based on the existing object.( copy state from existing object to new object) system given copy constructor works fine, as long as you dont have pointers as your non static variables. what we will do inside copy constructor 1. allocate memory for the pointer 2. copy the values from existing object to the new object. //Our copy constructor Device(Device &x) { deviceId = new int; *deviceId = *(x.deviceId);

} //System copy constructor Device(int x) { deviceId =new int ; *deviceId = x; }


1. copy constructor system given works when we have non pointer type variables. 2. pointer type variables in the class will put us into trouble if we depend on system given copy constructor because there will be pointer assignment. 3. need for writing our copy constructor, signature must be known and most important what we do inside that, is allocate memory and then assign values

----------------------------------------------------------------------------------------Note: in OO, global functions are banned. functions outside the class are banned. class A { public: void m1() { } // bad habit void m2(); //good habit } void A::m2() { define the function here } ---------------------------------------------------------------------------------------How does a c program work?? english words to use during stages. 1. precomilation -- expansion, macros, includes... 2. compilation -- intermediate assembly format. 3. assembling -- ELF ( exeuctable linking format) 4. linking --- binary code size expands. 5. running -- stack frame, program counter.

memory regions in C program. DAta segement -- intialized and non initlized, global and static variables. code segement -- functions reside here, cannot change, if we change segmentation fault

stack segment -- stackframes, function executes, local variables reside here heap -- when we use malloc , this is place we are using.
----------------------------------------------------------------------------------------------------------------------------------------------

what is const function?? a const function has to be a non static function of a class , it cannot change the value contained in the non static variables. const function cannot change the state of the object.( the value of non static variables.) const functions summary 1. const functions cannot change state of the object. 2. only non static functions of a class can be made as const. 3. const functions can call only other const functions. 4. constructors and destructors cannot be const functions. class A { int i; void f1() const { i =3; // wont work, why const functions cannot change the state . } }

const object is one whose state cannot be changed. const object can call only const functions.

non const object can it call a const function yes. this is answer. non const function can call all functions.
----------------------------------------------------------------------------------------------------------------------------------------------

MUTABLE : it is a storage class in C++. what is mutable. 1. mutable is a storage class that can be associated with the instance variable only. 2. mutable variable can be changed inside a const function. If a non static variable of a class is associated with a mutable storage class, then its value can be changed inside a const function also.

Eg, if class A is there. there is a non static variable called i inside class A. and there is a non static variable called j, inside class A, j is of storage class mutable. we have a function called f1, which is a const function. f1 cannot change value of i, but can change value of j.

Explicit. 1. a single argument constructor in C++ acts like a conversion function, to prvent the constructor from acting like a conversion function we need to make the constructor as a Explicit constructor. 2. only constructors can be made as explicit.

Anonymous variables creation happens. 1. when we return the the variable from a function. Anonymous objects in C++ can be created by writing classname();

short int -> int -> unsigned int ->long int -> unsigned long int -> float -> double -> long double

----------------------------------------------------------------------------------------------------------------------------------------------

inline function. it is "supposed" to reduce function switching costs. it will reduce the functionc alling costs, becusae the compiler wherever we are calling the function will replace the function calling code, with the function code. scary thing about inline function, it is a suggestion and not a order
----------------------------------------------------------------------------------------------------------------------------------------------

namespaces namespace isalways required in real projects to avoid naming collisions naming collisons come when we use third party code. namespace yahoo.messenger.ChatUI {

int freakvariable; class DEvice {} class User {} }


----------------------------------------------------------------------------------------------------------------------------------------------

what is shallow copy and what is deep copy?? what the system given copy constructor does is shallow copy what we do in our own copy constructor is deep copy. if instance variables values of one object gets copied to instance variables of the new object that is being created this is called as shallow copy.it will bitwise copy ( use this if you want trouble)

----------------------------------------------------------------------------------------------------------------------------------------------

this

very very important thing in OO.

1. this is not created by us, this is created by system. 2. what is "this", this is a implicit pointer present in non static functions of a class, which points to the calling object. 3. this usage is a more of a business logic compulsion. 4. No i dont like using this, ok dont use this. 5. this always belongs to the class whre we are using it. 6. this is local. 1. 2. 3. 4. this can be used only in non static functions of the class. it refers to the calling object. used for method chaining. used to differentiate instance variable from the local variable.

this can be used for method chaining. m1()->m2()->m3(); or m1().m2().m3(); m1 is called and it should return some object , on that object m2 will be called, m2 will rturn an object , on that object m3 will be called.

----------------------------------------------------------------------------------------------------------------------------------------------

templates. 1. data type independent way of writing code.

----------------------------------------------------------------------------------------------------------------------------------------------

what is overriding? overriding is a mechanism thru which we hide the base class functionality from the derived class object. to do overriding, ensure the base class function is virtual. --------------------------------------------------------------------------------------------------------------------------------inheritance is all about the derived class object.
----------------------------------------------------------------------------------------------------------------------------------------------

Exception Handling. 1. it is a mechanism thru whcih we handle runtime errors. the main advantage is we can separate the normal flow of code from the error rectification flow. try { normal scenarios initili mode trasnf data cloes the modem }catch(modelm not inilized) { what to do } catch(modem not transferring data ) { what to do. }
----------------------------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------------------

S-ar putea să vă placă și