Basic C Programming – 1

intro (2).jpg

As you all know, this is a blog dedicated to computer science. In my first post I would like to post some of the basic C programs to begin with. As this is my first blog post, the programs will be very basic and of beginners level. please be patient as I will post other high level programs in near future.

  1. Program to find out the cost price of an item.

In this program we will find out the cost price of an item. In this program we will always enter selling price higher then the cost price. this is a very basic program to understand how operators, variables, statements, work in C programming. I will also elaborate each and every program blow. So here is the first one.

#include <iostream>
#include <stdio.h>
int main()
{
float sp, profit, cp ;      // sp stands for selling price, cp stands for cost price
        printf(“enter the selling price of an Item: “);
                   scanf(“%f”, &sp);
                     printf(“enter the total profit earned on the Item: “);
                              scanf(“%f”, &profit);
                                  cp = sp – profit ;
                                          printf(“cost price of  Item is = %f”, cp);
return 0 ;

}

Program Description : 

In this program our task is to find out the cost price of a single item. we are taking this program to understand how basic things work in C programming. we are starting with  in-built library functions.

<iostream.h>

<stdio.h>

these are predefined library functions also known as header files. As the name suggest “predefined library function”. they are already defined in the C compiler to do some specific tasks. #include<iostream.h> and #include<stdio.h> belog to C++ and C programming respectively. the basic function of these header files is to provide input/ output functionality in C or C++ programming.  Now coming to #include.

#include

#include is a preprocessor directive. it include the <stdio.h> or <iostream.h> file in our program to add input/output functionality. we will discuss preprocessor directives and library functions in detail in upcoming blog post. for now treat it like a habit to write it on the beginning of the program to use input/output functionality in the code. Now coming to main ()

int main ()

To simply begin with, main () is a function. it is the starting point of any C program. The program execution in C takes place with main. C is a sequential language. everything we write in C will execute sequentially and the starting point of execution will be main (). In every C program there will be one and only one main ().

Now coming to key words

int, float, double, long, char, goto, void, for, if, else, while etc.. etc..

these and many more are keywords. they have an special significance in C programming. there are 32 keywords available in C programming. for now we will focus on int and float only. int is a keyword used to fetch/denote integer value (values which are not decimal) and float is used to fetch/denote decimal or floating point numbers/values.

Now coming to  C variables

Have you ever wonder that whatever data we enter in our C programs, where it will rest or save ? the data we enter in our programs get save in computer’s memory. this memory is divided in so many small memory locations. nderstand it like an array of memory locations. we can store different types of data in these memory locations and the name given to these memory locations are variable names.

e.g

int a = 10 ;

float b = 3.5;

char ch = ‘b’ ;

In first e.g  a is a variable of integer type which can store only integer value.

In second b is a variable of float type which can hold only floating point value.

In third ch is a variable of character type which can store a character.

    blog 1 draw

 

 

the printf and scanf functions.

printf and scanf are predefine functions in the <stdio.h> library. both the two are opposite of each other. printf is used to print output on console while scanf is used to receive input via keyboard.

e.g

printf (“hello world”) ;

printf (“enter a number through keyboard :”) ;

scanf (“%d”, &a) ;

In the above program we use both printf and scanf to print output and receive input from keyboard. NOTE every statement in C program is terminated by ‘;’ . also known as statement terminator. after receiving input from keyboard we performed arithmetic operation on operands (variables. ultimately on the values we given via keyboard). In the program above we perform arithmetic operation on selling price and profit and the result is stored on the variable  cp (cost price). after that we are printing this result using printf on the console.

 

Working :

after successful compilation when we run this program, we’ll get the message “enter the selling price of an Item: “ on the console window. we get this message with the help of printf function as it can print the output.

we can input any number as a selling price of an item. let 1000 Rs. As we enter the amount and hit the enter key, 1000.000000 will be save in a memory location, which is of float type and the next message will popup.

“enter the total profit earned on the Item: “. let the profit be 100 Rs. As we enter the value i.e 100.000000 and hit enter the value will get save on a memory location which holds the name profit.

after the arithmetic operation the result i.e 900.000000 will be store in the variable cp (memory location) and this result we can print using the printf statement.

2019-01-04 (1)

All the other things like return statement, format specifiers, data type properties will be covered in a separate post.

Thanks a lot,

This is Sachin, Signing off.

Leave a comment