Ad Code

Theory and Coding in C language Part 1

IN THIS BLOG THEORY AND PROGRAMS ABAILBLE 

Introduction of C Language

C language एक programming language है | जिसका use कर के applications बना सकते है | इसे unix operating system को दुबारा लिखने के लिए develop किया गया था | क्योकि unix operating system को B language में लिखा गया था। unix operating system मे जादातर programs भी B language मे लिखे गये थे |   Computer.  


B language को Ken Thompson के द्वारा 1970 में Bell laboratories में develop की गई थी। लेकिन B language ज्यादा popular नहीं हो पायी थी। फिर C language को Dennis Ritchie ने 1972 and 1973 में develop किया था। और C language को भी bell laboratories में ही develop किया गया था।

C language एक simple language है | C language के द्वारा जादा तर mathematical programs लिखे जाते है | और C language को popular होने मे जादा time नही लगा |

C language को Mother Language भी कहा जाता है क्यों C language के बाद जो भी language बनाई गयी for example ( Java , PHP , c# , या c++ ) इन सभी language मे C language का concept use किया गया है | 

Advantage of C language

1 – C language एक simple और easy language है | जिसका use आसानी से किया जा सकता है | और C language की सबसे बड़ी खासियत यह है | की C language मे लिखा गया code बहुत ही fast होता है | यानी C language की execution time fast होती है |

2 – C एक structured programming language है। और C language मे हम functions बना सकते है | और अपने code को और भी अच्छे से manage कर सकते है |

3 – C language मे C 32 reserved के द्वारा कुछ keyword provide की गयी है | जो ऐसे शब्द हैं, जिनका उपयोग किसी अन्य उद्देश्य के लिए नहीं किया जा सकता है | जिसके लिए वे पूर्वनिर्धारित हैं |

4 – C एक middle level language है। जो high level और low level की application बनाने मे सक्षम है | यह feature उन programmers के लिए एक advantage है जो high level और low level की applications create करना चाहते है।

5 – Assembly language के बाद सबसे fast language C language को ही माना जाता है | इसलिए ये दूसरी programming languages से fast होती है। C language में create की गयी applications की processing बहुत ही fast होती है।

Disadvantage of C language

C language एक powerful language है | लेकिन C language में कुछ कमियाँ भी है | जो C language को सीमित बनाती है। चलिए C language के कुछ Limitations के बारे मे जानते है |

1 – C language में run time checking नहीं होती है। C language में किसी भी variable के type को identify करने में सक्षम नही होती है।

2 – C language मे re-usability ( inheritance ) support नहीं करती है। और C language में exceptions को run time में handle नहीं किया जा सकता है।

3 – C language object oriented programming को support नहीं करता है | जैसे – classes, objects, interfaces आदि |  

Usage of C Language

C language का use system application या network drivers बनाने के लिए किया जाता है | क्योंकि यह कोड का उत्पादन करता है | जो Assembly language में लिखे गए कोड के रूप में तेजी से चलता है। C language मे उपयोग के लिए कुछ उदाहरण निचे दे रहे है |

  • Operating Systems
  • Language Compilers
  • Assemblers
  • Text Editors
  • Print Spoilers
  • Network Drivers
  • Modern Programs
  • Data Bases
  • Language Interpreters
  • Utilities

 Frist program in c :)

#include <stdio.h>

int main()
{
    printf("Hello World");

    return 0;

}    

        प्रोग्राम में  #include<studio.h> header file है जिसमे   printf  जैसे फंक्शनो  की definition दी गई होती है

 if you want to run this program copy code and  click

Printf()  is use for print any value so the output of given program  is  Hello World

Introduction of Tokens

अगर आप C language मे coding करना चाहते है | तो C language का syntax समझना आपके लिए बहुत आवश्यक है। एक C program tokens से मिलकर बना होता है। यदि आप C language के सभी tokens को use करना सिख जाते है | तो आप C language के code को बहुत ही fast type कर सकते है | in the Above example  printf,int,main is called tokens.

Tokens कुल 6 प्रकार के होते है। इनकी list हम नीचे दे रहे है |

  • Identifiers 
  • Operators 
  • Variables
  • Strings
  • Keywords
  • Constants 

Identifiers

C language मे Identifiers वह नाम होते है | जिसका use variables, constants और functions मे किया जाता है | इन नामो को use करने के लिए कुछ rule fallow करना पड़ता है | अगर आप इन rules को fallow नही करेंगे तो programs मे error आ जाते है |

  • Identifiers में %, @, – characters symbol का use नही किया जाता है |
  • C language एक case sensitive language है। इसलिए num और Num यह दो different identifiers नामो से जाने जायेगे |
  • Identifiers में हम operators का use नही कर सकते है। 
  • Identifiers को Digits से start नहीं किया जाता है | Identifiers की सुरुवात character या underscore से कर सकते है।  
  • C language मे A से लेकर Z तक small और capital दोनों तरह के letters का use कर सकते है |

Examples of Identifiers

Identifiers के कुछ example निचे दिये जा रहे है |

_name // right Identifier

2num // wrong Identifier

num-name // wrong Identifier

num_1_name // RIGHT

Operator in c language

An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C language is rich in built-in operators and provides the following types of operators − एक ऑपरेटर एक प्रतीक है जो संकलक को विशिष्ट गणितीय या तार्किक कार्य करने के लिए कहता है। C भाषा बिल्ट-इन ऑपरेटरों में समृद्ध है और निम्न प्रकार के ऑपरेटर प्रदान करती है -

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Misc Operators

We will, in this chapter, look into the way each operator works.

Arithmetic Operators

Airthmetic operators are use in mathematics operations when one,two operands given.

A and B are the operands value of these operands A=10,B=20. 

Show Examples

OperatorDescriptionExample
+use addtion of two operands.A + B = 30
use in Subtraction.A − B = -10
*Multiplies both operands.A * B = 200
/Divides numerator by de-numerator.B / A = 2
%Modulus Operator and remainder of after an integer division.B % A = 0
++Increment operator increases the integer value by one.A++ = 11
--Decrement operator decreases the integer value by one.A-- = 9

Relational Operators

The following table shows all the relational operators supported by C. Assume variable A holds 10 and variable B holds 20 then −

Show Examples

OperatorDescriptionExample
==Checks if the values of two operands are equal or not. If yes, then the condition becomes true.(A == B) is not true.
!=Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true.(A != B) is true.
>Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true.(A > B) is not true.
<Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true.(A < B) is true.
>=Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true.(A >= B) is not true.
<=Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true.(A <= B) is true.

Logical Operators

Following table shows all the logical operators supported by C language. Assume variable A holds 1 and variable B holds 0, then −

Show Examples

OperatorDescriptionExample
&&Called Logical AND operator. If both the operands are non-zero, then the condition becomes true.(A && B) is false.
||Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true.(A || B) is true.
!Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false.!(A && B) is true.

Bitwise Operators

Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &, |, and ^ is as follows −

pqp & qp | qp ^ q
00000
01011
11110
10011

Assume A = 60 and B = 13 in binary format, they will be as follows −

A = 0011 1100

B = 0000 1101

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

A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~A = 1100 0011

The following table lists the bitwise operators supported by C. Assume variable 'A' holds 60 and variable 'B' holds 13, then −

Show Examples

OperatorDescriptionExample
&Binary AND Operator copies a bit to the result if it exists in both operands.(A & B) = 12, i.e., 0000 1100
|Binary OR Operator copies a bit if it exists in either operand.(A | B) = 61, i.e., 0011 1101
^Binary XOR Operator copies the bit if it is set in one operand but not both.(A ^ B) = 49, i.e., 0011 0001
~Binary One's Complement Operator is unary and has the effect of 'flipping' bits.(~A ) = ~(60), i.e,. -0111101
<<Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.A << 2 = 240 i.e., 1111 0000
>>Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.A >> 2 = 15 i.e., 0000 1111

Assignment Operators

The following table lists the assignment operators supported by the C language −

Show Examples

OperatorDescriptionExample
=Simple assignment operator. Assigns values from right side operands to left side operandC = A + B will assign the value of A + B to C
+=Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand.C += A is equivalent to C = C + A
-=Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand.C -= A is equivalent to C = C - A
*=Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand.C *= A is equivalent to C = C * A
/=Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand.C /= A is equivalent to C = C / A
%=Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand.C %= A is equivalent to C = C % A
<<=Left shift AND assignment operator.C <<= 2 is same as C = C << 2
>>=Right shift AND assignment operator.C >>= 2 is same as C = C >> 2
&=Bitwise AND assignment operator.C &= 2 is same as C = C & 2
^=Bitwise exclusive OR and assignment operator.C ^= 2 is same as C = C ^ 2
|=Bitwise inclusive OR and assignment operator.C |= 2 is same as C = C | 2

Misc Operators ↦ sizeof & ternary

Besides the operators discussed above, there are a few other important operators including sizeof and ? : supported by the C Language.

Show Examples

OperatorDescriptionExample
sizeof()Returns the size of a variable.sizeof(a), where a is integer, will return 4.
&Returns the address of a variable.&a; returns the actual address of the variable.
*Pointer to a variable.*a;
? :Conditional Expression.If Condition is true ? then value X : otherwise value Y

Operators Precedence in C

Operator precedence determines the grouping of terms in an expression and decides how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has a higher precedence than the addition operator.

For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.

Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.

Show Examples

CategoryOperatorAssociativity
Postfix() [] -> . ++ - -Left to right
Unary+ - ! ~ ++ - - (type)* & sizeofRight to left
Multiplicative* / %Left to right
Additive+ -Left to right
Shift<< >>Left to right
Relational< <= > >=Left to right
Equality== !=Left to right
Bitwise AND&Left to right
Bitwise XOR^Left to right
Bitwise OR|Left to right
Logical AND&&Left to right
Logical OR||Left to right
Conditional?:Right to left
Assignment= += -= *= /= %=>>= <<= &= ^= |=Right to left
Comma,Left to right

Variables in c

A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.एक वैरिएबल और कुछ नहीं बल्कि एक स्टोरेज एरिया को दिया गया नाम है जिसे हमारे प्रोग्राम हेरफेर कर सकते हैं। C में प्रत्येक वेरिएबल का एक विशिष्ट प्रकार होता है, जो वेरिएबल की मेमोरी के आकार और लेआउट को निर्धारित करता है; उस स्मृति में संग्रहीत किए जा सकने वाले मानों की श्रेणी; और संचालन का सेट जिसे चर पर लागू किया जा सकता है।

Sr.No.Type & Description
1

char

Typically a single octet(one byte). It is an integer type.

2

int

The most natural size of integer for the machine.

3

float

A single-precision floating point value.

4

double

A double-precision floating point value.

5

void

Represents the absence of type.

C programming language also allows to define various other types of variables, which we will cover in subsequent chapters like Enumeration, Pointer, Array, Structure, Union, etc. For this chapter, let us study only basic variable types.

Variable Definition in C

A variable definition tells the compiler where and how much storage to create for the variable. A variable definition specifies a data type and contains a list of one or more variables of that type as follows −

type variable_list;

Here, type must be a valid C data type including char, w_char, int, float, double, bool, or any user-defined object; and variable_list may consist of one or more identifier names separated by commas. Some valid declarations are shown here −

int    i, j, k;
char   c, ch;
float  f, salary;
double d;

The line int i, j, k; declares and defines the variables i, j, and k; which instruct the compiler to create variables named i, j and k of type int.

Variables can be initialized (assigned an initial value) in their declaration. The initializer consists of an equal sign followed by a constant expression as follows −

type variable_name = value;

String:

 string is collections of characters .  char[20] is a string which size is 30.

char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

char greeting[] = "Hello";
#include <stdio.h>

int main () {

   char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
   printf("Greeting message: %s\n", greeting );
   return 0;          
}                    //for run this code copy code and  click

When the above code is compiled and executed, it produces the following result −

Greeting message: Hello

C supports a wide range of functions that manipulate null-terminated strings −

Sr.No.Function & Purpose
1

strcpy(s1, s2);

Copies string s2 into string s1.

2

strcat(s1, s2);

Concatenates string s2 onto the end of string s1.

3

strlen(s1);

Returns the length of string s1.

4

strcmp(s1, s2);

Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.

5

strchr(s1, ch);

Returns a pointer to the first occurrence of character ch in string s1.

6

strstr(s1, s2);

Returns a pointer to the first occurrence of string s2 in string s1.

The following example uses some of the above-mentioned functions −

#include <stdio.h>

#include <string.h>


int main () {


   char str1[12] = "Hello";

   char str2[12] = "World";

   char str3[12];

   int  len ;


   /* copy str1 into str3 */

   strcpy(str3, str1);

   printf("strcpy( str3, str1) :  %s\n", str3 );


   /* concatenates str1 and str2 */

   strcat( str1, str2);

   printf("strcat( str1, str2):   %s\n", str1 );


   /* total lenghth of str1 after concatenation */

   len = strlen(str1);

   printf("strlen(str1) :  %d\n", len );


   return 0;

}                  //for run this program copy code and click Run program

When the above code is compiled and executed, it produces the following result −
strcpy( str3, str1) :  Hello
strcat( str1, str2):   HelloWorld
strlen(str1) :  10

KEY Words    

कीवर्ड programming language में  रिजर्व word  होते हैं keywords का उपयोग हम अपने हिसाब से नहीं कर सकते programming language  में जिस काम के लिए keywords  बनाए गए हैं उसी काम के लिए कीवर्ड्स का यूज होगा
key words are the reserve words which is included for programming language in default formate.
Example- main,printf,return,clrscr etc.

Constant 

Constant की value कभी भी change नहीं होती है

Constant  never changed In constant when we intilize any value this value never changed. 

Example: a=20; in this example constant value alwayes 20.

Addition program in c

#include <stdio.h>
int main() {    

    int number1, number2, sum;
    
    printf("Enter two integers: ");
    scanf("%d %d", &number1, &number2);

    // calculating sum
    sum = number1 + number2;      
    
    printf("%d + %d = %d", number1, number2, sum);
    return 0;
}             

For Run this program copy code  click here - click

MULTIPLICATION PROGRAM  IN C 
#include <stdio.h>
int main() {    

    int number1, number2, sum;
    
    printf("Enter two integers: ");
    scanf("%d %d", &number1, &number2);

    // calculating sum
    sum = number1 * number2;      
    
    printf("%d * %d = %d", number1, number2, sum);
    return 0;
}                       

For Run this program copy code  click here - click

AREA OF CERCLE PROGRAM   
  1. #include<stdio.h>
  2. #include < conio.h > #define PI 3.141  
  3. int main()   
  4. {  
  5.     float radius, area;  
  6.     printf("Enter radius of circle\n");  
  7.     scanf("%f", & radius);  
  8.     area = PI * radius * radius;  
  9.     printf("Area of circle : %0.4f\n", area);  
  10.     getch();  
  11.     return 0;  
  12. }               

    For Run this program copy code  click here - click

AREA OF RECTANGLE
  1. #include <stdio.h>            
  2. int main()
  3. {
  4. int width=5;
  5. int height=10;
  6. int area=width*height;
  7. printf("Area of the rectangle %d",area);
  8. return  0;
  9. }                                 

    For Run this program copy code  click here - click

For more content click - 10 Basic Program in c Language

                                                                                   Next Page

Post a Comment

2 Comments

यदि आपको यह पोस्ट अच्छी लगी हो तो आप हमें कमेंट में बताएं

Coding with Annu