C programming Tutorial - learn basic of C language:
Introduction
Before we start , it would be interesting to find out what really is C and why we need any programming language .
A programming language is a set of instruction for device/computer , that produce various kind of output . As we know, digital computer can understand the instructions only the way of binary manner [1 or 0] , so when we write any program on C , a compiler read the instruction and convert those instruction to the binary code & ultimately we get the result as a output .
C is programming language which was developed by Dennis Ritchie at " AT & T's " Bell Laboratories of USA in 1972 . Due to high reliable , simple & easy to use , C programming language became popular . There are several reason to use C language now days , you can easily find on internet , thats reason . so with out wastage time lets begin about C.
C Programming Basic:
The C character:
A character denoted by any alphabet , digit or special symbol used to represent information
Fig below shows "The C character set" :
The alphabets , digits and special symbols when properly combined form Constants ,Variable and Keywords . A constant is an entity that does not change , whereas , a variable is an entity that may change . A keyword is a word that carries special meaning .
In programming languages , constants are called literals and variables are called identifiers .
Types Of constants in C Language :
Basically , there are two types are constants in C : -
- Primary Constants
- Secondary constants
In this blog , I will take focus only Primary constants . Later , we will learn about secondary constant .
Interger Constant in C language:
- An integer constant must have at least one digit .
- It can be any of zero , positive or negative & must not have a decimal point .
- No commas or blanks are allowed within an integer constant .
- If no sign precedes an integer constant , it is assumed to be positive .
Real constant or floating point constant in C language :
- A real constant must have at least one digit .
- It must have a decimal point .
- It could be either positive or negative . Default sign is positive .
- No commas and blanks are allowed within real constant .
In C programming language the exponential form is usually used if the value of the constant is either too small or too large .
Example : 3.42⨉10^-4 = 3.42e-4 or 3.42E-4
2.3⨉10^8 = 2.3e8 or 2.3E8
Character constant in C Programming language :-
- A character constant is a single alphabet , single digit or single special symbol enclosed within single inverted commas .
- Both the inverted commas should be point to the left .
Example : 'A'
'='
Variables in C programming language :
Variables are the names you give to computer memory locations which are used to store values in a computer program.
For example, assume you want to store two values 10 and 20 in your program and at a later stage, you want to use these two values. To store any value in programming language , variable comes to play that role .
A particular type of variables can hold only the same type of constant .
Rules for creating variable in c Programming language :
- A variable name is any combination of 1 to 26 alphabets ,digits or underscore.
- The first character in the variable name must be an alphabet or underscore(_) .
- No commas and blanks are allowed .
- No special symbol other than underscore can be used in variable name .
Creating variables is also called declaring variables in C programming. Different programming languages have different ways of creating variables inside a program. For example, C programming has the following simple way of creating variables −
#include<studio.h> int main() { int a=20; float b=25.5; char c_val='g'; printf("In this program the integer variable is : %i\n",a); printf("In this program the float variable is : %f\n",b); printf("In this program the character variable is : %c",c_val); return 0; }
Output of the above program :
In this program the integer varible is : 20 In this program the float varible is : 25.500000 In this program the character varible is : g ------------------------------- Process exited after 0.07131 seconds with return value 0 Press any key to continue . . . |
The above program hold two variables name a and b . In C Programming language , we have to declare variable type , i.e. , integer variable , either floating variable or character variable . Integer variable can holds only integer value(Example - int a =20) , float variable can hold only decimal number type value(Example - float b =25.5) , char variable can hold only character value (Example - char c_val= 'g') .
If you declare wrong variable type , let say , int a=25.5 . it will return 0 .let's understand with same example . In the below program we change b variable . We declare that the variable b is integer , but we are trying to store decimal number . C compiler does not hold the value and result will be zero .
#include<stdio.h> int main() {int a=20; int b=25.5; char c_val='g'; printf("In this program the integer varible is : %i\n",a); printf("In this program the float varible is : %f\n",b); printf("In this program the character varible is : %c",c_val); return 0; }
Output of the above program:
In this program the integer varible is : 20 In this program the float varible is : 0.000000 In this program the character varible is : g ------------------------------- Process exited after 0.07131 seconds with return value 0 Press any key to continue . . . |
Keywords in C programmming Language :
auto | continue | enum | if |
signed | typedef | break | default |
extern | int | sizeof | union |
case | do | float | long |
static | unsigned | volatile | char |
double | for | register | struct |
void | const | else | goto |
short | switch | while | return |