Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Tuesday, 7 November 2017

Data Types in C Language with Examples

Data Types in C Language

Now, Here we are going to discuss data types in c language with examples. 

Data type tell us which type of data or value we can store in a memory by the help of variables e.g integer type value, character type value, float type value, double type value, etc.

In other words, We can say that the data type is a keyword which identified the type of data and store the input of a program into a memory(Ram) by the help of variables.

For example: Declaration of data type

#include<stdio.h>
#include<conio.h>
void main()
{
int a = 50;
}

Here 'int' is a data type and 'a' is variable with value 50 of integer type. 


Data Types in C

There are different types of data types in c language which are given below.


Data Types in C Language

(1) Primitive or Primary Data Type

Some primitive data types which is int, char, float, double, and void.

(2) Derived Data Types

Some derived data types in c which is pointer, array, references, function.

(3) User - defined Data Types

Some user - defined data types in c which are structure, union, enumeration.


Primitive Data Types - Size and Range

The primitive data type contains integer and floating based data types. C language supports both signed and unsigned literals.

Let's understand first signed and unsigned then size and range of primitive data types.

Signed  : Signed keyword allows both negative and positive values and this is the default properties or data type modifier for every data type.

For example: 

#include<stdio.h>
#include<conio.h>
void main()
{
int a = 20;
int b = - 30;
signed int c = 40;
signed int d = - 50
}

Above all the data types declaration are valid declaration.

Unsigned : Unsigned keyword allows only positive value, It does not supports negative values.

For example:

#include<stdio.h>
#include<conio.h>
void main()
{
unsigned int b = 30;//valid declaration
unsigned int c = - 30;//invalid declaration 
}

The memory size of primitive data types can be change according to 16 or 32 or 64 bit operating system.

Data Type                         Memory Size               Range
char                                  1 byte                          - 128 to 127
signed char                      1 byte                          - 128 to 127
unsigned char                  1 byte                          0 to 255
short                                 2 byte                          - 32,768 to 32,767
signed short                     2 byte                          - 32,768 to 32,767
unsigned short                 2 byte                          0 to 65,535
int                                     2 byte                           32,768 to 32,767
signed int                         2 byte                           32,768 to 32,767
unsigned int                     2 byte                           0 to 65,535
short int                           2 byte                            32,768 to 32,767
signed short                     2 byte                            32,768 to 32,767           
unsigned short int           2 byte                             0 to 65,535
long int                             4 byte           - 2,147,483,648 to 2,147,483,647
signed long int                 4 byte           - 2,147,483,648 to 2,147,483,647
unsigned long int             4 byte           0 to 4,294,967,295
float                                  4 byte
double                               8 byte
long double                       10 byte

This is the table of data types in c with size and range.


Declaration of some data types with variables

#include<stdio.h>
#include<conio.h>
void main()
{
int a = 50;//positive integer data type
int aa = -48;//negative integer data type
char c = 'b'
float f = 3.1;
float ff = - 5.8
long l = 56850;
long ll = - 452658;
short s = 569;
short ss = - 569;
double d = 3.247895233
}


Data Types in Programming

This is simple c program where we will declare integer data type with variable and store value and print that value to console.

#include<stdio.h>
#include<conio.h>
void main()
{
int i = 7009;//integer data type with i variable
clrscr();
printf("Number is : %d ", i);
getch();
}

Output: 7009


Usage of sizeof Operator 

By using sizeof operator you can get size of type e.g int, char, float, etc.

For example:

#include<stdio.h>
#include<limits.h>
int main()
{
printf("Storage size for int : %d ", sizeof(int));

return 0;
}


Here we have learned data types in c language with examples.

Share:

Monday, 6 November 2017

Printf and Scanf Function in C Language

What is printf and scanf in C Language

printf and scanf in c language

After learning the concept of types of variables in c language, Here we are going to discuss printf and scanf function in c language with simple examples.

Let's understand what is use of printf and scanf in c language with simple and easy examples.


What is printf() in c ?

In c programming language, printf() is a function which is defined in "stdio.h" header file. 

C printf() function is used  to display output on the console. By using printf() function we can print any massage or data on the console. 

Syntax 1: For printing massage only on the console

printf("hello world");//It will print same massage whatever you write in double quotes(" ").

Syntax 2: For printing massage and data(value) on the console

printf("Format specifiers", Value1, Value2...);

Here format specifiers can be %d(integer), %c(character), %s(string), %f(float), %d(double) and printf() function can take any number of arguments i.e Value1, Value2, Value3, but separated with comma(,).

For example1:

#include<stdio.h>
#include<conio.h>
void main()
{
int a = 30;
clrscr();
printf("Hello World\n");
printf("Number is %d ", a);
getch();
}

Output: Hello World
             Number is 30

In the above example, We have printed simple "Hello world" massage and print the data of "a" by using format specifier %d in printf() function.

For example 2:

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

void main()
{
int a = 30;
float f = 5.2;
clrscr();
printf(" C programming \n ");
printf(" Value of integer only : %d  \n" , a);
printf(" Value of float only : %f  \n" , f);
printf("Both Values : %d %f " , a, f);
getch();
}

Output: C programming
             Value of integer only : 30
             Value of float only : 5.200000
             Both Values : 30 5.200000


What is scanf() in c ?

In c programming language, scanf() is also a function which is defined in "stdio.h" header file.

C scanf() function is used to read the input data from console or keyboard

Syntax: 

scanf("Format specifiers", &Value1, &Value2...);

For example 1:

#include<stdio.h>
#include<conio.h>
void main()
{
int age;
clrscr();
printf("What is your age :");//print massage on the console
scanf(" %d ", &age);//taking number from console and store in age variable 
printf("My age is : %d ", age);//print the age's value on console
getch();
}

Output: What is your age : 50
              My age is : 50



For example 2:

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

void main()
{
int a;
float f;
clrscr();
printf("Enter your numbers: ");
scanf("%d %f " , &a, &f);
printf("Your numbers : %d %f ", a, f);
getch();
}

Output: Enter your numbers: 4 5.2
             Your numbers : 4 5.200000


Printf and Scanf Example : Sum of 2 Numbers

This is the simple example of using printf() and scanf() function for addition of two number. We will take input or number from console and add them.

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

void main()
{
int a;
int b;
int sum;
clrscr();
printf("Enter your first number : ");
scanf("%d", &a);
printf("Enter you second number : ");
scanf("%d", &b);

sum = a+b;
printf("addition of 2 number is : %d ", sum);
getch();
}

Output: Enter your first number : 6
             Enter your second number : 5
             addition of 2 number is : 11


Difference between printf and scanf in C language

The difference between printf and scanf in c language is printf() function is used to display the output on the console e.g massage or data values and scanf() function is used to read the input data from console or keyboard.

This your printf and scanf in c language with simple examples. I hope it will be useful to you.

Share:

Sunday, 5 November 2017

How to Declare Variables in C Language


Variables in C Programming Language

Variables in C Language

In this article, We are going to discuss what is variable in c programming language, types of variables in c language, and how to declare variables in c language with simple syntax or examples.

In the last article of  c language tutorial, We saw how to print "hello world" in c and how to compile and run first program in c language. But here we will learn variable declaration and definition in c and types of variables in c programming.


What is Variable in C?

Variables names are names given to location in memory. These location can contain integer, real, or character constant. In c, a variable is an entity that may change during the program execution i.e the value of variable can be changed.

Variable is used to store the data of different-different types e.g integer, real, character, etc.

Variable is an identifier which stores the data in memory.

Syntax of C Variable

datatype variable_name;

             or

datatype variable_name, variable_name2, variable_name3;


Declaration of Variables 

Let's declare variables with data types.

int x;
char y;
float z;
double a;

In the above declaration, int, char, float, double is a data types and x, y, z, a are variables.


Initialization of Variables

We can initialize variables in c. In other words, we can put some values in variables.

For example:

int x = 10, b = 40;
char y = 'a';
float z = 2.1;

Some rules for declaring variables in c

There are some rules for declaring variables in c programming.
  • Each variable name can contains A - Z(capital letter), a - z(lower letter) and 0 - 9(digits), and the under score(_) character.
  • Each variable name should start with alphabets or underscore(_).
  • In variables, No white space are allowed.
  • Variable name cannot start with digits.
  • Except underscore symbol, we cannot use other symbols in the middle of the variable name. We can use only underscore(_) symbol in the middle of variable names.
  • A variable name must not be same as any keyword e.g int, float, etc. We cannot use int, float, char because it reserve word or predefined keyword.
  • Variable names are case sensitive.
  • Variable values can be alphabets or numeric.
  • Type of variable can be int, char, float, double, and void.

Let's talk about some types of variables in c programming language one -by-one.

Types of variables in C

There are some types of variables in c language and these are given below.

(1) Global Variable 

Global variable is a variable which we can declared only outside of a function or block is known as global variable. We cannot declare global variable inside any functions or blocks in c .

Global variable is available to all the functions. We can declare global variable on top of the program or above main() method.

For example:

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

int i;//Global Variable
void main()
{}


(2) Local Variable

Local variable is a variable which we can declare inside any functions or blocks is known as local variable in c. It can only use within the function or block where it is declared. We can't declare local variable outside of the function or block.

Before accessing local variables in c we have to initialized first then access.

For example:

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

void main()
{
int x = 30;//Local Variable
}


(3) Automatic Variable

All variables which declared inside the block or function is automatic variable by default. We can also use "auto" keyword to define automatic variable.

For example:

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

void main()
{
int i = 40;//local variable(automatic variable also)
auto int x = 50;//automatic variable with auto keyword
}


(4) Static Variable

When we declared any variable with static keyword in known as static variable in c.

For example:

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

void main()
{
int i = 40;//local variable
static int j = 50;//static variable
}


(5) External Variable

External variable is also a global variable and when we define any variable by using "extern" keyword is known as external variable in c.

For example:

//first.h

extern int x = 40;//external variable( also global)

use in any c program by including e.g #include "first.h"


C program with variable

This is the simple program of c language with variables where we declare and initialize variable and print the value of global and local variable.

For example:

#include<stdio.h>
#include<conio.h>
int x = 30;//global variable
void main()
{
int y = 40;//local variable 
printf("Value of global variable %d ", x);
printf("Value of local variable %d ", y);
getch();
}

here we discussed some basic concepts of c language e.g what is variables, types of variables and how to declare and initialize variables in c language.




Share:

Friday, 3 November 2017

First Program in C Language

Simple Program in C Language - First C Program

Here we will see, How to write first program in c language and how to compile and run first c program. In first program of c language, we will print simple "hello world" massage.

After learning the complete introduction of c language and features of c language, it is time to learn how to run first program in c.

Before executing a first program in c language you need to download and install Turbo C++ on your computer. There are many compiler available for c and c++ that you can use but mostly used compiler is Turbo C++.

You can download here, Turbo C++.

After Downloading and installing Turbo C++ on your computer, you will be able to write a first program in c.

Let's write a complete program that prints hello world to the screen.

To print hello world, Open c console i.e use turbo c++ and type simple code which is given below.


For example :

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("hello world");
getch();
}

Output: hello world

Let's understand the above code in detail.


(1) #include<stdio.h>

It includes the standard input and output library functions. The printf() function is defined in <stdio.h> file.


(2) #include<conio.h>

It includes the console input and output library functions. The getch() is defined in <conio.h> file.


(3) void main()

In c, void is a return type which returns nothing and main() is a function in c language where program execution start. In other words, We can say that main() is function and it is the entry point of every programs in c.


(4) clrscr() function

In c, Function clrscr() is used to clear the screen e.g suppose you execute different - different c program many times and do not use clrscr() function then the output of previous program will be add to the current program's output.


(5) printf() function

It is used to print the data or massage on the console.


(6) getch() function

It is used to hold the screen. Press enter or any key to release the screen.

Let's print hello world c program with c console i.e with Turbo c++ and see how to compile and run c program.


Firs C  Program


Output:


Hello World C Program

Press enter for back to the Turbo C++ console

After edit c program, We have to compile and run c program for displaying output.


How to Compile C Program

There are 2 ways you can compile c program.

(1) By menu :  Click compile menu and then choose compile sub menu to compile the c program.

(2) By shortcut key : Use Alt+F9 for compilation.

How to Run C Program

There are 2 ways you can run you c programs.

(1) By menu : Click on run menu and then choose run sub menu for run c program.

(2) By shortcut key : Use Clt+F9 for running c programs 

Note: Clt+F9 key compile and run c program at the same time.

Read More:

How to Use Pointers in C.
What is Recursion in C Language.

Here we discussed, How to write a first C program and compilation and execution of Firs C programs with Turbo C++.

Share:

Wednesday, 1 November 2017

Features of C Programming Language


Features of C Programming

Features of C Language

There are many features of C programming language which makes c programming more interesting, popular, and usable in development of system software, operating system, drivers, compilers, etc.

In the last post of  C tutorial we have already discussed that what is c language and where c language is used or why we use c programming language and history of c language with complete information.

Let's start features of C language with complete description one-by-one.


Features of C Language


(1) Simple 

C programming is very simple and easy to learn programming language for beginners because it written in simple English language and provide structured approach, data types, and functions so that we can categorised the problems and solve them easily.


(2) Mother or Base Language

C programming is known as mother and base language for all programming language because all the programming language uses the syntax of c language e.g c++, java, etc. So after learning c programming language, you can easily learn other programming language because all the programming language use the syntax of c language.


(3) Machine Independent or Portable 

C programming in machine independent programming language because programs once written in C can be run on another machines. e.g edit and compile c program in windows and that program can be run other windows but not UNIX or Mac OS because it is machine independent not platform independent language.

In other words, It can run on any compiler with little or no modification.

C language is machine independent programming language but it not platform independent programming language. C  is platform dependent programming language.


(4) Middle Programming Language

C programming written in assembly language and it is used to develop system application, drivers, kernel, compilers, etc. It supports both high -level and low - level programming instruction, this is the reason it known as middle level programming language.


(5) Structure Oriented

C programming is structure oriented programming language by using this approach we can divide problem into sub-problems and then solve it easily. Structure oriented programming approach reduce the complexity of the programs, and break the programs into parts by using function and then can modify and maintain the programs.


(6) Powerful Language

C programming is the most powerful language because it have wide verity of data types, functions, direct access of hardware, control statements and decision making statements.


(7) Use of Pointer

In C, There is pointer concept and by using pointer we can easily and directly interact with the memory because pointer is a variable and it hold the address of another variable. We can know the memory address of any variable by using this pointer.


(8)  Recursion

There is a recursion in C i.e we can call the function withing the function. It reduce the code length.


(9) Rich Library

C provides lots of ready made functions. C is a collection of C library functions. We can create our own function and add it to C library.

(10) Extensible

C is easily extensible because it can adopts new features easily. 

Here we discussed, Top 10 features of C programming language.

Share:

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate