Page 1 :
Programming in C, Program, Program is a collection of instructions that performs a specific task when executed by a computer., , Programming languages, A programming language is a set of commands, instructions and other syntax use to create a software, program. Programming languages are used in computer programming to implement algorithms., , Introduction to C Programming language, C is a high-level and general-purpose programming language that is ideal for developing firmware, or portable applications. Originally intended for writing system software, C was developed at AT, & T Bell Laboratories by Dennis Ritchie for the Unix Operating System in the early 1970s, C was derived from a language called B which was written by Ken Thompson; hence the name C, to indicate it is a successor of language B., , History of C, By 1960 computer languages had come into existence, almost each for a specific, purpose. For ex: COBOL was being used for commercial applications, FOTRAN for, engineering and scientific application. At this stage people started thinking that instead of, learning and using so many languages for different purpose, why not use only one language,, which can programs all possible applications. Therefore, an international committee was, set up to develop such language. This committee came out with a language called ALGOL, 60. However, ALGOL 60 never really becomes popular. So a new language called Combinedprogramming language (CPL) was developed at Cambridge University. CPL was turned out, to be a so big, having so many features, that it was hard to learn and difficult to implement., Basic Combined programming language (BCPL) was developed by Martin Richard at, Cambridge University aimed to solve this problem but it turned out to be too less powerful, and too specific. At the same time Ken Thompson at AT&T’s Bell laboratory wrote language, called B as a further simplification of CPL. But like BCPL B too turned out to be a very, specific. B language was modified by Dennis Ritchie and was implemented at Bell, laboratory in 1972; the new language was named C., C is a programming language which born at “AT & T‟s Bell Laboratory” of USA in 1972. C was, written by Dennis Ritchie, that‟s why he is also called as father of c programming language., Department of Computer Science, , Page 1
Page 2 :
C language was created for a specific purpose i.e. designing the UNIX operating system (which, is currently base of many UNIX based OS)., From the beginning, C was intended to be useful to allow busy programmers to get things, done because C is such a powerful, dominant and supple language, Its use quickly spread beyond Bell Labs in the late 70‟s because of its long list of strong, features, Why was Name "C" given to Language?, 1. Many of C‟s principles and ideas were derived from the earlier language B. (Ken Thompson was, the developer of B Language.), 2. BCPL and CPL are the earlier ancestors of B Language, 3. CPL is common Programming Language. In 1967, BCPL Language ( Basic CPL ) was created as a, scaled down version of CPL, 4. As many of the features were derived from “B” Language thats why it was named as “C”., 5. After 7-8 years C++ came into existence which was first example of object oriented programming, , Pictorial Summary of C Programming Language History, , Features of C programming language, It is a robust language with a rich set of built-in functions and operators that can be used, to write any complex program., C is a structured programming language in the sense that we can break the program, into parts using functions. So, it is easy to understand and modify., Department of Computer Science, , Page 2
Page 3 :
The C compiler combines the capabilities of an assembly language with features of a highlevel language. And therefore it is well suited for system programming, , (compliers, loaders, linkers, assemblers, etc) as well as for application, programming (word processor, spreadsheets, employee database, games)., Thus bridging the gap between the machine language and high-level language., This is the reason that C is also known as middle level language., Programs Written in C are efficient and fast. This is due to its variety of data type and, powerful operators. . It has relatively small instruction set and its strength lies in, , its built in function. Several standard functions are available which can be used, for developing programs., C is highly portable. This means that C programs written for one computer can, be run on another with a little or no modification. Portability is important if, we plan to use a new computer with a different operating system., C program has an ability to extend itself. A C program is basically a collection of functions, that are supported by C library. We can also create our own function and add it to C, library. With this availability of large number of functions, the programming, , task becomes simple. C compilers are available for computers of all sizes. The, compilers are usually compact and they generate object programs that are, small and highly efficient., C language is the most widely used language in operating systems and embedded system, development today., Programming style :, 1. Unlike COBOL and FORTRAN ‘c’ doesn’t have any restriction while typing and hence ‘c’ is a, free format language. However one should follow a particular style while coding a program., 2. ‘c’ is not a case sensitive language but we follow that program statements must be written in, lower case. Upper case must be used for naming symbolic constants., 3. While writing a group of statements within braces { }, the opening and closing braces must, be aligned. The statements within the braces must be intended., 4. Set of statements in “C” can be written in separate lines .we can write in the single line but it, is not preferred., , Simple C program, #include<stdio.h> void, main(), {, printf(“Hello world”); }, , Department of Computer Science, , Page 3
Page 4 :
Save C program:, Save any C program using .c extension with file name. For example your program name is helloworld,, then it save with hello world.c, , Basic structure of C, Any C program is consists of 6 main sections., 1., 2., 3., 4., 5., 6., , Documentation Section, Link Section, Definition Section, Global Declaration Section, main() Function Section, Subprogram Section, , Documentation Section, Department of Computer Science, , Page 4
Page 5 :
This section consists of comment lines which include the name of the program, the name of the, programmer, the author and other details like time and date of writing the program., Documentation section helps anyone to get an overview of the program, Link Section, The link section consists of the header files of the functions that are used in the program. It, provides instructions to the compiler to link functions from the system library such as using the, #include directive., Definition Section, All the symbolic constants are written in the definition section. Macros are known as symbolic, constants., Global Declaration Section, The global variables that can be used anywhere in the program are declared in the global, declaration section. This section also declares the user defined functions., , main() Function Section, It is necessary to have one main() function section in every C program. This section contains two parts,, declaration and executable part., The declaration part declares all the variables that are used in executable part. These two parts must be, written in between the opening and closing braces., Each statement in the declaration and executable part must end with a semicolon (;). The execution of, the program starts at opening braces and ends at closing braces., 1. Declaration part:, The declaration part declares all the variables used in the executable part., 2. Executable part:, There is at least one statement in the executable part. These two parts must appear between, the opening and closing braces. The program execution begins at the opening brace and ends at, the closing brace. The closing brace of the main function is the logical end of the program. All, statements in the declaration and executable part end with a semicolon., , Department of Computer Science, , Page 5
Page 6 :
Subprogram Section, The subprogram section contains all the user defined functions that are used to perform a specific task., These user defined functions are called in the main() function., If the program is a multifunction program then the sub program section contains all the user-defined, functions that are called in the main () function. User-defined functions are generally placed, immediately after the main () function, although they may appear in any order., , /*Documentation Section:, Program Name: Program to find the area of circle, */, #include<stdio.h>, , //link section, , #include<conio.h>, , //link section, , #define PI 3.14, , //definition section, , float area;, , //global declaration section, , void main(), {, float r;, , //declaration part, , printf("Enter the radius of the circle\n"); //executable, part scanf("%f",&r); area=PI*r*r;, printf("Area of the circle=%f \n",area);, message();, , //sub program, , }, void message(), { printf("This is Sub Function \n");, printf("Press any key to continue . . \n");, }, , Department of Computer Science, , Page 6
Page 7 :
Output:, Enter the radius of the circle, 3, Area of the circle=28.260000 This, is Sub Function, Press any key to continue, , Creating and Executing a C program, The steps for creating and running programs are: writing/editing, compiling, linking and, execution. This can be viewed diagrammatically as shown below:, , Department of Computer Science, , Page 7
Page 8 :
Writing/Editing: The first step in creating programs is, writing or editing the program. A, program can be written in any text editor like notepad. After writing a program, the, program must be saved, In C language, the program is saved with the extension “.c”., This is the source program written in a high-level language., , Compilation: After writing and saving the source program, the next step is compilation., Here we will use a software called as compiler, which converts a program written in, high-level language into machine language. The resultant file is known as an object file, in C. The extension of that file is “.obj”., , Linking: After compilation the next step is linking. Here software called linker is used., The linker links the program with external library files which contains the code for, Department of Computer Science, , Page 8
Page 9 :
predefined functions and creates an executable file. The extension of the executable file, is “.exe”., , Execution: Finally after the executable file is created after linking, the next step is, execution. The operating system executes the executable file which is the machine, code with the help of the CPU and other hardware components., , Compilation Process in C, The compilation is a method whereby the source code is converted into object code. It, is achieved with compiler assistance. The compiler tests the source code for syntactic, or structural errors and produces the object code if the source code is error-free., In object code or machine code, the compilation method c converts the source code, that has been taken as an input. The method of compiling can be divided into four, stages, i.e., pre-processing, compilation, assembly, and linking., A pre-processor selects the raw data as an input and extracts all the statements from, the code. The pre-processor takes in and interprets the pre-processor instruction. For, example, if the directive is accessible in the program <stdio.h>, then the pre-processor, interprets the directive and replaces it with the content of the file ‘stdio.h.’, , Following are the steps that a program goes through until it is translated into an, executable form:, •, , Preprocessor, , •, , Compiler, , •, , Assembler, , •, , Linker, , Department of Computer Science, , Page 9
Page 10 :
Preprocessor, Source code is the code written in a text editor, and an extension is provided to the, source code file “.c ». This source code is first transferred to the preprocessor, and then, that code is extended by the preprocessor. The extended code is transferred to the, compiler after the extension of the code., , Compiler, The code expanded by the preprocessor is passed to the compiler. The code is, translated to assembly code by the compiler. Or we can say that the C compiler, transforms the preprocessed code into assembler., , Department of Computer Science, , Page 10
Page 11 :
Assembler, With the help of an assembler, the assembly code is translated to object code. The, name of the assembler generated object file is similar to that of the source file. The, object file extension in DOS is ‘obj’, and the filename is ‘o’ in Unix. If the source file, name is sample.c, the object file name would be ‘sample.obj.’, , Linker, All C-written programs primarily use library functions. These library functions are precompiled, and the library files object code is stored with the .lib or .a extension. The, linker’s main function is to fuse the object code from library files with the object code of, our program. Often the situation occurs when the functions specified in other files are, referred to by our program; then linker plays a very important role here. It connects, those files’ object code to our software., So we infer that the linker’s role is to link our program’s object code to the library files’, object code and other programs. The linker’s output is the executable script. The, executable file name is similar to the source file, which varies only in its extensions. In, DOS, the executable file extension is ‘.exe’, and in UNIX, the executable file can be, renamed ‘a.out.’, , C programming Basic Concepts, A programming language is designed to help process certain kinds of data consisting of numbers,, characters and strings to provide useful output known as information. The task of processing of data is, Department of Computer Science, , Page 11
Page 12 :
accomplished by executing a sequence of precise instruction called a program. These instructions are, formed using certain symbols and words according to certain rules (or grammars). Every program, instruction must confirm precisely to the syntax rules of the language. Like any other language, C has its, own vocabulary and grammar., , Character Set:, The characters that can be used to form words, numbers and expressions depend upon the, computers on which the program is run. The character in C are grouped into the following categories., 1., 2., 3., 4., , Alphabets, Digits, Special Characters, White spaces., Alphabets:, Upper case, , A---Z, , Lower case, , a----z, , Digits:, All digits, , 0----9, , Special Characters:, *, &, <, >, +, -, #, ?, /, \, {}, [], $, @, ~, %, ^, ( ), |, etc., White spaces:, Blank space, „\b‟, Horizontal tab, New line, Vertical line, „\v‟, , „\t‟, „\n‟, , Tokens in C programming language, , Department of Computer Science, , Page 12
Page 13 :
C program is basically a collection of many functions. But it has basic building blocks, these basic, buildings blocks in C language which are constructed together to write a C program. This basic building, blocks are called Token., Each and every smallest individual unit in a C program is known as C tokens In, C, tokens are of six types. They are,, , Keywords, Keywords are predefined; reserved words used in programming that have special meanings to the, compiler. These meaning cannot be changed., Thus, the keywords cannot be used as variable names because that would try to change the existing, meaning of the keyword, which is not allowed., Keywords are part of the syntax and they cannot be used as an identifier., C language has reserved 32 words as keywords, All the Keywords are written in lower-case letters. Since C is case-sensitive, , C keywords:, Department of Computer Science, , Page 13
Page 14 :
Auto, Break, , Double, Else, , int, long, , struct, switch, , Case, Char, Const, Continue, Default, Do, , Enum, Extern, Float, For, Goto, If, , register, return, short, signed, sizeof, static, , typedef, union, unsigned, void, volatile, while, , Identifiers, Identifiers are the names of variables, union, function name, structure names., Identifiers must follow some rules., All identifiers must start with either a letter( a to z or A to Z ) or an underscore( _ ) ., They must not begin with a digit, After the first character, an identifier can have any combination of characters., C keywords cannot be used as an identifier., Identifiers in C are case sensitive, foo and Foo are two different identifiers. They, can be any length, Examples: sum, _array, x, , Department of Computer Science, , Page 14
Page 15 :
Constants or Literals, Constants in C refer to fixed values that do not change during the execution of the program., C constants can be divided into two major categories:, ➢ Numeric constants o, Integer constant o Real, constant, ➢ character constants o, Single character, constant o String, constant, Integer constants: refers to a sequence of digits. Types of integer are decimal, octal and hexadecimal., Decimal integers consist of a set of digits, 0 to 9, preceded by an optional – or + sign., Octal integer constant consists of any combination of digits from the set 0 through 7, with, leading 0., A sequence of digits preceded by 0x or 0X is considered as hexadecimal integer. Also includes, alphabets (A or a) through (F or f) which represents the numbers 10 through 15., Rules for constructing integer constant:, An integer constant must have at least one digit., It must not have a decimal point., It could be either positive or negative., If no sign is given an integer constant is assumed to be positive., No commas, non- digit characters and blanks are allowed within an integer, constant. The allowable range for integer constant is –32768 to +32767 Ex: 426,, +783, -8000., , Rules for constructing real constants :, Real constants are often called floating point constants. The real constants can be written in two form,, Fractional form and Exponential form., , Department of Computer Science, , Page 15
Page 16 :
Following rules must be followed for constructing the real constants expressed in fractional form:, A real constant must have at least one digit., It must have a decimal point It could, be either positive or negative, Default sign is positive., No commas or blanks are allowed within real constants, Ex : +325.34, -32.70, 460.90, The exponential form of representation of real constants is usually used if the value of the real constants, is either too small or too large. In exponential form of representation, the real constants are, represented in two parts. The parts appearing before „e‟ is called mantissa, where as the part following, „e‟ is called exponent., Mantissa e exponent, Following rules must be followed for constructing the real constants expressed in exponential, form:, A letter „e‟ should separate the mantissa and the exponential parts., The mantissa part may have a positive or negative sign., Default sign of mantissa is positive., The exponent must have at least one digit, which must be a positive or negative integer. Default sign is, positive., Exponent must be integer., spaces, comma and special symbols is not permitted., Range of real constants expressed in exponential form is -3.4e38 to 3.4e38 Ex, +3.2e-5, 4.2e8, -0.2e+3, -3.2e-5., Character constants are further divided into 3 types, 1. Single character constant, 2. String constant, 3. Backslash character constant, , Department of Computer Science, , Page 16
Page 17 :
1. Single character constant :, A single character constant is a single alphabet, a single digit or a single special symbol enclosed, within single inverted commas. Character constants have integer values known as ASCII values., The maximum length of a character constant can be one character. we can perform arithmetic, operations on character constants. Ex : `a`, `9`, `;`, „ „, , 2. String character constant :, A string constant is a sequence of characters enclosed within double quotes. The character may, be a letters, numbers, special character and blank space., Ex: “hello”, “1987”, “ ?..........!”, “5+3 “, “X “etc., , 3. Backslash character constant :, C supports some special backslash character constant that are used in output functions. For, ex, the symbol „\n‟ stands for new line character. A list of such backslash character is given, below., Constant, , Meaning, , „\a‟, , audible alert (bell), , „\b‟, , back space, , „\f‟, , form feed, , „\n‟, , new line, , „\r‟, , carriage return, , „\t‟, vertical tab, , horizontal tab, , „\v‟, , Symbolic constant :, We often use certain unique constants in a program. These constants may appear repeatedly in, a number of places in a program. One example of such constant is mathematic constant PI whose, value is, 3.145. Such constant are assigned by a symbolic name. The syntax is as follows, #define symbolic name value of constant, Ex : #define PI 3.145, #define f(x) x*x-25, , Department of Computer Science, , Page 17
Page 18 :
Symbolic names are also called as constant identifiers. The following rule apply to a #define, statement which define symbolic constant they are, No blank space is permitted between the sign #(hash) and the word define., A blank space is required between the #(hash)define and the symbolic name and also, between symbolic name and constant., #define shouldn‟t end with a semicolon(;)., #define may appear any where in the program but before it is used in the program., While defining a constant with #define the symbolic name should be given in capital letters only., , Variables :, A variable is a data name that may be used to store a data value. A variable may take, different values at different times during execution. Variables name can be chosen by the, programmer in a meaningful way so as to reflect its function or nature in the program., Examples: Average, height, total counter_1, class_stength., , Variable may consists of letters, digits and the underscore ( _ ) character, They must begin with a letter, some system permit under score as a first character., Length that is recognized by the system is 31 characters. However, the length should not be, more than eight characters, since first eight characters are treated as significant by many, compilers., Upper case and lower case are significant. That is Total is not same as total or, TOTAL. The variable name should not be a keyword White space is not allowed., , Assigning values to variables:, Value can be assigned to variables using the assignment operator “=”. The syntax is as follows, Data-type variable name=value;, Ex: int balance=200;, During assignment operation C converts the type of value on the right hand side to the type on the, left hand side. This may involve truncation when real value is converted to an integer. It is also, possible to assign a value to a variable at the time, when the variable is declared., Ex: float value=34.67;, , Department of Computer Science, , Page 18
Page 19 :
Declaration of variables :, Declaration of variable consists of a data type followed by one or more variable names and, ending with semicolon. Declaration of variables should be done before they are used in the program., Syntax for declaring a variable is, Data type v1, v2, -----vn;, Ex : int a,b,sum;, , Data types :, C language is rich in its data types. Data type indicates the type of data a variable can have. The, variety of data types available allows the programmer to select the type appropriate to the needs of, the application as well as machine., C supports four classes of data types, Primary (or fundamental) data types, User-defined data types, Derived data types, , Primary (or fundamental) data type:, All C compilers supports five fundamental data types, namely integer (int), character (char),, floating point (float), double precision floating point (double) and void. C also offers extended data, types such as long int and long double., , Character (char) %c :, The size of a variable of type char is 1 byte, which is 8 bit of internal storage. The qualifiers, that can be applied to char data type are unsigned and signed. The unsigned char variables have, the values between 0 to 255. The signed char variables have the values between –128 to 127., , Department of Computer Science, , Page 19
Page 20 :
Floating point (float) %f :, A float variable can have both integer and decimal part. The size of a float variable is 4, bytes. Normally six digits are used for decimals. The unsigned and signed qualifiers are not, applicable. The value it can store will range from -3.4 E -38 to +3.4 E 38., Integer (int) %d :, The int data type is used to declare the integer variables that can hold only integer values, with an optional sign. The qualifiers applying to int data type are signed, unsigned, short, and, long. The size of int variable will be normally equal to the size of the particular machine. Short, and long qualifiers provide different length of integers. The size of short int, int, long int is 8 bits,, 16 bits, 32 bits respectively. The qualifiers signed and unsigned can also be applied to short and, long to provide different range of values. The value it can store will range from -2 power (16-1), to +2 power (16-1)-1., Double :, When the accuracy provided by the floating point number using data type as float is not, sufficient, the date type double can be used to define the number. A double data type number, uses 64 bits giving a precision of 14 digits. The double type represents the data type that float, represents, but with a greater precision. To extend the precision further, we may use long, double which uses 80 bits., Note:, char: The most basic data type in C. It stores a single character and requires a, single byte of memory in almost all compilers, ., int: As the name suggests, an int variable is used to store an integer., float: It is used to store decimal numbers (numbers with floating point value) with single, precision., double: It is used to store decimal numbers (numbers with floating point value) with double, precision., void:void type as no values.This is usually used to specify the type of functions. The type of, function is said to be void when it does not return any value to the calling function., , Department of Computer Science, , Page 20
Page 21 :
Different data types also have different ranges upto which they can store numbers. These ranges, may vary from compiler to compiler, , Type, , Size(bytes), , Range, , int or signed int, , 2, , -32,768 to 32767, , unsigned int, , 2, , 0 to 65535, , short int or signed short, int, , 1, , -128 to 127, , unsigned short int, , 1, , 0 to 255, , long int or signed long int 4, , -2,147,483,648 to 2,147,483,647, , unsigned long int, , 4, , 0 to 4,294,967,295, , 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, , char or signed char, , 1, , -128 to 127, , unsigned char, , 1, , 0 to 255, , Derived data types:, Derived data types are nothing but primary data types but a little twisted or grouped together like, array, structure, union and pointer., , Used defined data type :, C supports a feature known as “type definition” that allows user to define an identifier that, would represent an existing data type. The user defined data type identifier can later be used to, declare the variables. It takes the general form as typedef type identifier;, Where type and identifier refers to existing data type and new name given to the data type, respectively., , Department of Computer Science, , Page 21
Page 23 :
Input and Output with C, , Formatted I/O Functions, C language provide us console input/output functions. As the name says, the console, input/output functions allow us to •, •, , Read the input from the keyboard by the user accessing the console., Display the output to the user at the console., , Note : These input and output values could be of any primitive data type., There are two kinds of console input/output functions •, •, , Formatted input/output functions., Unformatted input/output functions., , Formatted input/output functions, Formatted console input/output functions are used to take one or more inputs from the, user at console and it also allows us to display one or multiple values in the output to, the user at the console., , Some of the most important formatted console input/output functions are -, , printf- This function is used to display one or multiple values in the output to the user at, the console, scanf-This function is used to read one or multiple inputs from the user at the console, , Department of Computer Science, , Page 23
Page 24 :
Formatted Input and Output Functions, C provides standard functions scanf() and printf(), to perform, formatted inputs and outputs. These functions accept a format, specification string and a variable list as the parameters. The format, specification string is a character string that specifies the data type of, each variable to be input or output and the size or width of the I/O., scanf(), , The scanf() function is used for inputs formatted from standard inputs, and provides numerous conversion options for the printf() function., Syntax:, scanf(format_specifiers, &data1, &data2,……); // & is address operator, , The scanf() function reads and converts the characters from the, standard input according to the format specification string and stores, the input in the memory slots represented by the other arguments., Example:, scanf(“%d %c”,&data1,&data2);, , In the case of string data names, the data name is not prefixed with the, &., printf(), , The printf() function is used for output formatted as the standard, output according to a format specification. The format specification, string and the output data are the parameters of the printf() function., Syntax:, printf(format_specifiers, data1, data2,…..... );, , Examples:, printf(“%d %c”, data1, data2);, , The character specified after % is referred to as a conversion character, because it allows a data type to be converted to another type and, printed., , Department of Computer Science, , Page 24
Page 25 :
The format specification string can contain text as well., printf(“Number: %d\n”, data1);, printf(“%d\n%c”, data1, data2);, , Example program:, #include <stdio.h>, void main(), {, int a;, printf(“Enter a value: ”);, scanf(“%d”,&a);, printf(“Entered Value= %d”,a);, }, , Department of Computer Science, , Page 25
Page 26 :
Control Strings and escape sequences, Control strings are also known as format specifiers. They are used in formatted, input and output operations of the data. Remember, format specifiers are used for, formatting the data, for example, in printf() and scanf() ., , The Control String, or the Format String, which is the first parameter of Formatted, Console Input/Output (printf and scanf), specifies the sequence or the format or the, way in which Text will be read from the Standard Input, or printed on the Standard, Output., , Control strings are also known as format specifiers. They are used in, formatted input and output operations of the data., printf() and scanf() are used to print and recieve integer value., , Escape Sequence in C, An escape sequence in C language is a sequence of characters that doesn't, represent itself when used inside string literal or character., It is composed of two or more characters starting with backslash \. For example:, \n represents new line., , List of Escape Sequences in C, , Escape Sequence, , Meaning, , Department of Computer Science, , Page 26
Page 27 :
\a, , Alarm or Beep, , \b, , Backspace, , \f, , Form Feed, , \n, , New Line, , \r, , Carriage Return, , \t, , Tab (Horizontal), , \v, , Vertical Tab, , \\, , Backslash, , \', , Single Quote, , \", , Double Quote, , \?, , Question Mark, , \nnn, , octal number, , \xhh, , hexadecimal number, , \0, , Null, , Department of Computer Science, , Page 27
Page 28 :
#include<stdio.h>, 1. int main(){, 2., 3., , int number=50;, printf("You\nare\nlearning\n\'c\' language\n\"Do you know C language\"");, , 4. return 0;, 5. }, , Output:, You, are, learning, 'c' language, "Do you know C language", , Output specification with printf, function, In C, there are many predefined library functions are available for input and output operations. These, functions are collectively called standard input-output library functions, and all these functions are, available in stdio.h header file. The “stdio.h” is an abbreviation of the standard input-output header, file. So, in C language, any program which uses input or output operations must contain the statement, #include<stdio.h>, , The printf function in the C programming language is used for output formatting. It is used to, display information required by the user and also prints the value of the variables. It formats, the output, like the width of the output, the sign of the output e.t.c We will learn those, formatting using printf() C., , Department of Computer Science, , Page 28
Page 29 :
#include<stdio.h>, int main(), {, printf("Hi ");, printf("Hello 2025");, return 0;, }, Output:-, , Hi Hello 2025, , C Unformatted Functions, Unformatted input and output functions are only work with character data, type. Unformatted input and output functions do not require any format, specifiers. Because they only work with character data type., , Character IO Functions, getchar() Function, The getchar() function reads character type data form the input. The, getchar() function reads one character at a time till the user presses the enter, key., , Department of Computer Science, , Page 29
Page 30 :
getchar() C Program, getchar.c, , #include <stdio.h> //header file section, #include <conio.h>, int main(), {, char c;, printf("Enter a character : ");, c = getchar();, printf("\nEntered character : %c ", c);, return 0;, }, •, , Enter a character : y, • Entered character : y, , Note:, , Here, getchar() reads the input from the user and display back to the user., , Department of Computer Science, , Page 30
Page 31 :
putchar() Function, putchar() function prints only one character at a time., , putchar() C Program, putchar.c, , #include <stdio.h> //header file section, #include <conio.h>, int main(), {, char c = 'K';, putchar(c);, return 0;, }, •, , K, , Note:, , Here, variable c is assigned to a character 'K'. The variable c is displayed by, the putchar(). Use Single quotation mark ' ' for a character., , Department of Computer Science, , Page 31
Page 32 :
String IO Functions, gets() Function, The gets() function can read a full string even blank spaces presents in a string., But, the scanf() function leave a string after blank space space is detected. The, gets() function is used to get any string from the user., , gets() C Program, gets.c, , #include <stdio.h> //header file section, #include <conio.h>, int main(), {, char c[25];, printf("Enter a string : ");, gets(c);, printf("\n%s is awesome ",c);, return 0;, }, •, •, , Enter a string : Randy Orton, Randy Orton is awesome, , Note:, , The gets() function reads a string from through keyboard and stores it in, character array c[25]. The printf() function displays a string on the console., , Department of Computer Science, , Page 32
Page 33 :
puts() Function, The puts() function prints the charater array or string on the console. The puts(), function is similar to printf() function, but we cannot print other than characters, using puts() function., , puts() C Program, puts.c, , #include <stdio.h> //header file section, #include <conio.h>, int main(), {, char c[25];, printf("Enter your Name : ");, gets(c);, puts(c);, return 0;, }, •, •, , Enter your Name: john, john, , Department of Computer Science, , Page 33