Pages

Tuesday, May 16, 2006

C Questions: Lesson 2




Here I am back with one more lesson on C. And all these questions will be based on Unix/Linux OS. GCC is the compiler used in all source code compilation. And PowerPC is the hardware in most of the cases unless mentioned explicitly.



1.
See how it behaves when you have less space than required, here it is the string-copy operation in target.
main()
{
char *a;
//a> char a[];
//b> char a[12];
//c> char a[10];
char b[10]="Hello World"; //sizeof=11

strcpy(a,b);
printf("%s\n", a);
}

[guest@caramel mine]$ ./a.out
Segmentation Fault
[guest@caramel mine]$
a> Segmentation Fault
b> Hello World
c> Segmentation Fault
2.
Big Blue or Not?
Big Blue:
main()
{
int i=1;

char *ch = &i; // here ch points to least significant byte.

if(*ch == 1) // check least significant byte has value ?
{
printf("Littel Endian");
}
else
printf ("Big Endian");

}

[kongkon@~ >]gcc 2.c
2.c: In function `main':
2.c:5: warning: initialization from incompatible pointer type
[kongkon@~ >]./a.out
Big Endian[kongkon@~ >]
[kongkon@~ >]uname -a
Linux ltsbuild.austin.ibm.com 2.6.5-7.97-pseries64 #1 SMP Fri Jul 2 14:21:59 UTC 2004 ppc64 ppc64 ppc64 GNU/Linux
[kongkon@~ >]

Not:(intel:Leap Ahead)

[guest@caramel jnk]$ gcc 2.c
2.c: In function `main':
2.c:5: warning: initialization from incompatible pointer type
[guest@caramel jnk]$ ./a.out
Littel Endian[guest@caramel jnk]$ uname -a
Linux caramel.in.ibm.com 2.4.22-1.2199.nptl #1 Wed Aug 4 12:21:48 EDT 2004 i686 i686 i386 GNU/Linux

3.
Writing printf statement without using semicolon at the end:
#include
main()
{
if( ! printf("Write watever in this") )
{
// you will never reach here
}

while (! Printf("Here too u can write ") )
{
// You will never reach here
}
}

4.
Finding a number is a power of 2 in single line program in c:
main()
{
int n;
( n & n-1 ) ? printf("Number is power of 2") : printf("Number is power of 2") ;
}

5. extern var
main()
{
extern int i;
i=20;
printf("%d",i);
}

kongkon@ltsbml:~> gcc 5.c
/tmp/cckHzObg.o(.text+0x16): In function `main':: undefined reference to `i'
/tmp/cckHzObg.o(.text+0x1e): In function `main':: undefined reference to `i'
/tmp/cckHzObg.o(.text+0x22): In function `main':: undefined reference to `i'
/tmp/cckHzObg.o(.text+0x2e): In function `main':: undefined reference to `i'
collect2: ld returned 1 exit status
kongkon@ltsbml:~> gcc -c 5.c
kongkon@ltsbml:~>
There is a linker error, but no error at the time of compilation.


6. How const works?
main()
{
const int x=get();
printf("%d\n",x);
}
get(){return(20);}

kongkon@ltsbml:~> gcc 6.c
kongkon@ltsbml:~> ./a.out
20
kongkon@ltsbml:~>gcc --version
gcc (GCC) 3.3.3 (SuSE Linux)Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NOwarranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

7. Writing C program without main()
In turbo C, use this
#pragma startup my_main
my_main(){
...
...
}

8. Executing C programming without using ; to end the line.
Method A:
int main()
{
if (!printf("Hello"))
{}
}
Method B:
#define ABC ;
printf("abc")ABC

9. At the ground ZERO!!
kongkon@ltsbml:~> cat 9.c
main()
{
int a[0];
printf("sizeof a = %d\n", sizeof(a));
}
kongkon@ltsbml:~> gcc 9.c
kongkon@ltsbml:~> ./a.out
sizeof a = 0
kongkon@ltsbml:~>

10.
It's a matter of recreatibility. This is self reproducing code.
kongkon@ltsbml:~> cat 10.c
char *s="char *s=%c%s%c;main() { printf(s,34,s,34); }"; main() { printf(s,34,s,34); }
kongkon@ltsbml:~> gcc 10.c
kongkon@ltsbml:~> a.out
char *s="char *s=%c%s%c;main() { printf(s,34,s,34); }";main() { printf(s,34,s,34); }kongkon@ltsbml:~>

11. void or no-void
main()
{
add(0, 13); //pass any number of arguments, it will work, NO Warnings!
}
int add(void)
{
printf("Hello World");
}

12. Stack goes downward/upward?
To check whether the C program stack is growing upwards, or downwards the following C code will help you out.
int main()
{
int i,j;
printf("%u %u",&i,&j);
}

13.Size does matters!
#define SIZE(any_var) ((char *)((&any_var) +1) - (char *) (&any_var))

This will give you sizeof any variable...

Thanks a lot for reading my blog.

Saturday, March 25, 2006

C Questions: Lesson 1





Hello World!

1.
main()
{
printf("Hello World!\n");
}
>gcc 1.c
> ./a.out
Hello World!

Here is a space where I put some of my C questions collection.

2.
Here is a way to print/execute both if part and else part in an if-then-else construct.
main()
{
if(!printf("Hello ")){}
else
printf("World!");
}
>gcc 2.c
> ./a.out
Hello World!

3.
Sometime serious about how malloc works. Even if you pass the address of a member (variable) while you malloc a struct, see how free works. It can free up the space using that address also.
main()
{
struct a {
int b;
int c;
int d;
};
struct a *ptr;
ptr = (struct a *) malloc(sizeof(struct a ));
free(ptr->c);
}

4.
Here is something about address alignment. See how memory addresses are aligned in case of a struct.
main()
{
struct i{
int a;    //4
double b;    //8
char * c;    //4
char d[7];    //8
short e;    //4
int f;    //4
};
printf("sizeof(i)= %d", sizeof(struct i));
}

Ans: 4+8+4+8+4+4=32

5.
Inside into the printf function.
# include < stdio.h >
void main()
{
printf(NULL) ;
return ;
}
> ./a.out
>

6.
# include < stdio.h >
void main()
{
int i = 0;
printf(i) ;
return ;
}
Ans:

In newer gcc compiler this will not compile.
In older ones:
> ./a.out
>

But,

# include < stdio.h >
void main()
{
int i = 0;
printf("%f\n", i) ;
return ;
}
Ans:
random number like:
-0.008430
And the result of the following will be like this:

# include < stdio.h >
void main()
{
int i = 0;
printf("%f %d\n", i) ;
return ;
}
Ans:
random number like:
-0.008430   134513785
7.
#include <stdio.h>
void main()
{
printf("%s",NULL) ;
return ;
}
> ./a.out
(null)

But,

void main()
{
int i = -10;
printf("%s\n",i) ;
return ;
}
will result in "Segmentation fault" if the value of i is other than zero.

8.
This will work.
main()
{
char *a;
char b[13]="hello world";

strcpy(a,b);
printf("%s", a);
}

9.
main()
{
printf("%%",7);
}

10.
Array boundary condition behavior
main() {
int a[4]={1,2,3,4};
printf("%u %u", &a, &a+1);
}
>gcc 10.c
> ./a.out
3220829536 3220829552

11.
Again array boundary behaviour.
main() {
int a[3]={1,2,3,4};
printf("%u %u", &a, &a+1);
}

gcc 11.c
> gcc 6.c
11.c: In function `main':
11.c:2: warning: excess elements in array initializer
11.c:2: warning: (near initialization for `a')
> ./a.out
3220516784 3220516796

12.
main()
{
int i=10;
i=i++ + ++i;
printf("i=%d",i);
}
>./a.out
i=23

13. To find greatest among two numbers without using relational operator.
int main()
{
int a,b,c,d;
printf("Enter any two nos for a and b");
scanf("%d%d",&a,&b);
c=a-b;
d=b-a;
while(c && d)
{
--c;
--d;
}
if(c)
printf("b is greatest");
else
printf("a is greatest");
}

Try this...
Copyright @ Kongkon Jyoti Dutta, 2006-2011. Powered by Blogger.