C Programing Language Tutorial


What is C language ?

  • C is a general-purpose, imperative computer programming language.
  • It supports 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.

Develop By

  • C was originally developed by Dennis Ritchie between 1969 and 1973 at AT&T Bell Labs and used to (re-)implement the Unix operating system.
  • 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.
  • C has been standardized by the American National Standards Institute (ANSI) since 1989 (see ANSI C) and subsequently by the International Organization for Standardization (ISO).

Key Points

  • Many later languages have borrowed directly or indirectly from C, including C++, D, Go, Rust, Java, JavaScript, Limbo, LPC, C#, Objective-C, Perl, PHP, Python, Verilog (hardware description language), and Unix's C shell.
  • These languages have drawn many of their control structures and other basic features from C, usually with overall syntactical similarity to C. It is also used as an intermediate language for other languages, and for building standard libraries and runtime systems for higher-level languages, such as CPython.

The C language also exhibits the following characteristics

  • There is a small, fixed number of keywords, including a full set of flow of control primitives: for, if/else, while, switch, and do/while. There is one namespace, and user-defined names are not distinguished from keywords by any kind of sigil.
  • There are a large number of arithmetical and logical operators, such as +, +=, ++, &, ~, etc.
  • More than one assignment may be performed in a single statement.
  • Function return values can be ignored when not needed.
  • Typing is static, but weakly enforced: all data has a type, but implicit conversions can be performed; for instance, characters can be used as integers.
  • Declaration syntax mimics usage context. C has no "define" keyword; instead, a statement beginning with the name of a type is taken as a declaration. There is no "function" keyword; instead, a function is indicated by the parentheses of an argument list.
  • User-defined (typedef) and compound types are possible.
  • Heterogeneous aggregate data types (struct) allow related data elements to be accessed and assigned as a unit.
  • Array indexing is a secondary notation, defined in terms of pointer arithmetic. Unlike structs, arrays are not first-class objects; they cannot be assigned or compared using single built-in operators. There is no "array" keyword, in use or definition; instead, square brackets indicate arrays syntactically, e.g. month.
  • Enumerated types are possible with the enum keyword. They are not tagged, and are freely interconvertible with integers.
  • Strings are not a separate data type, but are conventionally implemented as null-terminated arrays of characters.
  • Low-level access to computer memory is possible by converting machine addresses to typed pointers.
  • Procedures (subroutines not returning values) are a special case of function, with an untyped return type void.
  • Functions may not be defined within the lexical scope of other functions.
  • Function and data pointers permit ad hoc run-time polymorphism.
  • A preprocessor performs macro definition, source code file inclusion, and conditional compilation.
  • There is a basic form of modularity: files can be compiled separately and linked together, with control over which functions and data objects are visible to other files via static and extern attributes.
  • Complex functionality such as I/O, string manipulation, and mathematical functions are consistently delegated to library routines.

"Hello, world" example

            #include

           int main(void)
           {
                       printf("hello, world\n");
            }

  • The first line of the program contains a preprocessing directive, indicated by #include. This causes the compiler to replace that line with the entire text of the stdio.h standard header, which contains declarations for standard input and output functions such as printf. The angle brackets surrounding stdio.h indicate that stdio.h is located using a search strategy that prefers headers in the compiler's include path to other headers having the same name; double quotes are used to include local or project-specific header files.
  • The next line indicates that a function named main is being defined. The main function serves a special purpose in C programs; the run-time environment calls the main function to begin program execution. The type specifier int indicates that the value that is returned to the invoker (in this case the run-time environment) as a result of evaluating the main function, is an integer. The keyword void as a parameter list indicates that this function takes no arguments.
  • The opening curly brace indicates the beginning of the definition of the main function.
  • The next line calls (diverts execution to) a function named printf, which is supplied from a system library. In this call, the printf function is passed (provided with) a single argument, the address of the first character in the string literal "hello, world\n". The string literal is an unnamed array with elements of type char, set up automatically by the compiler with a final 0-valued character to mark the end of the array (printf needs to know this). The \n is an escape sequence that C translates to a newline character, which on output signifies the end of the current line. The return value of the printf function is of type int, but it is silently discarded since it is not used. (A more careful program might test the return value to determine whether or not the printf function succeeded.) The semicolon ; terminates the statement.
  • The closing curly brace indicates the end of the code for the main function. According to the C99 specification and newer, main function will implicitly return a status of 0 upon reaching the } that terminates the function. This is interpreted by the run-time system as an exit code indicating successful execution.

Tutorial

Video Tutorial

  • Thenewboston

  • iTzAdam5X

Comments