C Programming Basics Contd.


Program Example
#include<stdio.h>

void main() {
    int number=12;
    char letter='S';
    float salary=25950.78;
    printf("Number : %d,Letter : %c,Salary : %f",number,letter,salary);

}

Code Explanation :

First we include the compulsory header file i.e, 'stdio.h' .
#include<stdio.h>
Then, our main function starts with declaration of 3 variables.
    int number=12;
    char letter='S';
    float salary=25950.78;
Then we print the values of our variables by using printf function and conversion specifiers (%d, %c, %f). Let's understand conversion specifier in detail.


Conversion Specifiers

Conversion Specifiers are used to tell or specify the compiler the type and size of data is used in the program. In C language you need to tell to compiler about what type of data being used by the programmer. this is done with the help of conversion specifiers.
Each conversion specification must begin with a percentage sign(%) . Some of the conversion specifier are given below:

Conversion Specifiers
Usage
%c
a single character
%d
an integer value
%f
a floating point number
%g
a floating point number
%lf
long floating number (for double data type)
%o
an octal number
%x
a hexadecimal integer
%i
a decimal, octal or hexadecimal integer
%s
a string

Conversion specifier is used in printf() and scanf() functions to print or accept the values of Variables. In printf() function conversion specifiers are replaced with their corresponding variable values.
So, the below code outputs :
printf("Number : %d,Letter : %c,Salary : %f",number,letter,salary);
OUTPUT :
Number : 12,Letter : S,Salary : 25,950.78

Note : You can have as much space between any two statements as you want, C compiler ignores the white spaces between statements. For Example -
    int number=12;
    char letter='S';
    float salary=25950.78;
    printf("Number : %d,Letter : %c,Salary : %f",number,letter,salary);

can be written as -
    int number=12;

    char letter='S';

    float salary=25950.78;

   printf("Number : %d,Letter : %c,Salary : %f",number,letter,salary);
and will cause no error and same result is produced.

Keywords or Reserved Words

While designing C language, the designers have predefined the meanings of few words. These words are known as keywords or reserved words. They performs the predefined functions and you cannot use them as variable names because they have been defined in the C structure set. There are only 32 keywords available in C which are given below:

auto                  brake 
case                  char 
const                continue 
default             do 
double             else 
enum               extern 
float                 for 
goto                  if 
int                    long 
register           return 
short               signed 
sizeof              static 
struct              switch 
typedef           union 
unsigned        void 
volatile           while

These are the keywords used in C language and we cannot be used them as the name of a variable or a function while defining them. It will result in an error.

Identifiers

Identifiers are the name of entities like variables or functions etc.; that is given by the programmer itself these are not be predefined and can be anything containing alphabets and underscore "_" sign. The first letter of the identifiers cannot be number or the variable name cannot contain any sign other than '_'sign
 For Example -
int number, n, num, my_num, num1, num2 ;
are all legal identifier names.

int 1number, n@, my num, num#1, num$2;
are all illegal identifier names.

Note : An identifier name cannot contain spaces thus, '_' sign is used instead of spaces.

Identifiers in C

Comments

Many programmers does not understand the importance of comments in a program. Comments are very useful to help program to speak by itself. comments are the lines or statements in the code which are ignored by the compiler. These are only used for increasing the readability of a program.
C language supports 2 types of comments :

(i) Single  Line Comments

Comments that are small and can be fitted in one line is called Single line comments. Single line comments starts with "//".
For Example :
// This is a single line Comment

(ii) Multi-line Comments 

Comments that are large and takes more than one line are called Multi-line Comments. Multi-line comments starts with "/*" and ends with "*/".
For Example :
/* This is a Multi-line Comment and can be extended to next line without any error. Any code written inside this comment will be completely ignored by the compiler and does not affect our program anyhow.
*/
Now the question arises

Why Comments are used ? What is the use of Comments ?

To answer this question, let's assume a scenario of a Software designing in an MNC. A Software is a large program written for specific purpose. As software programs are really really huge. Without comments, it can be really difficult to understand the functions of block. Let's assume, if the main programmer left his job, so the newcomer can understand the with the help of Comments. Thus, it is very suitable to use comments in a Program.

Note: While writing any piece of code, if you face any error in your code, don't delete it rather Comment it out. So later, you can compare it with the correct code. Thus, Comments are really important to increase the readability of the program.

Program Example :


#include <stdio.h>
// Main function starts here
int main(){
    int num = 5;
    /* Value 5 is assigned to num variable of int type */
    int square = num*num;
    /* num is multiplied to itself and the result
    is stored in square variable */
    printf("The square of %d is %d",num,square);
    /* OUTPUT:
    The square of 5 is 25
    */
 
}

Click on Run

Escape Sequences

You can not use enter key to move in a new line in C language or press tab key and expect the same result on output screen. C language uses some predefined symbols for this purpose. These special predefined symbols are called Escape Sequences.
C supports the combination of backslash (\) and some characters to print these special characters.
Some of the Escape Sequences are given below :

Escape Sequence
Meaning
Purpose
\b
Backspace
Moves the cursor to the previous position 
\n
New line
Moves the cursor to the beginning of next line
\t
Horizontal Tab
Moves the cursor to next horizontal tab position
\v
Vertical Tab
Moves the cursor to next vertical tab position.
\a
bell(alert)
Produces a beep sound for alert
\\
Backslash
Presents a character with backslash (\)
\'
SIngle Quote
Presents a character with single quote (')
\"
Double Quote
Presents a character with double quote (")

Program Example :


#include <stdio.h>
// Main function starts here
int main(){

    printf("Hello World!\n\n");
//outputs : Hello World!

    printf("Learning \t C \t is \t Fun");
/* This text will be printed in the next to next line */

/*  OUTPUT :
    Hello World!

    Learning         C        is        Fun
    */
}

You are done with the C Basics now, Let's start learning "Operators and Precedence".


Post a Comment