C Programming

Downloading and Installing Compiler:
  • Turbo C/C++ is the both editor and compiler software for C and C++.
  • We can download Turbo C by going to the below site highlighted in the red box in the address bar.








Running Hello C Program:
  • Open Turbo C.
  • Press Ctrl + N to create a new file and save it with .c extension.

  • After writing the code press Alt + F9 to compile.
  • Ctrl + F9 to run the code.
  • Alt + F5 to see the output. 


Variables:
  • Variables are used to store the data.
  • They provide a way to naming data with descriptive name.
  • A variable must be declared before using it.
  • There are certain rules for the declaration of variable.
Rules for Declaration:
  • A variable must start with an alphabet.
  • No spaces are allowed between the characters.
  • Variables are case sensitive.
  • Underscore( _ ) is the only special character allowed.
  • Variables can be combination of alphabets and digits.
  • Variables cannot start with a number.
  • Variables names cannot be C reserved keywords.
Data types:
  • Data types are used to specify what kind of value can be stored in a variable.
  • The memory size and type of value of variable are determined by data type.
  • Each variable or constant or array must have a data type.
  • The data types in C are classified into four categories.
Categories Data Types
Basic data types int, char, float, double
Enumeration data type enum
Derived data type pointer, array, structure, union
Void data type void
Basic data types:
  • Integer, Floating Point and Character types are considered as basic data types.
Integer types:
  • Used to store whole numbers.
TypeSize(bytes)Range
int or signed int2-32,768 to 32767
unsigned int20 to 65535
short int or signed short int2-128 to 127
unsigned short int20 to 255
long int or signed long int4-2,147,483,648 to 2,147,483,647
unsigned long int40 to 4,294,967,295
Floating Point Types:
  • Used to store real numbers.
TypeSize(bytes)Range
Float43.4E-38 to 3.4E+38
double81.7E-308 to 1.7E+308
long double103.4E-4932 to 1.1E+4932
Character Types:
  • Used to store characters.
TypeSize(bytes)Range
char or signed char1-128 to 127
unsigned char10 to 255
Variables Declaration and Initialization:
  • All the variables must be declared before using it.
  • C is a case sensitive language. So name, Name, NAME are all not the same. 
Declaration:
The variable declaration in C can be done as follows:
data_type variable_name;
or
data_type variable_1, variable_2, variable_3........, variable_n;
Example:
int a;
float x,y,z;
char 'ch';
Initialization:
The variable initialization in c can be done as follows:
data_type variable_name=value;
or 
After declaring the variable directly assign the value to the variable.
variable_name=value;
Example:
int a=10;
char ch='k';
String expressions and String functions:
  • String is an array of characters.
  • Strings are enclosed by double quotes.
Syntax:
char string[20]="Keshav Jha";
String functions:
  • There are predefined string handling functions available in string.h header file.
  • These functions include performing operations like comparing, concatenation, finding length and various other manipulations.
  • To use these functions we have to include string.h header file.
The below are some commonly used string function.
  • strcat() – This function is used to con concatenate two strings. 
  • strcmp() – This function is used to compare two strings. 
  • strcpy() – This function h is used to copy the second string given as second parameter to this function into first string. 
  • strlen() – This function is used to length of the string in other words the number of characters present in string . 
  • strstr() – This function is to obtain the first occurrence of substring in a string .
  • strlwr() –This function converts string to lower case.
  • strupr() – This function converts string to upper case.
Conditional Statements:
  • Conditional statements are used to execute a block of statements based on conditions.
  • There are predefined conditional statements which are evaluated to boolean expressions either true or false.
  • If the given condition is true then the block of code inside the body of conditional statement is executed or else it is skipped.
The below are the conditional statements in c:
  • if statement.
  • nested if statement.
  • if else statement.
  • nested if else statement.
  • if-else if ladder.
  • switch statement.
  • goto statement.
  • break statement.
  • continue statement.
1. if statement:
It is used to execute the code if the statement is true.
Syntax:
if(condition){
...........statement_1;
...........statement_2;
...........statement_3;
.
.
...........statement_n;
}
2. nested if statement:
It is used nesting of if statement. An if inside if or multiple ifs form a nested if statement.
Syntax:
if(condition){
if(condition){
// statements to be executed...
}
}
3. if else statement:
If the condition of if block satisfies, then if block is executed. On failing the if condition else block is executed.
Syntax:
if(condition){
//code that has to be executed on satisfying if condition.
}
else{
//code that has to executed on failing the if condition.
}
4. nested if else statement:
Using multiple if conditions inside an if is known as nested if statement.
Syntax:
if(condition){
//code that has to be executed on satisfying immediate above if condition.
if(condition){
//code that has to be executed on satisfying immediate above if condition.
if(condition){
//code that has to be executed on satisfying immediate above if condition.
}
}
}
else{
//code that has to be executed on failing the if conditions.
}
5. if else if ladder:
When a choice has to be made with more than two possibilities if else if ladder is used.
Syntax:
if (condition_1)
{
   //code to be executed if condition_1 is true
}
else if(condition_2)
{
   //code to be executed if condition_1 is false and condition_2 is true
}
else if (condition_3)
{
   //code to be executed if condition_1 and condition_2 is false and condition_3 is true
}
.
.
else
{
   // statements to be executed if all conditions are false
}
6. switch statement:
  • The if else if ladder statement checks for all the conditions and increases the complexity. 
  • To overcome this switch case checks the value of a expression against a case values, if condition matches the case values then the control is transferred to that point.
  Syntax:
switch (expression)
{
  case label_1:
    statements;
  case label_2:
    statements;
    break;
.
.
.
.

  case label_n:
    statements;
    break;
  default:
    statements;

}
Rules for switch case:
  • The default case is optional. When no match is found the default case is executed.
  • The case label should be integer or character constant.
  • Each compound statement of a switch case should contain break statement to exit from case.
  • Case labels must end with (:) colon.

7. break statement:
  • Break Statement is a loop control statement which is used to terminate the loop. 
  • When the break statement is encountered from within a loop, the loop iterations stops there and control returns from the loop immediately to the first statement after the loop.
8. goto statement:
  • The goto statement is a jump statement.
  • It is used to jump from anywhere to anywhere within a function.
Syntax:
goto label;     
.                
.                
.           
label:  
 or
label:
.
.
.
  
goto label;
9. continue statement:
  • The continue statement is a loop control statement.
  • It is opposite to the break statement, instead of terminating the loop, it forces to execute the next iteration of the loop.
Loops in C:
  • Loops are used to repeat a block of code until some end condition is met.
  • There are three different loops in C. They are:
1. for loop:
  • It is considered as open ended loop. 
  • The block of statements inside the loop are repeatedly executed until a test condition is satisfied.
Syntax:
for(initialization; condition; increment/decrement)
{
    statement-block;

}

The for loop is executed as;
1. It first evaluates the initialization code.
2. Then it checks the condition expression.
3. If it is true, it executes the for-loop body.
4. Then it evaluate the increment/decrement condition and again follows from step 2.
5. When the condition expression becomes false, it exits the loop.
Example:
The below program prints first 10 numbers using for loop:

#include <stdio.h>

void main()
{

    for (int i=1;i<=10;i++){
        printf("My number is %d \n",i);
    }

}

Output:
My number is 1                                                                                                                                                                   
My number is 2                                                                                                                                                                   
My number is 3                                                                                                                                                                   
My number is 4                                                                                                                                                                   
My number is 5                                                                                                                                                                   
My number is 6                                                                                                                                                                   
My number is 7                                                                                                                                                                   
My number is 8                                                                                                                                                                   
My number is 9                                                                                                                                                                   
My number is 10

2. While loop:
  • While loop is considered as entry control loop. 
  • The statements inside the loop are repeatedly executed till the condition is true.
Syntax:
variable initialization;
while(condition)
{
    statements;
    variable increment or decrement;

}

The while loop is executed as follows:
1. It first evaluates the variable initialization (e.g int i= 1;).
2. Checks if the condition(e.g while(i <= 10)) is either true or false. If it is false get out of the loop.
3. If it is true the body of the loop is executed. Variable increment or decrement ( i++ or i-- or i = i + 2 )
Example:
The below prints first 10 numbers using a while loop:

#include <stdio.h>

void main()
{
int i;
    while(i<=10)
        printf("My number is %d \n",i);
    }

}

Output:
My number is 1                                                                                                                                                                   
My number is 2                                                                                                                                                                   
My number is 3                                                                                                                                                                   
My number is 4                                                                                                                                                                   
My number is 5                                                                                                                                                                   
My number is 6                                                                                                                                                                   
My number is 7                                                                                                                                                                   
My number is 8                                                                                                                                                                   
My number is 9                                                                                                                                                                   
My number is 10

3. do while loop:
  • The do while loop is considered as exit control loop. 
  • At first the loop is executed and then it is tested for a condition. 
  • The statements inside the loop is executed at least once.
Syntax:
intialize the variable;
do
{
statements;
increment or decrement the value;
}
while(condition)

The do.. while loop works as follows:
1. It first evaluates the variable initialization (e.g int i= 1;).
2. The body of the loop is executed first. Variable increment or decrement ( i++ or i-- or i = i + 2 )
3. Checks if the condition(e.g while(i <= 10)) is either true or false. If it is false get out of the loop.
4. If it is true get into the loop and execute the statements.

Example:
The below program prints first 10 numbers using a do while loop:

#include <stdio.h>

void main()
{
int i=1;
do
{
     printf("My number is %d \n",i);
        i++;
}
    while(i<=10);
}

Output:
My number is 1                                                                                                                                                                   
My number is 2                                                                                                                                                                   
My number is 3                                                                                                                                                                   
My number is 4                                                                                                                                                                   
My number is 5                                                                                                                                                                   
My number is 6                                                                                                                                                                   
My number is 7                                                                                                                                                                   
My number is 8                                                                                                                                                                   
My number is 9                                                                                                                                                                   
My number is 10 

Input/Output functions in C:
The input/output functions in C are classified into two categories:
1. Console Input/Output Functions – These functions receive input from keyboard and write them on the Monitor.
2. File Input/Output Functions – These functions receives input from a file and writes output to the file.

1. Console Input/Output Functions:
The console input/output functions are classified into:
(i)Formatted Input/Output Functions. Example: printf(), scanf()
(ii)Unformatted Input/Output Functions. Example: puts(), gets()
To use these functions we must begin each C program with a pre-processor directive to include these standard library functions.
This can be done with a line entry - #include <stdio.h>.

printf() and scanf() functions:
  • The scanf() and printf() are used to get input from the user and print to the user output device.
  • They provide the flexibility to receive the input in some fixed format and to give the output in desired format.
  • Format string informs the scanf() function, what type of input to expect and in printf() it is used for what type of output to expect.
Format StringMeaning
%dScan or print an integer as signed decimal number
%fScan or print a floating point number
%cTo scan or print a character
%sTo scan or print a character string. The scanning ends at whitespace.
Syntax:
printf(<control string>, arg1, arg2, . . . , argn);
scanf(<control string>, &address1, &address2, . . . , &addressn);
Example:
#include<stdio.h>

void main()
{
   char name[20];
    
    printf("Please enter your name");
    
    scanf("%s",name);
    
    printf( "\nYou name is : %s", name);

}

puts() and gets() functions:
  • gets() is used to accept the input in the form of string along with the white spaces.
  • puts() is used to print the output string.
Example:

#include <stdio.h>

void main()
{
    char name[71]; //String variable declaration
    printf("\nEnter your Name: ");
    gets(name);
    puts(name);
}
2. File Input/Output functions:
C provides many functions to work with files some basic functions are listed below.
FunctionDescription
fopen()Create a new file or open a existing file
fclose()Closes a file
fgets()Reads a set of data from a file
fputs()Writes a set of data to a file
fgetc()Reads a character from a file
fputc()Writes a character to a file
A file can be created or opened in the following below modes.
Mode Description
r Opens an existing text file for reading.
w Opens a text file for writing. If it does not exist, then a new file is created. 
a Opens a text file for writing in appending mode. If it does not exist, then a new file is created.
r+ Opens a text file for both reading and writing.
w+ Opens a text file for both reading and writing. It first truncates the file to zero length if it exists, otherwise creates a file if it does not exist.
a+ Opens a text file for both reading and writing. It creates the file if it does not exist. The reading will start from the beginning but writing can only be appended.
When working with files we need to declare a pointer of type file. Ex: FILE *ptr_name;
Reading content in a file:
Program:

Output:

Writing to a file:
Program:

Output:

Graphics in C:
Program:

Output:
Programs:
Program for finding "First non-repeated character in given string".

Output:

Program for finding "Digit Identification if string have any numbers".

Output:

Program to sum a number in given string.

Output:
Previous
Next Post »