Amazon

Wednesday, May 18, 2016

GETTING STARTED WITH C LANGUAGE




GETTING STARTED WITH C LANGUAGE

Please keep this BLOG RUNNING. Wait for the ads to load, then Click Skip add at upper right.



Title: Getting Started with C Language:
Ø  Download the .Doc Format of this article. ClickHERE
Ø  Download the .Pdf Format of this article. ClickHERE


PART A - INTRODUCTION TO C LANGUAGE
   C is a general-purpose high level language that was originally developed by Dennis Ritchie for the Unix operating system. It was first implemented on the Digital Equipment Corporation PDP-11 computer in 1972.



     The Unix operating system and virtually all Unix applications are written in the C language. C has now become a widely used professional language for various reasons.
·         Easy to learn


·         Structured language
·         It produces efficient programs.
·         It can handle low-level activities.
·         It can be compiled on a variety of computers.

            Facts about C

·         C was invented to write an operating system called UNIX.
·         C is a successor of B language which was introduced around 1970
·         The language was formalized in 1988 by the American National Standard Institue (ANSI).
·         By 1973 UNIX OS almost totally written in C.
·         Today C is the most widely used System Programming Language.
·         Most of the state of the art software have been implemented using C
           
            Why to use C?
    C was initially used for system development work, in particular      the       programs that make-  up the operating system. C was adopted as a system development language because it produces code   that runs nearly as fast as code written in        assembly language. Some examples of the use of C might   be:
·         Operating Systems
·          Language Compilers
·          Assemblers
·          Text Editors
·          Print Spoolers
·          Network Drivers
·          Modern Programs
·         Data Bases
·         Language Interpreters
·         Utilities
·         C Program File

                        All the C programs are written into text files with extension ".c" for example hello.c. You            can             use "vi"(unix) or "notepad"(windows) editor to write your C program into a file.

            Preparing to Program
    You should take certain steps when you're solving a problem. First, you must   define the  problem. If you don't know what the problem is, you can't find a    solution! Once you know what the  problem is, you can devise a plan to fix it. Once  you have a plan, you can usually implement it. Once the plan is implemented, you   must test the results to see whether the problem is solved. This   same logic can be applied to many other areas, including programming.
           
      When creating a program in C (or for that matter, a computer program in any     language), you  should follow a similar sequence of steps:
            1. Determine the objective(s) of the program.
            2. Determine the methods you want to use in writing the program.
            3. Create the program to solve the problem.
            4. Run the program to see the results.          

 

            The Program Development Cycle

                        The Program Development Cycle has its own steps.
1.      In the first step, you use an editor to create your source code.
2.      In the second step, you compile the source code to create an object file.
3.      In the third step, you link the compiled code to create an executable file.
4.      The fourth step is to run the program to see whether it works as originally planned.

            Compiling the Source Code

   Although you might be able to understand C source code, your computer can't.    A computer    requires digital, or binary, instructions in what is called machine    language. Before your C program can run on a computer, it must be translated from  source code to machine language. This translation,  the second step in program  development, is performed by a program called a compiler. The compiler     takes your source code file as input and produces a disk file containing the machine language    instructions that correspond to your source code statements.

 

            Linking to Create an Executable File

   One more step is required before you can run your program. Part of the C     language is a function library that contains object code (code that has already    been compiled) for predefined functions. A predefined function contains C code that    has already been written and is supplied in   a ready-to-use form with your compiler package.

 

            Completing the Development Cycle

  Once your program is compiled and linked to create an executable file, you can run it by             entering its name at the system prompt or just like you would run any other program. If you run the    program and receive results different from what you thought you would, you need to go back to   the first step. You must identify what caused the problem and correct it in the source code. When you  make a change to the source code, you need to recompile and relink the program to create a corrected version of the executable file. You keep following this cycle until you get the program to execute    exactly as you intended.


PART B - THE COMPONENTS OF A C PROGRAM

           

            First C Program

            This program displays the words Hello, World! on-screen.
               
               #include <stdio.h>
      main(){
            printf("Hello, World!\n");
      }

 

            The Program's Components

                        #include

              The #include directive instructs the C compiler to add the contents of   an include file into your program during compilation. An include file is a   separate disk file that contains information needed by your program or the compiler. Several of these files    (sometimes called      header files) are supplied with your compiler. You never need to modify    the information in these files; that's why they're kept separate from your source      code. Include files should all have an .H extension (for example, STDIO.H).

 

                        main()

     The only component that is required in every C program is the main() function. In its  simplest form, the main() function consists of the name main followed by a pair of empty   parentheses (()) and a pair of braces ({}). Within the braces are statements that make up the main body of the program. Under normal circumstances, program execution starts at the first   statement in main() and terminates at the last statement in main().

 

                        printf()

  The printf() statement is a library function that displays information on-screen. The         printf() statement can display a simple text message or a message and the value of one or more         program variables.

 

                        Braces

 You use braces ({}) to enclose the program lines that make up every C function--   including the main() function. A group of one or more statements enclosed within braces is         called a block. C has many uses for blocks.

 

            A Short C Program

Text Box: 7

This is a very simple program. All it does is input two numbers from the    keyboard and calculate their product.
               1:  /* Program to calculate the product of two numbers. */
      2:  #include <stdio.h>
      3:
      4:  int a,b,c;
      5:
      6:  main()            
      7:  {              
      8:     /* Input the first number */              
      9:     printf("Enter a number between 1 and 100: ");              
      10:     scanf("%i", &a);                 
      11:
      12:     /* Input the second number */                 
      13:     printf("Enter another number between 1 and 100: ");              
      14:     scanf("%i", &b);          
      15:
      16:     /* Calculate and display the product */                
      17:     c = a*b;             
      18:     printf ("%i times %i = %i\n", a, b, c);               
      19:
      20:     
      21: }

            Program Comments (Lines 1, 8, 12, and 16)

      Any part of your program that starts with /* and ends with */ is called a   comment. The    compiler ignores all comments, so they have absolutely no effect on         how a program works. You     can put anything you want into a comment, and it won't   modify the way your program operates. A    comment can span part of a line, an entire       line, or multiple lines. Here is an example:
               /* A single-line comment */
               int a,b,c; /* A partial-line comment */

           

            The Variable Definition (Line 4)

Text Box: 7A variable is a name assigned to a data storage location. Your program uses   variables to       store various kinds of data during program execution. In C, a variable  must be defined before it   can be used. A variable definition informs the compiler     of the variable's name and the type of data it is to hold. In the sample program,   the definition on line 4, int a,b,c;, defines three variables--named a, b, and c--     that will each hold an integer value.Text Box: 7     A variable is a name assigned to a data storage location. Your program uses   variables to store various kinds of data during program execution. In C, a variable  must be defined before it can be used. A variable definition informs the compiler    of the variable's name and the type of data it is to hold. In the sample program,  the definition on line 4, int a,b,c;, defines three variables--named a, b, and c--     that will each hold an integer value.

            scanf()

       The scanf() statement (lines 10 and 14) is another library function. It reads data from the    keyboard and assigns that data to one or more program variables.

 

PART C - FUNDAMENTALS OF INPUT AND OUTPUT

 

            The printf() Format Strings

                        A printf() format string specifies how the output is formatted. Here are the three possible                       components of a format string:
·      Literal text is displayed exactly as entered in the format string.
·      An escape sequence provides special formatting control. An escape sequence consists of a backslash (\) followed by a single character. In the preceding example, \n is an escape sequence. It is called the newline character, and it means "move to the start of the next line." Escape sequences are listed in Table 7.1.
·      A conversion specifier consists of the percent sign (%) followed by a single character. In the example, the conversion specifier is %i. A conversion specifier tells printf() how to interpret the variable(s) being printed. The %i tells printf() to interpret the variable x as a integer.

 

The most frequently used escape sequences.

Sequence
Meaning
\n
Newline
\t
Horizontal tab
\\
Backslash

 

            scanf() function
                        This is the function which can be used to read an input from the command line. Try         following program to understand scanf() function.
           
                        #include <stdio.h>
          main()
              {
              int x;
              int args;

              printf("Enter an integer: ");
              if (( args = scanf("%d", &x)) == 0)
                   {
                   printf("Error: not an integer\n");
                   }
              else
                   {
                         printf("Read in %d\n", x);
                   }
              }


                        Here %d is being used to read an integer value and we are passing &x      to store the vale read  input. Here &indicates the address of variable x.

This program will prompt you to enter a value. Whatever value you will    enter at command      prompt that will be output at the screen using printf() function. If you enter a non-integer value then it     will display an error message.

The most commonly needed conversion specifiers.

Specifier
Meaning
Types Converted
%c
Single character
char
%d or %i
Signed decimal integer
int, short
%ld
Signed long decimal integer
long
%f
Decimal floating-point number
float, double
%s
Character string
char arrays
%u
Unsigned decimal integer
unsigned int, unsigned short
%lu
Unsigned long decimal integer
unsigned long

            Reserved Words
                        The following names are reserved by the C language. Their meaning is already    defined, and    they cannot be re-defined to mean anything else.
 While naming your functions and variables, other than these names, you can        choose any      names of reasonable length for variables, functions etc.

PART D - STORING DATA: VARIABLES

 

            Variables

                        A variable is a named data storage location in your computer's memory. By using a         variable's name in your program, you are, in effect, referring to the data      stored there.

            Variable Names

                        To use variables in your C programs, you must know how to create variable names. In C,             variable names must adhere to the following rules:
·         The name can contain letters, digits, and the underscore character “_”.
·         The first character of the name must be a letter. The underscore is also a legal first character, but it’s not recommended.
·         Case sensitive. A is different from a.
·         C keywords can't be used as variable names. A keyword is a word that is part of the C language.
           

            The following list contains some examples of legal and illegal C variable names:
Variable Name
Legality
Percent
Legal
y2x5__fg7h
Legal
annual_profit
Legal
_1990_tax
Legal but not advised
Savings#account
Illegal: Contains the illegal character #
Double
Illegal: Is a C keyword
9winter
Illegal: First character is a digit

                        For many compilers, a C variable name can be up to 31 characters long. (It can actually be             longer than that, but the compiler looks at only the first 31 characters of the name.)
                        DO use variable names that are descriptive.
                        DO adopt and stick with a style for naming your variables.
                        DON'T start your variable names with an underscore unnecessarily.
                        DON'T name your variables with all capital letters unnecessarily.

 

            Numeric Variable Types

                        C provides several different types of numeric variables. You need different types of variables             because different numeric values have varying memory storage    requirements and differ in the ease with which certain mathematical operations can             be performed on them.

            C's numeric data types. Table 1.1

Variable Type
Keyword
Bytes Required
Range
Character
char
1
-128 to 127
Integer
int
2
-32768 to 32767
Short integer
short
2
-32768 to 32767
Long integer
long
4
-2,147,483,648 to 2,147,438,647
Unsigned character
unsigned char
1
0 to 255
Unsigned integer
unsigned int
2
0 to 65535
Unsigned short integer
unsigned short
2
0 to 65535
Unsigned long integer
unsigned long
4
0 to 4,294,967,295
float
float
32
3.4 E-38 to 3.4 E 38
Double precision
double
64
17.4 E-308 to 17.4 E 308

           

 

            Variable Declarations

                        Before you can use a variable in a C program, it must be declared. A variable      declaration      tells the compiler the name and type of a variable and optionally          initializes the variable to a specific    value. If your program attempts to use a        variable that hasn't been declared, the compiler generates            an error message. A      variable declaration has the following form:
 
typename varname;
           
            typename specifies the variable type and must be one of the keywords listed in   
           
                        Table 1.1. varname is the variable name, which must follow the rules. You can     declare             multiple variables of the same type on one line by separating the variable            names with commas:
                               
                               int count, number, start;    /* three integer variables */
            float percent, total;        /* two float variables */
 

            Initializing Numeric Variables

                        When you declare a variable, you instruct the compiler to set aside storage          space for the             variable. However, the value stored in that space--the value of the            variable--isn't defined. It             might be zero, or it might be some random "garbage"        value. Before using a variable, you should             always initialize it to a known value. You can do this independently of the variable declaration by using             an assignment statement, as in this example:
               
                               int count;   /* Set aside storage space for count */
            count = 0;   /* Store 0 in count */


PART E - PROGRAM DEVELOPMENT

 

Set01 (10pts)

     Problem:

     Create a C program that will accept 5 numbers and will print out its summation and                         average.

                                Sample Output:
                                                Enter 1st Number: 5
                                                Enter 2nd Number: 8
                                                Enter 3rd Number: 5
                                                Enter 4th Number: 4
                                                Enter 5th Number: 6
                                                The Summation is: 28
                                                The Average is: 5.6000
Set02 (100pts)
Problem:
 Create a C program that will accept student's grade for prelim, midterm, and final, then the Final     Grade will be computed by getting 30% of prelim, 30% of midterm and 40% for final.
                                Sample Output:
                                                Enter Prelim: 75
                                                Enter Midterm: 80
                                                Enter Final: 82


                                                The Final Grade is: 79.3000
                Scoring Rules:
                                100 pts If the problem is solved within 30 minutes. 
                                80 pts If the problem is solved within 45 minutes.
                                70 pts If the problem is solved within 60 minutes.
                                50 pts If the problem is solved past 61 minutes.
                                40 pts If the problem is solved past 61 minutes but with runtime errors or bugs.
                                30 pts for running program with incorrect output.

                                0 zero for non running program



Please keep this BLOG RUNNING. Wait for the ads to load, then Click Skip add at upper right.

Title: Getting Started with C Language:
Ø  Download the .Doc Format of this article. ClickHERE
Ø  Download the .Pdf Format of this article. ClickHERE


No comments:

Post a Comment