Showing posts with label languages. Show all posts
Showing posts with label languages. Show all posts

07 October, 2014

C Pointers With Examples Programs



C Pointers With  Examples Programs


1.What Is The Output Of The Program?
#include <stdio.h>
int main ()
{
    int a = 10;
    int *p = &a;
    printf ("%d %d\n", a, *p);
    (*p)++;
    printf ("%d\n", a);
    ++(*p);
    printf ("%d\n", a);
   return 0;
}

Ans:10 10
         11
         12


2. What Is The Output Of The Program?

#include <stdio.h>


int main ()
{

    int array [5] = {5, 4, 3, 2, 1};

    int *pointerVariable;

    pointerVariable = array;

    printf ("%d", *pointerVariable);

    printf ("%d", ++*pointerVariable);

    printf ("%d", *++pointerVariable);

    printf ("%d", ++*++pointerVariable);

    return 0;

}

Ans:5644

3. What Is The Output Of The Program?
int main ()
{
    int *p, i;

    i = 5;

    p = &i;

    printf ("%d", *p);

    printf ("%d\n", i**p);

    printf ("%d\n", i**p*i+*p);

    return 0;
}

Ans:5 25 130


4. What Is The Output Of The Program?
#include <stdio.h>
int main ()
{
    int a, b, c, *p, *q, *r;

    a = 10;     b = 20;     c = 30;

    p = &a;     q = p;

    printf ("%d\n", *q);

    (*p)++;

    printf ("%d\n", *p);

    printf ("%d\n", *q);

    r = q;

    r++;

    printf ("%d\n", *q);

    r--;

    printf ("%d\n", *r);

    (*q)++;

    printf ("%d\n", *r);

    return 0;
}

Ans:10
11
11
11
11
12

5. What Is The Output Of The Program?
#include <stdio.h>
void changeValue(int *p)
{
    (*p)++;
}

int main ()
{
    int a = 10;
    int b = 20;

    changeValue(&a);

    printf ("%d %d\n", a, b);

    changeValue(&b);

    printf ("%d %d\n", a, b);

    }

Ans:11 20
 11 21

6. What Is The Output Of The Program?

void changeValue (int *p, int q)
{
    q = *p + 1;
    (*p)++;
    p++;
}

int main ()
{
    int p, q;
    p = 10;     q = 20;

    changeValue(&p, q);

    printf ("%d %d\n", p, q);

    changeValue(&q, p);

    printf ("%d %d\n", p, q);

    return 0;
}

Ans: 11 20
11 21

7. What Is The Output Of The Program?
int main ()
{
    int array [5] = {5, 4, 3, 2, 1};

    printf ("%d\n", *array);

    printf ("%d\n", *array + 1);

    printf ("%d\n", *(array + 2));

    return 0;
}


Ans: 5
6
3


8. What Is The Output Of The Program?

#include <stdio.h>
int main () {

    int *p, **q;

    int a = 10;

    p = &a;

    q = &p;

    (*p)++;

    (**q)++;

    printf ("%d\n", a);

    return 0;
}


Ans:12

02 October, 2014

Return value of printf() function in C

Return value of printf() function in C


Ans:
A function always returns a value and printf function returns the number of characters successfully printed.


main() 
{ 
int s=1; 
printf("%d",printf("%d %d %d", s,s,s)); 
}
In this above program the inner printf is first called which prints value of s, three times with space between each value, 1 1 1.
Total 5 characters get printed = 3 values and 2 spaces).
As explained earlier the inner printf after printing the values, returns the number of characters printed, 5 which is printed by the outer printf.
The output of the above program is
1 1 1 5


14 September, 2014

`gets' function is dangerous how to solve this error and why gets function is dangerous?

  `gets' function is dangerous how to solve this error and why gets function is dangerous?







Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will
read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use.
It has been used to break computer security. Use fgets() instead.

so we can use fgets():
In the C Programming Language, the fgets function reads characters from the stream pointed to by stream.
The fgets function will stop reading when n-1 characters are read, the first new-line character is encountered in s, or at the end-of-file, whichever comes first. Then the fgets function will append a null character to the string.

Syntax

The syntax for the C Language fgets function is:

char *fgets(char *s, int n, FILE *stream);
  
s is the array where the characters that are read will be stored.

n is the size of s.

stream is the stream to read.

The fgets function returns s. The fgets function will return a null pointer if an error occurs while trying to read the stream or the end of the stream is encountered before any characters are stored.

In the C Language, the required header for the fgets function is:

#include 
 

#include 
char * fgets(char *restrict s, int n, FILE *restrict stream);
Frightening, no? That’s because fgets() is a file function, which reads text from a file, as in “file get string.” That’s how programmers talk after an all-nighter.
Because the operating system considers standard input like a file, you can use fgets() to read text from the keyboard.
Here’s a simplified version of the fgets() function as it applies to reading text input:
fgets(string,size,stdin);
In this example, string is the name of a char array, a string variable; size is the amount of text to input plus one, which should be the same size as the char array; and stdin is the name of the standard input device, as defined in the stdio.h header file.
THE FGETS() FUNCTION READS A STRING
#include 
int main()
{
char name[10];
printf("Who are you? ");
fgets(name,10,stdin);
printf("Glad to meet you, %s.\n",name);
return(0);
}

how to check linux kernel is 32 bit or 64 bit




how to check linux kernel is 32 bit or 64 bit

uname -m. It seems like the uname -m actually gives
x86_64 ==> 64-bit kernel
i686   ==> 32-bit kernel