0% found this document useful (0 votes)
10 views5 pages

Introduction - To - C

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Introduction - To - C

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Introduction to C

C has been around for quite some time and it is one of the foundational languages
of computer science.

C is an older language compared to other languages in use and yet it is still very
popular.

Most operating systems today including the Linux Kernel are implemented with C
code.

The main version of the Python programming language is named CPython because
it is implemented using C. C has also been extended using the languages C++ and
C#.

C is also common in embedded systems which are smaller computing units that
control things like home appliances, lighting controllers, and other small physical
devices.

Introduction to C 1
Compiling C code
www.replit.com (needs account)

https://www.onlinegdb.com/

install CodeBlocks

C files are saved using .c extension

Hello World!
So let’s look at our first C program!

#include <stdio.h>

int main() {
// output a line
printf("Hello World!\n");
}

When this code is run the following text is displayed in the terminal.

Hello World!

#include <stdio.h> : This line is needed to run the line of code that starts with printf .

int main(){ } : This is the starting point of the code. All the code inside the curly
braces {} runs first.

// output a line : This is a comment. It is not a line of code but a message we can
add to code to tell ourselves or others what the code does. When the code is run
this line will be ignored.

printf("Hello World!"); : This line of code prints, or outputs, the text “Hello World!” to
the console. Printing text to the console is one way for a program to communicate
with the user.

Syntax and Errors

Introduction to C 2
When writing in C, we need to follow a set of rules in order for the code to run properly.
These rules are known as syntax.

#include <stdio.h>

int main() {
// output a line
printf("Hello World!\n");
}

Case Sensitivity
Most of the words in the code use all lowercase letters. This is known as case-
sensitivity. Whether lowercase or uppercase, certain words in the code must follow the
correct case in order for the code to run. The only lines of text that can change case are
the comment and the text between quotes.

The Semicolon
All statements, like the printf() statement, need to end with a semicolon. This identifies
the end of the statement and is needed for the code to run correctly.

Double Quotes
The text in between the double quotes " is known as a string (think of a string of
characters). All strings must be surrounded by double-quotes.

So what happens when we break the rules? The answer is errors. The below text is an
error that is output when we leave off the semicolon from the printf() statement in our
Hello World code.

script.c: In function ‘main’:


script.c:6:1: error: expected ‘;’ before ‘}’ token
}
^

The text above gives the following information:

The component location, In function ‘main’

The line and column number, 6:1

Introduction to C 3
A description, expected ‘;’ before ‘}’

What is wrong in the following code?

#include <stdio.h>

int Main() {
// output a line
printf("Hello World!\n")
}

Output

printf("Hello World!\n");

printf() is known as a function and performs the action of printing text to the
console.

"Hello World!\n" is a string. A string is text in between a pair of double quotes.

the \n at the end of the string is called an escape sequence and is used to add a
non-visual character within a string. In this case, \n adds a new line to the end of
the string.

Another escape sequence is \t . This is equivalent to the tab key and will insert
spaces within a string

When we run this code, everything is written in a single line. To add spaces, use the \n

sequence.

#include <stdio.h>

int main() {
// Simple Recipe
printf("2 Cups: All Purpose Flour");
printf("1 Cups: Unsalted Butter(Room Temperature)");
}

Introduction to C 4
Comments
When we write code it is important to document the code’s behavior. One way to do this
is to add comments to our code.

Starting a line with a double forward slash, // , will create a comment and the entire line
will be ignored when we run the code.

// This is a single line comment

// This is 2 single line comments


// together to explain a little more

Introduction to C 5

You might also like