C Programming Basics

C Basics

There are some basic elements in every language. Same is the case with C language. These basic elements are variables, data types, constants, keywords (reserved words), variable declaration, expression, statements etc. All these are used to construct a C program.

Let's discuss each of them in detail:

Variable

In C language, we operate on data. This data is needed to be stored at some place. The data is stored in the memory and the name given to the place of memory where data is stored is known as Variable. So Variables may be defined as the name given to store values. Variables can take different values but one at a time. This value can be changed during the execution of the program. It means  their values does not remain constant therefore they are called variables.

Data Types

When we store data in memory, the data may be of different types It may be a character like 'a', 'b' or 'c' or a number like '12' for some decimal values like '23.5'. So, Data Type may be defined as the type of data that is stored in the variable. C basically supports 4 Data Types :
  1. char - used to store any single Character values like 'a', 'r', 's' etc.
  2. int - used to store Integer type values like 12,27,14 etc.
  3. float - used to store decimal or floating values with single precision like 12.35, 27.14, 35.03 etc. 
  4. double - used to store decimal values with double precision like 34.76, 78.45, 98.56 etc.
Note : The difference between float and double data types is that the float will accurately show decimal points to 2 places of decimal whereas, the double accurately shows 4 places of decimals.

There are two types of type qualifiers that are used with data types:
  1. Size Qualifiers - short, long (used to modify the size of the data type)
  2. Sign Qualifiers - signed, unsigned (used to describe the sign of data type)

Constants

Constant is a value that cannot be changed during the execution of the program. While writing a C program we use some constant values like any number or any character, these are called Constants.  There are three types of constants :
1. Numeric Constant
Numeric Constant includes numeric values. Examples of Numeric Constants includes all integer values as well as decimal values.
2. Character Constant
Character Constant includes any single Character. Examples includes alphabets.
3. String Constant
String constant are the group of characters, which will be discussed later.

In the below image, all the values given to the variables are the constants .
Datatypes in C

Variable Declaration

Declaring a variable means telling the compiler that it a variable being formed by the programmer. It is must to declare a variable before it is used in the program. Declaration of a variable specified by its name and data type.

The syntax or format of Variable Declaration is :


data-type variable-name;
data-type may be int ,char, float, double .
variable-name may be any supported name by the C language.

Legal Variable Names in C :

A Variable Name in C may be a word starting with any alphabet or underscore sign '_' .
example - 
int name;  
int name1 ;
int name2;   
int _name;  
int n; 
int my_name; 
are valid variable declaration; 

int 1name; 
int 2name; 
int name@; 
int 1name;
int  my name;
are invalid variable declaration;
We can also declare more than one variable of the same type in a single statement by separating variable names with comma ',' .
Ex- int n, x, y, name, xyz, anything ;

Initialisation of Variables

Initialisation of variables means providing values to the defined variables or declared variables. The value is provided to variable names by using equal to '=' or known as Assignment operator in C language.
For Example - int n=45, num=35, age=25;
The above statement tells the compiler variable 'n' is assigned value 45, 'num' is assigned value 35 and 'age' is assigned value 25.

are all different variables, which contains different values.

Let's first see a program using the above concepts :
#include<stdio.h>
int main() {
    int number=12;
    char letter='S';
    float salary=25950.78;
    printf("Number : %d,Letter : %c,Salary : %f",number,letter,salary);
}

We will explain this code in the next page.

Post a Comment