Showing posts with label C programming. Show all posts
Showing posts with label C programming. Show all posts

Learn C programming - Addition of two numbers

Tuesday, February 16, 2010

Let us study a simple C program to add two numbers , input from keyboard and print the sum.


#include <stdio.h>
#include <conio.h>

void main ( )
{ int a, b, sum;
clrscr( );
printf ("Enter two integer numbers");
scanf ("%d%d,&a,&b);
sum=a+b;
printf("\n The sum of two numbers is %d\n",sum);
getch( );
}

The first two lines with #include are called pre processor directives which loads two function libraries in memory. Because of which we can use functions like clrscr, printf , scanf in our program.

Third line is main function , which is must in any C program. void means the function will not return any value. If we do not write void , we have to add one line return (0) at the end of the program. Because in C every function must return a value.

Fourth line is declaration of variables a,b and 'sum' as integers. In C we must declare type of the variable before we can use it .

Fifth line is clear screen function which clears the previous screen i.e. any previous output on the screen is cleared .

Printf in sixth line displays message on screen to enter two numbers

Scanf in seventh line accepts two numbers from keyboard and assign these to variables a , b

The eighth line adds two variables a and b and assigns it value to variable 'sum'

Ninth line prints the value of sum on screen . The printf statement here uses format specifier %d here , we will study about this later.

The getch in tenth line holds the output screen till we press a key.

From the above program we can say that a general program in C will have

#include lines to include necessary library functions

void main ( )

Declaration and initialization of the variables required in the program

Data input statements

Processing or logic statements generating desired output

Statement to display output.

Please note that the all C keywords in program has to be in lower case because C is case sensitive language and any capital letter for commands/keywords gives error.

Difference between Getchar( ),Getch( ) ,Getche( )

Sunday, January 31, 2010

What is difference between Getchar( ) , Getch ( ) , Getche ( ) ?

Getchar ( ) - It will accept a character from keyboard , displays immediately while typing and we need to press Enter key for proceeding.

Getch ( ) - It just accepts a keystroke and never displays it and proceeds further. Normally we use it at the end of the main ( ).

Getche ( ) - It will accept a character from keyboard , displays it immediately and does not wait for Enter key to be pressed for proceeding.

C character set, Keywords, Identifiers

C character set :-
The valid characters for C are alphabets ( A-Z, a-z) , numbers (0 - 9) and special symbols .

Reserved words ( Keywords) :-
Keywords are the words whose meaning has already been known to the C compiler. They are also called instructions. e.g. int , return
They are reserved words and can not be used as variable names.
There are 32 keywords available in C.

Identifiers
:-
Identifiers are the names you supply for variable types, functions and labels in your program.You can not use keywords as identifiers.

What is programming ?

Saturday, January 30, 2010

Let us first understand what is programming .

A program is a steps taken to perform a task or job.

A real life example will be we program our selves to come from home to college.

We have planning in our mind as when we will get up , then in how much time we will get ready, then at what time we should catch bus or train etc. We have a order of sequence for the steps we perform

i.e. we do not sit in the bus and then start brushing our teeth.

Now let us take example of addition of two numbers 22 and 33 with help of a calculator.

Our steps or program will be as follows

1. Enter 22
2. Press '+' key
3. Enter 33
4. Press '=' key
5. See the result on screen.

Similarly we can write logical steps to perform a task in computer language and execute it to get the desired result.

It will be important to understand that logical steps to perform a task remains same even if computer language changes . The only thing changes from language to language is there commands and syntax.

TYBcom Computer Syllabus for C programming

Sunday, August 23, 2009

TYBcom Computer Systems and Application subject (from 2009) has Unit IV as 'Introduction to C programming' having syllabus as follows.

1) Introduction :
C character set
Reserved words
Identifiers
C data types
C type modifiers
Constants
Variables
Expressions
Operators (Unary , Binary, Relational, Logical)
C assignment statements
Operator Precedence
Concept of header files (stdio.h, math.h, conio.h only)

2) Simple programming concepts :
Form of a C program, printf() (%d,%f,%c,%s,%lf,%ld), scanf(), gets(), getchar(), puts(), putchar(), getch(), putch(), pow(), sqr(), clrscr(), storage class specifiers , Preprocessor directive #include <>


3) Control Statements:
If else , for, while , do while , switch , break , continue .

4) Arrays :
Concept of one dimensional array (character and integer ) , sorting of numbers , printing array of numbers.