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
There are many text editors and compilers to choose from. In this tutorial, we
will use an IDE (see below).
C Install IDE
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 [Link]
Download the [Link] file, which will install the text editor with a
compiler.
C Quickstart
Let's create our first C file.
Open Codeblocks and go to File > New > Empty File.
Write the following C code and save the file as myfirstprogram.c (File >
Save File as):
myfirstprogram.c
#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.
In Codeblocks, it should look like this:
Then, go to Build > Build and Run to run (execute) the program. The result
will look something to this:
Hello World!
Process returned 0 (0x0) execution time : 0.011 s
Press any key to continue.
Congratulations! You have now written and executed your first C program.
Learning C At W3Schools
When learning C at [Link], you can use our "Try it Yourself" tool, which
shows both the code and the result. It is used to write, run, and test code right
in your browser:
myfirstprogram.c
Code:
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
Result:
Hello World!
C Syntax
❮ PreviousNext ❯
Syntax
You have already seen the following code a couple of times in the first chapters.
Let's break it down to understand it better:
Example
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
Try it Yourself »
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
❮ PreviousNext ❯
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!");
Try it Yourself »
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'
Try it Yourself »
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;
Try it Yourself »
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.
Coming up: The next chapter will teach you how to control the output and how
to insert new lines to make it more readable.
C Output (Print Text)
❮ PreviousNext ❯
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;
}
Try it Yourself »
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.);
Try it Yourself »
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;
}
Try it Yourself »
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;
}
Try it Yourself »
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;
}
Try it Yourself »
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;
}
Try it Yourself »
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
C Comments
❮ PreviousNext ❯
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!");
Try it Yourself »
This example uses a single-line comment at the end of a line of code:
Example
printf("Hello World!"); // This is a comment
Try it Yourself »
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!");
Try it Yourself »
C Variables
❮ PreviousNext ❯
Variables are containers for storing data values, like numbers and characters.
In C, there are different types of variables (defined with different keywords), for
example:
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'. Characters are
surrounded by single quotes
Declaring (Creating) Variables
To create a variable, specify the type and assign it a value:
Syntax
type variableName = value;
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.
So, to create a variable that should store a number, look at the following
example:
Example
Create a variable called myNum of type int and assign the value 15 to it:
int myNum = 15;
You can also declare a variable without assigning the value, and assign the
value later:
Example
// Declare a variable
int myNum;
// Assign a value to the variable
myNum = 15;
Output Variables
You learned from the output chapter that you can output values/print text with
the printf() function:
Example
printf("Hello World!");
Try it Yourself »
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:
Example
int myNum = 15;
printf(myNum); // Nothing happens
Try it Yourself »
To output variables in C, you must get familiar with something called "format
specifiers", which you will learn about in the next chapter.
Format Specifiers
Format specifiers are used together with the printf() function to tell the
compiler what type of data the variable is storing. It is basically
a placeholder for the variable value.
A format specifier starts with a percentage sign %, followed by a character.
For example, to output the value of an int variable, use the format
specifier %d surrounded by double quotes (""), inside the printf() function:
Example
int myNum = 15;
printf("%d", myNum); // Outputs 15
Try it Yourself »
To print other types, use %c for char and %f for float:
Example
// Create variables
int myNum = 15; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number
char myLetter = 'D'; // Character
// Print variables
printf("%d\n", myNum);
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);
Try it Yourself »
To combine both text and a variable, separate them with a comma inside
the printf() function:
Example
int myNum = 15;
printf("My favorite number is: %d", myNum);
Try it Yourself »
To print different types in a single printf() function, you can use the following:
Example
int myNum = 15;
char myLetter = 'D';
printf("My number is %d and my letter is %c", myNum, myLetter);
Try it Yourself »
You will learn more about Data Types in a later chapter.
Print Values Without Variables
You can also just print a value without storing it in a variable, as long as you use
the correct format specifier:
Example
printf("My favorite number is: %d", 15);
printf("My favorite letter is: %c", 'D');
Try it Yourself »
Real-Life Example
Often in our examples, we simplify variable names to match their data type
(myInt or myNum for int types, myChar for char types, and so on). This is done
to avoid confusion.
However, for a practical example of using variables, we have created a program
that stores different data about a college student:
Example
// Student data
int studentID = 15;
int studentAge = 23;
float studentFee = 75.25;
char studentGrade = 'B';
// Print variables
printf("Student id: %d\n", studentID);
printf("Student age: %d\n", studentAge);
printf("Student fee: %f\n", studentFee);
printf("Student grade: %c", studentGrade);
Try it Yourself »
Calculate the Area of a Rectangle
In this real-life example, we create a program to calculate the area of a
rectangle (by multiplying the length and width):
Example
// Create integer variables
int length = 4;
int width = 6;
int area;
// Calculate the area of a rectangle
area = length * width;
// Print the variables
printf("Length is: %d\n", length);
printf("Width is: %d\n", width);
printf("Area of the rectangle is: %d", area);
scanf in C
Last Updated : 04 Feb, 2025
In C, scanf is a function that stands for Scan Formatted String. It is
the most used function to read data from stdin (standard input
stream i.e. usually keyboard) and stores the result into the given
arguments. It can accept character, string, and numeric data from
the user using standard input. It also uses format specifiers like
printf.
Example:
#include <stdio.h>
int main() {
int n;
// Reading an integer input
scanf("%d", &n);
printf("%d", n);
return 0;
}
Input
10
Output
10
Explanation: In this example, scanf(“%d”, &n) reads an integer
from the keyboard and stores it in the integer variable n.
The %d format specifier indicates that an integer is expected,
and &n provides the memory address of n so that scanf can store
the input value there.
scanf Syntax
The syntax of scanf() in C is similar to the syntax of printf().
scanf(“format”, address_of_args… );
Here,
format: It is the format string that contains the format
specifiers(s).
address_of_args: Address of the variables where we want to
store the input.
We use & operator to find the address of the variables by
appending it before the variable name. If you’re interested in
learning more about input handling and integrating it into complex
data structures, the C Programming Course Online with Data
Structures covers practical applications of input functions in C.
Example format specifiers recognized by scanf:
%d to accept input of integers.
%ld to accept input of long integers
%lld to accept input of long long integers
%f to accept input of real number.
%c to accept input of character types.
%s to accept input of a string.
To know more about format specifiers, refer to this article – Format
Specifiers in C
Return Value of scanf
The scanf in C returns three types of values:
>0: The number of values converted and assigned successfully.
0: No value was assigned.
<0: Read error encountered or end-of-file (EOF) reached before
any assignment was made.
Examples of scanf
The below examples demonstrate the use of scanf for different
types of input:
Reading Floating Point Value
#include <stdio.h>
int main() {
float f;
printf("Enter the value:\n");
// Reading real number and storing in f
scanf("%f", &f);
printf("You entered: %.2f", f);
return 0;
}
Console
Enter two numbers:
10.4 (Press Enter)
You Entered: 10.40
Explanation: The scanf(“%f”, &f) statement reads a floating-point
number from user input and stores it in the variable f.
The %f format specifier ensures the correct interpretation of the
input, and &f passes the memory address of f to store the