Learn C programming language in few days

C PROGRAMMING LANGUAGE 

Introduction of C language


C is a general purpose structured programming language. C language was developed originally in 1970. By Dennis Ritchi at AT and T Bell's laboratory at California , USA.
The main features of C language include low-level access to memory, is simple set of keywords and clean style, the features make C language suitable for system programming like an operating system or compiler development.


Advantages of C language

  • It has limited fundamental data types and keywords it contain rich variety of drive data type.
  • As a middle level language, C combines the features of both high level and low level languages it can be used for low level programming, such as scripting for drivers and kernels and it also supports functions of high level programming language such as scripting for software application etc.
  • C is very flexible or we can say machine independent that can help you to run your code on any machine without making any changes or just few changes in a code.
  • C is structured based. It means that the issues of complex problems are divided into smaller blocks of functions. This modular structure help in easy and simpler testing and maintenance.




Structure of C program

Step by step representation of structure of C programs:


Header ---------> #include<stdio.h>

Main()------------> int main()/ void main()

Variable declaration-------> int a=10;

Body ----------> printf("%d",a);

Return ----------> return 0;/ getch();

----------------------------------------------------------

1. Header File:- A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source files.

Some header file used in C language:

Stddef.h - It tell the compiler about several useful macros used in C program.

Stdio.h - It tell the compiler about the several input and output function used in C program.

Math.h - It is a compiler about the mathematical functions used in a C program.

Stdint.h - It tell the compiler about the exact width of integer types.

String.h - This header  file is used whenever we use string function in a C program.

Note:- All all this header file are are written between #include< header file >.

2. Main function:- The main function can be define in C source file in your programs source code. Function named main is required, because it is effectively the entry point (for starting point) of your program.when main calls a function, it passes the execution control to that function. The function return control to main when a return statement is executed or when and function is reached.

3. Variable declaration:- A variable declaration, it refers to the variable that are to be used in the function. In C program, no variable can be used without being declared also in a C program, the variable are to be declared before any operation in the function.
Example:-
                   Void main()
                   {  int a;

3. Body:- In C program, Body refers to the operations that are performed in the functions. It can be anything like manipulations, searching, sorting, printing etc.

4. Return statement:- In C program the return statement refers to the returning of the values from a function. Return statement and return value depend upon the return type of the function. For example if the return type is void, then there will be no return statement. There will be a return statement and return value will be of the type of the specified return type.
---------------------------------------------------------------------------------

Tokens of C language

  • Variable
  • Data type/ Constants
  • Identifiers
  • Operator
  • keywords
Variable:- Variable is the name of memory location where we can keep the value that value may be or may not be change the execution of the program.
Example:- a=10;

Identifier:- An Identifier is name it may be variable name, array name, pointer name etc.
The first letter of the name must be alphabet or underscore.
Their is no blank space allow b/w the character.
An Identifier also have a digit or symbol which is predicted by alphabet or underscore.

Constant/ Data types:- Data types is away to define the type of data. It has four types:-


  1. Integer ---- (int ).........1,2,3,4,5.........
  2. Float ------ ( float ).......1,2,3,3.4,7.4,6.1,......
  3. Character ------( char )......a,c,f,j,k.........
  4. Boolean........( true/false ).........

Integer:- An Integer is a combination of one or more digits. There is no decimal point allow b/w the digits. It hassn 2 byte memory space. A range of Integer is b/w -32768 to +32767.

Float:- At least one digit must be there. Decimal point must be available in b/w the digits. No blanks space allow b/w the digits. the range of the real constant is -3.4*e

Character(char):- Character constant is a constant which have digits or symbols or alphabets, which is encoding by the single inverted. The storage memory of char constant is 1 byte.

                                             Constant
Primary Constant

  • Integer ( int )
  • Float
  • Character( char )
Secondary Constant
  • Array
  • Pointer
  • Structure( struct )
  • Union
  • Enum
Input/Output functions

1. printf():- printf() is used to print or display data on the console in a formatted form. It controls things like how many character to display or how decimal to display or how many decimal to display or weather the fields is right or left justified etc.

syntax:- printf("%specifier",variable name);

Specifiers 

Code                 Meaning
%C =            It is used for character.
%d =             It is used for integers.
%x =             It is used for hexadecimal in lowercase.
%X =            It is used for hexadecimal in uppercase.
%e =             It is used for lower case.
%E =            It is used for upper  case.
%f =             It is used for  float/Real Constant.
%s =             It is used for string.
%u =            It is used for unsigned decimal integers.( for                                 addressing )    



Escape sequence ( It is only use in printf() )

Code           Meaning
\b =          It is used for blank space.

\n =          It is used for new line.
\t =           It is used for horizontal tab.
\a =          It is used for beep sound.
\v =          It is used for vertical tab.

*// C program without using escape sequence. //*
#include<stdio.h>
void main();
{ int a=10;
printf("The value of a=%d",a);
getch();
}
Output:- The value of a=10

*// C program with using escape sequence. //*
#include<stdio.h>
void main()
{ int a=10;
printf("The value of a=\n%d",a);
getch();
}
Output:- The value of a=
10

2. scanf():- The scanf() statement accepts as input from the keyboard with the help of format specifiers. 
syntax:- scanf("specifier",&variable name);

example:- 
int a;
scanf("%d",&a);

The ampersand (&) processed the variable to be read this is compulsory while operating on the basic data type or any other statement.
scanf() does not take any escape sequence character.


OPERATORS

An operator is a symbol that tells computer to perform certain logical & mathematical manipulation. 

There are many type of operator:- 

  1. Arithmetic operator
  2. Relational operator
  3. Logical operator
  4. Assignment operator
Arithmetic operator:- Arithmetic operator is used to perform mathematical manipulation.
There are two types of arithmetic operator:-
  1. Unary operator
  2. Binary operator
Unary operator:- Unary operator are those operator, which applicable on single operant. For ex- ( -A ).
There are three Unary  operators:-
  1. Unary -
  2. Increment operator  
  3. Decrement operator
Unary- :- Example- int x,y=10;
                                  x= -y;
                                  x= -10;
Note:- There are no unary+ operator.

Increment/Decrement operator:- There are two type of increment/Decrement operator:-
  1. Pre increment/Decrement
  2. Post increment/Decrement
Pre increment/Decrement:- This operator is used on a single operant in the prer increment/Decrement operator first increment/decrement of the value will be done then use in your statement or expressions.
Note:- In the case of Increment or Decrement the value 1 will be increase or decrease.

Post increment/Decrement:- In the post increment/Decrement operator first use the value of operant then perform the increment or decrement.

*// Program on post and pre increment/decrement operator. //*
#include<stdio.h>
#include<conio.h>
void main()
{
int x=10, y=20;
printf("%d",++x);
printf("%d",y--);
printf("%d",x+y);
printf("%d",x--);
printf("%d",--x);
printf("%d",++y);
printf("%d",--y);
printf("%d",x+y);
printf("\n The value of  x=%d",x);
printf("\n The value of y=%d",y);
getch();
}

Binary operator:- Binary operator are those operator which is lies b/w two operator & perform certain task.
example:- A+b
A-B
A/B
A*B

Relational operator:- A relational operator is a programming language construct or operator that tests or defines some kind of relation between two entities. ... In languages such as Crelational operators return the integers 0 or 1, where 0 stands for false and any non-zero value stands for true.

Symbols                             Meaning
                               <                                  Greater than
                               >                                  Less than
                              <=                           Greater than equal to
                              >=                             Less than equal to
                                        ==                                Equal to Equal to
                               /=                                 Not Equal to

Logical operator:- These operators are used to perform logical operations on the given expressions. There are 3 logical operators in C language.

                             Symbols                              Meaning
                                  &&                                  Logical AND
                                    ||                                    Logical OR
                                    !                                    Logical NOT

Assignment operator:- It is used to assign the right value to the left element ( variable ). The symbol of assignment operator is ( = ).

Example:-  a=10
                   a=(a-5) or a-=5
                   a=(a+5) or a+=5
                   a=(a*5) or a*=5

Conditional operator:- It is also known as ternary operator. Conditional operators return one value if condition is true and returns another value is condition is false. This operator is also called as ternary operator.

Syntax:- exp=1 ? exp=2;
                exp=3;

Bitwise operator:- Bitwise operator is used for manipulation of data at bit level.

                  Operator                                     Meaning
                        &                                          Bitwise AND
                         |                                           Bitwise OR
                        ^                                           Bitwise exclusive OR
                       <<                                         Shift left
                       >>                                         Shift right
                         _                                          Once compliment

Special operator:- C support some special operator such as (,) operator, size operator, pointer operator ( &,* ) and member selection operator.
---------------------------------------------------------------------------------


Post a Comment

0 Comments