Wednesday, September 24, 2014

INTRODUCTION TO C

1. Explain in detail about „C‟ declarations and variables.

In C, lowercase and uppercase characters are very important. All commands in C must be lowercase. The C programs starting point is identified by the word main( ). This informs the computer as to where the program actually starts.
The brackets that follow the keyword main indicate that there are no arguments supplied to this program.
The two braces, { and }, signify the begin and end segments of the program. The purpose of the statement include <stdio.h> is to allow the use of the printf statement to provide program
output. Text to be displayed by printf() must be enclosed in double quotes. The program has only one statement printf("Programming in C is easy.\n");
printf() is actually a function (procedure) in C that is used for printing variables and text. Where text appears in double quotes "", it is printed without modification. There are some exceptions however. This has to do with the \ and % characters. These characters are modifier‟s, and for the present the \ followed by the n character represents a newline character. Thus the program prints Programming in C is easy.
and the cursor is set to the beginning of the next line. As we shall see later on, what follows the \ character will determine what is printed, ie, a tab, clear screen, clear line etc. Another important thing to remember is that all C statements are terminated by a semi-colon ;
General rules of „C‟ language:
· program execution begins at main()
· keywords are written in lower-case
· statements are terminated with a semi-colon
· text strings are enclosed in double quotes

· C is case sensitive, use lower-case and try not to capitalize variable names
· \n means position the cursor on the beginning of the next line
· printf() can be used to display text to the screen
· The curly braces {} define the beginning and end of a program block.

BASIC STRUCTURE OF C PROGRAMS
C programs are essentially constructed in the following manner, as a number of well defined sections.
/* HEADER SECTION */
/* Contains name, author, revision number*/
/* INCLUDE SECTION */
/* contains #include statements */
/* CONSTANTS AND TYPES SECTION */
/* contains types and #defines */
/* GLOBAL VARIABLES SECTION */
/* any global variables declared here */
/* FUNCTIONS SECTION */
/* user defined functions */
/* main() SECTION */
int main()
{
}
A Simple Program
The following program is written in the C programming language.
#include <stdio.h>
main()
{
printf(“Programming in C is easy.\n”); }
INITIALISING DATA VARIABLES AT DECLARATION TIME
In C, variables may be initialized with a value when they are declared. Consider
the following declaration, which declares an integer variable count which is initialized to
10. int count = 10;
SIMPLE ASSIGNMENT OF VALUES TO VARIABLES
The = operator is used to assign values to data variables. Consider the following statement, which assigns the value 32 an integer variable count, and the letter A to the character variable letter
count = 32;
letter = „A‟
Variable Formatters
%d decimal integer
%c character
%s string or character array
%f float
%e double
HEADER FILES
Header files contain definitions of functions and variables which can be incorporated into any C program by using the pre-processor #include statement. Standard header files are provided with each compiler, and cover a range of areas, string handling, mathematical, data conversion, printing and reading of variables.
To use any of the standard functions, the appropriate header file should be
included. This is done at the beginning of the C source file. For example, to use the function printf() in a program, the line
#include <stdio.h> should be at the beginning of the source file, because the definition for printf() is found in the file stdio.h All header files have the extension .h and generally reside in the /include sub directory.
#include <stdio.h>
#include “mydecls.h”
The use of angle brackets <> informs the compiler to search the compilers include directory for the specified file. The use of the double quotes “” around the file name inform the compiler to search in the current directory for the specified file. 

No comments:

Post a Comment