My Anna

Indias Biggest Engineering Education Community Enriching Students Minds with Latest Placement Papers, Study Materials, Fresher Jobs and Free E Books

A Unit of Aim Clear Technologies Ltd.
Search Study Materials, Jobs, Placement Papers, Interview Tips, Training Videos & More

Advertise with us

Who is online



Post new topic Reply to topic  [ 13 posts ]  Go to page Previous  1, 2

neha
Marketing Executive
Marketing Executive
User avatar

Joined:
Mon Aug 24, 2009 10:57 pm
Beans: 767
In Bank: 10965
College: IET
Degree: B.E
Position: Teacher
 
#2
Sat Sep 19, 2009 6:31 pm
Post subject: Re: C Programming Tutorials Post
Input and Output

Input : In any programming language input means to feed some data into program. This can be given in the form of file or from command line. C programming language provides a set of built-in functions to read given input and feed it to the program as per requirement.

Output : In any programming language output means to display some data on screen, printer or in any file. C programming language provides a set of built-in functions to output required data.

printf() function
This is one of the most frequently used functions in C for output. ( we will discuss what is function in subsequent chapter. ).

Try following program to understand printf() function.

#include <stdio.h>

main()
{
int dec = 5;
char str[] = "abc";
char ch = 's';
float pi = 3.14;

printf("%d %s %f %c\n", dec, str, pi, ch);
}
The output of the above would be:

5 abc 3.140000 c
Here %d is being used to print an integer, %s is being usedto print a string, %f is being used to print a float and %c is being used to print a character.

scanf() function
This is the function which can be used to to read an input from the command line.

Try following program to understand scanf() function.

#include <stdio.h>

main()
{
int x;
int args;

printf("Enter an integer: ");
if (( args = scanf("%d", &x)) == 0)
{
printf("Error: not an integer\n");
} else {
printf("Read in %d\n", x);
}
}
Here %d is being used to read an integer value and we are passing &x to store the vale read input. Here &indicates the address of variable x.

This program will prompt you to enter a value. Whatever value you will enter at command prompt that will be output at the screen using printf() function. If you enter a non-integer value then it will display an error message.

Enter an integer: 20
Read in 20

Profile E-mail
Offline

neha
Marketing Executive
Marketing Executive
User avatar

Joined:
Mon Aug 24, 2009 10:57 pm
Beans: 767
In Bank: 10965
College: IET
Degree: B.E
Position: Teacher
 
#2
Sat Sep 19, 2009 6:31 pm
Post subject: Re: C Programming Tutorials Post
Pointing to Data


A pointer is a special kind of variable. Pointers are designed for storing memory address i.e. the address of another variable. Declaring a pointer is the same as declaring a normal variable except you stick an asterisk '*' in front of the variables identifier.

There are two new operators you will need to know to work with pointers. The "address of" operator '&' and the "dereferencing" operator '*'. Both are prefix unary operators.

When you place an ampersand in front of a variable you will get it's address, this can be stored in a pointer variable.

When you place an asterisk in front of a pointer you will get the value at the memory address pointed to.

Here is an example to understand what I have stated above.

#include <stdio.h>

int main()
{
int my_variable = 6, other_variable = 10;
int *my_pointer;

printf("the address of my_variable is : %p\n", &my_variable);
printf("the address of other_variable is : %p\n", &other_variable);

my_pointer = &my_variable;

printf("\nafter \"my_pointer = &my_variable\":\n");
printf("\tthe value of my_pointer is %p\n", my_pointer);
printf("\tthe value at that address is %d\n", *my_pointer);

my_pointer = &other_variable;

printf("\nafter \"my_pointer = &other_variable\":\n");
printf("\tthe value of my_pointer is %p\n", my_pointer);
printf("\tthe value at that address is %d\n", *my_pointer);

return 0;
}
This will produce following result.

the address of my_variable is : 0xbfffdac4
the address of other_variable is : 0xbfffdac0

after "my_pointer = &my_variable":
the value of my_pointer is 0xbfffdac4
the value at that address is 6

after "my_pointer = &other_variable":
the value of my_pointer is 0xbfffdac0
the value at that address is 10
Pointers and Arrays
The most frequent use of pointers in C is for walking efficiently along arrays. In fact, in the implementation of an array, the array name represents the address of the zeroth element of the array, so you can't use it on the left side of an expression. For example:

char *y;
char x[100];
y is of type pointer to character (although it doesn't yet point anywhere). We can make y point to an element of x by either of

y = &x[0];
y = x;
Since x is the address of x[0] this is legal and consistent. Now `*y' gives x[0]. More importantly notice the following:

*(y+1) gives x[1]
*(y+i) gives x[i]

and the sequence

y = &x[0];
y++;

leaves y pointing at x[1].
Pointer Arithmetic:
C is one of the few languages that allows pointer arithmetic. In other words, you actually move the pointer reference by an arithmetic operation. For example:

int x = 5, *ip = &x;

ip++;
On a typical 32-bit machine, *ip would be pointing to 5 after initialization. But ip++; increments the pointer 32-bits or 4-bytes. So whatever was in the next 4-bytes, *ip would be pointing at it.

Pointer arithmetic is very useful when dealing with arrays, because arrays and pointers share a special relationship in C.

Profile E-mail
Offline

gomathi
Born Baby
Born Baby
User avatar

Joined:
Fri Sep 18, 2009 10:26 pm
Beans: 5
In Bank: 20
College: Trinity
Degree: BE
Department: EEE
 
#2
Sat Sep 19, 2009 11:04 pm
Post subject: Re: C Programming Tutorials Post
Thanks for the share mate, nice worthy information.

Profile E-mail
Offline

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ]  Go to page Previous  1, 2
Bookmark & Share

Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB Group.

phpBB SEO