Skip to main content


Showing posts from September, 2010

C Program 2 – Electricity Bill calculation

This program is to display the electricity bill calculation based on the number of units consumed every monthInput : the number of units – variable name – unitOutput – Amount of rupee – variable name – amountLogic: UnitsRupees1-50 units0.75/unit51-1000.85/unit101-2001.50/unit201-3002.20/unit>3003.00/unitIn all the cases, there will be service charge of Rs.20 will be collected.#include <stdio.h>
#include <conio.h>
int main()
    float amount=0,units;;
    printf("Enter the number of units");
    scanf("%f", &units);
    if(units <=50)
     amount = units * 0.75;       
    else if(units >50 && units <=100)
         amount=0.75 * 50 + 0.85*(units-50);
    else if(units >100 && units <200)
         amount=(0.75*50) + (0.85*50 )+ (1.5 *(units-100));
    else if(un…

C Program 1 – To find the reversal of a given number

Assume the number 1234, after reversal the number becomes 4321. Input: One number, Ex 1234, declared to numOutput : One number, Ex 4321, rev is the variableLogic: separate each digit using modulo operator from the right and bring that number to frontdivide the given number by 10, so the right most digit will be till the number becomes <=0output the result#include <stdio.h>
#include <conio.h>
int main()
    int num,rev=0;
    printf("enter the number to be reversed");
    scanf("%d", &num);
    while(num>0) //till the number is positive, perform the process
    rev=rev*10 + (num%10);
//separate each digit and bring it to the first
    printf("The reversal is %d ",rev);
    return 0;

Decision Making and Looping

C supports Decision Making and Looping (Interations)In decision Making, if, if-else, if- else if – else, switch – case is used.if(condition){//These statements are executed only if the condition is true or 1}if – elseif(condition){//These statements are executed only if the condition is true or 1}else{//these statement are executed only if the condition is false or 0}(in the above example, there is no else part means that for some situation, there may not be a need for else part)if – else if – else if(condition 1){//These statements are executed if the condition 1 is true or 1}else if (condition 2){//These statements are executed if the condition 2 is true or 1}else{//These statements are executed neither condition 1 nor condition 2 are true // if all the conditions fails, then this statement will execute}Switch Case switch (expression){case 1: statement 1;break;case 2: statement 2;break;case 3: statement 3;break;default: statement d;break;}//Statement n;The switch case works similar …

Data types

Declaration of variable names does two thingsit tells the compiler what the variable name is specifies what type of data the variable will holdSo it is mandatory that each variable should belong to a particular data type. C supports different data types like integer, character, float, double, etc.Data type Size on a 16 bit machineRangechar1 byte or 8 bits-128 to 127signed char1 byte-128 to 127unsigned char1 byte0 to 255short int or short1 byte-128 to 127int2 bytes-32768 to 32767unsigned int2 bytes0 to 65535long int or long4 bytes-2,147,483,648 to 2,147,483,647unsigned long int4 bytes0 to 4,294,967,295float4 bytes3.4E-38 to 3.4 E+38double8 bytes1.7E-308 to 1.7E+308long double10 bytes3.4E-4932 to 1.1E+4932Example of declaring variablesint a=20,b=20,c;int a=10;int b=9;int c=20;float c=3.5;char a =’A’;long int al or long a;short int b or short b;double c;long double c;

Tokens in C

Tokens are classified as KeywordsIdentifiersOperatorsConstantsSpecial SymbolsKeywordsKeywords are reserved by the compilers and keywords will not be redefined (means the keywords are not been declared as variable names, etc). There are standard 32 keywords in C compiler, and some compilers uses more keywords based on their specification. Here is the list of 32 keywordsautobreakcasecharconstcontinuedefaultdodoubleelseenumexternfloatforgotoifintlongregisterreturnshortsignedsizeofstaticstructswitchtypedefunionunsignedvoidvolatilewhileIdentifiersIdentifiers are variable names declared inside the program. They use alphabets, number, symbols like _ and $They must begin with  alphabet or _ They are case sensitive i.e Var_name is different from var_nameSome valid identifiers arejohnValuer_siex1x2ChennaiSum1abcdinvalid identifiers123%(super)25th1stab-dsum of totalOperatorsoperators are further classified into different categories likeArithmetic operators +  - for addition   1+3 = 4-   for subt…

printf() and scanf() Functions

Printf() is a function to print strings to the display unit and scanf() is to scan the input through the keyboardthe f in the printf and scanf are called as format. That is, printf() and scanf() will print and scan in some particular format, it is the duty of the developer to provide that formats.For exampleprintf(“a=%d, b=%d”, a,b); //This function will print a=10, b=20 if the values of a and b are 10 and 20scanf(“%f”, &a); //this will accept a float number as the specifier is %f which is for float, See the Ampersand symbol which to tell the compiler to store the variable into the address.Here is the list of format codes or format specifiersscanf() format codesCodeMeaning%cread a single character%dread a decimal integer%fread a floating point value%eread a floating point value (even in exponential format)%gread a floating point value%hread a decimal, heaxadecimal or octal integer%oread an octal integer%sread a string%uread an unsigned integer%xread a hexadecimal integerh for shor…

Sample C Program

/* This program is written by T S Pradeep Kumar on 28th Sep 2010This is to display Hello World to the display unit*/#include <stdio.h> //including the standard IO functions like printf(), scanf()int main(){printf(“Hello World \n”);return 0;}stdio.h is the library which contains the printf(), scanf() and other standard IO functions, so before we use any function we need to include in our C program. Most of the compilers will take stdio.h automatically, even if we dont include in our programint main() {} is the main functionprintf(“Hello World\n”); printf() is the function which is printing to the display unit and it is displaying Hello World. Whatever is there inside the double quotes will displayed as it is in the monitor except the format specifiers or format codes (%d, %f, %c)return 0; is the final statement which returns the integer value 0 (this is just to make the compiler happy)Here in the above program, you can see there are semi colons ;Every C statement must end with a …

Introduction to C Programming

Features of C ProgrammingC is a small language having lesser number of keywords than Java or pascal.C is a native language of Unix, Linux. Not only that many of the windows packages, database programs, graphics libraries are written in C programC is portable – it provides standard set of libraries that work on the same way with all machinesC is modular, as it supports functions to divide the program in to sub program C is efficient on most machines, because certain constructs are machine dependant. Structure of a C programComments the comments are usually ignored by the compilerso this part informs about the author of the programwhat is the usefulness of this programupdation or creation of the programExample: //  - Single line comment/* ----- multi line comments    ….*/Preprocessor Directives before the compiler runs, the preprocessing takes place.the preprocessors are prefix with a symbol #Examples: #include, #define, #ifdef, #pragma, etc.Function prototypesThis is to inform the comp…

Xgraph [dialog.o] error

If you are installing ns2 allinone package in fedora or ubuntu or any other linux, all the packages will be installed successfully. Occasionally the recent releases of linux flavors failed to install xgraph.Problem:The possible error you may get is [dialog.o] error,Solution:Open the file dialog.c which is available under the folder xgraph-12.1/ (this is in my case) Find out the function getline() (will be at two places), usually line number 780 and 891 or 889, replace getline() function to newgetline() (at the both the places) save the file and at the prompt give ./configure give the command make If there are no errors, xgraph will work now..