Skip to content

C tutorial 2

by on February 16, 2011

Till now we learnt ho to write a first C program. In this tutorial we’ll go a little more deeper.

We saw some library functions printf(), scanf() and we also introduced the clrscr() and getch() function in tutorial 1.

Let us first discuss the format specifiers first which must be troubling you in the program we discussed. Format specifiers does two jobs. Place the output into the specified string, and to notify the type of given value (constant) to the compiler. The compiler thus check the value type (if placed in the string or obtained from the string in case of user input) and if the type mismatches then an error is generated. Thus if we want to print the two or more results in the same string then just place the arguments from left to right order just after the quoted string separated by comma. The number of argument passed should be exactly as many as the format specifiers placed in the string at the appropriate places. See example:

printf(“%d is greater than %d but equal to the real value %f”,a,b,c);

now suppose that a and b are integer values 12 and 9 respectively and c is a real (float) value 12.0, then the result of above code will be:

12 is greater than 9 but equal to the real value 12.000000

So this was all about format specifiers. The list of format specifiers can be obtained from various other sites available on web.

The second thing that must be troubling you is the hot key & which means address of. It is a pointer which is used to access the address of variable. Variables are nothing but the memory locations where the data is saved.

Whenever we input some data, it must be stored at the specified memory location. Thus whenever we use scanf() the user is given the address where he’ll save the data. These addresses are automatically calculated by C compiler thus the user don’t have to worry about the addresses. The string argument simply contains a format specifier which notifies the compiler that what type of data is been entered by the user in the input string.

Very special thing I would like to tell you about the functions. It’s the calling conventions. But before that let me explain you about the parameters we place in the functions and why do we need them.

Parameters are simply the data that a function may need from the other function. Many times we come across the situation where we have to use something that was used before. A good programmer don’t write the same code again and again when he want to solve two different problems. He simply write it once in the form of a function and simply call it again and again whenever he wish to. Now suppose a programmer want to jumble the words of his string (kind of encrypting the string). Now he has 3 sets of string entered at once by the user. What we do is, we write a function named jumblewords(<param>) and pass the string parameter into it every time it was entered. Thus three function calls are made of the same function of three different outputs. This is a very good programming practice and we practice it every time we write a C code. Take an example of printf() function itself. It takes different inputs each time it is being called and each time different outputs onto the console screen. So do you think that the developer has defined different functions named as printf() and represented to the user in an overloaded form? Not at all.

Now let us see the same example as discussed before in order to get a brief idea of how to make use of functions.

#include<stdio.h>

#include<conio.h>

int sum(int,int);

void main()

{

int a,b,c=0;

scanf(“%d%d”,&a,&b);

c=sum(a,b);

printf(“%d”,c);

}

int sum(int x,int y)

{

int z=0;

z=x+y;

return z;//return statement returns the result as well as control back to the calling function main

}

This program displays the same result as the previous one.

Now let us talk about the calling convention when more than one arguments are passed in a function call.

The calling convention generally followed by C is right to left convention. Look at the following code:

int i=1;

printf(“%d%d%d”,i++,++i,i);

what result are you expecting?

133 ?

I would like to tell you that the output will be:

221

why?

because of the calling convention. It was from right to left. first i was called, then ++i and at last i++, though the result is displayed in order as it should be.

Why did it happen? here’s a concept of stack cleanup. Whenever a function is called a stack of variables passed to the function and used by that function is made. Whenever function call finishes execution, this  stack should be cleaned. It may be either calling function or called function which performs the cleanup. In above case it is done by calling function. The calling convention entirely depend upon which function is performing the cleanup.

So this was all in this tutorial, I hope that you must have found pretty more interesting than previous one. So please add your comments if you wanna add something or you find this post incomplete. Thanks!

From → C, Tutorials

Leave a Comment

Leave a comment