Typedef‘C’ allows to define new datatype names using the ‘typedef’ keyword. Using ‘typedef’, we cannot create a new datatype but define a new name for already existing type.Syntaxtypedef datatype newname;Exampletypedef int bhanu; int a; bhanu a; %dThis statement tells the compiler to recognize ‘bhanu’ as another name for ‘int’.‘bhanu’ is used to create another variable ‘a’ .‘bhanu a ‘declares ‘a’ as a variable of type ‘int’.Example#include main (){ typedef int hours; hours h; //int h; clrscr (); printf("Enter hours”); scanf ("%d”, &h); printf("Minutes =%d”, h*60); printf("Seconds = %d”, h*60*60); getch (); ... Read More
ProblemCan we declare a structure with no members in C, if yes what will be the size of that structure?SolutionYes, it is allowed in C programming language that we can declare a structure without any member and in that case the size of the structure with no members will be 0 (Zero). It will be a Zero size structure.Example Live Demo#include //structure with no members struct temp{ }; int main(){ //declaring structure variable struct temp T; printf("Size of T: %d", sizeof(T)); return 0; }OutputIn this C program, we are declaring a structure named "temp" without declare ... Read More
The atoi() is predefined function used to convert a numeric string to its integer value.Create a customized atoi()The atoi() only converts a numeric string to integer value, so we need to check the validity of the string.If this function encounters any non-numeric character in the given string, the conversion from string to integer will be stopped.Example Live Demo#include #include #include int main(){ int value; char string1[] = "3567"; value = atoi(string1); printf("String value = %s", string1); printf("Integer value = %d", value); char string2[] = "TutorialsPoint"; value = atoi(string2); printf("String value ... Read More
Following is the logic we implement to find alphabets, digits and special characters −for(number=0;string[number]!='\0';number++) {// for loop until endof string if(string[number]>='a'&&string[number]='A'&&string[number]='0'&&string[number]='a'&&string[number]='A'&&string[number]='0'&&string[number]
Character analysis and conversion functionsThe predefined functions in “ctype.h” library is for analyzing the character input and converting them.Analysis functionsS.NoFunctionDescription1isalpha()An alphabet or not2isdigit()A digit or not3isspace()A space, a new line or tab4ispunct()A special symbol or not5slower()A lower case letter of alphabet6isupper()An upper case letter of alphabet7isalphanumeric()An alphabet/digit or notConverting functionsFunctionDescriptiontolower()Converts an upper case alphabet to lower casetoupper()Converts a lower case alphabet to upper caseExampleLet us see a program to demonstrate character analysis and conversion functions − Live Demo#include #include void main(){ //Initializing compile time character variable// char variable = 'A'; //Reading User I/P// //printf("Enter the character : "); ... Read More
Before going to know about how to convert upper case to lower case letters without string convert function.Let us have a look on program to convert upper to lower using convert function, then you will get a clarity on what we are doing in the program −Example#include #include int main(){ char string[50]; printf("enter a string to convert to lower case"); gets(string); /reading the string printf("The string in lower case: %s", strlwr(string)); //strlwr converts all upper to lower return 0; }Outputenter a string to convert to lower case CProgramming LangUage The string ... Read More
The strlen () functionIt returns the number of characters in a string.Syntaxint strlen (string name)In this program, with the help of gets function reading the name at run time and trying to print the length of that name using strlen() function, this function returns an integer value and try to print that no using printf.Example 1 Live Demo#include #include void main(){ //Declaring string and length// char name[25]; int length; //Reading Input from user// printf("Enter your name : "); gets(name); length=strlen(name); //Printing name// printf("Your name is : "); puts(name); printf("Length of ... Read More
ProblemWhat do you mean by String to number and number to string conversion in C programming language?SolutionThere are two functions available for conversion. They are −sscanf() − convert string to numbersprintf () − used for converting number to stringString to number conversionWe can convert string to number using the sscanf() function −Syntaxsscanf (string name, “control string”, variable list)Example#include main (){ char a[20] = “02 01 2010”; int day, mon, yr; clrscr(); sscanf (a, “%d%d %d”, &day, &mon, &yr); printf ( “Day =%d”, day); printf ( “Month = %d”, mon); printf ( “Year = ... Read More
Arrays of pointers (to strings)Array of pointers is an array whose elements are pointers to the base address of the string.It is declared and initialized as follows −char *a[3 ] = {"one", "two", "three"}; //Here, a[0] is a ptr to the base add of the string "one" //a[1] is a ptr to the base add of the string "two" //a[2] is a ptr to the base add of the string "three"AdvantagesUnlink the two-dimensional array of characters. In (array of strings), in array of pointers to strings there is no fixed memory size for storage.The strings occupy as many bytes as ... Read More
Pointer is a variable which stores the address of other variable.Features of PointersFollowing are the features of pointers −Saves the memory spaceExecution time is faster because of direct access to memory location.The memory is accessed efficiently with the pointer i.e. dynamically memory is allocated and deallocated.Pointers are used with data structures.Here is an example for search demonstration −We can access and print a particular character in a string by using pointers.Following example shows how to access the elements using pointer −Example Live Demo#include int main(){ char array[5] = "Tutorial", *ptr, i, *ptr1; ptr = &array[1]; ptr1 = ptr ... Read More