Sunday, May 6, 2012

C Programming : Beginning

Hello, Friends !!! Hope, you all are well by the grace of Almighty!!! The very first thing you need to do, before starting out in C, is to make sure that you have a compiler. What is a compiler, you ask? A Compiler turns the program that you write into an executable that your computer can actually understand and run. If you're taking a course, you probably have one provided through your school. If you're starting out of your own, your best bet is to use: Microsoft Visual Studio. If you're on Linux, you can use gcc, and if you're on Mac OS X, you can use XCode. If you haven't yet done so, go ahead and get a compiler set up.




Let's look at a working Program:
#include<stdio.h>
int main(void)
{
    printf("What's Up ?");
    return 0;
}
Output: What's Up ?


You can alse write this code by following way:

#include<stdio.h>
void main(void)
{
    printf("What's Up ?");
}
Output: What's Up?


We can also write "stdio.h" instead of <stdio.h> this code by following way:






Output: What's Up?


About the C preprocessor:


  • The C preprocessor is the first part of the compilation process. It happens before the "true" compilation.
  • It processes the source code using text substitution rules that are specified by the programmer.
  • Such rules are specified within the source file, each on a line beginning with a #(known as directives).

The # include directive:


  • Replaces the directive line with the contents of the file referred to in the directive. For Example: #include<filename.h>
  • The fil filename.h known as a header file.
  • The header file will (hopefully) be found in one of the folders specified in the compiler's include path.

Header files:

  • Information about all the library function is found here.
  • Compiler uses this information to handle the library functions properly.
  • All header files end with a extension .h
  • #include<stdio.h>
  • #include<stdlib.h>
  • #include<math.h>
You can download some header files' function information from the following  link:
Header Files


Functions:

  • Building blocks of C.
  • All C program consists of one or more functions.
  • General form of C function are: 
          return_type  function_name (parameter list)
         {
              statements;
         }
  • Every C program must have a function name main( ).

User Defined Functions:

  • User can define any function to perform operations.
  • Typical example is:
          void course_name(void)
         {
              printf("Programming Language");
         }

Library Functions:
  • These are function provided by C compiler.
  • Collection of these function usually referred to C standard library.
  • Standard library functions performed input/output operation, mathematical computation, string manipulation and much more.
  • printf, scanf are the example of library function.


Statements:

  • Statements are actually perform the operation.
  • All C statements ends with a semicolon (;)
  • A simple statement is:
          a = b+c; /* a is assigned the value of b+c */

Comments:


  • Comments begin with a /* and ends with a */
  • They can span multiple lines.
  • New style (c++ style) comments are single line comments starting with a // and running to the end of the line.
  • Comments are strongly recommended!!!
       You can't make a comment inside a comment.



Data Types & Modifier:


Basic data types are:
  • char
  • int
  • float
  • double
Modifiers are:
  • signed
  • unsigned
  • short
  • long


Rules of declaring Variables:

  • A variable has three parts. 1. Name 2. Value 3. Address.
  • Alphabetic character (a....z; A.....Z), digits(0, 1.....9), ( _ ) and ( $ ) can be used in variable name.
  • First character must be letter cannot be digit.
  • Both upper and lower case are permitted.
  • No space is allowed in the variable name.
  • Keywords are not allowed.
  • Variable name should not be greater than 31 char.
  • Variables have unique name.

Variable Use:


  • Variables must be set(initialized) before they can be read.
  • Variables are assigned values by use of the "=". 

      int x;   //Variable declaration
      x = 5;   //Variable initialization
or,
      int x = 5



Printf Function:

  • The printf statement allows to send output to standard out; standard out is generally the screen.
  • printf  general form:
  1. printf("string");
  2. printf(''format_specifier", variable name);
Examples:

01.
#include<stdio.h>
void main()
{
    printf("What's up?");
}

Output: What's up ?

02.
#include<stdio.h>
void main()
{
    int x = 10;
    printf("%d", x);
    printf("The Value of x is %d", x);
}

Output: The Value of x is 10


Scanf Function:

  • scanf function allows to accept input from standard in, generally the keyboard.
  • General Form:
      => scanf("format_specifier", &variable);
      => "&variable" means address of the variable.
  • Examples:

      =>    int age;
              scanf("%d", &age);
      =>    float cgpa;
              scanf("%f", &cgpa);
      =>    char grade;
               scanf("%c", &grade);
      =>    double number
               scanf("%lf", &number);

  • More Examples:
          #include<stdio.h>
           void main(void)
          {
               int num;
               float f;
               scanf("%d", &num);
               scanf("%f", &f);
           }


Format Specifier:


  • %d      signed decimal Integer( int )
  • %i      signed integer( int )
  • %u      unsigned decimal Integer ( unsigned )
  • %o      octal
  • %X      unsigned Hexadecimal with uppercase letters
  • %x      unsigned Hexadecimal with lowercase letters
  • %ld     long integer( long int )
  • %lld    long long integer ( long long int )
  • %lu     unsigned decimal number( unsigned long )

  • %f      floating point data type( float )
  • %lf     double data type( double )
  • %Lf     long double( long double )
  • %e      float data in exponential e notation( float/double )
  • %E      float data in E notation( float/double )
  • %g      use %e or %f, whichever is shorter( float/double )
  • %G      use %E or %f, whichever is shorter( float/double )
  • %lg     number with up to six digits of precision(double)
  • %le     number with up to six digits of precision, scientific notation(double)

  • %c      character( char )
  • %s      string, prints characters until a null-terminator is pressed.
  • %%      prints the % character

Arithmetic Operators:









Relational Operators:










































You can download Precedence Table from here.

Thank you all..:)

6 comments:

  1. how can i open a new project in visual studio 10. help plz

    ReplyDelete
  2. Open Visual studio 2010 > New project > Visual C++ > Win 32 > Win 32 Console Application > Enter Name > Location > Next > Empty project > Finish. After this, go to solution explorer > right click on source file > Add > New item > C++ file(.cpp)> Name > Add...@Anik

    If you cant understand tell me which part you can't understand.

    Thank you..:)

    ReplyDelete
  3. কোড ব্লকস কম্পাইলারটা কেমন ? আমি নতুন ত আমার কাছে সি এর জন্য ভিজুয়াল স্টুডিও এর চেয়ে এটাই ভাল লাগে বেশী :)

    ReplyDelete
    Replies
    1. আসলে code blocks কে খারাপ বলব না। কিন্তু, ভালভাবে লক্ষ করলে দেখা যাবে code blocks এ অনেক সময় অনেক program রান করে, যা রান করার কথা না। এটা তো C এর সমস্যা। C++ এ গেলে আরও অনেক সমস্যা বের হবে। আর সবচেয়ে বড় কথা সামনের দিকে higher language এর জন্য Visual studio use করা লাগবে। তাই, এখন থেকে ই ওটা use করে অভ্যাস করা ভালো।

      ধন্যবাদ।

      Delete
  4. Which software is better? Visual studio 2010 ultimate or 2013 ultimate???

    ReplyDelete