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.
No comments:
Post a Comment