1 Introduction to C

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

Structured Programming

Language
History of C
Why name “C” was given to Language?

 Many of C’s principles and ideas were derived from the earlier
language B. (Ken Thompson was the developer of B Language.)

 BCPL and CPL are the earlier ancestors of B Language

 CPL is common Programming Language. In 1967, BCPL Language


( Basic CPL ) was created as a scaled down version of CPL

 As many of the features were derived from “B” Language thats


why it was named as “C”.
The first C program
uses standard library
input and output #include <stdio.h>
functions
(printf) int main (void)
begin of program {
printf ("Programming is fun.\n")
statements
return 0;
end of program }
main: a special name that indicates where the program must begin
execution. It is a special function.
first statement: calls a routine named printf, with argument the
string of characters “Programming is fun \n”
last statement: finishes execution of main and returns to the system
a status value of 0 (conventional value for OK)
The format in C
• Statements are terminated with semicolons
• Indentation is nice to be used for increased readability.
• Free format: white spaces and indentation is ignored by
compiler
• C is case sensitive – pay attention to lower and upper
case letters when typing !
– All C keywords and standard functions are lower case
– Typing INT, Int, etc instead of int is a compiler error
• Strings are placed in double quotes
• New line is represented by \n (Escape sequence)
Second program

#include <stdio.h>
int main (void)
{
printf ("Programming is fun.\n");
printf ("And programming in C is even more fun
return 0;
}
Displaying multiple lines of text

#include <stdio.h>
int main (void)
{
printf ("Testing...\n..1\n...2\n....3\n"
return 0;
}
Output:
It is not necessary
to make a separate
call to printf for each Testing...
line of output ! ..1
...2
....3
Variables
• Programs can use symbolic names for
storing computation data and results
• Variable: a symbolic name for a memory
location
– programmer doesn’t has to worry about
specifying (or even knowing) the
location/address
• In C, variables have to be declared before
they are used
Using and Displaying Variables
#include <stdio.h>
int main (void)
{
int sum;
sum = 50 + 25;
printf ("The sum of 50 and 25 is %i\n", sum)
return 0;
}
Variable sum declared of type int
Variable sum assigned expression 50+25
Value of variable sum is printed in place of %i
The printf routine call has now 2 arguments: first argument a string
containing also a format specifier (%i), that holds place for an integer
value to be inserted here
printf function
Displaying multiple values

#include <stdio.h>
int main (void)
{
int value1, value2, sum;
value1 = 50;
value2 = 25;
sum = value1 + value2;
printf ("The sum of %i and %i is %i\n",value1, value2, sum
return 0;
}

The format string must contain as many placeholders as expressions to b


Using comments in a program
• Comment statements are used in a program to document it and to enhance
its readability.
• Useful for human readers of the program – compiler ignores comments
• Ways to insert comments in C:
– When comments span several lines: start marked with /*, end
marked with */
– Comments at the end of a line: start marked with //
Header Files

The subdirectory called INCLUDE


contains header files.
 Each header file has a “.h” file extension.
Format of simple C program

main ( ) Function name


Opening brace
{ Start of Program

…..
…… Program statements

……
Closing brace
} End of program
The main ( ) is a special function used by the C system to tell the
computer where the program starts. Every program must have
exactly one main function. The empty parentheses immediately
following main indicates the function main has no arguments.
main ( ) If we compile this program, it shows
three errors.
{  Function printf should have a
printf ( “ ”) prototype
 Statement missing
}  Compound statement missing

# include <stdio.h> #include <header file>


main ( )
stdio.h refers to the standard
{
I/O header file containing
printf ( “ ”); standard input an output
functions
}
# include <stdio.h>
main ( )
{
printf ( “Hello, How are you? ”);
}
Both is OK

# include <stdio.h>
int main ( )
{
printf ( “Hello, How are you? ”);
return 0;
}
# include <stdio.h> /* header file for printf ( ) */
# include <conio.h> /* header file for getch ( ) */
main ()
{

printf (“ \n Hello, How are you?”);


getch(); /* wait for a key input */

getch ( ) header file


library function used
conio.h

/* this is a comment */
Tokens in C
A C program consists of various tokens and a token is
either a keyword, an identifier, a constant, a string
literal, or a symbol. For example, the following C
statement consists of five tokens:

printf("Hello, World! \n");

The individual tokens are:


printf
(
"Hello, World! \n"
)
;
Semicolon (;)
In C program, the semicolon is a
statement terminator. That is, each
individual statement must be ended with a
semicolon. It indicates the end of one
logical entity.
For example, following are two different
statements:
printf("Hello, World! \n");
return 0;
Comments
Comments are like helping text in your C
program and they are ignored by the
compiler. They start with /* and terminates
with the characters */ as shown below:

/* my first program in C */
Identifiers
A C identifier is a name used to identify a variable,
function, or any other user-defined item. An
identifier starts with a letter A to Z or a to z or an
underscore _ followed by zero or more letters,
underscores, and digits (0 to 9).
C does not allow punctuation characters such as
@, $, and % within identifiers. C is a case
sensitive programming language. Thus, Manpower
and manpower are two different identifiers in C.
Here are some examples of acceptable identifiers:
Keywords
The following list shows the reserved words in C.
These reserved words may not be used as
constant or variable or any other identifier names.
Whitespace in C
A line containing only whitespace, possibly with a comment, is
known as a blank line, and a C compiler totally ignores it.
Whitespace is the term used in C to describe blanks, tabs, newline
characters and comments. Whitespace separates one part of a
statement from another and enables the compiler to identify where
one element in a statement, such as int, ends and the next element
begins. Therefore, in the following statement:
int age;

There must be at least one whitespace character (usually a space)


between int and age for the compiler to be able to distinguish them.
On the other hand, in the following statement:
fruit = apples + oranges; // get the total fruit
No whitespace characters are necessary between fruit and =, or
between = and apples, although you are free to include some if you
Executing A ‘C’ Program
• Executing a program written in C involves
a series of steps. These are:
• Creating the program
• Compiling the program
• Linking the program with functions that are
needed from the C library
• Executing the program
Summary
 Every C program requires a main () function (use of more
than one main () is illegal). The place main is where the
program execution begin.
 The execution of a function begins at the opening brace of the
function and ends at the corresponding closing brace.
 C programs are written in lowercase letters. However,
uppercase letters are used for symbolic names and output
strings.
 All the words in a program line must be separated from each
other by at least one space, or a tab.
 Every program statement in a C language must end with a
semicolon.
 All variables must be declared for their types before they are
used in the program.
Continue
 We must make sure to include header files using #include
directive when the program refers to special names and
functions that it does not define.
 Compilers directives such as define and include are special
instructions to the compiler to help it compile a program. They
do not end with a semicolon.
 The sign # of compiler directives must appear in the first column
of the line.
 When braces are used to group statements make sure that the
opening brace has a corresponding closing brace.
 C is a free form language and therefore a proper form of
indentation of various sections would improve legibility of the
program.
 A comment can be inserted almost anywhere a space can
appear. Use of appropriate comments in proper places
increases readability and understandability of the program and
helps users in debugging and testing. Remember to match the
symbols /* and */ appropriately.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy