Friday, October 25, 2019

C Language Basics

In this tutorial let's learn about basics of C programming language. Theory and practical both play key role in the learning process.
                

Programs/Applications need inputs to produce output.
  • char 1 - a single character.
  • int 2 or 4 - integer: a whole number.
  • float 4 - floating point value: ie a number with a fractional part. 3.14
  • double 8 - a double-precision floating point value. 5678.899999
  • void - Valueless.
Derived Data Types:
  • Arrays
  • Pointers
  • Structures
  • Enumeration

Escape Sequences in C:

Escape Sequence   Meaning
            \t                 Horizontal Tab
            \n               New line
            \'                Single quote
            \"               Double quote
            \?               Question mark
            \\                Backslash

Keywords in C 

Meaning is already defined by Compiler

auto        double   struct       int
break      long       else          switch
char        enum     register    typedef
case        extern    return union
const       for         short       unsigned
continue  float      signed     void
goto         default  sizeof     volatile
do            if           static      while

Wrong way of usage:

int if;  //wrong

Note: Keywords cannot be used for creating variable and function names.

Reading Values from Keyboard in C

  • We use scan statement to take input from the keyboard. We can take one or multiple inputs using one scanf statement. 

Rules for Creating Variables:

  • Should start with alphabet or an underscore only. int abc; int ABC;
  • No spaces allowed.
  • Only _(Underscore is allowed to use).
  • keywords cannot be used as variable names.
  • Case sensitive.

scanf in C 


Explanation: 
scanf(“format string”, argument list);

Example:
int a=2;

int a;
scanf("%d",&a);

int a,b;
scanf("%d%d",&a,&b);

"For Loop" in C: 

"For Loop" in C: Initially it looks difficult, but after a while it will be the one of the common thing that you use in every program or application.
Let’s see how and why in my words!!!!!
To print my name the simple code is here:
  #include 
  int main()
  {
    printf("Kirthi");
    return 0;
  }


Output: Kirthi


Let’s say if I want to print my name(Kirthi) for 100 times, then I should write printf statement 100 times.


Or I should mention my name 100 times in the printf statement.
To simplify this, we need to use of “for loop”.


  #include<stdio.h> 
  int main()
  {
    for(int i=1;i<=100;i++)
    {
      printf("Kirthi \n");
    }
  return 0;
}
Output:
Kirthi
Kirthi
.
.
Kirthi(100 th time).


In above example for loop iterates 100 times because of the condition that we've specified. To make program smaller, to reduce lines of code for loop is used.

No comments:

Post a Comment