0% found this document useful (0 votes)
62 views52 pages

Chapter 1 Introduction of C Program 2025 (Part 1)

Uploaded by

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

Chapter 1 Introduction of C Program 2025 (Part 1)

Uploaded by

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

Introduction to C

Chapter 1.2 : Fundamentals of C Programming


[Link] Gaikar
[Link] Gaikar (Subject: CP)
Classification of Programming Language
• Program :A program is a set of instructions that is arranged in a sequence to
guide a computer to find a solution for the given problem.
• Program: a series of instructions that can be put into a computer in order to
make it perform an operation:
• Programming: The process of writing a program is called programming.
• Programming languages: are used in computer programming to
implement algorithms. Most programming languages consist
of instructions for computers

[Link] Gaikar (Subject: CP)


Classification of Programming Language

BASIC ,FORTRAN, C++ , JAVA ,C#


PASCAL, COBOL,C

[Link] Gaikar (Subject: CP)


Classification of Programming Language: Low Level Language
• A low level language is a programming language that is much closer to the hardware. It
requires a thorough knowledge of the hardware for which the program is being created. It
can be divided into two types, Machine Language and Assembly Language.
• Machine language is the lowest level of programming language. It is the only language that
the computer understands. All the commands and data values are expressed using 1s & 0s.
• The main advantage of machine language is that the code can run very fast and efficiently,
since it is directly executed by the CPU.
• Assembly languages are symbolic programming languages that use mnemonics (symbols)
to represent machine-language instructions. Since assembly language is close to the
machine, it is also called low-level language.
• Basically, an assembly language statement consists of a label, an operation code, and one
or more operands.
[Link] Gaikar (Subject: CP)
Classification of Programming Language
• High Level Language is much closer to human language so it is more suitable to
write code in high level language. It is more or less independent of the
particular type of computer used. And, High level language are classified in two
broad categories; they are procedure oriented programming language and
object oriented programming language.
• Most of the programmers preferred to use general purpose high level
languages like BASIC.(Beginners' All-purpose Symbolic Instruction Code),
FORTRAN, PASCAL, COBOL,C, C++ or Java to write the code for their
applications.

[Link] Gaikar (Subject: CP)


C Programming : Introduction
• C was developed in the early 1970s by Dennis Ritchie at Bell Laboratories.
• C was initially developed for writing system software(OS).
• Many other commonly used programming languages such as C++ and Java are
also based on C.
Characteristics of C
• A high level programming language.
• Small size. C has only 32 keywords. This makes it relatively easy to learn.
• Makes extensive use of function calls.
• C is well suited for structured programming. In this programming approach,
[Link] Gaikar (Subject: CP)
Introduction
• Unlike PASCAL it supports loose typing (as a character can be treated as an
integer and vice versa).
• Stable language.
• Quick language.
• Facilitates low level (bitwise) programming.
• Supports pointers to refer computer memory, array, structures and functions.
• C is a core language.
• C is a portable language.
• C is an extensible language.
[Link] Gaikar (Subject: CP)
Uses of C
• C language is primarily used for system programming. The portability, efficiency, the main()
{
Statement 1;
ability to access specific hardware addresses and low runtime demand on system Statement 2;
……………
……………
resources makes it a good choice for implementing operating systems and Statement N;
}
Function1()
embedded system applications. {
Statement 1;
• C has been so widely accepted by professionals that compilers, libraries, Statement 2;
……………
……………
and interpreters of other programming languages are often implemented Statement N;
}
Function2()
in C. {
Statement 1;
• For portability and convenience reasons, C is sometimes used as an Statement 2;
……………
……………
intermediate language by implementations of other languages. Example of }
Statement N;

FunctionN()
compilers which use C this way are BitC, Gambit, the Glasgow Haskell {
Statement 1;
Statement 2;
Compiler, Squeak, and Vala. ……………
……………
• C is widely used to implement end-user applications. }
Statement N;

[Link] Gaikar (Subject: CP)


Structure of a C Program
• A C program contains one or more functions.
• The statements in a C program are written in a logical sequence to perform a
specific task.
• Execution of a C program begins at the main() function.
• You can choose any name for the functions. Every program must contain one
function that has its name as main().

[Link] Gaikar (Subject: CP)


Your First C Program
// This is my first program in C

#include<stdio.h>
int main()
{
printf("\n Welcome to the world of C ");
return 0;
}

[Link] Gaikar (Subject: CP)


Files used in a C Program
Files in a C program

Source File Header File Object File Executable File

Source code file


• The source code file contains the source code of the program. The file
extension of any C source code file is “.c”. This file contains C source code
that defines the main function and maybe other functions. The main() is the
starting point of execution when you successfully compile and run the
program. A C program in general may include even other source code files
(with the file extension .c).

[Link] Gaikar (Subject: CP)


Files used in a C Program
Header Files
• When working with large projects, it is often desirable to make sub-routines and
store them in a different file known as header file. The advantage of header files
can be realized when
a) The programmer wants to use the same subroutines in different programs.
b) The programmer wants to change, or add, subroutines, and have those changes
be reflected in all other programs.
• Conventionally, header files names ends with a “.h” extension and its name can use
only letters, digits, dashes, and underscores.
• While some standard header files are available in C, but the programmer may also
create his own user defined header files.
[Link] Gaikar (Subject: CP)
Files used in a C Program
Object Files
• Object files are generated by the compiler as a result of processing the
source code file. Object files contain compact binary code of the function
definitions. Linker uses this object file to produce an executable file (.exe file)
by combining the of object files together. Object files have a “.o” extension,
although some operating systems including Windows and MS-DOS have a
“.obj” extension for the object file.
Binary Executable File
• The binary executable file is generated by the linker. The linker links the
various object files to produce a binary file that can be directly executed. On
Windows operating system, the executable files have “.exe” extension.

[Link] Gaikar (Subject: CP)


Compiling and Executing C Program
• The compilation process is done in two steps.
• In the first step, the preprocessor program reads the source file as text, and
produces another text file as output. Source code lines which begin with the
hash symbol are actually not written in C but in the preprocessor language.
• The output of the preprocessor is a text file which does not contain any
preprocessor statements. This file is ready to be processed by the compiler.
The linker combines the object file with library routines (supplied with the
compiler) to produce the final executable file.

[Link] Gaikar (Subject: CP)


[Link] Gaikar (Subject: CP)
Compiling and Executing C Program

Source Pre- Compiler Object


File proce
Files
ss
Library Library Linker Executable
Files Files Files

Source Pre- Compiler Object


File proce Files
ss
Library
Files

[Link] Gaikar (Subject: CP)


1.1 : Fundamentals of C Programming
• Character-set
• Identifiers
• Keywords
• data types
• constants
• variables
• From this Unit onwards, we will actually start studying about
various elements of C programming language.

[Link] Gaikar (Subject: CP)


The C Character Set: Building Blocks of Code

Alphabets
A-Z, a-z

Digits
0-9

Special Chars
(), {}, [], &, #, %

Whitespace
Space, Tab, Newline
The C character set defines the valid building blocks for writing programs,
including alphabets, digits, and a wide range of special characters crucial
for syntax. Whitespace characters like space and newline enhance code
readability. Additionally, escape sequences such as \n for a new line or \\
for a backslash allow the representation of non-printable characters and
special actions within string literals, ensuring precise text formatting.
The C character-set
• A character denotes any alphabet, digit or special symbol used to represent information.
The C character-set can be defined as a set of permissible under C language to represent
information.
• Under C language, the upper-case letters ‘A to Z’ the lower-case letters ‘a to z’ the digits
'0 to 9' & certain special symbols are all valid characters that can be used as building
blocks to construct basic program-elements.
• Depicted below is the entire C character-set:
Alphabets : A,B,C,…………………….,Y,Z (Upper Case)
a,b,c………………………,y,z (Lower Case)

Digits : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
+ - * / = % # &
Special Symbols: : ! ? ^ “, ‘ ~ \ | < >
[ ] { } : ( ) ; . , _ (blank space)

[Link] Gaikar (Subject: CP)


Tokens of C
• C programs are made up of different things termed as token.
• Smallest individual units in c program are known as tokens.
• We cab code a c program using the tokens and its syntax rule. Below listed
different units
• Keyword, identifiers, constants & variable, data types, operators

[Link] Gaikar (Subject: CP)


Identifiers: Naming Your Code Elements
Rules for Creation
• Must start with a letter (A-Z, a-z) or an underscore (_).
• Can contain letters, digits (0-9), and underscores.
• Case-sensitive: 'count' and 'Count' are different.
• Cannot be a keyword (e.g., int, if).
Identifiers are user-defined names given to various programming
elements like variables, functions, and arrays. They enable unique
referencing and manipulation of data within your code, ensuring
clarity and organization. Following these strict naming
conventions is vital for valid and readable C programs.

Valid Examples:
• totalSum
• _index
• user_data
• calculateValue

• MAX_THREADS
Identifiers C Program
Identifiers
• In a C program, every word is either classified as an identifier or a keyword.
Identifiers, as the name suggests, are used to identify or name various
program-elements such as variables, symbolic constants, functions,
structure, union etc.
• On the other hand, Keywords are a kind of reserved words which have
standard, predefined meanings in C.

[Link] Gaikar (Subject: CP)


Identifiers C Program
Rules of Identifiers
As stated before, identifiers are names that are given to various program-elements such as
variables, symbolic constants, functions, arrays, etc. There are certain rules regarding identifier
names in C, as stated below:
• Identifiers must consist of alphabets and digits and special symbol underscore(_)
• Identifiers cannot start with digit. It can start either with alphabet or underscore.
• It cannot contain any special symbol except underscore. Blank space is also not allowed.
• Identifier can be as long as required and minimum of one character: An identifier can be
arbitrarily long. Some implementations of C recognize only the first eight characters, though
most implementations recognize more (typically, 31 characters). Additional characters are carried
along for the programmer’s convenience.
• It is case sensitive i.e. uppercase and lowercase not interchange e.g. rate,RATE,Rate,ABC,abc,Abc
• It cannot be keyword: There are certain reserved words called keywords that have standard
predefined meanings in C. These keywords can be used only for their intended purpose they
cannot be used as identifiers. It is required to note here that the keywords are all lower-case.
• Since uppercase and lower- case characters are not equivalent. It is possible to utilize an upper-
case keyword as an identifier normally, however, this is not done as it is considered a poor
programming practice. [Link] Gaikar (Subject: CP)
Identifiers C Program
Examples of valid identifiers :
z xll sum_l names area tax _rate x1 x123
Examples of invalid identifiers :
7th The first character must be a letter.
“,a”, Illegal characters (“)
order-no Illegal character (-)
error flag Illegal character(blank space )
int keyword cannot declared as identifier

[Link] Gaikar (Subject: CP)


Keywords: Reserved Words with Fixed
Meaning

Type-related Storage classes


int, char, float, void ,double ,long static, extern, auto

Control Flow
if,else for, while, switch, do, break continue

Keywords are reserved words in C, each carrying a predefined meaning


to the compiler. There are 32 standard keywords, always written in
lowercase, that form the language's fundamental syntax. They cannot be
used as identifiers (names for variables, functions, etc.), as doing so
would confuse the compiler about their intended role. Categorized by
function, they include type definitions, storage specifiers, and control
flow statements, acting as the bedrock of C program structure.
KEYWORDS C Program
KEYWORDS
• Keywords are the words whose meaning has already been explained to the C
compiler. The keywords are also called 'Reserved Words’. The keywords
cannot be used as identifier names because, if we do so, we are trying to
assign a new meaning to the keyword, which is not allowed by the computer
• Following is the list of 32 keywords in C
• Note:C has a set of 32 reserved words often known as keywords. All
keywords are basically a sequence of characters that have a fixed meaning.
By convention all keywords must be written in lowercase (small) letters.

[Link] Gaikar (Subject: CP)


KEYWORDS C Program

int signed if while Auto struct


float unsigned else for Reister union
char return switch do Static volatile
double void case break extern typedef
short const default continue enum
long sizeof goto

[Link] Gaikar (Subject: CP)


Data Types in C: Defining Variable Nature
Specify the kind and size of data a variable holds.

char int
Single character, 1 byte. Integers, usually 4 bytes.

float double
Floating-point, 4 bytes. Double precision float, 8 bytes.

Modifiers _Bool (C23)


signed, unsigned, short, long. True/false values.
Data Types: Defining the Nature of Data

Integer (int) Character (char) Floating-Point (float, Void (void)


double)
Whole numbers (e.g., 25, - Single characters (e.g., 'A', Represents "no type." Used
100). Modifiers: short, '$'). Stored as small Numbers with decimals for functions that return no
long, signed, unsigned. integers. (e.g., 3.14, -0.001). double value or generic pointers.
offers higher precision.

Data types are fundamental as they define the kind of values a variable can hold and the operations applicable to them,
directly impacting memory allocation and data interpretation. C provides various basic types, along with modifiers that allow
fine-tuning of range and memory usage, enabling efficient data management in your programs.
Data Types in C
• When programming, we store the variables in our computer's memory, but the computer must Know what
we want to store in them since storing a simple number, a letter or a large number is not going to occupy the
same space in memory.
• bytes: Our computer's memory is organized in bytes. A byte is the minimum amount of memory that we can
manage. A byte can store a relatively small amount of data, usually an integer between 0 and 255 or one
single character.
• But in addition, the computer can manipulate more complex data types that come from grouping several
bytes, such as long numbers or numbers with decimals. Next you have a list of the existing fundamental data
types in C, as well as the range of values that can be represented with each one of them.
• A data type decide the type of data(value) and memory location required for storing that type of data in the
memory.
• C data types are defined as the data storage format that a variable can store a data to perform a specific operation.
• Data types are used to define a variable before to use in a program.
• Size of variable, constant and array are determined by data types.
[Link] Gaikar (Subject: CP)
Data Types in C

[Link] Gaikar (Subject: CP)


Data Types in C
1) Build-in data type: 1)int data-type. 2)char data-type. 3)float data-type. 4)double data-type.
• Now, let us discuss each one of these data-types in detail as following.
1)int data-type:
 int data-type represents the whole number (i.e., integer) quantities. Integers are required not to contain a
decimal point or an exponent. Range -32768 to +32767. eg: int a=1000;
 int data-type can also be further sub-divided into two broad categories, viz.,
(i) short int and long int (ii) signed int and unsigned int
(i) short int and long int:
 The qualifier short, when placed in front of the int declaration as C system that the particular variable being
declared will be used fairly small integer values(the topics of declaration and variable are discuss letter in this
chapter). The motivation for using short variables is primarily one of conserving the computer's memory space,
which may be an issue in cases where the program needs a lot of memory and the amount of memory available
is limited.
 On the other hand, the qualifier long, when placed in front of the int declaration, tells the C system that the
particular variable being declared will be used to store fairly large integer values, long integers cause the
program to run a bit slower, but the range of values that we can use is expanded tremendously. (range is -
2147483648 to +2147483648)
[Link] Gaikar (Subject: CP)
Data Types in C
(ii) signed int and unsigned int:
 In case of signed int (or even in case of ordinary int, short int, long int as by default even int is signed int) the
left most bit (of computer-memory) is reserved for the sign. However, In case of unsigned int, there is no
such kind of reservation and so all of the bits are used to represent the numerical value. Thus, an unsigned
int can be approximately twice as large as an ordinary int.
 One important thing to be noted here is that unsigned int is used only when it is known in Advance that the
value stored in a given integer-variable will always be positive.
(2) char data type :
 char data-type is used to represent single characters. These character values is enclosed in pair of single
quotes.
char a=’A’ ; char b=’a’;
 Hence, the char data type is one byte long ( 8 bit) used to store single character , they will have range
specified as -128 to +127 in signed format signed char & unsigned char ,both occupying one byte each ,but
different ranges. signed char range is -128 to +127 & unsigned char is 0 to 255.
 C does not provide any data type for storing text. This is because text is made up of individual characters.
[Link] Gaikar (Subject: CP)
Data Types in C
 Char supposed to store characters not number so why writing
 Character is supposed to store characters not number so why writing rang -128 to 127?
Solution is that in memory characters are stored using their ASCII codes.
//ASCII value of Alphabet and digits
‘A’-65……….‘Z’-90
‘a’-97………..‘z’-122
‘0’-48………...‘9’-57

[Link] Gaikar (Subject: CP)


Data Types in C
3) float data-type :
• Float data-type (also called floating point) represents values containing decimal places. A floating point value
is distinguished by the pre sense of a decimal point. It is permissible to omit digits before the decimal point,
or digits after the decimal point, but obviously not permissible to omit both. The values 3., 125.8 and -.0001
are all valid examples of floating point values.
• Floating point values can also be expressed in so-called scientific notation. The value 1. 7e4 is a floating point
value expressed in this notation, and represents the value 1.7 x 104. The value before the letter e is known as
the mantissa while the value that follows the letter e is called the exponent. This exponent, which may be
preceded by an optional plus or minus sign represents the power of 10 that the mantissa is to be multiplied
by. So, in the value 2 .25e-3 the 2.25 is (he value of the mantissa and -3 is the value of the exponent. This
value represents 2.25 * 10 -3 or 0.00225. Incidentally, the letter e which separates the mantissa from the
exponent can be written in either lower- or upper-case.
• A float occupied 4 byte in memory and can range from -3.4e38 to +3.4e38.

[Link] Gaikar (Subject: CP)


Data Types in C
• (4) double data-type:
• double data-type is very similar to type float. It is used whenever the accuracy provided by a float variable is
not sufficient. Variables declared be of type. double can store nearly twice as many significant digits as the
float data type can double data type occupies 8 byte in memory & has range from -1.7e308 to+1.7e 308 a
variable of double type can be declared as
• double i,j;
• if the situation demands usage of real number which lie even beyond the range offered by double data type
then there exists a long double which can range from -1.7e 4932 to +1.7e4932 .A long double occupies 10
bytes in memory

[Link] Gaikar (Subject: CP)


Data Types in C
Size in
Name Bits length Range
Bytes
int 2 16 bits length. -32768 to 32767

short int 2 16 bits length. signed: -32768 to 32767

signed int 2 16 bits length -32768 to 32767

unsigned int 2 16 bits length. unsigned: 0 to 65535

signed:-2147483648 to2147483647
long int 4 32 bits length.
unsigned: 0 to 4294967295
char 1 8 bits length. signed: -128 to 127 unsigned: 0 to 255
float 4 32 bits -3.4e38 to+3.4e38(7 digits)
double 8 64 bits -1.7e308 to+1.7e308(15digits)
long double 10 80 bits -1.2e4932 to+1.2e4932(19 digits)
[Link] Gaikar (Subject: CP)
Data Types in C
DATA TYPE SIZE IN BYTES RANGE
char 1 -128 to 127
unsigned char 1 0 to 255
signed char 1 -128 to 127
int 2 -32768 to 32767
unsigned int 2 0 to 65535
signed short int 2 -32768 to 32767

signed int 2 -32768 to 32767


short int 2 -32768 to 32767
unsigned short int 2 0 to 65535

long int 4 -2147483648 to 2147483647

unsigned long int 4 0 to 4294967295

signed long int 4 -2147483648 to 2147483647

float 4 3.4E-38 to 3.4E+38


double 8 1.7E-308 to 1.7E+308

long double 10 3.4E-4932 to 1.1E+4932

[Link] Gaikar (Subject: CP)


Variables in C
 A variable can be defined as “a quantity that varies during program execution”.
 A variable is a symbol that represents a storage location in the computer's memory. The information that is stored in
that location is called the value of the variable. One common way for a variable to obtain a value is by an assignment.
This has the syntax :
 A variable is defined as a meaningful name given to the data storage location in computer memory.
 When using a variable, we actually refer to address of the memory where the data is stored. C language supports two
basic kinds of variables. Numeric variables can be used to store either integer values or floating point values.
 While an integer value is a whole numbers without a fraction part or decimal point, a floating point number, can have a
decimal point in them.
 Numeric values may also be associated with modifiers like short, long, signed and unsigned.
 Character variables can include any letter from the alphabet or from the ASCII chart and numbers 0 – 9 that are put
between single quotes.
Note: A variable in simple terms is a storage place which has some memory allocated to it. Basically, a variable used to
store some form of data. Different types of variables require different amounts of memory
By default, C automatically a numeric variable signed.
[Link] Gaikar (Subject: CP)
Variables in C
Rules apply in variable:
 Length of variable name could be 1 to 8 characters some compilers allow variable name whose length could be up-to 40 character.
 Variable name could be combination of alphabets, digits & special symbol underscore(_).
 1st character in variable name must be an alphabet.
 No commas or blank space are allowed within variable name.
 No special symbol other than an underscore(_) can be used in a variable name .
si_int m_hra pop_e_89
Example of variable: int print; char name; float name;
Variables Declarations In C
A declaration associates a group of variables with a specific data-type. All variables must be declared before they can appear in executable
statements.
A declaration consists of a data-type, followed by one or more variable names, ending With a semicolon ,Example
int a,b,c;
float rootl,root2;
char flag;
Here a,b, and c are declared to be integer variables, rootl and root2 are floating-point variables and flag is a char-type variable.
[Link] Gaikar (Subject: CP)
Variables in C
• To declare a variable specify data type of the variable followed by its name.
• Variable names should always be meaningful and must reflect the purpose of
their usage in the program.
• Variable declaration always ends with a semicolon. Example,
syntax :
variable = expression; Variables

int emp_num; Numeric Variable Character Variables


float salary;
char grade;
double balance_amount;
unsigned short int acc_no;

[Link] Gaikar (Subject: CP)


Constants in C
• Constants are identifiers whose value does not change.
• Constants are used to define fixed values like PI or the charge on an electron so that
their value does not get changed in the program even by mistake.
• To declare a constant, precede the normal variable declaration with const keyword
and assign it a value. For example,
const float pi = 3.14;
• Another way to designate a constant is to use the pre-processor command define.
#define PI 3.14159
When the preprocessor reformats the program to be compiled by the compiler, it
replaces each defined name with its corresponding value wherever it is found in the
source program. Hence, it just works like the Find and Replace command available in a
text editor.

[Link] Gaikar (Subject: CP)


2 Way to defining constants in c programming
1) Defined constants (#define pre processor) :
You can define your own names for constants that you use quite often without having to resort to variables,
simply by using the #define preprocessor directive.
Syntax : #define identifier contsnat_value or #define symbolic_name constant_value
For example: #define PI 3.14159265
#define NEWLINE '\n'
#define WIDTH 100
They define three new constants. Once they are declared, you are able to use them in the rest of the code as
any if they were any other constant, for example:
circle = 2 * PI * r;
printf(“%d”,NEWLINE);
he #define directive is not a code instruction, it is a directive for the preprocessor, therefore it assumes the
whole line as the directive and does not require a semicolon (;) at the end of it. If you include a semicolon
character (;) at the end, it will also be added when the preprocessor will substitute any occurence of the
defined constant within the body of the program.
[Link] Gaikar (Subject: CP)
2 Way to defining constants in c programming
2) Declared constants (const keyword) :
With the const keyword declared before variable with a specific data_ type exactly as you would do with
normal a variable declaration :
Syntax : const data_type variable name = value;
const int width = 100;
const char tab = '\t';
const zip = 12440;
In case that the type was not specified (as in the last example) the compiler assumes that it is type int.

[Link] Gaikar (Subject: CP)


2 Way to defining constants in c programming
#define const keyword
#include <stdio.h> #include <stdio.h>
#define LENGTH 10 int main()
#define WIDTH 5 {
#define NEWLINE '\n' const int LENGTH = 10;
int main() const int WIDTH = 5;
{ const char NEWLINE = '\n';
int area; int area;
area = LENGTH * WIDTH; area = LENGTH * WIDTH;
printf("value of area : %d", area); printf("value of area : %d", area);
printf("%c", NEWLINE); printf("%c", NEWLINE);
return 0; return 0;
} }
/*** output /*** output
value of area : 50 */ value of area : 50 */
[Link] Gaikar (Subject: CP)
Streams
• A stream acts in two ways. It is the source of data as well as the destination of data.
• C programs input data and output data from a stream. Streams are associated with a
physical device such as the monitor or with a file stored on the secondary memory.
• In a text stream, sequence of characters is divided into lines with each line being
terminated with a new-line character (\n). On the other hand, a binary stream
contains data values using their memory representation.
• Although, we can do input/output from the keyboard/monitor or from any file but in
this chapter we will assume that the source of data is the keyboard and destination of
the data is the monitor.

[Link] Gaikar (Subject: CP)


Streams

Streams in a C program

Keyboard Data

Text Stream Binary Stream

Monitor Data

[Link] Gaikar (Subject: CP)


COMMENTS in C Program
USING COMMENTS
• It is a good programming practice to place some comments in the code to help the reader understand
the code clearly.
• Comments are just a way of explaining what a program does. It is merely an internal program
documentation.
• The compiler ignores the comments when forming the object file. This means that the comments are
non-executable statements.
C supports two types of commenting:
• // is used to comment a single statement. This is known as a line comment. A line comment can be
placed anywhere on the line and it does not require to be specifically ended as the end of the line
automatically ends the line.
• /* is used to comment multiple statements. A /* is ended with */ and all statements that lie within
these characters are commented.

[Link] Gaikar (Subject: CP)


The printf() Function
• The printf function is used to display information required to the user and
also prints the values of the variables. Its syntax can be given as
printf (“conversion string”, variable list);
• The parameter control string is a C string that contains the text that has to be
written on to the standard output device. The prototype of the control string
can be given as below:
%[flags][width][.precision][length]specifier

[Link] Gaikar (Subject: CP)


The printf() Function
flag description length Description

h When the argument is a short int or unsigned short int.


- Left-justify within the data given field width
When the argument is a long int or unsigned long int for integer
l
Displays the data with its numeric sign (either specifiers.
+
+ or -)
When the argument is a long double (used for floating point
L
Used to provide additional specifiers like o, x, specifiers)
X, 0, 0x or 0X for octal and hexa decimal
#
values respectively for values different than specifier Qualifying Input
zero. c For single character
The number is left-padded with zeroes (0) d For decimal values
0
instead of spaces F For floating point numbers
E, e Floating point numbers in exponential format
G, G Floating point numbers in the shorter of e format
o For Octal number.
s For a sequence of (string of) characters
u For Unsigned decimal value
x,X For Hexadecimal value.

[Link] Gaikar (Subject: CP)


The scanf() Function
• The scanf() is used to read formatted data from the keyboard. The syntax of the scanf() can be
given as,
scanf (“control string”, arg1, arg2, ………….argn);
• The control string specifies the type and format of the data that has to be obtained from the
keyboard and stored in the memory locations pointed by the arguments arg1, arg2,…, argn. The
prototype of the control string can be give as:
[=%[*][width][modifiers]type=]
• * is an optional argument that suppresses assignment of the input field. That is, it indicates that
data should be read from the stream but ignored (not stored in the memory location).
• width is an optional argument that specifies the maximum number of characters to be read.
• modifiers is an optional argument that can be h, l or L for the data pointed by the corresponding
additional arguments. Modifier h is used for short int or unsigned short int, l is used for long int,
unsigned long int or double values. Finally, L is used long double data values.
• Type is same as specifier in printf().

[Link] Gaikar (Subject: CP)


The scanf() Function
• EXAMPLE OF printf() and scanf():
• int num;
• float fnum;
• char ch, str[10];
• double dnum;
• short snum;
• long int lnum;
• printf(“\n Enter the values : “);
• scanf("%d %f %c %s %e %hd %ld", &num, &fnum, &ch, str, &dnum, &snum, &lnum);
• printf("\n num = %d \n fnum = %.2f \n ch = %c \n str = %s \n dnum = %e \n snum = %hd
\n lnum = %ld", num, fnum, ch, str, dnum, snum, lnum);

[Link] Gaikar (Subject: CP)

You might also like