C Notes till Variables

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

What is C?

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. The main reason for its
popularity is because it is a fundamental language in the field of computer
science.

C is strongly associated with UNIX, as it was developed to write the UNIX


operating system.

Why Learn C?
 It is one of the most popular programming languages in the world
 If you know C, you will have no problem learning 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 technologies

Difference between C and C++


 C++ was developed as an extension of C, and both languages have
almost the same syntax
 The main difference between C and C++ is that C++ support classes
and objects, while C does not

Get Started With C


To start using C, you need two things:

 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
Installation of Turbo C
Install the Turbo C from their website Download Turbo C++ for Windows 7, 8, 8.1, 10
and Windows 11 (32-64 bit) with full/window screen mode and many more extra features
(developerinsider.co) , This IDE enables us to write the code and compile the code in same
environment
Sections of a C program
 Comments
 Preprocessor Directives
 Globals & Constants
 Main Function
 User defined Functions
C program Structure (Basic)
/*
Aim: ...
Author: ...
Class: ...
*/

#include <stdio.h>

int main ()
{
// Your code starts here

return 0;
}

C program structure (Advanced)


/* Documentation */
/*
Aim: ...
Author: ...
Class: ...
*/
/* Libraries */
#include <stdio.h>

/* Function Declarations */
int example(int x);

/* Global Variables & Constants */


int x = 100;
#define PI 3.142

/* Main Function */
int main ()
{
// Your code starts here

return 0;
}

/* Function Definitions */
int example(int x){

Myfirstprogram.c
// My first program
#include <stdio.h>

int main() {
printf("Hello World!");
return 0;
}

Don't worry if you don't understand the code above - we will discuss it in
detail in later chapters. For now, focus on how to run the code.
Example explained
Line 1: #include <stdio.h> is a header file library that lets us work with
input and output functions, such as printf() (used in line 4). Header files add
functionality to C programs.

Don't worry if you don't understand how #include <stdio.h> works. Just think
of it as something that (almost) always appears in your program.

Line 2: A blank line. C ignores white space. But we use it to make the code
more readable.

Line 3: Another thing that always appear in a C program is main(). This is


called a function. Any code inside its curly brackets {} will be executed.

Line 4: printf() is a function used to output/print text to the screen. In our


example, it will output "Hello World!".

Note that: Every C statement ends with a semicolon ;

Note: The body of int main() could also been written as:
int main(){printf("Hello World!");return 0;}

Remember: The compiler ignores white spaces. However, multiple lines


makes the code more readable.

Line 5: return 0 ends the main() function.

Line 6: Do not forget to add the closing curly bracket } to actually end the
main function.

C Statements
A computer program is a list of "instructions" to be "executed" by a
computer.

In a programming language, these programming instructions are


called statements.

The following statement "instructs" the compiler to print the text "Hello
World" to the screen:

Example
printf("Hello World!");
It is important that you end the statement with a semicolon ;

If you forget the semicolon (;), an error will occur and the program will not
run:

Example
printf("Hello World!")

error: expected ';' before 'return'

Many Statements
Most C programs contain many statements.

The statements are executed, one by one, in the same order as they are
written:

Example
printf("Hello World!");
printf("Have a good day!");
return 0;

Example explained

From the example above, we have three statements:

1. printf("Hello World!");
2. printf("Have a good day!");
3. return 0;

The first statement is executed first (print "Hello World!" to the screen).
Then the second statement is executed (print "Have a good day!" to the
screen).
And at last, the third statement is executed (end the C program
successfully).
You will learn more about statements while reading this tutorial. For now,
just remember to always end them with a semicolon to avoid any errors.

Output (Print Text)


To output values or print text in C, you can use the printf() function:

Example
#include <stdio.h>

int main() {
printf("Hello World!");
return 0;
}

Double Quotes
When you are working with text, it must be wrapped inside double quotations
marks "".

If you forget the double quotes, an error occurs:

Example
printf("This sentence will work!");

printf(This sentence will produce an error.);

Many printf Functions


You can use as many printf() functions as you want. However, note that it
does not insert a new line at the end of the output:
Example
#include <stdio.h>

int main() {
printf("Hello World!");
printf("I am learning C.");
printf("And it is awesome!");
return 0;
}

C New Lines
To insert a new line, you can use the \n character:

Example
#include <stdio.h>

int main() {
printf("Hello World!\n");
printf("I am learning C.");
return 0;
}

You can also output multiple lines with a single printf() function. However,
this could make the code harder to read:

Example
#include <stdio.h>

int main() {
printf("Hello World!\nI am learning C.\nAnd it is awesome!");
return 0;
}

Tip: Two \n characters after each other will create a blank line:

Example
#include <stdio.h>
int main() {
printf("Hello World!\n\n");
printf("I am learning C.");
return 0;
}

What is \n exactly?
The newline character (\n) is called an escape sequence, and it forces the
cursor to change its position to the beginning of the next line on the screen.
This results in a new line.

Examples of other valid escape sequences are:

Escape Sequence Description

\t Creates a horizontal tab

\\ Inserts a backslash character (\)

\" Inserts a double quote character


Comments in C
Comments can be used to explain code, and to make it more readable. It can
also be used to prevent execution when testing alternative code.

Comments can be singled-lined or multi-lined.

Single-line Comments
Single-line comments start with two forward slashes (//).

Any text between // and the end of the line is ignored by the compiler (will
not be executed).

This example uses a single-line comment before a line of code:

Example
// This is a comment
printf("Hello World!");

This example uses a single-line comment at the end of a line of code:

Example
printf("Hello World!"); // This is a comment

C Multi-line Comments
Multi-line comments start with /* and ends with */.
Any text between /* and */ will be ignored by the compiler:

Example
/* The code below will print the words Hello World!
to the screen, and it is amazing */
printf("Hello World!");

Single or multi-line comments?


It is up to you which you want to use. Normally, we use // for short
comments, and /* */ for longer.

Good to know: Before version C99 (released in 1999), you could only use
multi-line comments in C.

You might also like