World Of Books Python Book Java Book C++ Book C Book Home
Introduction

C is a general-purpose programming language, developed in 1972, and still quite popular.
C is a general-purpose programming language, developed in 1972, and still quite popular.

C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

  • It is one of the most popular programming language in the world If you know C, you will have no problem to learn other popular programming languages such as Java, Python, C++, C#, etc, as the syntax is similar C is very fast, compared to other programming languages, like Java and Python C is very versatile; it can be used in both applications and technologi
  • C++ was developed as an extension of C, and both languages have almost the same syntax
  • The main diffference between C and C++ is that C++ support classes and objects, while C does not
What you should already know

This guide assumes you have the following basic background:

  • A text editor, like Notepad, to write C code
  • A compiler, like GCC, to translate the C code into a language that the computer will understand
  • Some programming experience. If you are new to programming, try one of the tutorials linked on the main page about C language.
Getstarted

An IDE (Integrated Development Environment) is used to edit AND compile the code. Popular IDE's include Code::Blocks, Eclipse, and Visual Studio. These are all free, and they can be used to both edit and debug C code.Note: Web-based IDE's can work as well, but functionality is limited. We will use Code::Blocks in our tutorial, which we believe is a good place to start. You can find the latest version of Codeblocks at http://www.codeblocks.org/. Download the mingw-setup.exe file, which will install the text editor with a compiler.

A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix. It was applied to re-implementing the kernel of the Unix operating system.[6] During the 1980s, C gradually gained popularity. It has become one of the most widely used programming languages,[7][8] with C compilers available for almost all modern computer architectures and operating systems. C has been standardized by ANSI since 1989 (ANSI C) and by the International Organization for Standardization (ISO).

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support. Despite its low-level capabilities, the language was designed to encourage cross-platform programming. A standards-compliant C program written with portability in mind can be compiled for a wide variety of computer platforms and operating systems with few changes to its source code.[9]

Hello world
To get started with writing C language, open the Turboc++ and write your first "Hello world" C code: #include int main() { printf("Hello World!"); return 0; } Select the code in the pad and hit Ctrl+f9 to watch it unfold in your browser!
Variables

Where type is one of C types (such as int), and variableName is the name of the variable (such as x or myName). The equal sign is used to assign a value to the variable.

int - stores integers (whole numbers), without decimals, such as 123 or -123 float - stores floating point numbers, with decimals, such as 19.99 or -19.99 char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes

You can use ISO 8859-1 or Unicode letters such as å and ü in identifiers. You can also use the Unicode escape sequences as characters in So, to create a variable that should store a number, look at the following example:

Declaring variables
Create a variable called myNum of type int and assign the value 15 to it:

With the keyword var. For example, int myNum = 15; This syntax can be used to declare both local and global variables.

You can also declare a variable without assigning the value, and assign the value later: int myNum = 15; // myNum is 15 myNum = 10; // Now myNum is 10Note: If you assign a new value to an existing variable, it will overwrite the previous value:

You learned from the output chapter that you can output values/print text with the printf() function: printf("Hello World!"); In many other programming languages (like Python, Java, and C++), you would normally use a print function to display the value of a variable. However, this is not possible in C:

int myNum = 15; printf(myNum); // Nothing happens
Variable scope

In C every variable defined in scope. You can define scope as the section or region of a program where a variable has its existence; moreover, that variable cannot be used or accessed beyond that region. A scope is a region of the program, and the scope of variables refers to the area of the program where the variables can be accessed after its declaration.

A local scope or block is collective program statements put in and declared within a function or block (a specific region enclosed with curly braces) and variables lying inside such blocks are termed as local variables. There's a provision for nested blocks also in C which means there can be a block or a function within another block or function but those variables cannot be accessed outside the block.

#include int main () { //local variable definition and initialization int x,y,z; //actual initialization x = 20; y = 30; z = x + y; printf ("value of x = %d, y = %d and z = %d\n", x, y, z); return 0; }

Variables that are declared within the function block and can be used only within the function is called local variables.

#include int main() { int my_num = 7; { int new_num = 10; } printf("new_num is %d",new_num); //this is line 9 return 0; }
Global variables

Global variables are defined outside a function or any specific block, in most of the case, on the top of the C program. These variables hold their values all through the end of the program and are accessible within any of the functions defined in your program.

Variables that are declared outside of a function block and can be accessed inside the function is called global variables.Any function can access variables defined within the global scope, i.e., its availability stays for the entire program after being declared.

#include int main() { printf("%d", value); return 0; } int value = 10;
Constants

Another thing about constant variables, is that it is considered good practice to declare them with uppercase. It is not required, but useful for code readability and common for C programmers:

const int BIRTHYEAR = 1980;

When you don't want others (or yourself) to override existing variable values, use the const keyword (this will declare the variable as "constant", which means unchangeable and read-only):.

The scope rules for constants are the same as those for let block scope variables. If the const keyword is omitted, the identifier is assumed to represent a variable.

const int myNum = 15; // myNum will always be 15 myNum = 10; // error: assignment of read-only variable 'myNum'

You should always declare the variable as constant when you have values that are unlikely to change:

const int minutesPerHour = 60; const float PI = 3.14;
Data types

The data types are:

  • Data types that are primitives:

    • The data type specifies the size and type of information the variable will store.
    • Boolean. true and false.
    • int 2 or 4 bytes Stores whole numbers, without decimals
    • float 4 bytes Stores fractional numbers, containing one or more decimals. Sufficient for storing 7 decimal digits
    • double 8 bytes Stores fractional numbers, containing one or more decimals. Sufficient for storing 15 decimal digits

Although these data types are a relatively small amount, they enable you to perform useful functions with your applications. and functions as procedures that your application can perform.
if...else statement
Use the if statement to execute a statement if a logical condition is true. Use the optional else clause to execute a statement if the condition is false. An if statement looks as follows: if (condition) { // block of code to be executed if the condition is true } condition can be any expression that evaluates to true or false. See Boolean for an explanation of what evaluates to true and false. If condition evaluates to true, statement_1 is executed; otherwise, statement_2 is executed. statement_1 and statement_2 can be any statement, including further nested if statements.

You may also compound the statements using else if to have multiple conditions tested in sequence, as follows:

if (20 > 18) { printf("20 is greater than 18"); } In the case of multiple conditions only the first logical condition which evaluates to true will be executed. To execute multiple statements, group them within a block statement ({ ... }) . In general, it's good practice to always use block statements, especially when nesting if statements: if (condition) { // block of code to be executed if the condition is true } else { // block of code to be executed if the condition is false } It is advisable to not use simple assignments in a conditional expression, because the assignment can be confused with equality when glancing over the code. For example, do not use the following code: int time = 20; if (time < 18) { printf("Good day."); } else { printf("Good evening."); } // Outputs "Good evening." If you need to use an assignment in a conditional expression, a common practice is to put additional parentheses around the assignment. For example: int x = 20; int y = 18; if (x > y) { printf("x is greater than y"); }
while statement
A while statement executes its statements as long as a specified condition evaluates to true. A while statement looks as follows: while (condition) { // code block to be executed } The while loop loops through a block of code as long as a specified condition is true:

In the example below, the code in the loop will run, over and over again, as long as a variable (i) is less than 5:.

The example below uses a do/while loop. The loop will always be executed at least once, even if the condition is false, because the code block is executed before the condition is tested:.

Example:

In the example below, the code in the loop will run, over and over again, as long as a variable (i) is less than 5:

int i = 0; while (i < 5) { printf("%d\n", i); i++; }

With each iteration, the loop increments n and adds that value to x. Therefore, x and n take on the following values:

  • After the first pass: n = 1 and x = 1
  • After the second pass: n = 2 and x = 3
  • After the third pass: n = 3 and x = 6

After completing the third pass, the condition n < 3 is no longer true, so the loop terminates.

Function declarations
A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions.
  • The name of the function.
  • A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function.
  • A function can also be referred as a method or a sub-routine or a procedure, etc.

For example, the following code defines a simple function named max()

/* function returning the max between two numbers */ int max(int num1, int num2) { /* local variable declaration */ int result; if (num1 > num2) result = num1; else result = num2; return result; }

Function declaration is required when you define a function in one source file and you call that function in another file. In such case, you should declare the function at the top of the file calling the function.

int max(int, int);

When a program calls a function, the program control is transferred to the called function. A called function performs a defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns the program control back to the main program.

Reference