User:D!p.IT/C
This page will be used to guide me while searching/learning C programing through wikipedia. All its purpose is to study C and nothing else.
If you want to help me to understand the C programing you are always welcome. To share your opinion please write it on the discussion page. Also if you want other things to say except C please say it on the main talk page of my user page.
please do not write or delete any thing here that may disturb anyone.
About C
[edit]main article C (programming language)
C is an imperative (procedural) systems implementation language. It 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.its source-code expressions can be translated in a straightforward manner to primitive machine operations in the executable code.
C also exhibits the following more specific characteristics:
- Variables may be hidden in nested blocks
- Partially weak typing; for instance, characters can be used as integers
- Low-level access to computer memory by converting machine addresses to typed pointers
- Function and data pointers supporting ad hoc run-time polymorphism
- array indexing as a secondary notion, defined in terms of pointer arithmetic
- A preprocessor for macro definition, source code file inclusion, and conditional compilation
- Complex functionality such as I/O, string manipulation, and mathematical functions consistently delegated to library routines
- A relatively small set of reserved keywords
- A large number of compound operators, such as
+=
,-=
,*=
,++
, etc.
C's lexical structure resembles B more than ALGOL. For example:
- Compound statement blocks are delimited with
{ ... }
rather thanbegin ... end
=
is used for assignment (copying), like Fortran, rather than ALGOL's:=
==
is used to test for equality (rather than.EQ.
in Fortran, or=
in BASIC and ALGOL)- Logical "and" and "or" operators are represented with
&&
and||
, which do not evaluate the right operand if the result can be determined from the left alone (short-circuit evaluation), and are semantically distinct from the bit-wise operators&
and|
Some reasons for choosing C over interpreted languages are its speed, stability, and near-universal availability. In fact, the Windows OS is also a very very very large (millions of lines) computer program written in C. Linux has also been written in C.
Lessons to learn
[edit]what to learn one by one
- Introduction:
- Using a compiler:
- The form of a C program:
- Functions:
- Variables and declarations:
- Scope:
- Expressions and operators:
- Parameters:
- Pointers:
- Decisions:
- Loops:
- Preprocessor directives:
- Libraries:
- Arrays:
- Strings:
- Input and output:
- Putting a program together:
- Advanced operators:
- More data types:
- Data structures:
- Recursion:
- Style:
- Debugging:
- Example programs:
- A note from the original author:
- Reserved words in C:
- Precedence of operators:
- Special characters:
- Character conversion table:
- A word about goto:
- Answers to questions:
- Bibliography:
Basic structure of a c programme
[edit]Here is the traditional Hello World program written in C.
#include
[edit]Most C programs have #include statements, the most common being "#include <stdio.h>" What this statement really does is tell the compiler: "Hey! Before you compile my program, just copy and paste the contents of stdio.h where this statement is". This is used to include the contents of standard C libraries.
In our program we have this line which does just that:
#include <stdio.h>
Functions
[edit]In C, code is cut up into little "blocks" of code called functions. You can have a function called add_two_numbers() which would do just that. Don't worry about making functions for now though. Just know about the main() function:
int main(void)
{
This fragment of code marks the beginning of the main() function, the most important function in almost every C program. The main function is the function automatically run when the program starts executing. Any and all other functions must be called directly or indirectly from main() in order to be executed. The keyword "int" indicates that the return value of main() is an integer. The keyword void inside the parentheses indicates that main takes no arguments.
The main() function can also accept command line arguments, which you will be familiar with if you've used CLI systems such as Unix or DOS, or the Windows Command Prompt. In that case, main is declared as follows. Don't worry too much about this for now; it will become relevant later.
int main(int argc, char **argv)
{
printf
[edit]The printf() function is arguably the most used function in C. It is used to display formatted text to the standard output and is extremely powerful, even allowing you to make tables of data. But our use is very modest:
printf("Hello World!\n");
The heart of our program, this code displays "Hello World!" (followed by a newline character "\n") onto the standard output (usually the console). First, we call the function printf as such "printf();". Within the parentheses, we put the arguments to be passed to printf, in this case there is only one argument, the string "Hello World!\n". But keep in mind that some functions, like printf(), can take multiple arguments as needed.
Ending Functions
[edit]Good things must end, functions included. Functions that return a value to the caller have to do so by using a return followed by an expression that evaluates to the type that must be returned. A function that does not return a value can be terminated with a plain return;
statement. A closing brace terminates the definition of the function. return
statements can appear anywhere in the function definition.
return 0;
}
The end of our program! return 0;
returns the value 0 to whatever called the main function (in this case the command line). The value 0 indicates successful completion of the program. The value 0 ties in directly with the int
keyword in the declaration of main
. The closing brace signals the end of the main
function.
so it is
#include <stdio.h>
int main(void)
{
printf("hello, world\n");
return 0;
}
Basic output in C
[edit]Output is information that the program tells the user. The most basic way to do this is writing text to the console. The program below will demonstrate the writing of the text Hello world! to the console. Note that system passes its argument to the host environment to be executed by a command processor, it is up to the command processor how it deals with a command it does not understand.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
printf("Hello world!\n\n");
if ( system(NULL) )
/* The following is a DOS command, it will fail in UNIX */
system("PAUSE");
else
puts("No command processor is available");
return 0;
}
If you compile the above program and run it on an MS-DOS system, you will get this output:
Hello world! Press any key to continue...
C syntax
[edit]see C syntax
Escape sequence
[edit]Here is a table of most of the escape sequences.
Escape Sequence | Description |
---|---|
\' | Single quote |
\" | Double quote |
\\ | Backslash |
\nnn | Octal number (nnn)* |
\0 | Null character (really just the octal number zero) |
\a | Audible bell |
\b | Backspace |
\f | Formfeed |
\n | Newline |
\r | Carriage return |
\t | Horizontal tab |
\v | Vertical tab |
\xnnn | Hexadecimal number (nnn)* |
Terms to know
[edit]- Floating point