Thursday, May 28, 2015

C language : MULTIPLE LINE PRINTF , FILENAME and LINENUMBER output



It's going pretty busy these days , but lot of new learning's are there. I think that's what they called EXPERIENCE.



One thing I learned that in C , there are some special macros like __FILE__ and __LINE__ which can print the filename and linenumber from which you have called a fucntion.



For example , if you have written a function which get called for a particular event and you want to know from which file this function is called . You can have code like below


#define FILENAME(x) do{ printf("I am called from %s",__FILE__);

For example in below code snippet I have 3 files

main,c

#include"header.h"

int  main()
{
  int a=10;
 printf("I am main file");
FILENAME(a);
main2();

}


main2.c

#include"header.h"

void  main2()
{
  int a=20;
 printf("I am main 2 file");
FILENAME(a);

}

void add2(int x)
{
  printf("%d",x+2);
}

header.h
#include<stdio.h>


#define FILENAME(x) printf("I am called from %s \n",__FILE__);add2(x);


If you compile them and link them
> gcc -c main.c
> gcc -c main2.c
> gcc -o out main.o main2.o
>./out

You can see from the list FILE NAME getting printed .   __L__  is  int value , you have to use %d for it.


Other thing is how to have printf span multiple lines to have code in readable format.

Ex :

printf(" My name is %s number %s address%s college %s email %s",name,number,address,college);

Now suppose there was more information and you don't want to use multiple printf , you can use forwardslash \   

printf("My name is %s \
             number is  %s \
             address is  %s ,\
            name,number,address); 


During the coding of above functions , I also learned that  MACROS needs to be included in the .C files they are getting called.

So for MACROS needed in multiple files we need to have then in  .H file and then include this file.

Tuesday, May 26, 2015

i will do good thing one day like below

only thing I have to increase my efficency



Latha Venkataraman

Madonna

Marlon Brando

Monday, May 25, 2015

Graphs and Trees : Part 3



Yippy  ...I have written a program and that too in java.

The thing I learned is that Java is much better than C for most of the programs we write until and unless we are specifically writing code for machines with memory...like embedded device.


The program is to find outdegree and indegree of Graph.
The input will be stored in 2-D matrix format.


Since the program is in java , we have one class

it will have method to calculate Outdegree and Indegree

The method will be not of Class but of Objects/Instance and since the methods and not STATIC.
We will have variables 2D matrix , size of matrix. Now these will be also not STATIC, since these will be class variables.

Something I learned during debugging is
1) main method is always static, since it will be called once and is not object method , it is class method

2) From the Main method, we can access only static methods and variables. hence we need to instantiate Object to do anything in Object Oriented Class.

3) Good thing about Object is 1) by default all object have their own variables without us specifically mentioning , it provides SAFETY as change in one object variables can't change other Object variable. 2) Object variables act as GLOBAL variables of that object , so we can access any variable of the OBJECT in any method.

4) In case we need some common variable across all objects, we need essentially CLASS variable so just have STATIC in front of that variable of method which is common of all objects.

Below is GD link for the program

https://drive.google.com/file/d/0B6HF3JcgAklvYjN4QmVOVTlkRkU/view?usp=sharing