Unit 1 Introduction To C Language

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 58

UNIT – I

Introduction to
C Language
Introduction to C Language
Background,
C Programs,
Identifiers,
Data Types,
Variables,
Constants, Input / Output Statements
Arithmetic Operators and Expressions: Evaluating Expressions,
Precedence and Associativity of Operators, Type Conversions.
Background

C programming is a general-purpose, procedural computer programming language


developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the
UNIX operating system.

C is a structured programming language. It is considered a high-level language because it


allows the programmer to concentrate on the problem at hand and not worry about the
machine that the program will be using.
Why to Learn C Programming
• Easy to learn
• Structured language
• It produces efficient programs
• It can handle low-level activities
• It can be compiled on a variety of computer platforms
Facts about C
• C was invented to write an operating system called UNIX.
• C is a successor of B language which was introduced around the early
1970s.
• The language was formalized in 1988 by the American National
Standard Institute (ANSI).
• The UNIX OS was totally written in C.
• Today C is the most widely used and popular System Programming
Language.
• Most of the state-of-the-art software have been implemented using C.
Applications of C Programming
• Operating Systems
• Language Compilers
• Assemblers
• Text Editors
• Print Spoolers
• Network Drivers
• Modern Programs
• Databases
• Language Interpreters
Basics of a Typical C Program Development Environment
Program is created in
Phases of C Programs: the editor and stored
Editor Disk on disk.
1. Edit Preprocessor program
2. Preprocess Preprocessor Disk processes the code.
Compiler creates
3. Compile Compiler Disk object code and stores
it on disk.
4. Link
Linker Disk Linker links the object
5. Load code with the libraries
Primary Memory

6. Execute Loader
Loader puts program
in memory.
Disk ..
..
..

Primary Memory CPU takes each


instruction and
CPU executes it, possibly
storing new data
values as the 7
..
.. program executes.
..
Structure of a C Program
Anatomy of a simple C program
#include <stdio.h> C program comprises
two parts:
int main(void)
{ - Preprocessor directives
printf("Hello World!\n"); - Program code
return (0);
Preprocessor directives give commands to C
} preprocessor
which “modifies” C program text before
#include <stdio.h> complilation.
– Preprocessor directive
• Tells computer to load contents of a certain file
• Begins with a # symbol
– <stdio.h> contains standard input/output operations
• Allows C program to perform I/O from keyboard/screen
12
int main()
{
– C programs contain one or more functions, exactly one of which
must be function main
– Parenthesis () used to indicate a function
– int means that main "returns" an integer value (status code) to
operating system when it terminates.
– void means that the main function receives no data from the
operating system when it starts executing.
– Left brace { indicates start of a block (of code)
• The “bodies” of all functions must be contained in braces -
the body of a function is a block of code
• Here it is the body of the function main
13
printf( “Hello World!\n" );
– Instructs computer to perform an action
• Specifically, print the string of characters within quotes (“ ”)
• Makes use of standard I/O function, printf
– Entire line called a statement
• All statements must end with a semicolon (;)
• the semicolon is a statement terminator
– Escape character (\)
• Indicates that printf should do something out of the ordinary
• \n is the newline character

14
return (0);
}
– A way to exit a function
– return (0) means that the program terminated normally
– Right brace } indicates end of a block (of code)
– Here indicates end of main function has been reached

Comments
– Comments are like helping text in your C program and they
are ignored by the compiler. They start with /* and terminate
with the characters */ as shown below −
– \\ xyz
– /* my first program in C */

15
One feature present in all computer languages is the
IDENTIFIERS
identifier. Identifiers allow us to name data and other
objects in the program. Each identified object in the
computer is stored at a unique address. An identifier is a
name used to identify a variable, function, or any other
user-defined item.

RULES
1.An identifier starts with a letter A to Z, a to z, or an
underscore '_' followed by zero or more letters,
underscores, and digits (0 to 9).
2. There must not be any white space in the string.
3. And, all the subsequent characters after the first
character must not consist of any special characters like $,
Examples of Valid and Invalid Names

C does not allow punctuation characters such as


@, $, and % within identifiers
Keywords
• Keywords are reserved words that have special meaning in
the C language. The meaning of C language keywords has
already been described in the C compiler.
• They are explicitly reserved and cannot be redefined.
• ANSII C has 32 key words.
• Some implementations such as Borland’s C or Microsoft’s C
have additional key words.
•A keyword name can not be used as a variable
name.

•Keywords must be written in lower case.

•It specifies the type/kind of entity.


ANSII C Keywords
auto do goto signed unsigned
break double if sizeof void
case else int static volatile
char enum long struct while
const extern register switch
continue float return typedef
default for short union
Check Valid/ Invalid Identifiers
• int
• Char
• X+y
• 1x
• Sum
• Average _x
• _sum_
• a_ Mark_Maths
Differences between Keyword and
Identifier
Keyword Identifier
Keyword is a pre-defined word. The identifier is a user-defined word

It must be written in a lowercase It can be written in both lowercase


letter. and uppercase letters.
Its meaning is pre-defined in the c Its meaning is not defined in the c
compiler. compiler.
It is a combination of alphabetical It is a combination of alphanumeric
characters. characters.
It does not contain the underscore It can contain the underscore
character. character.
Data Types
A data type defines a set of values and a set
of operations that can be applied on those
values.
sizeof (short) ≤ sizeof (int) ≤ sizeof (long) ≤ sizeof (long long)
sizeof (float) ≤ sizeof (double) ≤ sizeof (long double)
Type Summary
A string literal is a sequence of characters enclosed in
double-quote marks.

Some Strings
Use single quotes for character constants.
Use double quotes for string constants.

Null Characters and Null Strings


Symbolic Names for Control Characters
Variables
Variables are named memory locations that have a
type, such as integer or character, which is inherited
from their type. The type determines the values that
a variable may contain and the operations that may
be used with its values.
Rules for naming a variable
• A variable name can only have letters (both uppercase
and lowercase letters), digits and underscore.
• The first letter of a variable should be either a letter or
an underscore.
• There is no rule on how long a variable name
(identifier) can be. However, you may run into problems
in some compilers if the variable name is longer than 31
characters.
Examples of Variable Declarations and Definitions
When a variable is defined, it is not initialized.
We must initialize any variable requiring
prescribed data when the function starts.
PROGRAM 2 Print Sum of Three Numbers
PROGRAM 2 Print Sum of Three Numbers (continued)
PROGRAM 2 Print Sum of Three Numbers (continued)
Constants
Constants are data values that cannot be
changed during the execution of a program.
Like variables, constants have a type. In this
section, we discuss Boolean, character,
integer, real, complex, and string constants.
A character constant is enclosed in single quotes.

There are two simple ways in C to define constants −


•Using #define preprocessor.
•Using const keyword.
Examples of Integer Constants
Examples of Real Constants
PROGRAM 3 Memory Constants
PROGRAM 3 Memory Constants (continued)
Input / Output Statements
• Input means to provide the program with some data to be
used in the program and
• Output means to display data on screen or write the data
to a printer or a file.
• C programming language provides many built-in functions
to read any given input and to display data on screen
How does this program work?
• All valid C programs must contain the main() function. The
code execution begins from the start of the main() function.
• The printf() is a library function to send formatted output to
the screen. The function prints the string inside quotations.
• To use printf() in our program, we need to include stdio.h
header file using the #include <stdio.h> statement.
• The return 0; statement inside the main() function is the
"Exit status" of the program. It's optional.
• Libc.so file
contains the
implementati
on of
printf( ), and
scanf( ).

• Stdio.h
contains
declaration of
printf( ),
scanf( ),etc.
1. C Output
Example 1 Example 2
2. C Input Example 6: Float and Double Input/Output
gets( ) & puts( ) functions
• The gets() function
reads a line from
stdin(standard input)
into the buffer pointed
to by str pointer, until
either a terminating
newline or EOF (end of
file) occurs. The puts()
function writes the
string str and a trailing
newline to stdout.
Console Input/Output function access the three major files before the execution of a C
Program.
getchar() & putchar() functions

• The getchar() function reads a character from the


terminal and returns it as an integer. This function reads
only single character at a time.

• The putchar() function displays the character passed to


it on the screen and returns the same character. This
function too displays only a single character at a time.

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