Traditionally the standard functions are declared in header files and defined in the library routines only.
This blog provides some ready-made tips, tricks, tweaks & interview questions-answers on C, C++, Perl, Shell Scripting and UNIX Commands.
Wednesday, February 27, 2013
What is the difference between '#include' and '#include "File"'?
It is compiler dependent. Generallly '#include "File"' prioritizes headers to be checked in the current working directory over system headers. And if there is not a suitable match, it moves on to check the system paths. "#include <File>" always looks out for system headers.
How could you find the size of a variable with out using sizeof operator?
#include <stdio.h>
#define SIZEOF(var) ((size_t)(&(var)+1) - (size_t)(&(var)))
main( )
{
int x;
printf ("The size of x is %d\n", SIZEOF(x) );
}
Monday, February 25, 2013
How could you multiply 2 Integers using bit-wise operators?
#include<stdio.h>
main()
{
int a, b, num1, num2, result=0;
printf("\nEnter the numbers to be multiplied :");
scanf("%d%d",&num1,&num2);
a=num1;
b=num2;
while(num2 != 0)
{
if (num2 & 01)
{
result+=num1;
}
num1<<=1;
num2>>=1;
}
printf("\nMultiplication of %d*%d=%d", a, b, result);
}
What is the difference between '#define' and 'typedef'?
(a) typedef keep the property of attributes whereas #define doesn’t. For example
typedef char *type_t;
#define char *type_d
type_t s1,s2;
type_d s3,s4;
In the above declarations, s1,s2 and s3 are all declared as char* but s4 is declared as a char, which is probably not the intention.
(b) #define keeps string attributes whereas typedef doesn’t.
#define int INT;
typedef int MYINT
unsigned MYINT a;
unsigned INT a; /* Illegal */
For the typedef scenario, it doesn't change to unsigned 'int a' but in #define case it works!!!.
Wednesday, January 23, 2013
How could you comment out block of code in Ruby?
Multiple lines of text or code can be marked as comments using the Ruby "=begin" and "=end" markers. These are known as the comment block markers.
=begin
This is a comment line.
It shows that the next line of code is
marked as a comment.
=end
=begin
This is a comment line.
It shows that the next line of code is
marked as a comment.
=end
How could you comment out block of code in Python?
Use triple-quoted strings.
#!/usr/bin/python
'''
print "This is a \
multiline \
comment.";
'''
#!/usr/bin/python
'''
print "This is a \
multiline \
comment.";
'''
Monday, January 21, 2013
What are the various ways you can run UNIX commands in Ruby?
Backticks (``) - Backticks (aka backquotes) runs the command in a sub-shell and returns the standard output from that command.
#!/usr/local/bin/ruby -w
today = `date`
puts today
System (Kernel.system) - This command runs in a subshell and returns true if the command ran successfully and false otherwise.
system "ls -1"
Exec (Kernel.exec) - It runs the given command by replacing the current process with the one created by the external command.
exec "ls -1"
spawn (available in Ruby v1.9. aka Kernel.spawn also available as Process.spawn) - It doesn‘t wait for end of the command but returns the pid of the sub-process. Therefore, you'll have to use Process.wait or Process.detach on the resulting pid.
pid = kernel.spawn ("ls -lA", {:chdir => "/home/ravi"})
Process.wait pid
%x operator - The operator makes it easy and readable to use quotes in a command.
#!/usr/bin/env ruby
list=%x[ls -1]
puts list
IO.popen - It is another way to run a command in a sub-process. It helps you control the connection to IO object between standard input and standard output in that the sub-process.
Open3.popen3 - The Ruby standard library includes the class Open3. It is easy to use and returns stdin, stdout and stderr.
Open4.popen4 - It is a Ruby Gem put together by Ara Howard. It operates similarly to open3 except that we can get the exit status from the program. It also returns a process id for the subshell and we can get the exit status from that waiting on that process.
#!/usr/local/bin/ruby -w
today = `date`
puts today
System (Kernel.system) - This command runs in a subshell and returns true if the command ran successfully and false otherwise.
system "ls -1"
Exec (Kernel.exec) - It runs the given command by replacing the current process with the one created by the external command.
exec "ls -1"
spawn (available in Ruby v1.9. aka Kernel.spawn also available as Process.spawn) - It doesn‘t wait for end of the command but returns the pid of the sub-process. Therefore, you'll have to use Process.wait or Process.detach on the resulting pid.
pid = kernel.spawn ("ls -lA", {:chdir => "/home/ravi"})
Process.wait pid
%x operator - The operator makes it easy and readable to use quotes in a command.
#!/usr/bin/env ruby
list=%x[ls -1]
puts list
IO.popen - It is another way to run a command in a sub-process. It helps you control the connection to IO object between standard input and standard output in that the sub-process.
Open3.popen3 - The Ruby standard library includes the class Open3. It is easy to use and returns stdin, stdout and stderr.
Open4.popen4 - It is a Ruby Gem put together by Ara Howard. It operates similarly to open3 except that we can get the exit status from the program. It also returns a process id for the subshell and we can get the exit status from that waiting on that process.
How could you run UNIX commands in Python?
#!/usr/bin/python
import os
os.system("ls -l")
or
import os
buffer=os.popen("ls -l")
for i in buffer.readlines():
print "Result:",i,
or
from subprocess import call
call(["ls", "-l"])
import os
os.system("ls -l")
or
import os
buffer=os.popen("ls -l")
for i in buffer.readlines():
print "Result:",i,
or
from subprocess import call
call(["ls", "-l"])
What are the modules available in Perl to run external commands or 3rd party programs?
On UNIX:
- IPC::Open2
- IPC::Open3
- IPC::Run
On Windows:
- Win32::Process::Create
Subscribe to:
Posts
(
Atom
)