C Programming Tutorial - 3 - Conversion Characters






Hey Guys,

In this lecture we are going to see how to do conversion characters in c programming. First we have to know what a string is. String is basically bunch of words or texts. 

lets look at an example.

#include<stdio.h>

int main() {

   printf("%s is the best programmer ever","Thileban" );
}

In this code you can see that I have included %s in the printf statement. It represents place holder for strings. Strings are basically charecters or any letters. Then I wrote my name at the end with a seperate quote mark. For this code I got the following output. 

Thileban is the best programmer ever

As you can see that %s is replaced with my name this is called conversion characters. You can do this as many times as you want. for example look at the following code. 

#include<stdio.h>

int main() {
  
   printf("%s is the best %s ever","Thileban","programmer" );
}

Here you can see I have included %s twice. As the result I got the following output. 

Thileban is the best programmer ever

Moreover, we can do this to numbers too. But when we do for numbers %s doesn't work so we have to type something else. Lets look at an example of integer. To do conversion character for integer we have to type %d instead of %f. Integers are numbers without decimal points. 

Lets look at this example. 

#include<stdio.h>

int main() {
  
   printf("I ate %d corndogs last night", 9 );
}

You can see that this is similar to strings but the only difference is we didn't put the number inside the quote mark. 

Lets look at an example of float. float is the number with decimal point. for float we have to write %f instead of %d or %s. 

Take a look at this code.

#include<stdio.h>

int main() {
  
   printf("I ate %f corndogs last night", 3.1415926535 );
}

For the code above we got the following output

I ate 3.141593 corndogs last night 

here you can see that %f can do conversion characters for decimal numbers upto 6 numbers after decimal points. 
There are some more things we can do with decimal numbers. 

Take a look at this code


#include<stdio.h>

int main() {
  
   printf("I ate %f corndogs last night \n", 3.1415926535 );
   printf("I ate %.2f corndogs last night \n", 3.1415926535 );
   printf("I ate %.4f corndogs last night \n", 3.1415926535 );
}

 For the above code the output is

I ate 3.141593 corndogs last night                                                                                                                                                                
I ate 3.14 corndogs last night                                                                                                                                                                    
I ate 3.1416 corndogs last night    

Here you can see that for the first line of code it prints 6 digits after decimal point but for the second line it prints only 2 numbers after decimal point. It's because we've included .2 infront of f. The same goes to next line, here we have included .4 before f. That's why it just printed 4 numbers after decimal point. 

I hope this lecture helps. Thanks for reading :).                                                                                                                                                               
                                  

Comments