Monday, November 19, 2012

Programming Session 2


previous sessions:

we already have taken printf function and we knew that it is used for printing anything on the screen.

This session :

we are going to learn the following :

  1. practice on printf function 
  2. \n ,\b, \a and \t
  3. variables

Practice 1 

write a program that prints your name and age on the screen.

\n  ,\b, \a and \t

\n  used when we want to print something in a separate line. 
\b  used when we want to make a backspace. 
\a  used when we want to make an alert. 
\t   used when we want to make a tab space. 

modify the previous program to print your name and age each in separated linesso your code would be like this:
#include<stdio.h>
int main()
{
         printf("my name is xyz\n my age is xyz\n"); 
         return 0; 
compile and run

  • gcc se2.c -o se2.out  -  Press Enter 
  • ./se2.out  -  Press Enter
Now modify your program by replacing \n with \t and see the difference.
your output should look like this:
My name is xzy              my age is xyz
Then modify your program by adding\a with \t and see the difference.
your output should look like this:

#include<stdio.h>
int main()
{
      printf("my name is xyz\t my age is xyz\a"); 
     return 0; 
you will hear a small beep after running.

Variables 

what does that mean?
when you need a space in memory for doing something you ask for a variable.


As you can see a variable is a place in memory where we can store data and this data could be replaced any time we want.
What type of data can be stored in this place?Well you can store data only in places that can hold.



If the previous place can hold only one piece of this data can we put two inside it?
Sure no as we need a bigger place so it can hold more pieces from this kind of data as we see in the next picture.  


To be continued ... check our next session :)


No comments:

Post a Comment