C Language Elements
Preprocessor Directive
Preoprocessor directive are commands that give instruction to C preprocessor, whose jobs it is to modify the text of a C program before it compiled. A preprocessor directive begin with symbol (#) as it first nonblank character. Example: #include, or #defineMain Function
Main function heading example:int
main(void)
Marks the beginning of the main function where program execution begins. The remaining lines of the program form the body of the function which is enclosed in braces {, } .
Function body has two parts: declarations and executable statements. The declarations tell the compiler what memory cells are needed in the function. To create this part of the function, the programmer uses the problem data requirements identified during problem analysis. The executable statements (derived from the algorithm) are translated into machine language and executed.
Reserved words
Some of different words classified as reserved words , identifiers from standard libraries, and names for memory cells.Reserved words |
Standard Identifier
Standard Identifier are identifiers that come in two varieties: standard and user-defined. Like reserved words, standard identifiers have special meaning in C. Example: printf and scanf are used in standard input/output library.Variable Declaration and Data types
Variable Declaration
The memory cells used for storing a program’s input data and its computational results are called variables because the values stored in variables can change (and usually do) as the program executes. The variable declarations in a C program communicate to the C compiler the names of all variables used in a program.Data Types
A data type is a set of values and a set of operations on those values. Knowledge of the data type of an item (a variable or value) enables the C compiler to correctly specify operations on that item.Various kinds of data types in C:
- Data type int: containing integer, but not all integers can be represented by data type int, because of the limited size of the memory cells in C. int must include at least the values −32767 through 32767 .
- Data type double: A real number has an integral part and a fractional part that are separated by a decimal point.
- Data type char: represents an individual character value—a letter, a digit, or a special symbol.
Executable Statements
The executable statements follow the declarations in a function. They are the C statements used to write or code the algorithm and its refinements. The C compiler translates the executable statements into machine language; the computer executes the machine language version of these statements when we run the program.Programs in memory
Look what computer memory looks like before and after miles-to-kilometers conversion program executes.Picture above shows the program loaded into memory and the program memory area before the program executes. The question marks in memory cells miles and kms indicate that the values of these cells are undefined before program execution begins. During program execution, the data value 10.00 is copied from the input device into the variable miles . After the program executes, the variables are defined as shown in (b) picture.
Input/Output Operations and Functions
Data transfer from the outside world into memory is called an input operation. As it executes, a program performs computations and stores the results in memory. These program results can be displayed to the program user by an output operation . All input/output operations in C are performed by special program units called input/output functions.The printf Function
The printf function is to display a line of program output. A function call consists of two parts: the function name and the function arguments, enclosed in parentheses. The arguments for printf consist of a format string (in quotes) and a print list (the variable kms ). The function call below display the line:The scanf Function
The scanf function is to copy data into a variable.Syntax: scanf( format string , input list );
Example: scanf("%c%d", &first_initial, &age);
The return Statement
return statement is the last statement in the main functionreturn (0);
Transfers control from your program to the operating system. The value in parentheses, 0, is considered the result of function main ’s execution, and it indicates that your program executed without error.
General Form of a C Program
Every programs start with a preprocessor directives serves to provide information about the function of the standard library and constant definitons required program.Here is example of general form of C program:
preprocessor directives
main function heading
{
declarations
executable statements
}
Arithemetic Expressions
To solve most programming problems, you will need to write arithmetic expressions that manipulate type int and double data. This section describes the operators used in arithmetic expressions, and rules for writing and evaluating the expressions.Operator / and %
The division operator ( / ) computes the integral part of the result of dividing its first operand by its second.The remainder operator ( % ) returns the integer remainder of the result of dividing its first operand by its second.
Tidak ada komentar:
Posting Komentar