Showing posts with label C programming. Show all posts
Showing posts with label C programming. Show all posts

Wednesday, November 17, 2010

Conversion of macros into String literals

The # preprocessor operator converts the argument that follows it into a string literal. The preprocessor operator # can be used only in a function-like macro definition.

#include <stdio.h>

#define PRINT(name) printf("The value of " #name " is %d\n", name)

main()
{
 int abc = 100;

 PRINT(abc);
}
Output: The value of abc is 100
Macro Expansion
--> printf("The value of " #name " is %d\n", name)
--> printf("The value of " "abc" " is %d\n", 100)
--> printf("The value of abc is %d\n", 100)
The unary # operator produces a string from its operand. Adjacent string literals are getting concatenated in above example. If the operand to # contains double quotes or escape sequences, they are also expanded.
Similarly ## operator concatenate two tokens passed in macro to a single token.

#define JOIN(a,b)  a ## b
main()
{
   int myname = 100;
   printf("%d", JOIN(my,name));
}
It will be converted into printf("%d", myname);

Thursday, September 16, 2010

setting mode permission from command line

type following command in command prompt.
chmod {mode} filename, file2, file3

In C language its used as:
int chmod(const char *File_path, mode_t mode_value);

How to decide mode octal value:
mode is of 4 octal digits e.g. 0754, equivalent to (userID)(user)(group)(Other)

0755: u: full, GO: read and execute

0777: all : full permission

0555: all : read and execute


 symbolic representation of file permissions:

Representation
Class
Description
u
User
Owner
g
Group
Members of file group
o
Others
Neither owner nor group
a
All
Everyone

Octal notation:

Octal
System
Description
0
---
No permission
1
--x
execute
2
-w-
write
3
-wx
Write and execute
4
r--
Read
5
r-x
Read and execute
6
rw-
Read and write
7
rwx
Read, write and execute

These octal values are decided based on binary position of R W X.
R W X
0  0  0
0  0  1 : execute
0  1  0 : Write
1  0  0 : Read

Ex: changing permission for files in whole directory recursively as read, write, execute
chmod -R -v 777 ./*

reference: chmod