C Notes

August 24, 2017 | Author: JenishBhavsarJoshi | Category: C (Programming Language), Computer Engineering, Computer Programming, Notation, Computing
Share Embed Donate


Short Description

Download C Notes...

Description

1|Page

C Programming, Library Function

‘C’ Programming Library Function Topics Covered: 1. What is the mean of #include? 2. Full form of different library header file 3. Different library function 4. Formatted Input 5. Formatted Output



What is the mean of #include ? This statement tells the compiler to search for a file ‘stdio.h’ and place its contents at this point in the program. The contents of the header file become part of the source code when it is compiled.



Full form of the different library header file: File Name stdio.h math.h conio.h stdlib.h ctype.h string.h. time.h



Full form Standard Input Output Header file. It contains different standard input output function such as printf and scanf Mathematical Header file. This file contains various mathematical function such cos, sine, pow (power), log, tan, etc. Console Input Output Header file. This file contains various function used in console output such as getch, clrscr, etc. Standard Library Header file. It contains various utility function such as atoi, exit, etc. Character TYPE Header file. It contains various character testing and conversion functions STRING Header file. It contains various function related to string operation such as strcmp, strcpy, strcat, strlen, etc. TIME handling function Header file. It contains various function related to time manipulation.

Different Library function: 1. getchar( ): This function is used to read simply one character from standard input device. The format of it is: variablename = getchar( ) ; Here the variablename should be character type. e.g char name; name = getchar ( ) ; Here computer waits until you enter one character from the input device and assign it to character variable name. You have to press enter key after inputting one character, then you are getting the next result. This function is written in ‘stdio.h’ file 2.

getche( ): This function is used to read the character from the standard input device. The format of it is: variablename = getche( ) ; Here the variablename should be character type. e.g

char name; C. B. PATEL COMP. COLLEGE, SURAT

2|Page

C Programming, Library Function name = getche( ) ; Here computer waits until you enter one character from the input device and assign it to character variable name. In getche( ) function there is no need to press enter key after inputting one character but you are getting next result immediately while in getchar( ) function you must press the enter key. This getche( ) function is written in standard library file ‘conio.h’.

3.

getch( ): This function is used to read the character from the standard input device but it will not echo (display) the character which you are inputting. The format of it is: variablename = getch( ) ; Here the variablename should be character type. e.g. char name; name = getch( ) ; Here computer waits until you enter one character from the input device and assign it to character variable name. In getch( ) function there is no need to press enter key after inputting one character but you are getting next result immediately while in getchar( ) function you must press the enter key and also it will echo (display) the character which you are entering. This getch( ) function is written in standard library file ‘conio.h’.

4.

putchar( ): This function is used to print one character on standard output device. The format of this function is: putchar(variablename) ; Here the variable name should be character type. e.g. char name; name=’p’; putchar(name); After executing the above statement you get the letter ‘p’ printed on standard output device. This function is written in ‘stdio.h’ file.

5.

isalpha( ): This function is used to check whether the character is alphabet (A to Z) or not. The format of this function is: returnvalue = isalpha(character) ; Here the returnvalue should be int type. If your character is alphabet then it will return true (non-zero value) otherwise it will return false (zero value). Here you can pass either the character variable or character constant as argument. e.g. int returnvalue; returnvalue = isalpha(‘a’) ; Here the value of the returnvalue is non-zero since the ‘a’ is alphabet. This function is written in ‘ctype.h’ file.

6.

isdigit( ): C. B. PATEL COMP. COLLEGE, SURAT

3|Page

C Programming, Library Function This function is used to check whether the character is digit (0 to 9) or not. The format of this function is: returnvalue = isdigit(character) ; Here the returnvalue should be integer type. If your character is digit then it will return true (non-zero value) otherwise it will return false (zero value). Here you can pass either the character variable or character constant as argument. e.g. int returnvalue; returnvalue = isdigit(‘3’) ; Here the value of the returnvalue is non-zero since the ‘3’ is digit. This function is written in ‘ctype.h’ file.

7.

isalnum( ): This function is used to check whether the character is digit (0 to 9) or alphabet (A to Z). The format of this function is: returnvalue = isalnum(character) ; Here the returnvalue should be integer type. If your character is digit or alphabet then it will return true (non-zero value) otherwise it will return false (zero value). Here you can pass either the character variable or character constant as argument. e.g. int returnvalue; returnvalue = isalnum(‘3’) ; Here the value of the returnvalue is non-zero since the ‘3’ is digit. This function is written in ‘ctype.h’ file.

8.

islower( ): This function is used to check whether the character is in lowercase (small a to z) or not. The format of this function is: returnvalue = islower(character) ; Here the returnvalue should be integer type. If your character is in lowercase then it will return true (non-zero value) otherwise it will return false (zero value). Here you can pass either the character variable or character constant as argument. e.g. int returnvalue; returnvalue = islower(‘a’) ; Here the value of the returnvalue is non-zero since the ‘a’ is in lowercase. This function is written in ‘ctype.h’ file.

9.

isupper( ): This function is used to check whether the character is in uppercase (A to Z) or not. The format of this function is: returnvalue = isupper(character) ; Here the returnvalue should be integer type. If your character is in uppercase then it will return true (non-zero value) otherwise it will return false (zero value). Here you can pass either the character variable or character constant as argument. e.g. int returnvalue; returnvalue = isupper(‘A’) ; Here the value of the returnvalue is non-zero since the ‘A’ is in uppercase. This function is written in ‘ctype.h’ file. C. B. PATEL COMP. COLLEGE, SURAT

4|Page 10.

C Programming, Library Function tolower( ): This function is used to convert your character into lower case from uppercase. The format of this is: returnvalue = tolower(character) ; Here the returnvalue should be character type. e.g. char returnvalue; returnvalue = tolower(‘A’) ; Here the value of the returnvalue is ‘a’ after execution. If you are using character other then uppercase alphabet as argument then it is remain as it is after executing tolower( ) function. This function is written in ‘ctype.h’ file.

11.

toupper( ): This function is used to convert your character into uppercase from lowercase. The format of this is: returnvalue = toupper(character) ; Here the returnvalue should be character type. e.g. char returnvalue; returnvalue = toupper(‘a’) ; Here the value of the returnvalue is ‘A’ after execution. If you are using character other then lowercase alphabet as argument then it is remain as it is after executing toupper( ) function. This function is written in ‘ctype.h’ file.

12.

isprint( ): This function is used to check whether the character is printable or not. Printable characters are alphabet (A to Z), digit (0 to 9), and special character ($, %, #, &, etc.) while control, alter, shift, bell, null character are non-printable character. The format of this function is: returnvalue = isprint(character) ; Here the returnvalue should be integer type. If your character is printable then it will return true (non-zero value) otherwise it will return false (zero value). Here you can pass either the character variable or character constant as argument. e.g. int returnvalue; returnvalue = isprint(‘3’) ; Here the value of the returnvalue is non-zero since the ‘3’ is printable character. This function is written in ‘ctype.h’ file.

13.

ispunct( ): This function is used to check whether the character is punctuation mark (special character. Not space, digit, alphabet, non-printable character). The format of this function is: returnvalue = ispunct(character) ; Here the returnvalue should be integer type. If your character is punctuation mark then it will return true (non-zero value) otherwise it will return false (zero value). Here you can pass either the character variable or character constant as argument. e.g. int returnvalue; returnvalue = ispunct(‘$’) ;

C. B. PATEL COMP. COLLEGE, SURAT

5|Page

C Programming, Library Function Here the value of the returnvalue is non-zero since the ‘$’ is punctuation mark. This function is written in ‘ctype.h’ file.

14.

isspace( ): This function is used to check whether the character is space character (horizontal tab, new line, vertical tab, form feed, carriage return, space) or not. The format of this function is: returnvalue = isspace(character) ; Here the returnvalue should be integer type. If your character is space character then it will return true (non-zero value) otherwise it will return false (zero value). Here you can pass either the character variable or character constant as argument. e.g. int returnvalue; returnvalue = isspace(‘ ’) ; Here the value of the returnvalue is non-zero since the ‘ ’ is space character. This function is written in ‘ctype.h’ file.



Formatted Input: 1. When you specify scanf(“%wx”,&variablename); mean you are reading the value of variablename having type ‘x’ and the width of the field is ‘w’. e.g scanf(“%2d %5d”,&a, &b) and I am inputting two number 50 and 32432 then 50 is assigned to ‘a’ and 32432 is assigned to ‘b’. But if I am entering 32432 and 50 then only 32 is assigned to ‘a’ since its field width is two and 432 is assigned to ‘b’ and 50 value is not assigned to any one. 2.

When you are reading a value of integer variable as %d and if you input float value during execution then only integer part is assigned to that variable and also it will skip the reading of other variable value. e.g. scanf(“%d”, &a); scanf(“%d”, &b); scanf(“%d”, &c); If in first scanf( ) statement I am inputting 12.34 then 12 is assigned to ‘a’ and other variables are not read. If you are reading float or integer value and you enter character value other then digit then it will skip the reading of all the other variables and not any variable has assigned the value.

3.

When you write ‘*’ instead of the field width then the value read by that field from standard input device is skipped and not assigned to any variable. e.g. scanf(“%d %*d %d”, &a, &b); and if I am inputting 12, 453 and 234 then ‘a’ has assigned value 12, value is 453 is skipped due to ‘*’ and ‘b’ has assigned value 234.

4.

You can read float value in scientific format using %e instead of %f. e.g. scanf(“%e”, &a); and if I am entering 12.5e-2 in scientific format then ‘a’ has assigned the value 0.125. C. B. PATEL COMP. COLLEGE, SURAT

6|Page 5.

C Programming, Library Function You can read string in specific format by using the special specification %[^character] and %[character] Here the first one can read the character up to the specific character is not given while second one read the character up to the specific characters are given.



Formatted Output: 1. To print the integer number for fixed no. of field we have to use %wd format. Here ‘w’ indicates the width of the field and ‘d’ indicates we are reading decimal value. e.g int a = 30; printf(“%4d”, a); Then in the output total width of character is 4 and it will print 2 space+30 on screen. Here the output is right justified printf(“%-4d”,a); Here the output is same but it is left justified. Here we have put – sign before 4d which indicates the justification is left printf(“%04d”,a); If you put zero after % sign then there is leading zeros in output instead of blank space. Here the output is 0030 in above case. printf(“%+4d”,a); We know computer put – sign before your number if number is negative but it is not put + sign before the positive number. If we put + sign after % in printf statement then it will print the + sign for the positive number. Here the sign is included into space i.e. output is + sign the 1 space (1 space used by + sign) and 30 in above case. Here if the no. of digits is greater then the specified width then automatically computer increase the width e.g. If your printing 1234 and you have specified %2d then automatically your computer will increase the field width up to the number of digits specified and print the output 1234. 2.

To format the output in fractional number (when we use float, double or long double) we have to specify the output format: %w.pf Here ‘w’ indicates the total width of the output, ‘p’ indicates the number of digits displayed after the decimal point, and ‘f’ indicates we are reading float value. e.g float a = 30.234; printf(“%10.4f”, a); Then in the output total width of character is 10 and it will print 4 digits after the decimal point. Output is 3 space, and 30.2340 If the number is 30.23424 and we have specify the %10.4f format then the your computer perform the rounding automatically after the decimal point then the output is 3 space, and 30.2342 C. B. PATEL COMP. COLLEGE, SURAT

7|Page 3.

C Programming, Library Function To print the output in scientific format: %w.pe Here ‘w’ indicates the total no. of fields allocated, ‘p’ indicates no. of digits after decimal point and ‘e’ indicates the output is in scientific format. e.g. %10.2e will print 56.345 as 5

4.

.

6

3

e

+

0

1

We can also provide the total field width and the digits after decimal point dynamically by using %*.*f format. e.g. int width, precision; float a; width=12; precision=4; a=34.2354; printf(“%*.*f”, width, precision, a); Here we have three variable ‘width’, ‘precision’, and ‘a’. We have initialized all the variables. We can also read the value from user at run time. What ever the value you are specifying for the ‘width’ and ‘precision’ that is dynamically put in place of two ‘*’.

5.

To print the character we are using same format as the integer number e.g. char c=’a’; printf(“%4c”,c); Then value of ‘c’ that is ‘a’ is printed with the preceding 3 space. If you put – sign before it the output is left justified.

6.

To print the string we are using following format: %w.ps Here ‘w’ indicates the total width, ‘p’ indicates no. of characters you want to print, and ‘s’ indicate we are printing string value. e.g.

char str[100]=”Hello”; printf(“%10.3s”, str); Here in output we are getting total width=10 but the output character is only 3 that is “Hel” since we have ‘p’ value =3.

C. B. PATEL COMP. COLLEGE, SURAT

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF