Ticker

6/recent/ticker-posts

Header Ads Widget

Responsive Advertisement

C language

 Introduction: C is a computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the UNIX Operating System. C is a simple and structure oriented programming language.

C is also called mother Language of all programming Language. It is the most widely use computer programming language, This language is used for develop system software and Operating System. All other programming languages were derived directly or indirectly from C programming concepts. Here we discuss complete C Tutorial in simple and easy way. C is a programming language that serves as a medium for programmer to instruct computer to do a job or making programs/software.

History of C

C language is developed by Mr. Dennis Ritchie in the year 1972 at bell laboratory at USA, C is a simple and structure Oriented Programming Language.
In the year 1988 C programming language standardized by ANSI (American national standard institute), that version is called ANSI-C. In the year of 2000 C programming language standardized by ISO that version is called C-99. All other programming languages were derived directly or indirectly from C programming concepts .

Overview of C

C is a computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the UNIX Operating System. C is a simple and structure oriented programming language.
C is also called mother Language of all programming Language. It is the most widely use computer programming language, This language is used for develop system software and Operating System. All other programming languages were derived directly or indirectly from C programming concepts.
Founder of C Language
In the year 1988 'C' programming language standardized by ANSI (American national standard institute), that version is called ANSI-C. In the year of 2000 'C' programming Language standardized by 'ISO' that version is called C-99 .

Keywords in C

Keyword is a predefined or reserved word in C library with a fixed meaning and used to perform an internal operation. C Language supports 32 keywords.


Types of Constant in C

It is an identifier whose value can not be changed at the execution time of program. In general constant can be used to represent as fixed values in a C program. Constants are classified into following types.

constant in c

If any single character (alphabet or numeric or special symbol) is enclosed between single cotes ' ' known as single character constant.

If set of characters are enclosed between double cotes " " known as string character constant.


* Variable: A variable is a container that stores a value. this value can be changed during the execution of program.

example:
     int  number = 8 

*Rules for declaring a variable name: There are flowing rules for declaring a variable name.
 1. Must not begin with a digit.

 2. Name is case sensitive.

   
 3. Should not be keyword.

 4. White space are not allowed.

  
 5. It contain alphabet,doller($),character and digit. if the other condition are invalid.

* Datatype: There are two type of datatype. Which are given below

   1. Primitive Datatype 

   2. Non Primitive Datatype

1.Primitive Datatype: The variable must be declared byte code are used. There are following eight (8) primitive datatype.

1. Byte : value range from ➨ -128 to 127

          take one(1) byte ➨ 
          Default value is Zero 

2.Short : value range from ➨ -65536 to 65535
          take two(2) byte  ➨
          Default value is Zero 

3. Int  : value range from  ➨ 

          take four(4) byte ➨
          Default value is Zero

4.Float : value range from  ➨ (sec docs)

          take four(4) byte ➨ 
          Default value is Zero 

5. Long : value range from  ➨ (sec docs)
         take eight(8)byte  ➨ 
          Default value is Zero 

6.Double: value range from  ➨ (sec docs)

          take eight(8)byte ➨ 
          Default value is Zero 

7.Char :  value range from ➨ 0 to 65535 

          take two(2) byte ➨ 
          Default value is Zero 

8.Boolean: value can be true ya false

           size depend upon jvm
           Default value is false

Operators

Operator is a special symbol that tells the compiler to perform specific mathematical or logical Operation.

Types Of operator

  • Arithmetic Operators => [+, -, *, /, %, ++, --]
  • Assignment Operators => [=, +=]
  • Comparison Operators => [==, >=, <=]
  • Logical Operators => [&&, ||, !]
  • Bitwise Operators => [&, |] (operates bitwise)

Arithmetic operators cannot work with Booleans.

% operator can work on floats and doubles

Precedence of operators


The operators are applied and evaluated based on precedence. For example (+, -) has less precedence compared to (*, /). Hence * and / are evaluated first.

In case we like to change this order, we use parenthesis ().

Associativity


Associativity tells the direction of the execution of operators. It can either be left to right or vice versa.

/ * -> L to R

+ - -> L to R

++, = -> R to L

Resulting data type after arithmetic operation


Result = byte + short -> integer

Result = short + integer -> integer

Result = long + float -> float

Result = integer + float -> float

Result = character + integer -> integer

Result = character + short -> integer

Result = long + double -> double

Result = float + double -> double

Increment and Decrement operators


a++, ++a (Increment Operators)

a--, --a (Decrement Operators)

 Introduction to Strings

  • A string is a sequence of characters.

  • A string is instantiated as follows:

  • String name;

  • name = new String(“Pankaj”);

  • String is a class but can be used as a data type.

  • Strings are immutable and cannot be changed.

  • String name = “Pankaj”

  • (Here, name is a reference, and “Pankaj” is an object.)


Different ways to print in C

We can use the following ways to print in Java:

  • print() // No newline at the endprintln() // Prints a new line at the end
  • printf()
  • printf()

  printf(“%c”,ch)

{ %d for int

  %f for float

  %c for char

  %s for string }

 String Methods in C

String Methods operate on Java Strings. They can be used to find the length of the string, convert to lowercase, etc.

Some of the commonly used String methods are:

String name = “Harry”;

(Indexes of the above string are as follows: 0-H, 1-a, 2-r, 3-r, 4-y)

  1. length() – Returns the length of String name. (5 in this case)
  2. toLowerCase() – Returns a new String which has all the lowercase characters from the String name.
  3. toUpperCase() – Returns a new String which has all the uppercase characters from the String name.
  4. trim() – Returns a new String after removing all the leading and trailing spaces from the original string.
  5. substring(int start) – Returns a substring from start to the end. Substring(3) returns “ry”. [Note that index starts from 0]
  6. substring(int start, int end) – Returns a substring from the start index to the end index. The start index is included and the end is excluded.
  7. replace(‘r’, ‘p’) – Returns a new string after replacing r with p. Happy is returned in this case. (This method takes char as argument)
  8. startsWith(“Ha”) – Returns true if name starts with string “Ha”. (True in this case)
  9. endsWith(“ry”) – Returns true if name ends with string “ry”. (True in this case)
  • charAt(2) – Returns the character at a given index position. (r in this case)
  • indexOf(“s”) – Returns the index of the given string.

For e.g. name.indexOf(“ar”) returns 1 which is the first occurrence of ar in string “Harry”, -1 otherwise.

  • indexOf(“s”, 3) – Returns the index of the given String starting from index 3(int). (-1 is returned in this case)
  • lastIndexOf(“r”) – Returns the last index of the given string. (3 in this case)
  • lastIndexOf(“r”,2) – Returns the last index of the given string before index 2.
  • equals(“Harry”) – Returns true if the given string is equal to “Harry” false otherwise [Case sensitive]
  • equalsIgnoreCase(“harry”) – Returns true if two strings are equal ignoring the case of characters.

Escape Sequence Characters


The sequence of characters after backslash ‘\’

= Escape Sequence Characters

Escape Sequence Characters consist of more than one character but represent one character when used within the strings.

Examples: \n (newline), \t (tab), \’ (single quote), \\ (backslash), etc.

All these are decisions which depends on a certain condition being met. In java, we can execute instructions on a conditional being met.

Decision-making instructions in Java

  • If-Else Statement
  • Switch Statement

If-Else Statement

The syntax of an if-else statement in C looks like that of C++ and JavaScript. Java has a similar syntax too. It looks like.

Syntex

 if (condition-to-be-checked) {

statements-if-condition-true;

}

else {

statements-if-condition-false;

}

Example If-else Statement 

int a = 29;

if (a>18) {

        printlf(“You can drive”);

}

Relational Operators in C

Relational operators are used to evaluate conditions (true or false) inside the if statements.

Some examples of relational operators are:

== (equals)

>= (greater than or equals to)

> (greater than)

< (less than)

<= (less than or equals to)

!= (not equals)

Note: ‘=’ is used for assignment whereas ‘==’ is used for equality check.

The condition can be either true or false.

Logical Operators

&&, || and ! are the most commonly used logical operators in Java.

These are read as:

&& - AND

|| - OR

! – NOT

The above logical operators are used to provide logic to our Java programs.

AND Operator

Evaluates to true if both the conditions are true.

Y && Y = Y                # Y – True and N - False

Y && N = N

N && Y = N

N && N = N

OR Operator

Evaluates to true when at least one of the conditions is true.

Y || Y = Y                   # Y – True and N - False

Y || N = Y

N || Y = Y

N || N = N

NOT Operator

Negates the given logic (true becomes false and vice-versa)

!Y = N                         # Y – True and N - False

!N = Y

Else-if clause

Instead of using multiple if statements, we can also use else if along with if thus forming an if-else-if-else ladder.

Using such kind of logic reduces indents. Last else is executed only if all the conditions fail.

                     Syntex

 if (condition1) {

            //Statements;

else if {

            // Statements;

}

else {

            //Statements

}

        Switch Case-Control Instruction

Switch-Case is used when we have to make a choice between the number of alternatives for a given variable.

                  Syntex

 Switch(var) {

Case C1:

//Code;

break;

Case C2:

//Code;

break;

Case C3:

//Code

break;

default:

//Code

Var can be an integer, character, or string in Java.

A switch can occur within another but in practice, this is rarely done.










Post a Comment

0 Comments