0% found this document useful (0 votes)
37 views7 pages

Handouts For C Language

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

Handouts For C Language

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

CHAPTER

1
Introduction to C Language

A Brief History of C

C is a general-purpose language which has been closely associated with the UNIX operating


system for which it was developed - since the system and most of the programs that run it are
written in C.

Many of the important ideas of C stem from the language BCPL, developed by Martin Richards.
The influence of BCPL on C proceeded indirectly through the language B, which was written by
Ken Thompson in 1970 at Bell Labs, for the first UNIX system on a DEC PDP-
7. BCPL and B are "type less" languages whereas C provides a variety of data types.

In 1972 Dennis Ritchie at Bell Labs writes C and in 1978 the publication of The C Programming
Language by Kernighan & Ritchie caused a revolution in the computing world.

In 1983, the American National Standards


Did You Know?
Institute (ANSI) established a committee
to provide a modern, comprehensive Basic Combined Programming
definition of C. The resulting definition, Language, BCPL is a programming
the ANSI standard, or "ANSI C", was language developed in 1966 by Martin
Richards of the University of Cambridge.
completed late 1988.
It quickly became popular, largely
because of its high portability. BCPL is the
Uses of C
successor to the CPL programming
language, the first language to print the
C was initially used for system infamous "Hello World!" test message.
development work, in particular the Combined Programming
programs that make-up the operating Language, CPL is a programming
system. Why use C? Mainly because it language developed in the early 1960s
produces code that runs nearly as fast as by the University of Cambridge and the
University of London. Later this
code written in assembly language. Some
programming language was replaced
examples of the use of C might be:
with BCPL.
1. Operating Systems
2. Language Compilers
3. Assemblers
4. Text Editors
5. Print Spoolers
6. Network Drivers
7. Modern Programs
8. Data Bases
9. Language Interpreters
10. Utilities

The C Character Set:

C does not use, nor requires the use of, every character found on a modern computer keyboard.
The only characters required by the C Programming Language are as follows:

1. Letter: A–Z or a -z
2. Digits: 0-9
3. Special characters: ~ . , : ; ' $ "# % & ! _ {} [] () | + - / \ * =

The use of most of this set of characters will be discussed throughout the course.

Data types in C Language

Data types specify how we enter data into our programs and what type of data we enter. C
language has some predefined set of data types to handle various kinds of data that we can use in
our program. These data types have different storage capacities.
C language supports 2 different type of data types:

1. Primary data types:

These are fundamental data types in C namely integer(int), floating point(float),


character(char) and void.

2. Derived data types:

Derived data types are nothing but primary datatypes but a little twisted or grouped together
like array, stucture, union and pointer.
Data type determines the type of data a variable will hold. If a variable x is declared as int. it
means x can hold only integer values. Every variable which is used in the program must be
declared as what data-type it is.
Integer type
Integers are used to store whole numbers.

Floating point type


Floating types are used to store real numbers.

Character type
Character types are used to store characters value.

Void type
void type means no value. This is usually used to specify the type of functions which returns
nothing. We will get acquainted to this data type as we start learning more advanced topics in C
language, like functions, pointers etc.

Operators

An operator is a symbol which operates on a value or a variable. For example: + is an operator to


perform addition.

C programming has wide range of operators to perform various operations. For better
understanding of operators, these operators can be classified as:

Operators in C programming
1. Arithmetic Operators
2. Increment and Decrement Operators
3. Assignment Operators
4. Relational Operators
5. Logical Operators

Arithmetic Operators

An arithmetic operator performs mathematical operations such as addition, subtraction and


multiplication on numerical values (constants and variables).
Operator Meaning of Operator

+ addition or unary plus

- subtraction or unary minus

* multiplication

/ division

% remainder after division( modulo division)

Increment and decrement operators

C programming has two operators increment ++ and decrement -- to change the value of an
operand (constant or variable) by 1.

Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. These two
operators are unary operators, meaning they only operate on a single operand.

C Assignment Operators

An assignment operator is used for assigning a value to a variable. The most common
assignment operator is =

C Relational Operators

A relational operator checks the relationship between two operands. If the relation is true, it
returns 1; if the relation is false, it returns value 0.

Relational operators are used in decision making and loops.

Operator Meaning of Operator Example

== Equal to 5 == 3 returns 0

> Greater than 5 > 3 returns 1


Operator Meaning of Operator Example

< Less than 5 < 3 returns 0

!= Not equal to 5 != 3 returns 1

Greater than or equal


>= 5 >= 3 returns 1
to

<= Less than or equal to 5 <= 3 return 0

C Logical Operators

An expression containing logical operator returns either 0 or 1 depending upon whether


expression results true or false. Logical operators are commonly used in decision making in C
programming.

Operato
Meaning of Operator Example
r

Logial AND. True only if all If c = 5 and d = 2 then, expression ((c == 5)


&&
operands are true  && (d > 5)) equals to 0.

Logical OR. True only if either If c = 5 and d = 2 then, expression ((c == 5) ||


||
one operand is true (d > 5)) equals to 1.

Logical NOT. True only if the


! If c = 5 then, expression ! (c == 5) equals to 0.
operand is 0

The Printf() and Scanf() Functions in C Language


 printf() and scanf() functions are inbuilt library functions in C programming language which
are available in C library by default. These functions are declared and related macros are
defined in “stdio.h” which is a header file in C language.
 We have to include “stdio.h” file as shown in below C program to make use of these printf()
and scanf() library functions in C language.
1. PRINTF() FUNCTION IN C LANGUAGE:
 In C programming language, printf() function is used to print the “character, string, float,
integer, octal and hexadecimal values” onto the output screen.
 We use printf() function with %d format specifier to display the value of an integer variable.
 Similarly %c is used to display character, %f for float variable, %s for string
variable, %lf for double and %x for hexadecimal variable.
 To generate a newline,we use “\n” in C printf() statement.

Note:
 C language is case sensitive. For example, printf() and scanf() are different
from Printf() and Scanf(). All characters in printf() and scanf() functions
must be in lower case.

EXAMPLE PROGRAM FOR C PRINTF() FUNCTION:


#include <stdio.h>

int main() OUTPUT:


Character is A
{ String is fresh2refresh.com
   char ch = 'A'; Float value is 10.234000
Integer value is 150
   char str[20] = "fresh2refresh.com"; Double value is 20.123456
Octal value is 226
   float flt = 10.234; Hexadecimal value is 96
   int no = 150; _____________________________________________
You can see the output with the same data which are placed
   double dbl = 20.123456; within the double quotes of printf statement in the program
except
   printf("Character is %c \n", ch);
• %d got replaced by value of an integer variable (no),
   printf("String is %s \n" , str);
• %c got replaced by value of a character variable (ch),
   printf("Float value is %f \n", flt); • %f got replaced by value of a float variable (flt),
• %lf got replaced by value of a double variable (dbl),
   printf("Integer value is %d\n" , no); • %s got replaced by value of a string variable (str),
• %o got replaced by a octal value corresponding to
   printf("Double value is %lf \n", dbl); integer variable (no),
• %x got replaced by a hexadecimal value
   printf("Octal value is %o \n", no); corresponding to integer variable
• \n got replaced by a newline.
   printf("Hexadecimal value is %x \n", no);

   return 0;

}
2. SCANF() FUNCTION IN C LANGUAGE:

• In C programming language, scanf() function is used to read character, string, numeric


data from keyboard

• Consider below example program where user enters a character. This value is assigned to
the variable “ch” and then displayed.

• Then, user enters a string and this value is assigned to the variable “str” and then
displayed.

EXAMPLE PROGRAM FOR PRINTF() AND SCANF() FUNCTIONS IN C


PROGRAMMING LANGUAGE:

#include <stdio.h>
OUTPUT:
int main()
Enter any character
{
a
   char ch; Entered character is a
Enter any string ( upto 100 character )
   char str[100]; hi
Entered string is hi
   printf("Enter any character \n");
• The format specifier %d is used in
   scanf("%c", &ch);
scanf() statement. So that, the value entered
   printf("Entered character is %c \n", ch); is received as an integer and %s for string.
• Ampersand is used before variable name
   printf("Enter any string ( upto 100 character ) \n"); “ch” in scanf() statement as &ch.
• It is just like in a pointer which is used
   scanf("%s", &str); to point to the variable. For more information
about how pointer works, please click here.
   printf("Entered string is %s \n", str);

KEY POINTS TO REMEMBER IN C PRINTF() AND SCANF():


1. printf() is used to display the output and scanf() is used to read the inputs.
2. printf() and scanf() functions are declared in “stdio.h” header file in C library.
All syntax in C language including printf() and scanf() functions are case sensitive.

You might also like