Module-1-Language-compilation-and-intro-to-C-6PROGFUN
Module-1-Language-compilation-and-intro-to-C-6PROGFUN
INFORMATION SCIENCE
Language
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
Natural language vs programming
language
Language is a tool for expressing and
recording human thoughts
• At least one language accompanies
us throughout our whole lives – it's our
native language, our mother tongue
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
Natural language vs programming
language
Programming Language is defined by a
set of certain rigid rules, much more
inflexible than any natural language.
Any program we write must be correct in
these three ways: lexically, syntactically
and semantically.
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
Natural language vs programming
language
• Lexicon - symbols (letters, digits,
punctuation marks, and so on) that could
be used in the language
• Syntax - the appropriate ways of collating
the symbols
• Semantics - to be able to recognize the
meaning of every statement expressed in
the given language
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
Natural language vs programming
language
• A computer can only follow commands
• Instruction List - a complete set of well-
known commands
• High-Level Programming Languages -
bridge between the human language
(natural language) and the computer
language (machine language)
• It is at least somewhat similar to a natural
language
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
Compilation
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
Compilation
• The process of translating from a high-level
language into a machine language is called
compilation.
• It is made by a specialized computer
program called a compiler.
• A program (which in fact is just text) is
called the source code, while the file which
contains the source is called the source
file.
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
Compilation
• It is important to store the source code to
a text file that is not formatted
• If the source code is in the C language, it
is important to store it in a file that have a
“.c” suffix.
• Ex.: “program.c”, “myfile.c”
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
Compilation
• After compiling a source code without an
error, the compiler will then make an
executable file.
• Unix/Linux system will create an output
file named “a.out” by default
• MS Windows® can give this file the same
name as the source file, only changing the
suffix from “.c” to “.exe”.
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
About “C”
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
About C
It was created in
the early
seventies of the
twentieth
century by
Dennis Ritchie
while he was
working in Bell
Laboratories
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
About C
• The “C” language is one of a huge number
of programming languages currently in
use, and one of the oldest.
• There are many other programming
languages widely used - like “C++”, “C#”,
Perl, Java or JavaScript
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
About C
• C is the so-called general-purpose
programming language
• It’s best if used for coding drivers,
embedded applications or operating
systems.
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
A Simple Program
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
A Simple Program
#include <stdio.h>
int main()
{
puts(“Hello World");
return 0;
}
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
A Simple Program
Steps that the program #include <stdio.h>
A Simple Program
• This sort of structured and semi-formal
description of each step of the program is
called an algorithm.
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
A Simple Program
#include <stdio.h>
A Simple Program
int main()
{
puts(“Hello World");
return 0;
}
• This is the main function
• The program starts here
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
A Simple Program
int main()
{
puts(“Hello World");
return 0;
}
• puts (put string) function displays the
string of word/s that is inside the
parenthesis and quotation marks
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
A Simple Program
int main()
{
puts(“Hello World");
return 0;
}
• puts(“Wakanda”) will output Wakanda
• puts(“Hi”) will output Hi
• This is called function invocation or
function call
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
A Simple Program
int main()
{
puts(“Hello World");
return 0;
}
• return ends the function execution
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
Numbers
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
Numbers And How The Computers See
Them
In modern computing, there are two main types of
numbers:
• Integers(int)-those which are devoid of
the fractional part
• floating-point numbers (float)-
contains fractional part or decimal point
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
Numbers And How The Computers See
Them
Both of these kinds of numbers significantly
differ in:
• how they are stored in a computer’s
memory
• the range of acceptable values
Variables
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
Variables
• Variables can be considered as
containers of value
• Variables have always have three things:
• type
• name/identifier
• value
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
Naming a Variable
• the name of the variable can only be
composed of upper-case or lower-case
Latin letters, digits and the character “_”
(underscore)
• the name of the variable must begin with a
letter
• the underline/underscore character is a
letter
• upper- and lower-case letters are treated
as different (alice is different from ALICE)
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
Naming a Variable
*Note that the same restrictions apply to function names.
Naming a Variable
And now some incorrect names:
Naming a Variable
There are also different case styles such as:
• camelCase
• Consists of multiple words and no spaces.
• First letter of the first word must be in lower-case
• First letter of succeeding words are in upper-case
• EX.:
• firstName
• containerVariable
• finalVariableName
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
Variable Types
The type is an attribute that uniquely defines
which values can be stored inside the
variable.
• you can only put in a value that is compatible with
the variable's type.
• The variable comes into existence as a result of a
declaration.
• Type variableName;
• Type variableName = value;
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
Declaring Variables
Examples of declaring a variable:
int num1;
float decimalNumber;
char my_character;
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
Declaring Variables
Examples of declaring multiple variable in one
line:
Declaring Variables
Examples of declaring a variable and assigning a
value:
int num1 = 3;
float decimalNumber = 1.2;
char my_character = ‘b’;
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
Declaring Variables
Examples of declaring a variable and assigning an
expression:
int num1 = 3 + 1;
float decimalNumber = 1.2 – 3.51;
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
Declaring Variables
Examples of declaring a variable and assigning a
variable:
int num1 = 3;
int num2 = num1;
int num3 = num2 + num1;
Int num4 = num3 – 5;
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
Reserved Keywords
There are some words
that play special roles
in the C language.
They’re reserved
because you mustn't
use them as names:
neither for your
variables, nor
functions, nor any
other named entities
you want to create.
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
Reserved Keywords
You can’t use int as a
variable name but,
though it is not
recommended, you can
use INT as an
identifier. Why is that
the case?
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
Comments
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
Comments
The developer may want to put in a few words
addressed not to the compiler but to humans:
• to explain to other readers of the code how the
tricks used in the code work
• to tell the meanings of variables and functions
• to keep stored information on who the author is
and when the program was written
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
Comments
Good and responsible developers describe each
function; in particular, they explain the role of
the parameters, the value the function
returns as a result and what the function
actually does.
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
Comments
Inline Comments
// This is a comment
// This is also a comment
Multiline Comments
/*This is a multiline comment
Whatever written inside here will not affect the code
It’s like magic!*/
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
Comments
For example:
/* This program takes age input from the user It stores it in the age variable and,
print the value using printf() */
#include <stdio.h>
int main() {
return 0;
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
Comments
For example:
/******************************
This is another multiline comment
We used a slash and 30 asterisks to signal the start of this comment
You can write anything here as long as you don’t type an extra asterisk
And remember to end the comment with the same number of asterisks
******************************/
INSTITUTE OF COMPUTING STUDIES AND LIBRARY
INFORMATION SCIENCE
Comments
For example:
#include <stdio.h>
int main() {
//puts(“Hello Word”)
return 0;
}