INTRODUCTION TO C LANGUAGE
C was brought to us by Dennis Ritchie and Brian at Bell Labs. AT&T people had themselves this Operating System called UNIX that needed a programming language. They made it, and since the previous version was called B, they decided to call it C for obvious reasons.
There was never an A programming language. The B in B stood for Bell. C is a computer programming language. That means that you can use C to create lists of instructions for a computer to follow. C is one of thousands of programming languages currently in use.
Edukta |
C has been around for several decades and
has won widespread acceptance because it gives programmers maximum control and efficiency. C
is what is called a compiled language. This means that once you write your C program, you must
run it through a C compiler to turn your program into an executable that the computer can run
(execute).
The C program is the human-readable form, while the executablethat comes out of the compiler is the machine-readable and executable form. What this means is that to write and run a C program, you must have access to a C compiler.
If you are using a UNIX machine (for example, if you are writing CGI scripts in C on your host's UNIX computer, or if you are a student working on a lab's UNIX machine), the C compiler is available for free. It is called either "cc" or "gcc" and is available on the command line.
CHARACTERISTICS OF ‘C’ LANGUAGE
Modularity: Ability to breakdown a large module into manageable sub modules called as modularity that is an important feature of structured programming languages.
Advantages:
1.Projects can be completed in time.
2. Debugging will be easier and faster.
Portability:
The ability to port i.e. to install the software in different platform is called portability. Highest degree of portability: ‘C’ language offers highest degree of portability i.e., percentage of changes to be made to the sources code is at minimum when the software is to be loaded in another platform.
Extendability:
Ability to extend the existing software by adding new features is called as extendability.
SPEED:
‘C’ is also called as middle level language because programs written in ‘c’ language run at the speeds matching to that of the same programs written in assembly language so ‘c’ language has both the merits of high level and middle level language and because if this feature it is mainly used in developing system software
AREAS OF APPLICATIONS:
The C programming language is used in many different areas of application, but the most
prolific area is UNIX operating system applications.
The C language is also used in
computer games:
• UNIX operating system
• Computer games
Getting started with C Language
C is a general-purpose, imperative computer programming language, supporting structured
programming, lexical variable scope and recursion, while a static type system prevents many
unintended operations.
By design, C provides constructs that map efficiently to typical machine
instructions, and therefore it has found lasting use in applications that had formerly been coded in
assembly language, including operating systems, as well as various application software for
computers ranging from supercomputers to embedded systems.
C was originally developed by Dennis Ritchie between 1969 and 1973 at Bell Labs and used to reimplement the Unix operating systems. It has since become one of the most widely used programming languages of all time, with C compilers from various vendors available for the majority of existing computer architectures and operating systems.
Common Compilers
The process to compile a C program differs between compilers and operating systems. Most
operating systems ship without a compiler, so you will have to install one.
Some common
compilers choices are:
• GCC, the GNU Compiler Collection
• clang: a C language family front-end for LLVM
• MSVC, Microsoft Visual C/C++ build tools
Code style:
Because white space is insignificant in C (that is, it does not affect the operation of the code), programmers often use white space to make the code easier to read and comprehend, this is called the code style. It is a set of rules and guidelines used when writing the source code. It covers concerns such as how lines should be indented, whether spaces or tabs should be used, how braces should be placed, how spaces should be used around operators and brackets, how variables should be named and so forth.
Examples:
Hello World To create a simple C program which prints "Hello, World" on the screen, use a text editor to create a new file (e.g. hello.c — the file extension must be .c) containing the following source code:
#include<stdio.h>
int main(void)
{
puts("Hello, World");
return 0;
}
Stategies Involved:
#include<stdio.h> -This line tells the compiler to include the contents of the standard library header file stdio.h in the
program. Headers are usually files containing function declarations, macros and data types, and
you must include the header file before you use them. This line includes stdio.h so it can call the
function puts().
int main(void)-This line starts the definition of a function. It states the name of the function (main), the type and number of arguments it expects (void, meaning none), and the type of value that this function returns (int). Program execution starts in the main() function.
puts("Hello, World");-This line calls the puts() function to output text to standard output (the screen, by default), followed by a newline. The string to be output is included within the parentheses.
"Hello, World" is the string that will be written to the screen. In C, every string literal value must be inside the double quotes "…".
In C programs, every statement needs to be terminated by a semi-colon (i.e. ;).
return 0;
When we defined main(), we declared it as a function returning an int, meaning it needs to return an integer. In this example, we are returning the integer value 0, which is used to indicate that the program exited successfully. After the return 0; statement, the execution process will terminate.
Compile using GCC:
GCC (GNU Compiler Collection) is a widely used C compiler. To use it, open a terminal, use the command line to navigate to the source file's location and then run: gcc hello.c -o hello If no errors are found in the the source code (hello.c), the compiler will create a binary file, the name of which is given by the argument to the -o command line option (hello). This is the final executable file. We can also use the warning options -Wall -Wextra -Werror, that help to identify problems that can cause the program to fail or produce unexpected results. They are not necessary for this simple program but this is way of adding them: gcc -Wall -Wextra -Werror -o hello hello.c
character classification &
conversion
Classifying characters read from a stream
#include <ctype.h>
#include <stdio.h>
typedef struct {
size_t space;
size_t alnum;
size_t punct;
} chartypes;
chartypes classify(FILE *f)
{
chartypes types = { 0, 0, 0 };
int ch;
while ((ch = fgetc(f)) != EOF) {
types.space += !!isspace(ch);
types.alnum += !!isalnum(ch);
types.punct += !!ispunct(ch);
}
return types;
}