Wednesday 5 February 2014

chapter 2 Function printf() and scanf()

In my previous post we made a program but that program not gives any output. because the we were not using method/function for get printed something on monitor. Now we learn about functions. After function we again write our first program.
There are two type of function

  • Built in Function/Predefined function
  • User defined Function



Built in function


We can use many function in C. Such function that only use but modification ( definition) is not possible is known as Built in function or library function or predefined function. These function are already define by developer of C language. When we use any predefined function we must include a library file/ header file.


User defined function


First example of user defined function is main() function. This function must define by programmer. The execution of program is starts from main() function. Programmer writes whole codes or statement here, because of this the program become large and complex.Therefore programmer writes some of statement after main() function. Because of this the program can easy to understand.

We can give name of function in main function. These process is known as function calling. We will better understand in our example

Again rewrite our first program 'Sum'. Addition of 10 and 20.




int main()
{

int x,y,z;
x=10;
y=20;
z=x+y;
printf("%d",z);

return 0;
}

In our new program a predefined function printf() is used. This function can be called to get something printed on monitor.Print F stands for print as specified format.
it will print 30 as a string not like an integer.

Because of this 30 should get printed on monitor. But this program not give any output even this program is not execute because it has an error. In my next post we will rewrite this program. :)

note:
In our program we are using default value of x and y, that are already assigned by the programmer (me). It means when our program run it add the number 10 and 20 only.There is a limitation in our program. To solve this problem we must introduce a predefine function scanf().

printf() and scanf()


printf() and scanf() are predefined functions and its prototype is stored in stdio.h header file. printf() function is used to something gets printed on monitor of specification in format like integers , constant etc. The scanf() function is same as the printf() function but it reads characters from standard input device keybord instead of print.

Because of scanf() function the user can also provide the input to our program.

note:  if we want to use printf() and scanf() we must include stdio.h header file.It is compulsory to include header file for predefine functions...

What is header Files ?


header files and library files are the files in which the compiled/machinery code of predefine function is stores. Here we are only use two predefine functions printf() and scanf() and that's prototype ( like definaion ) is available in stdio.h header file.

Our first valid program is following:


The output of above program is :
#include<stdio.h>
void main()
{
int x,y,z;
printf("Enter a number ");
scanf("%d",&x);
printf("Enter another number ");
scanf("%d",&y); z=x+y;
printf("Sum is %d",z);
}



Now our first program is working.
note:"; " is used for terminating the statements.

Saturday 18 January 2014

making first program 'Sum'

Now we write our first program with C. Our program is use to compute the sum of 10 and 20. It is a basic program to understand the little things that including the programming language. We taking three variable as x, y and z.

x=10
y=20
z=x+y



The sum should be 30 but how it works in program and how to write ? Before this we have to learn what is it ?

Rule


Here x,y and z are variable and its value 10 and 20 is constant. = and + are arithmetic operators for performing a particular operation.


Before using any variable in program we must declare first. To declare a variable means to specify type of information and the most important thing that all variable which is declared in a function can alive in its own function. We give an example to understand this all rule.

The following are the basic and most important keyword for declare variable/data type.


Data/Info Name of Data type Keyword Format specifies
1,554,78 etc Integer nm int %d
2.54 ,5.5887 etc Fractional/real nm float, double %f
O,A,c,l,e etc Character char %c


Note: All the statements within a function should be terminated by semicolon ';'. Because of semicolon rule C language is also called as free form language.



Our first Program



int main()
{
int x,y,z;
x=10;
y=20;

z=x+y;
return 0;
}


The above program is our first program. The first line int main() is a function named as main(). Without main() function program not work. int is a return type used because this is an integer based program and it will return an integer value. We can also use void instead of int  when we write void we don't have to write return 0 because void is not return value.

After function we must write all of his code in block of codes, block of codes means between opening curly braces and ending curly braces

Next we declare three variable x,y and z and the data type of every variable is int ,int can store 32 bit of memory. Because of declaration memory will allocate in ram for these three variable. It will send request for allocation of memory send to Operating System.
consider that above rectangle is ram and here allocation for x, y and z is done, while variable x, y and z are int type so it can hold only 4 bytes/32 bit. Means that in small rectangle of  x , y and z only 4 byte or less value can be place.

In next line x=10 and y=20 the assignment for variable x and y is done. means the box/rectangle of x is now holding the binary number of 10 and y is holding binary number of 20. In statement z=x+y four steps are take place first the value of x is picked up second the value of y is picked up third the addition of the value of x and y will proceed and the last step is the addition of x and y is assign to z in binary form
 

Note : The above program has no output. Because we had not use printf() function. In my next post I will describe predefine function and header files.

Friday 17 January 2014

Chapter 1 Grammar of Programming Language

Learning programming language We first learn about its grammar. I know that you don't hear about grammar of programming language but its matter. Before making program we first think like a machine because the program that we make is run by a machine like calculator,computer and other digital machine which is compatible. Basically grammar of programming languages is just guideline and limitation. Here we will talk about C programming language and its grammar.


Keyword/Reserved word of C


Just think why people don't named his pet as If, You, They, Me, I, To, We, Us...
Because it is a grammatical result. In programming languages some word are already reserved and that is not use as identifier. This reserved word is called as Keyword. There are many keywords of C languages and we can not use this word as identifier. C is a case sensitive language. All keywords of C are write in lower case



List of Keywords of C


continue    goto        sizeof         volatile
default     if          static         while
do          int         struct         _Packed
auto        else        long           switch
break       enum        register       typedef
case        extern      return         union
char        float       short          unsigned
const       for         signed         void
double

  the highlighted keyword are basic and most important kayword


What is role of Keyword ?


Keywords are reserved word in C and every keywords have its unique behavior when we apply it in our program. When we talk about every language we use many grammatical such that in programming this keyword act as a grammatical word. Here we define only those keyword that we use in our program. The next grammatical part is Function.



What is Function  ?


The function is very little part of program or you can say function is Sub-Program. Actually function is group of statement/instruction for doing particular work. When we make program we have to write some codes again and again because of this the time and complexity of program is increase and short sized programs are become large. Function play important role in programming without function you can't done. There are two type of function C such as predefine function (Built in functions) and User defined function. I will describe function in my post letter.

Thursday 16 January 2014

Basic of Computer Programming



There are different types of programming languages with the help of this programming language we can make a program. Generally a program is a set of instruction.
Before making any program we first learn the basics of computer system and programming.



What is a Program ?


I have already described you about program in my first post introduction. Human language compare to the Programming language is very different. Computer or digital machines are work on when an operation is done. Suppose we know English language but the computer does't know the English language. It only know the machine language (1,0). This is work as instruction for a machine.
Programming languages helps us to give instruction to the machines in his language while we don't know the machine language.



How to make program ?


Computer/digital machine have its own memory but it do not have sense. Computer can do only those work that is instructed by programmer/users. Programs are the the sense for computer to do a particular work. If we want to create a program we must keep in mind every condition/instruction (flow chart) that will occur in real life.
MS Word, Notepad, WordPad are editors. With the help of this editors we can not make a program. If we want to write a program we need a GCC Compiler. In this compiler we first write source codes than compile it and we get a compiled code. Here the source code is write in simple programing language while the compiled code is change to machine code. This machine code is execute by the Operating System.



Dos/Windows/Unix/Linux are Operating System


There are many Operating System such as Windows xp,7,8,vista, Unix, Linux, mack, backtrack etc. This operating System Operate the system. We can use more than one operating system in a computer but in a particular time only one operating system can operate the computer.When Program is execute than operating system run the program. Programming language use to make the program, Compiler used to compile the program and when compiled code get executed the operating system run that  program


User Interface


Now we know basic of programing. This time you are reading this post in a computer or other any device this is called User Interface. When something get printed on screen/monitor it known as user interface. if only text get printed than it is known as Text User Interface and if the text and graphics/rectangle window with color both get printed on monitor than it is known as Graphic User Interface. The TUI and GUI is depends on hardware and operating system of a particular device.
Think before 7 years calculators are best way to calculate and this have only Text User Interface. But now days you have a mobile with Text User Interface and Graphic User Interface.

Interface: When any object use the feature of another object than it is known as Interface. With the help of User Interface we communicate with machines.



Difference between program and software


It is simple to define A software is containing many programs. Software is collection of programs. The graphical user interface, text mode user interface, coding, little functionality and many more items are containing by a software but a program is only one part of software.

Wednesday 15 January 2014

Programming Introduction

hello friends i am introducing very simple concept to learn programming language. If you read all of my post you will definitely become a good programmer. If you are familiar with the other programming languages then you have to no problem about the basics of programming but for those who are new in  this field i am giving simple tips and concept for easy learning programming.




What is program in programming language ?


The programs are set of instruction that is used for complete a work in very short time. before 1980's there is no programming languages, no software, no programs and programmers and the easy work was become complex. Now days you just complete most of work without headache in very short time.
The program are useful for anyone who are using technology. Without programming the program is like a body without mind.




How should Program help us ?


Very simple, before the programming languages are not develops the work become complex.
(before) Suppose you are the worker of an company and you come to office daily. A man sitting on receptionist. You go there and he write your attendance in a black register with time . When you leave the office he again write your name and time.

(now) This time your company gives you a card introducing your identity. You come to the office and go to door and swipe this card in an machine on the door than the door automatically open. Your attendance and time is automatically reached on data base. Same process when you leaving the office.
In the second approach there is no need of man and register. In the second approach the time and money is also save.

When you swipe your card to the machine the machine get a instruction and it will watch for a unique number. If the unique number is existing than it will open the door and get your name and a paticular time for check in. That is the power of program....

There are many programing languages. With the help of this programing languages we can make programs.Programming language is medium between computer system and programmer.