Chapter 2
Chapter 2
Introduction to C Programming
• C is general purpose Structured programming language.
• C is high level language and is the upgraded version of ‘Basic Combined Program
Language’
• C language was designed at Bell laboratories in the early 1970’s by Dennis Ritchie.
• This language also has the features of low level languages and hence
Features of C language
• Simple general purpose language
• It has rich set of Operators
• Program execution are fast and efficient
• Can easily manipulates with bits, bytes and addresses
• Varieties of data types are available
• Separate compilation of functions is possible and such functions can be called by any C
program
• Can be applied in System programming areas like operating systems, compilers &
Interpreters, Assembles, Text Editors, Print Spoolers, Network Drivers, Modern Programs,
Data Bases, Language Interpreters, Utilities, etc.
Execution of C Program
Preprocessor directives
#define age 20
#define PI 3.1415 // symbolic constant
#define:
#include:
#undef:
#define PI 3.14
#undef PI
#ifdef:
#ifndef:
It tests the program using a certain condition; these directives can be nested too.
#include <stdio.h>
#define xyz 10
int main()
{
#ifdef xyz
printf("Your lottery number is %d.\n", xyz);
#else
printf("error printing lottery number");
#endif
return 0;
}
Header files
A C header file is a text file that contains pieces of code written in the C programming language.
The name of a header file, by convention, ends with the .h extension.
In C language, header files contain a set of predefined standard library functions. The .h is the
extension of the header files in C and we request to use a header file in our program by
including it with the C preprocessing directive “#include”.
C Header files offer the features like library functions, data types, macros, etc by importing
them into the program with the help of a preprocessor directive “#include”.
The printf () and scanf () functions are used for input and output in C language. Both functions
are inbuilt library functions, defined in stdio.h (header file).
printf () function
The printf () function is used for output. It prints the given statement to the console.
The syntax of printf () function is given below:
printf ("format string", argument_list);
The format string can be %d (integer), %c (character), %s (string), %f (float) etc.
scanf() function
The scanf() function is used for input. It reads the input data from the console.
scanf ("format string", &argument_list);
getch() function
The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly
used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes
a single value to exit from the console screen. It can also be used to read a single byte character
or string from the keyboard and then print.
syntax:
getch());
The main function is an integral part of the programming languages such as C, C++, and Java.
The main function in C is the entry point of a program where the execution of a program
starts.
It is a user-defined function that is mandatory for the execution of a program because when a
C program is executed, operating system starts executing the statements int main () function.
return_type main ()
{
// Statement 1;
// Statement 2;
// and so on..
return;
}
• Document section
comments lines
• Link Section
provides instruction to compiler to link function with program: preprocessor directives
and header files
• Definition Section
all symbolic constants are defined
• Global Declaration Section
global variables are defined
• Main Function
void main() {}
• Subprogram Section
user defined functions
#include<stdio.h>
#include<conio.h>
int main()
{
/* my first program in C */
printf("Hello, World! \n");
return 0;
}
C Tokens
• C tokens are basic building block in c language
• Individual words and punctuation marks are called tokens
• C keywords are the words that convey a special meaning to the c compiler.
• The keywords cannot be used as variable names because by doing so, we are trying to
assign a new meaning to the keyword which is not allowed.
• These are user defined names consisting of arbitrarily long sequence of letters and
digits with either a letter or the underscore (_) as a first character.
There are certain rules that should be followed while naming c identifiers:
• They must consist of only letters, digits, or underscore. No other special character is
allowed.
• record1
• file_3
• #tax
• goto
• Name-and-address
• void
Operators
• C operators are symbols that triggers an action when applied to C variables and other
objects. The data items on which operators act upon are called operands.
• Depending on the number of operands that an operator can act upon, operators can be
classified
• Unary Operators:
Those operators that require only single operand to act upon are known as unary
operators. Eg: (x++)
• Binary Operators:
Those operators that require two operands to act upon are called binary operators. Eg:
(x+y)
• Ternary Operators:
These operators requires three operands to act upon. Eg: (A > 100 ? 0 : 1)
Constants
• C constants refers to the data items that do not change their value during the program
execution.
For example “13” is a string literal and not number 13. ‘a’ and “a” are different.
2.Real Constants
3.Character Constants
1.Integer constants
• It must have at least one digit and may contain either + or – sign.
• The numbers having fractional parts are called real or floating point constants.
form or the exponent form and may also have either + or – sign preceding it.
3. Character Constants
• A character constant contains one single character enclosed within single quotes.
• It should be noted that character constants have numerical values known as ASCII
values, for example, the value of ‘A’ is 65 which is its ASCII value.
String
For example,
• “hello”
• “abc”
• “hello911”
Special Symbols
• The following special symbols are used in C having some special meaning and thus,
cannot be used for some other purpose.
[](){},;:*…=#
• Braces {}: These opening and ending curly braces marks the start and end of a block of
code containing more than one executable statement.
• Parentheses (): These special symbols are used to indicate function calls and function
parameters.
• Brackets [ ]: Opening and closing brackets are used as array element reference. These
indicate single and multidimensional subscripts.
Data Types in ‘C’
- They are
i. Integer data,
a. Character data:
• Any character of the ASCII character set can be considered as a character data types and
its maximum size can be 1 byte or 8 bits long.
• The keyword ‘int’ stands for the integer data type in C and its size is either 16 or 32 bits.
C. Floating point data: -
• The numbers which are stored in floating point representation with mantissa and
exponent are called floating point (real) numbers.
• These numbers can be declared as ‘float’ in C.
Variables
• To indicate the storage area, each variable should be given a unique name (identifier).
• Here, playerScore is a variable of integer type. The variable is assigned value: 95.
Constants
If you don't want others (or yourself) to change existing variable values, you can use
the const keyword.
This will declare the variable as "constant", which means unchangeable and read-only:
#define TRUE 1
#define pi 3.14
The escape sequence in C is the characters or the sequence of characters that can be
used inside the string literal.
The purpose of the escape sequence is to represent the characters that cannot be
used normally using the keyboard.
Some escape sequence characters are the part of ASCII charset but some are not.
Sequence Name Description
\n New Line It moves the cursor to the start of the next line.
Horizontal It inserts some whitespace to the left of the cursor and moves
\t
Tab the cursor accordingly.
Double
\” It is used to display double quotation marks.
Quote
Question
\? It is used to display a question mark.
Mark
Type Conversions
The operands of a binary operator must have the same type and the result is also of the same
type.
Integer division:
c = (9 / 5)*(f - 32)
The operands of the division are both int and hence the result also would be int. For correct
results, one may write
Typecasting in C is the process of converting one data type to another data type by the
programmer using the casting operator during program design.
In typecasting, the destination data type may be smaller than the source data type when
converting the data type to another data type, that’s why it is also called narrowing conversion.
Syntax:
int x;
float y;
y = (float) x;
Implicit type casting in C is used to convert the data type of any variable without using
the actual value that the variable holds.
It performs the conversions without altering any of the values which are stored in the
data variable.
Conversion of lower data type to higher data type will occur automatically.
Integer promotion will be performed first by the compiler. After that, it will determine
whether two of the operands have different data types.
Using the hierarchy below, the conversion would appear as follows if they both have varied
data types:
Example:
int x = 1;
char y = 'A';
x = x + y;
Ans:
x=int+char;
x=int+ASCII Value of char; x=1+65; x=66
float z = x + 1.0;
printf("x = %d, z = %f", x, z);
int a = 15, b = 2;
float div;
div=a/b;
div=?
Ans: a/b=15/2=7; div=7.0
There are some cases where if the datatype remains unchanged, it can give incorrect
output. In such cases, typecasting can help to get the correct output and reduce the
time of compilation.
In explicit type casting, we have to force the conversion between data types.
This type of casting is explicitly defined within the program.
double x = 2.6;
int sum = (int)x + 1;
printf("sum = %d", sum);
What is IDE
IDE stands for Integrated Development Environment. It is a software application that provides
facilities for developing software. It consists of tools such as source code editor, automation
tools, and debugger. Most IDEs have compilers and interpreters. Therefore, it is easier to write
the code and compile it. Some IDEs support various languages.
Tubo C
Code::Blocks
Visual Studio
NetBeans
Eclipse
Dev C++