C Programming Language
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:-
/* anything inside */
// (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:-
Primitive data types
Derived Data type
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
Post a Comment