Sunday, November 25, 2012

C puzzles

1. What is the output of following C program ?

int main()
{
func(5);
return 0;
}
func(int a)
{
int a;
a=10;
printf("%d",a);
}


=> This will end up in error.


error: 'a' redeclared as different kind of symbol
 
Reason: As int a is a local variable it will be stored in stack, when once 
again a is re-declared it will thrown an error.
 
2.What is the output of following C program ?
 
int a=5;
int main()
{
func(5);
return 0;
}
func(int a)
{
a=10;
printf("%d",a);
}
 
Output => 10
 
Reason: In statement int a =5; a will be stored in data segment of memory, 
where as int a in func(int a) will be stored in stack. So no redeclaration 
scenario like earlier case.
 
3. In the below program 'a' is a global variable. So it can be accessed by 
all functions.Is there any way to restrict the access of variable 'a' from 
func3() ?

int a;
int main()
{
func1();
func2();
func3();
return 0;
}
func1()
{

}
func2()
{

}
func3()
{

}

Answer => Yes. Look into the previous example. By declaring 'a' inside func3() 
we can restrict access to global variable 'a'

int a;
int main()
{
func1();func2();func3();
return 0;
}
func1()
{

}
func2()
{

}
func3()
{
int a;
}
 
4. What is the output of the following two programs?
 
int main()
{
char *p = NULL;
printf("%d %d",p,p++);
return 0;
}
 
Output => 0 1
Reason: NULL is always stored in the zeroth location. Increment pointer of char type
will shift to position 1 as char takes 1 byte.
  
int main()
{ 
int *p = NULL;
printf("%d %d",p,p++);
return 0;
}
 
Output => 0 4
Reason: NULL is always stored in the zeroth location. Increment pointer of int type
will shift to position 4 as int takes 4 byte. 
 
5. What is the prototype of malloc and free ?
 
Answer =>
 
void *malloc(size_t size); 
void free(void *ptr);  

size_t is of unsigned type.

 
  

Friday, November 9, 2012

How to compile only the last modified C file in the directory using Makefile?

Assume work is the current directory. Assume there are p1.c, p2.c, p3.c files are there. Each .c file has main function. I want to compile only the last modified file using Makefile. Is it possible ?

Yes.

Write a Makefile[ vim Makefile in linux terminal] and type below commands.


SRC=$(shell ls -rt | tail -1)
test: $(SRC)
        gcc -o test $(shell ls -rt | tail -1)

Save the Makefile using [escape :wq!]

Execute using make command.
./test will give the output of last modified file. If I have modified p2.c it will show output of p2.c.

chmod - change mode


Exectute below commands

ls –l filename

Now observe the result

chmod 644 filename

ls –l filename

Now observe the result after doing chmod. When you observe you will find out the file access permissions would have changed.

user | group | others
6          4          4
- rw -    r - -      r - -


rwx    Mode
111       7
110       6
101       5
100       4
011       3
010       2
001       1
000       0

r = read only
w = write only
x = execute only

To tar and untar folder


Assume "work" is the directory you want to tar. [ Tar is nothing but compressing the file to zip folder]
Execute the below command to tar folder
tar –czf work.tar.gz work
To untar the tar folder. [ Untar is extracting the files]
Execute below command to untar folder.
tar –xzvf work

Linux compiler version: How to find Linux compiler version ?

Execute "gcc -v" in Linux terminal

Wednesday, November 7, 2012

Enum Enumerations

#include<stdio.h>
enum day
{
sun,
mon,
tue,
wed,
thur,
fri,
sat
}which_day;
 
int main()
{
which_day = mon;
 
switch(which_day)
{
case sun: printf("Today is sunday");
          break;
 
case mon: printf("Today is monday");
          break;
 
case tue: printf("Today is tuesday");
          break;
 
case wed: printf("Today is wednesday");
          break;
 
case thur: printf("Today is thursday");
          break;
 
case fri: printf("Today is friday");
          break;
 
case sat: printf("Today is saturday");
          break;
 
}
return 0;
}
 
Output: Today is monday