C
- Appeared in:
- 1969-1973
- Influenced by:
- Influenced:
- Paradigm:
- Typing discipline:
- File extensions:
- .c, .h
- Dialects:
- Versions and implementations (Collapse all | Expand all):
C is a standardized imperative (procedural) general-purpose computer programming language.
It was developed in early 1970s by Dennis Ritchie at the Bell Telephone Laboratories as a successor of B.
In 1978 Kernighan and Ritchie published “The C Programming Language” book which served as an informal language standard known as K&R C.
The first official standard ANSI X3.159-1989 was ratified in 1989 and was adopted as ISO/IEC 9899:1990 the year after that; it is known as ANSI C. Next standards were ISO 9899:1999 (C99) and ISO/IEC 9899:2011 (C11).
The language was designed to be compiled using a relatively straightforward compiler, to provide low-level access to memory, to provide language constructs that map efficiently to machine instructions, and to require minimal run-time support. C was therefore useful for many applications that had formerly been coded in assembly language. Eventually it became the most popular language for system programming.
Despite its low-level capabilities, the language was designed to encourage machine-independent programming. A standards-compliant and portably written C program can be compiled for a very wide variety of computer platforms and operating systems with little or no change to its source code. The language has become available on a very wide range of platforms, from embedded microcontrollers to supercomputers.
Language features:
- small number of keywords.
- most language functionality is separated from core language and moved out to libraries.
- a preprocessor is used for macro definition, source code files inclusion and conditional compilation.
- static weak typing: all data has fixed types but implicit casts are allowed.
- user-defined and compound data types are possible.
- low-level memory access is possible by converting machine addresses to pointers.
-
a procedure is a kind of function which returns a special type
void
. - source code files can be compiled independently and linked together.
Elements of syntax:
Inline comments | // (C99 only) |
---|---|
Nestable comments | #if 0 ... #endif |
Non-nestable comments | /* ... */ |
Case-sensitivity | yes |
Variable identifier regexp | [_a-zA-Z][_a-zA-Z0-9]* |
Variable assignment | = |
Variable declaration | type variable |
Variable declaration with assignment | type variable = value |
Block | { ... } |
Physical (shallow) equality | == |
Physical (shallow) inequality | != |
Comparison | < > <= >=, strcmp for strings |
Function definition | returntype f(type1 p1, type2 p2, ...) |
Function call | f(a, b, ...) |
Function call with no parameters | f() |
If - then | if (condition) ... |
If - then - else | if (condition) ... else ... OR ( condition ? ... : ... ) |
Loop forever | while (1) ... |
While condition do | while (condition) ... |
Do until condition | do ...while (!condition) |
For each value in a numeric range, 1 increment | for (int i = 1; i <= 10; i++) ... |
For each value in a numeric range, 1 decrement | for (int i = 10; i >= 1; i--) ... |
Examples:
Factorial:
Example for versions gcc 3.4.5, gcc 3.4.5 (Objective-C), gcc 4.2.4, tcc 0.9.25This example uses recursive factorial definition. Note that 13! and larger causes an overflow, so the last lines of the output look like this:
13! = 1932053504
14! = 1278945280
15! = 2004310016
16! = 2004189184
#include <stdio.h>
unsigned long long factorial(unsigned long long n)
{
if (n == 0) {
return 1;
} else {
return n * factorial (n - 1);
}
}
int main(void)
{
int n;
for (n = 0; n <= 16; n++) {
printf("%i! = %lld\n", n, factorial(n));
}
return 0;
}
CamelCase:
Example for versions gcc 3.4.5, gcc 4.2.4, tcc 0.9.25This example is based on character-wise string processing. fgets
here reads at most 99 characters into the string, and stops when it finds end-of-string character, so a long line might be split. C doesn’t provide boolean data type, so it has to be simulated using integer variable.
#include <stdio.h>
void main() {
char text[100],cc[100];
fgets(text, sizeof text, stdin);
int i,j=0,lastSpace=1;
for (i=0; text[i]!='\0'; i++)
if (text[i]>='A' && text[i]<='Z' || text[i]>='a' && text[i]<='z')
{ if (lastSpace>0)
cc[j] = toupper(text[i]);
else
cc[j] = tolower(text[i]);
j++;
lastSpace = 0;
}
else
lastSpace = 1;
cc[j]='\0';
printf("%s\n",cc);
}
Quadratic equation:
Example for versions gcc 4.2.4This example requires C99, since it uses complex
type introduced in it.
#include <stdio.h>
#include <complex.h>
#include <math.h>
void print(int ind, double complex x) {
printf("x%d = ", ind);
if (fabs(cimag(x)) < 1e-6)
printf("%f\n", creal(x));
else printf("(%f, %f)\n", creal(x), cimag(x));
}
int main() {
double A, B, C;
double D;
printf("A = ");
scanf("%lf", &A);
if (fabs(A)<1E-3) {
printf("Not a quadratic equation\n");
return 1;
}
printf("B = ");
scanf("%lf", &B);
printf("C = ");
scanf("%lf", &C);
A *= 2;
D = B*B-A*C*2.0;
if (fabs(D)<1E-3)
printf("x = %f", creal(-B/A));
else {
print(1, (-B+csqrt(D))/A);
print(2, (-B-csqrt(D))/A);
}
return 0;
}
Quadratic equation:
Example for versions Borland C++ Builder 6, Microsoft Visual C++ 9 (2008), g++ 3.4.5, gcc 3.4.5, gcc 3.4.5 (Objective-C), gcc 4.2.4, tcc 0.9.25This example works both for C and C++, as well as for Objective-C which is superset of C.
#include <math.h>
#include <stdio.h>
int main()
{
int A, B, C;
double D;
printf("A = ");
scanf("%d", &A);
if (A == 0) {
printf("Not a quadratic equation.\n");
return 0;
}
printf("B = ");
scanf("%d", &B);
printf("C = ");
scanf("%d", &C);
D = B * B - 4 * A * C;
if (D == 0) {
printf("x = %f\n", -B / 2.0 / A);
return 0;
}
if (D > 0) {
printf("x1 = %f\nx2 = %f\n",
(-B + sqrt(D)) / 2.0 / A, (-B - sqrt(D))/ 2.0 / A);
} else {
printf("x1 = (%f, %f)\nx2 = (%f, %f)\n",
-B / 2.0 / A, sqrt(-D) / 2.0 / A, -B / 2.0 / A, -sqrt(-D) / 2.0 /A);
}
return 0;
}
Hello, World!:
Example for versions Borland C++ Builder 6, Turbo C++ 1.01, g++ 3.4.5, gcc 3.4.5, gcc 3.4.5 (Objective-C), gcc 4.2.4, tcc 0.9.25#include <stdio.h>
int main()
{
printf("Hello, World!\n");
return 0;
}
Fibonacci numbers:
Example for versions gcc 3.4.5, gcc 3.4.5 (Objective-C), tcc 0.9.25This example uses recursive definition of Fibonacci numbers. Note the difference from C++ example: loop counter must be declared outside of the loop, and printf
is used for output instead of std::cout
.
#include <stdio.h>
int fibonacci(int n)
{
return ( n<=2 ? 1 : fibonacci(n-1) + fibonacci(n-2) );
}
int main(void)
{
int n;
for (n=1; n<=16; n++)
printf("%d, ", fibonacci(n));
printf("...\n");
return 0;
}
Comments
]]>blog comments powered by Disqus
]]>