Introduction to C language

Introduction to C


     C is a general purpose programming language. Programming language is the means to communicate with the system to get our thing done.

Need for a programming language:

  We all of us know system can only understand binary representation i.e. in the form of 0's and 1's.

Suppose we have to program which adds two numbers and save the result.

Steps involved:
  • First we should write the values in the memory.
  • Move those values into the registers.(registers are also a kind of memory where the CPU can perform operation on the values stored in the registers.)
  • Add the values in the registers.
  • Save the result in the register to the memory.

Machine Language                                                                                                                                                                                                                                                                               At the beginning machine code is used to write the program. 

Machine level programming consists of binary or hexadecimal instructions on which an processor can respond directly.  

Structure of machine language instruction.
Opcode   Operand 

Opcode represents the operation to be performed by the system on the operands.

In machine code:
Memory     opcode     operand 
location                                                                     
0000:           55            e4     10   // 55 represent move instruction, move 10 to location e4          
0001:           89            e5     20
0003:           83            e4     e5
0006:           54            c4     e4    

Its difficult to read and write opcode for every instruction. So instead of numerical value for instruction there should be an alternative, Then comes the assembly code.

Assembly language:

 Mnemonics are used to represent the opcode.
An Assembly code:
MOV  A, $20           // move value 20 to register A (MOV is a mnemonics)
MOV  B, $10           // move value 10 to register B
ADD   A, B              // Add value in register in A and B
MOV  #30, A           // move value in register A to address 30

Assemblers converts the assembly language into binary instructions.
Assembly language is referred to as low level language,
Assembler languages are simpler than opcodes. Their syntax is easier to read than machine language but as the harder to remember. So, their is a need for other language which is much more easy to read and write the code.
Then comes the C programming language.

C language

In C language:
 int main()
{
     int a,b,c;
     a=10;
     b=20;
     c=a+b;
}

Compiler converts the C language code into assembly language code.

C language is easier to write and read. 

Factorial program in python

#write a python program for factorial of a given number.

num = int(input("Enter a number: "))
factorial = 1
if num < 0:
   print("factorial not exist for negative numbers")
elif num == 0:
   print("The factorial of 0 is 1")
else:
   for i in range(1,num + 1):
       factorial = factorial*i
   print("The factorial of",num,"is",factorial)

  • We store the number we give in the num variable and by using the if, elif, and else loop we are executing the program. if statement executes when the given condition is true and same for elif also. After the iteration, it finally returns the factorial of the number we are given.
  • Normally we write as 12!=12*11*10*9*8*7*6*5*4*3*2*1=479001600




find substring python

 find substring():

  • The find substring is used to find the substring from the string.
  • Substring is the piece of characters from the string.
  • If the string consists of the given string then it returns the index number of the substring. 
  • The string must be enclosed with single or double-quotes.
  • It returns -1 if the substring does not found instead of an error.
  • find() has 3 parameters,they are:
  1. value: The value to be search
  2. start:  from where to search
  3. end:  from where to stop searching.
Syntax: str.find(value,start,end)

#write a python code to demonstrate the find substring function.

  1. txt="learning is a life time process"
  2. txt=txt.find("i")
  3. print(txt)
  4. s="apple"
  5. s=s.find("b")
  6. print(s)

Output: 5
              -1







python replaceall

  • In python, replace all is similar to replace() function. 
  • Here we are going to replace all the characters with new characters.
  • It also consists of 3 parameters like new value, old value, count.
  • The old value is the string that we have already given in string.
  • The new value is the that is to be replaced with.
  • The count is the number of times that string to be replaced in the string.
Syntax:  str.replace(old value,new value,count)

#write a python code to demonstrate the replace function.

  1. s="hello world"
  2. s=s.replace("hello world","my name is hello")
  3. print(s)
Output:    my name is hello


format function in python

  •  The format() function is used to format a specific value and insert that value in string placeholders.
  •  The placeholders are cost{20}, number index{1},empty{}
  • The place holder is defined by" curly braces"{}.
  • The format function consists of parameters called value.
  • It returns the formatted string.
Syntax:  str.format(values)

  • Inside the placeholders, we can add different formatting types
  • :+    indicates positive values.
  • : -    indicates negative values.
  • :      indicates the space before positive and also negative numbers.
  • :_    indicates as a thousand separator.
  • :,     uses as comma thousand separator.
  • :=    left most position.
  • :<    result in left alignment.
  • :>    result in right alignment.
  • :^     result in the center.
  • :%    percentage format.
  • :b     Binary format.
  • :c     Unicode format.
  • :d     Decimal format.
  • :e      format with lower case.
  • :E     format with upper case.
  • :f      format with fixpoint number.
  • :o    format is octal.
  • :x      format is Hexa.
  • :X     format is Hexa in uppercase.
  • :n      number format.

#write a python code to demonstrate format function.

  1. s0="my name is {fname},I m {age}".format("fname=abc",age=19)
  2. s1="my name is{0},I m {1}".format("abc",19)
  3. s2="my name is {},I m {}".format("abc",19)
  4. print(s0)
  5. print(s1)
  6. print(s2)

Output:    my name is abc, I m 19.
                 my name is abc, I m 19.
                 my name is abc, I m 19.


string reverse in python

  • The reverse string in python is not an in-built function.
  • By creating the slice operation we can perform the reverse function.
  • In this slice operation, we create the slice that starts with the end of the string and moves backward.
  • String reverse in python example program
Example: " abcdefg"[::-1]

                     gfedcba

 right to left indexing:              [-7]    [-6]    [-5]    [-4]      [-3]     [-2]      [-1]

                                                    a          b      c        d          e          f         g 

left to right indexing:             [0]         [1]     [2]     [3]     [4]       [5]        [6]

  • In the above example, we are having the string  "abcdefg " which we want to reverse. so create a slice that starts with the end of the string and moves backward.
  • here it starts with the end index that is indicated with -1 and moves backward as 5,4,3,2,1,0.
  • -1 index  is g
  • 5 index is f
  • 4 index is e
  • 3 index is d
  • 2 index is c
  • 1 index is b
  • 0 index is a
  • so the final output is gfedcba.

#write a python code to demonstrate string reverse.

  1. txt="today climate is very cool"[::-1]
  2. txt1="hello world"[::-1]
  3. print(txt)
  4. print(txt1)
Output:    looc yrev si etamilc yadot
                  dlrow olleh


Select Menu