C language is rich in data types. The fundamental data types which can be used in C are integer data types, character data types and floating point data types. Integer data type is used to represent an integer-valued number. Character data type represents one single alphabet or a single-digit integer. Each character type has an equivalent integer representation. Integer and character data types can be augmented by the use of the data type qualifiers, short, long, signed and unsigned. The range of values which belong to each category varies with respect to the qualifiers and so, the memory requirement.
Floating point data types are used to represent real numbers. Basic floating point data type offers six digits of precision. Double precision data type can be used to achieve a precision of 14 digits. To extend the precision further more, we may use the extended double precision floating point data type. Each of the above discussed data types and their corresponding keywords are given in the table below.
Floating point data types are used to represent real numbers. Basic floating point data type offers six digits of precision. Double precision data type can be used to achieve a precision of 14 digits. To extend the precision further more, we may use the extended double precision floating point data type. Each of the above discussed data types and their corresponding keywords are given in the table below.
This is a summary of the library defined by the ANSI standard. The standard library is not part of the C language proper, but an environment that supports standard C will provide the function declarations and type and macro definitions of this library.
The functions, types and macros of the standard library are declared in standard headers:
A header can be accessed by
#include
Headers may be included in any order and any number of times. A header must be included outside of any external declaration or definition and before any use of anything it declares. A header need not be a source file. External identifiers that begin with an underscore are reserved for use by the library, as are all other identifiers that begin with an underscore and an uppercase letter or another underscore.
Input and Output:
The input and output functions, types, and macros defined in represent nearly one third of the library. . When a program begins execution, the three streams stdin, stdout, and stderr are already open.
The functions, types and macros of the standard library are declared in standard headers:
A header can be accessed by
#include
Headers may be included in any order and any number of times. A header must be included outside of any external declaration or definition and before any use of anything it declares. A header need not be a source file. External identifiers that begin with an underscore are reserved for use by the library, as are all other identifiers that begin with an underscore and an uppercase letter or another underscore.
Input and Output:
The input and output functions, types, and macros defined in
File Operations
The following functions deal with operations on files.
fopen opens the named file, and returns a stream, or NULL if the attempt fails. Legal values for mode include:
"r" open text file for reading
"w" create text file for writing; discard previous contents if any
"a" append; open or create text file for writing at end of file
"r+" open text file for update (i.e., reading and writing)
"w+" create text file for update, discard previous contents if any
"a+" append; open or create text file for update, writing at end
freopen opens the file with the specified mode and associates the stream with it. It returns stream, or NULL if an error occurs. freopen is normally used to change the files associated with stdin, stdout, or stderr.
int fflush(FILE *stream)
On an output stream, fflush causes any buffered but unwritten data to be written; on an input stream, the effect is undefined.
int fclose(FILE *stream)
fclose flushes any unwritten data for stream, discards any unread buffered input, frees any automatically allocated buffer, then closes the stream. It returns EOF if any errors occurred, and zero otherwise.
int remove(const char *filename)
remove removes the named file, so that a subsequent attempt to open it will fail. It returns non-zero if the attempt fails.
int rename(const char *oldname, const char *newname)
rename changes the name of a file; it returns non-zero if the attempt fails.
Formatted Output
The printf functions provide formatted output conversion.
int fprintf(FILE *stream, const char *format, ...)
fprintf converts and writes output to stream under the control of format.
The printf functions provide formatted output conversion.
int fprintf(FILE *stream, const char *format, ...)
fprintf converts and writes output to stream under the control of format.
Formatted Input
The scanf function deals with formatted input conversion.
int fscanf(FILE *stream, const char *format, ...)
fscanf reads from stream under control of format, and assigns converted values through subsequent arguments, each of which must be a pointer.
Character Input and Output Functions
fgetc returns the next character of stream as an unsigned char (converted to an int), or EOF if end of file or error occurs.
fgets reads at most the next n-1 characters into the array s, stopping if a newline is encountered; the newline is included in the array, which is terminated by '\0'
fputc writes the character c (converted to an unsigend char) on stream. It returns the character written, or EOF for error.
fputs writes the string s (which need not contain \n) on stream; it returns nonnegative, or EOF for an error.
getc is equivalent to fgetc except that if it is a macro, it may evaluate stream more than once.
getc is equivalent to fgetc except that if it is a macro, it may evaluate stream more than once.
Direct Input and Output Functions
fread reads from stream into the array ptr at most nobj objects of size size. fread returns the number of objects read; this may be less than the number requested.
feof and ferror must be used to determine status.
fwrite writes, from the array ptr, nobj objects of size size on stream. It returns the number of objects written, which is less than nobj on error.
fwrite writes, from the array ptr, nobj objects of size size on stream. It returns the number of objects written, which is less than nobj on error.
File Positioning Functions
fseek sets the file position for stream; a subsequent read or write will access data beginning at the new position.
ftell returns the current file position for stream, or -1 on error.
void rewind(FILE *stream)
rewind(fp) is equivalent to fseek(fp, 0L, SEEK_SET); clearerr(fp).
fgetpos records the current position in stream in *ptr, for subsequent use by fsetpos.
Character Class Tests:
The header
isalnum(c) isalpha(c) or isdigit(c) is true
isalpha(c) isupper(c) or islower(c) is true
iscntrl(c) control character
isdigit(c) decimal digit
isgraph(c) printing character except space
islower(c) lower-case letter
isprint(c) printing character including space
ispunct(c) printing character except space or letter or digit
isspace(c) space, formfeed, newline, carriage return, tab, vertical tab
isupper(c) upper-case letter
isxdigit(c) hexadecimal digit
islower(c) lower-case letter
isprint(c) printing character including space
ispunct(c) printing character except space or letter or digit
isspace(c) space, formfeed, newline, carriage return, tab, vertical tab
isupper(c) upper-case letter
isxdigit(c) hexadecimal digit
In addition, there are two functions that convert the case of letters:
int tolower(c) convert c to lower case
int toupper(c) convert c to upper case
If c is an upper-case letter, tolower(c) returns the corresponding lower-case letter,
toupper(c) returns the corresponding upper-case letter; otherwise it returns c.
String Functions:
These are the groups of string functions defined in the header
char *strcpy(s,ct) copy string ct to string s, including '\0'; return s.
char *strcat(s,ct) concatenate string ct to end of string s; return s.
int strcmp(cs,ct) compare string cs to string ct, return <0 cs="="ct,">0
char *strchr(cs,c) return pointer to first occurrence of c in cs or NULL if not present.
strspn(cs,ct) return length of prefix of cs consisting of characters in ct.
size_t
char *strstr(cs,ct) return pointer to first occurrence of string ct in cs, or NULL if not present.
strlen(cs) return length of cs.
Mathematical Functions:
The header
In the following table, x and y are of type double, n is an int, and all functions return double. Angles for trigonometric functions are expressed in radians.
sin(x) sine of x
cos(x) cosine of x
tan(x) tangent of x
asin(x) sin-1(x) in range [-pi/2,pi/2], x in [-1,1].
acos(x) cos-1(x) in range [0,pi], x in [-1,1].
atan(x) tan-1(x) in range [-pi/2,pi/2].
atan2(y,x) tan-1(y/x) in range [-pi,pi].
sinh(x) hyperbolic sine of x
cosh(x) hyperbolic cosine of x
tanh(x) hyperbolic tangent of x
exp(x) exponential function ex
log(x) natural logarithm ln(x), x>0.
log10(x) base 10 logarithm log10(x), x>0.
pow(x,y) xy. A domain error occurs if x=0 and y<=0, or if x<0 and y is not an integer.
sqrt(x) sqare root of x, x>=0.
ceil(x) smallest integer not less than x, as a double.
floor(x) largest integer not greater than x, as a double.
fabs(x) absolute value x
ldexp(x,n) x*2n
frexp(x, int *ip)
splits x into a normalized fraction in the interval [1/2,1) which is returned, and a power of 2, which is stored in *exp. If x is zero, both parts of the result are zero.
modf(x, double *ip)
splits x into integral and fractional parts, each with the same sign as x. It stores the integral part in *ip, and returns the fractional part.
Utility Functions:
The header declares functions for number conversion, storage allocation, and similar tasks.
void *calloc(size_t nobj, size_t size)
calloc returns a pointer to space for an array of nobj objects, each of size size, or NULL if the request cannot be satisfied. The space is initialized to zero bytes.
void *malloc(size_t size)
malloc returns a pointer to space for an object of size size, or NULL if the request cannot be satisfied. The space is uninitialized.
Void *realloc(void *p, size_t size)
realloc changes the size of the object pointed to by p to size. The contents will be unchanged up to the minimum of the old and new sizes.
void free(void *p)
free deallocates the space pointed to by p; it does nothing if p is NULL. p must be a pointer to space previously allocated by calloc, malloc, or realloc.
void abort(void)
abort causes the program to terminate abnormally, as if by raise(SIGABRT).
The header
void *calloc(size_t nobj, size_t size)
calloc returns a pointer to space for an array of nobj objects, each of size size, or NULL if the request cannot be satisfied. The space is initialized to zero bytes.
void *malloc(size_t size)
malloc returns a pointer to space for an object of size size, or NULL if the request cannot be satisfied. The space is uninitialized.
Void *realloc(void *p, size_t size)
realloc changes the size of the object pointed to by p to size. The contents will be unchanged up to the minimum of the old and new sizes.
void free(void *p)
free deallocates the space pointed to by p; it does nothing if p is NULL. p must be a pointer to space previously allocated by calloc, malloc, or realloc.
void abort(void)
abort causes the program to terminate abnormally, as if by raise(SIGABRT).
C is a powerful, flexible language that provides fast program execution and imposes few constraints on the programmer. It allows low level access to information and commands while still retaining the portability and syntax of a high level language. These qualities make it a useful language for both systems programming and general purpose programs.
C's power and fast program execution come from it's ability to access low level commands, similar to assembly language, but with high level syntax.
C's power and fast program execution come from it's ability to access low level commands, similar to assembly language, but with high level syntax.
It's flexibility comes from the many ways the programmer has to accomplish the same tasks. C includes bitwise operators along with powerful pointer manipulation capabilities. C imposes few constraints on the programmer. The main area this shows up is in C's lack of type checking. This can be a powerful advantage to an experienced programmer but a dangerous disadvantage to a novice.
Another strong point of C is it's use of modularity. Sections of code can be stored in libraries for re-use in future programs. This concept of modularity also helps with C's portability and execution speed. The core C language leaves out many features included in the core of other languages. These functions are instead stored in the C Standard Library where they can be called on when needed.. An example of this concept would be C's lack of built in I/O capabilities. I/O functions tend to slow down program execution and also be machine independent when running optimally. For these reasons, they are stored in a library separately from the C language and only included when necessary.
C is often called a middle-level computer language. This does not mean that C is less powerful, harder to use, or less developed than a high-level language such as Pascal; nor does it imply that C is similar to, or presents the problems associated with, assembly language. The definition of C as a middle-level language means that it combines elements of high-level languages with the functionalism of assembly language. As a middle-level language, C allows the manipulation of bits, bytes, and addresses—the basic elements with which the computer functions. Despite this fact, C code is surprisingly portable. Portability means that it is possible to adapt software written for one type of computer to another. For example, if a program written for one type of CPU can be moved easily to another, that program is portable. All high-level programming languages support the concept of data types. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common data types are integer, character, and real. Although C has several basic built-in data types, it is not a strongly typed language like Pascal or Ada. In fact, C will allow almost all type conversions. For example, character and integer types may be freely intermixed in most expressions. Traditionally C performs no run-time error checking such as array-boundary checking or argument-type compatibility checking. These checks are the responsibility of the programmer. A special feature of C is that it allows the direct manipulation of bits, bytes, words, and pointers. This makes it well suited for system-level programming, where these operations are common. Another important aspect of C is that it has only 32 keywords (5 more were added by C99, but these are not supported by C++), which are the commands that make up the C language. This is far fewer than most other languages.
C was developed at Bell Laboratories in 1972 by Dennis Ritchie. Many of its principles and ideas were taken from the earlier language B and B's earlier ancestors BCPL and CPL. CPL ( Combined Programming Language ) was developed with the purpose of creating a language that was capable of both high level, machine independent programming and would still allow the programmer to control the behavior of individual bits of information. The one major drawback of CPL was that it was too large for use in many applications. In 1967, BCPL ( Basic CPL ) was created as a scaled down version of CPL while still retaining its basic features. In 1970, Ken Thompson, while working at Bell Labs, took this process further by developing the B language. B was a scaled down version of BCPL written specifically for use in systems programming. Finally in 1972, a co-worker of Ken Thompson, Dennis Ritchie, returned some of the generality found in BCPL to the B language in the process of developing the language we now know as C.
C's power and flexibility soon became apparent. Because of this, the Unix operating system which was originally written in assembly language, was almost immediately re-written in C ( only the assembly language code needed to "bootstrap" the C code was kept ). During the rest of the 1970's, C spread throughout many colleges and universities because of it's close ties to Unix and the availability of C compilers. Soon, many different organizations began using their own versions of C causing compatibility problems. In response to this in 1983, the American National Standards Institute ( ANSI ) formed a committee to establish a standard definition of C which became known as ANSI Standard C. The standardization process took six years. The ANSI C standard was finally adopted in December 1989, with the first copies becoming available in early 1990. The standard was also adopted by ISO (International Standards Organization), and the resulting standard was typically referred to as ANSI/ISO Standard C, or simply ANSI/ISO C. Today C is in widespread use with a rich standard library of functions.
C's power and flexibility soon became apparent. Because of this, the Unix operating system which was originally written in assembly language, was almost immediately re-written in C ( only the assembly language code needed to "bootstrap" the C code was kept ). During the rest of the 1970's, C spread throughout many colleges and universities because of it's close ties to Unix and the availability of C compilers. Soon, many different organizations began using their own versions of C causing compatibility problems. In response to this in 1983, the American National Standards Institute ( ANSI ) formed a committee to establish a standard definition of C which became known as ANSI Standard C. The standardization process took six years. The ANSI C standard was finally adopted in December 1989, with the first copies becoming available in early 1990. The standard was also adopted by ISO (International Standards Organization), and the resulting standard was typically referred to as ANSI/ISO Standard C, or simply ANSI/ISO C. Today C is in widespread use with a rich standard library of functions.
Subscribe to:
Posts (Atom)