Skip to main content

C Program : Introduction To C Programming Language

C Programming Language

C Program

C programs consist of functions and variables. A function contains statements that specify the computing operations to be done and variables store values used during the computation.


The #include<stdio.h> tells the compiler to include information about the standard input and output library. Stdio.h is a header file and it is included to every c program file at the top of that program.

Features of C Language:-

  • Structured

  • Fast and Efficient

  • Easy to Extend

  • Portable

  • Function Rich Libraries

  • Modularity


C is a robust language which has a rich set of built-in functions, operators which make it code highly efficient.


It is also an extensible language because it continuously adds the library functions supported by the c library for various extra features.


C is a highly portable language; it means that programs once were written can be executed on another machine with little or no change.


C programs are efficient and fast because the use  a variety of data types, functions and operators. 


The program is a collection of functions and it allows the user to create his own function and add it to the c library. It provides code reusability so we can call the function within the function.


This language provides Dynamic Allocation of memory, to allocate memory during the run time.

Syntax of C Program:-


#include<stdio.h>

#include<conio.h>

return_type main()

{

   //code body of a program  or statements.

  return return_type;

}


#include, include the information about the standard library. The conio.h is also a header file which is used for the console input and output function.


The return_type could be int, void, double, char, float. But generally  we use int of void for return type. 


If we declared the main function to return int. It  means, return value will be 0 (zero) or 1.


When the program compiled and executed successfully, we returned 0 else 1. If the main function returns 1 to abort the program execution.


A main function is defined that receives no argument values. All the statements of main function are included in curly braces.


Main function uses a library function printf() to display the result on the screen of a computer. 

Simple example:-


C program to print a message “Learning C Programming Language” on screen.


#include<stdio.h>

#include<conio.h>

void main()

{

  printf(“Learning C Programming Language”);

}


Output:-

Learning C Programming Language


Explanation:-


In this c program, first included header files for standard input and output. The return type of the main function in this program is void, meaning we do not need to return anything at the end of the main program.


Printf function displays the message on the computer screen. Semicolon is denoted at the end of printf() function completion of the execution of that function.




Write a c program to find the sum of  a=10 and b=35;


#include<stdio.h>

#include<conio.h>

int main()

{

  int sum, a=10, b=35;

  sum= a+b;

  printf(“Sum = %d\n”,sum);

  return 0;

}


Output:-

Sum = 45


Explanation:-


Inside main, variable a and b are initialized and defined as of type int. Sum is declared as of type int.


Additions of a and b are stored in the sum and printf() function is used to display the aggregate.


\n denotes the newline, after the sum of two numbers are displayed the cursor moves to the new line on the computer screen.


At the end of the program return zero, it means the program is executed successfully. 


Comments in C Language:-


There are two ways to denote comments in the c programming:-

  1. /* anything inside */

  2. // (double slash)

If you want to write the multiple lines of comments in a program we use the first method to write comments.


But we do not use multiple lines for commenting, sometimes we need to write only a sentence then we prefer the second.

Example:-

C program to display single comment and multiple line comments in a program


#include<stdio.h>

#include<conio.h>

int main()

{

  int sum, a=10, b=35;

   // a and b are initialized to 10 and 35

  sum= a+b;

  /* sum of a and b are aggregated and

   stored in sum variable */

  printf(“Sum = %d\n”,sum);

  return 0;

}


Output:-

Sum = 45

Data Types in C Language:-

There are three classification of c data types:-

  1. Primitive data types

  2. Derived Data type 

  3. User Defined Data type


Primitive data types

  •  int

  • char 

  • float

  •  char

  •  void

Derived Data type

  • Array

  • String

  • Pointer

User Defined Data type

  • Structure

  • Union

  • typedef

  • enum



Further, int data type is divided into short, int and long int and these are also classified into signed and unsigned data types.


char data type is also divided into signed and unsigned and float is into double float and long double.


The signed data types refer to both positive and negative values of data. But in case of unsigned data types it only refers to the positive values.

Format Specifier in C Language:-

Data Type                 Specifier

int                                %d

char                             %c

float                             %f

double                         %lf

Variables:-

  • Variables are the containers. These are used to store the data in it.

  • Whenever we initialize a variable with some values that values are stored into a variable.

  • Variables also store the address of the other variable in the program.

Example:-

int x, y; 

//Declaration of  two variables x and y of type integer.

x=45;

y=14;

//x stores the value 45 and y stores value 14


  • C variables must  be declared before they are used. Usually at the beginning of the function before any executable statements.

  • A declaration announces the properties of variables, it consists of a type name. The range of the data types depends on the machine architecture.

  • A 16 bits int which lies between -32768 and +32767, a float is typically a 32 bits quantity, which lies between 10^-38 and 10^+38. 


Note:- printf() is not part of the c language, there is no input or output defined in c itself, printf() is just a useful function from the standard library of functions that are normally accessible to c programs. The behaviour of printf is defined in the ANSI standard, however, so its properties should be the same with any compiler and library that conforms to the standard.



Comments

Popular posts from this blog

Merge Arrays : Merge Two Arrays Of Different Size In C++

Merge Two Arrays C++ Program C++ program to merge two arrays of different size. First array size is m and second array size is n. The size of the first array is greater than the first array i.e. m>n.                               Algorithm :- Enter the size of first array m. Enter the size of 2nd array n. Enter the elements of 1st and 2nd array and number of elements of first array should be greater than the number of elements of second array. After m index of first array, insert the element of second array into the first array up to m+n-1. Sort the merge array of size m+n. Display the first array, which will display all the elements of the merge array of size m+n. Program: - #include<iostream> using namespace std; int main() { int i,n,m,a[15],b[5],j;//array a[] and b[] are declared cout<<"Enter the size 1st array:-"<<endl; cin>>m; cout<<"Enter the size of 2nd array(n<m...

C++ Vector : Basics Of Vector in C++ STL

In C++, Vector is unlike array, it stores different data types such as int, double, string, float, etc. It works like the dynamic array with the ability to resize automatically itself. Vector stores data in the contiguous memory location. Two functions are needed to traverse from starting to end that is begin() and end() functions.                               Syntax of the vector declaration:-             vector<data_type> variable_name (number_of_elements);  Here number_of_elements is optional, we can also declare a vector with empty vector that contains zero elements. The data_type in the angle-brackets indicates any type of data type which is valid in c++. Vector declaration examples:- vector<int> numbers (10); //In this example, we declared a vector name number of 10 integers. vector<string> names;  //In this, a vector name declared of string. In eve...

Data Structure And Algorithm Important Topics Overview

  Overview Of Data Structures And Algorithms This post will give you the clear idea about learning data structures and algorithms . So, Data structure? => In computer science a data structure is a data organization , management and storage format that enables efficient access and modification. Or in the other words it is a way in which data is stored on a computer. Types of data structures:- Array  String Stack Queue Linked List  Binary Tree Binary Search Tree Heap Hash Table Graph Above topics, array is a linear data structure which stores the data in the sequence order, dynamic array, every data is stored in the next contiguous memory location.   String is a collection of characters. If it contains the alphabets and it also uses the digit as character. If a string is composed of numbers and characters then numbers are also treated as characters. Stack is also a linear data structure. It works either on Last In First Out or First In Last Out . It has only one ...