C Programming Language - PDF
Document Details
Uploaded by Deleted User
Tags
Summary
This document details the fundamental language elements and tokens used in C programming. It covers topics like reserved words, identifiers, constants, and operators. The text provides definitions and rules related to C language structures and syntax for beginners to the language.
Full Transcript
Language Character Set and Tokens The basic building block in C programs are characters. It contains 92 characters. They are numeric digits, the 26 letters of the English alphabet, both lower and upper case, space and all other printable special characters that you se...
Language Character Set and Tokens The basic building block in C programs are characters. It contains 92 characters. They are numeric digits, the 26 letters of the English alphabet, both lower and upper case, space and all other printable special characters that you see on the standard English language keyboard with the exception of three: ` $ and @ Tokens -is a language element that can be used in forming higher-level constructs SIX KINDS of TOKENS a. Reserved words b. Identifiers c. Constants d. String literals e. Punctuators f. Operators Reserved Words - Are keywords that identify language entities such as statements, data types, and language elements attributes IDENTIFIERS Rules for Constructing Identifiers 1. Identifiers can consist of the capital letters A to Z, the lower , letters a to z, the digits 0 to 9 and the underscore _. 2. The first character must be a letter or an underscore. 3. There is virtually no length limitation. 4. There can be no embedded blank. 5. Reserved words cannot be used as identifiers. 6. Identifiers are case-sensitive. In choosing identifiers, you should conform to some style guidelines 1. Avoid excessively short and cryptic names such as x or wt. Instead, to add to the readability of your program, use reasonably descriptive names 2. Use underscore or capital letters to separate words in identifiers that consists of two or more words. CONSTANTS - Are entities that appear in the program code as fixed values 4 types of constants a. Integer b. Floating-point c. Character d. Enumeration Integer constants – are positive or negative whole numbers with no fractional part - Can be decimal (base 10), octal(base 8) or hexadecimal( base 16) - Commas are not allowed in integer constants Floating-point constants - Are positive or negative decimal numbers with an integer part, a decimal point and a fractional part C supports 3 types of floating-point constants a. Float b. Double c. Long double Character constants and escape sequence a. A character is enclosed in single quotation mark b. The single quotation marks serve to delimit a character constant. The representation ‘’‘ is ambiguous and hence illegal. c. The backslash is also called the escape character Escape sequence - the escape character along with any character that follows it. String literals (string constants) - A sequence of any number of characters surrounded by double quotation marks. Format control string - Part of the output statement and it controls the appearance of the output Format specifier (for output) - Converts the internal representation of data to readable characters Punctuators (separators) - [ ] ( ) { } , ; :... * # - Used to delimit various syntactical units Operators Tokens that results in some kind of computation or action when applied to variables or other elements in an expression STATEMENT - Is a specification of an action to be taken by the computer as the program executes Compound statement – is a list of statements enclosed in braces { } Expression – is a syntactically correct and meaningful combination of operation and operand. Expression statement - is any expression followed by a semicolon. The Structure of a C Program a. Components b. Program comments d. Preprocessor directives e. Type declaration f. Named constants g. Statements h. Function declarations (prototypes) i. Function definitions j. Function calls Program Comments - Explanations or annotations that are included in a program for documentation and clarification purposes a. Describe the purpose of a program, function or statement b. Completely ignored by the compiler during compilation and they have no effect on program execution Preprocessor Directives (compiler directive) - Lines that begin with a pound sign # are not C language statements. They are preprocessor directives - Is an instruction to the preprocessor Functions of preprocessor - named file inclusion in a source program Conditional compilation - Macro substitution - A named inclusion is concerned with adding the content of a header file to a source program file HEADER file (include file) - A file that contains some kind of appropriate C code 2 Kinds of Header Files Standard header file - Supplied by the vendor of the C compiler system - Contain standard interfere information expressed in C Programmer-defined header file - Is a C code written by the programmer and stored in some directory - Ex. #include “d:header1.h” Basic Parts of a Turbo C Programming Header Files – these files provide function prototype declarations for library functions. The most commonly used headers or libraries are as follows: 1. conio.h – declares various functions used in calling the Disk Operating System (DOS) console Input/Output (I/0) routines. 2. dos.h – defines various constants and gives declarations needed for DOS and 8086 specific calls. 3. float.h – contains parameters for floating-point routines. 4. graphics.h – declares prototypes for the graphics functions. 5. math.h – declares prototypes for the math functions; also defines the macro HUGE-Val, and declares the exception structure used by the matherr and _matherr routines. 6. mem.h – declares the memory manipulation functions (many of these are also defined in string.h) 7. process.h – contains structures and declarations for spawn… and exec… functions. 8. stdio.h – defines types and macros needed for the standard I/0. 9. stdlib.h – declares several commonly used routines: conversion routines, search/sort routines, and other miscellany. 10. string.h – declares several string manipulation and memory manipulation routines. #Include Directory The #include preprocessor directive instructs the compiler to include another source file with one that has the #include directive in it. The source file to be read must be enclosed between double quotes or angle brackets. Main Body This refers to the set of statements in a computer program at which the execution of the program begins and from which the execution branches to the subroutines of the program. Most programming languages require programs to have a main body. Execution begins with the first statement in the main body; it usually ends when the last statement in the main body has finished executing, although it can end earlier. The main body is also a part of the program containing the master sequence of instructions unlike subroutines, procedures, and functions that the main program calls. Main Function This is the main body of a program that performs the principal function of a program over and over until termination is somehow signaled. In event-driven programs, this loop checks for events received from the operating system and handles them appropriately.