Found 34494 Articles for Programming

What is the difference between printf() and cout in C++?

Chandu yadav
Updated on 25-Jun-2020 09:47:49

2K+ Views

printf()This is mainly used in C language. It is a formatting function that prints to the standard out. It prints to the console and takes a format specifier to print. It returns an integer value. It is not type safe in input parameters. It can be used in C++ language too.Here is the syntax of printf() in C and C++ language, printf(“string and format specifier”, variable_name);Here, String − Any text/message to print on console.Format Specifier  − According to the variable datatype, use format specifiers like %d, %s etc.variable_name  − Any name given to declare the variable.Here is an example of ... Read More

How do I convert a char to an int in C and C++?

Arjun Thakur
Updated on 15-Sep-2023 02:26:23

22K+ Views

In C language, there are three methods to convert a char type variable to an int. These are given as follows −sscanf()atoi()TypecastingHere is an example of converting char to int in C language, Example Live Demo#include #include int main() {    const char *str = "12345";    char c = 's';    int x, y, z;    sscanf(str, "%d", &x); // Using sscanf    printf("The value of x : %d", x);    y = atoi(str); // Using atoi()    printf("The value of y : %d", y);    z = (int)(c); // Using typecasting    printf("The value of z ... Read More

#pragma Directive in C/C++

Ankith Reddy
Updated on 25-Jun-2020 09:50:00

3K+ Views

The preprocessor directive #pragma is used to provide the additional information to the compiler in C/C++ language. This is used by the compiler to provide some special features.Here is the syntax of #pragma directive in C/C++ language, #pragma token_nameThe table of some of #pragma directives in C/C++ language is given as follows, Sr.No.#pragma Directives & Description1#pragma startupBefore the execution of main(), the function specified in pragma is needed to run.2#pragma exitBefore the end of program, the function specified in pragma is needed to run.3#pragma warnUsed to hide the warning messages.4#pragma GCC dependencyChecks the dates of current and other file. If ... Read More

Pass an array by value in C

Arjun Thakur
Updated on 25-Jun-2020 09:51:32

654 Views

Here is an example of passing array by value in C language, Example Live Demo#include float avg(float a[]) {    int i;    float avg, sum = 0.0;    for (i = 0; i < 6; ++i) {       sum += a[i];    }    avg = (sum / 6);    return avg; } int main() {    float avg1, a[] = {63, 21, 34.4, 12.5, 3, 2.2};    avg1 = avg(a);    printf("Average : %f", avg1);    return 0; }OutputHere is the outputAverage : 22.683332In the above program, The actual code of calculating average is ... Read More

Reverse a string in C/C++

Chandu yadav
Updated on 25-Jun-2020 09:32:04

1K+ Views

Here is an example to reverse a string in C language, Example#include #include int main() {    char s[50], t;    int i = 0, j = 0;    printf("Enter the string to reverse :");    gets(s);    j = strlen(s) - 1;    while (i < j) {       t = s[i];       s[i] = s[j];       s[j] = t;       i++;       j--;    }    printf("Reverse string is : %s", s);    return (0); }OutputHere is the outputEnter the string to reverse: Here ... Read More

Character arithmetic in C

George John
Updated on 25-Jun-2020 09:33:55

4K+ Views

Character arithmetic is used to implement arithmetic operations like addition and subtraction on characters in C language. It is used to manipulate the strings. When the characters are used with the arithmetic operations, it converts them into integer value automatically i.e. ASCII value of characters.Here is an example of character arithmetic in C language, Example Live Demo#include int main(){    char s = 'm';    char t = 'z' - 'y';    printf("%d", s);    printf("%c", s);    printf("%d", (s+1));    printf("%c", (s+1));    printf("%d", (s-1));    printf("%c", (s-1));    printf("%d", t);    // printf("%c", t);   ... Read More

C Program for LowerCase to UpperCase and vice-versa

Ankith Reddy
Updated on 02-Sep-2023 13:20:59

61K+ Views

Here is the program to convert a string to uppercase in C language,Example Live Demo#include #include int main() {    char s[100];    int i;    printf("Enter a string : ");    gets(s);    for (i = 0; s[i]!='\0'; i++) {       if(s[i] >= 'a' && s[i] = 'a' && s[i] = 'A' && s[i] = 'A' && s[i]

Clearing input buffer in C/C++

Arjun Thakur
Updated on 25-Jun-2020 09:36:21

22K+ Views

The function fflush(stdin) is used to flush or clear the output buffer of the stream. When it is used after the scanf(), it flushes the input buffer also. It returns zero if successful, otherwise returns EOF and feof error indicator is set.Here is the syntax of fflush(stdin) to clear the input buffer in C language, int fflush(FILE *stream);Here is an example of fflush(stdin) to clear the input buffer in C language, Example Live Demo#include #include int main() {    char s[20];    printf("Enter the string : ", s);    scanf("%s", s);    printf("The entered string : %s", s); ... Read More

Standard header files in C

George John
Updated on 04-Oct-2023 12:26:13

25K+ Views

In C language, header files contain the set of predefined standard library functions. The "#include" preprocessing directive is used to include the header files with ".h" extension in the program. Here is the table that displays some of the header files in C language, Sr.No. Header Files & Description 1 stdio.hInput/Output ... Read More

Header files “stdio.h” and “stdlib.h” in C

Ankith Reddy
Updated on 25-Jun-2020 09:39:07

14K+ Views

stdio.hThe header file stdio.h stands for Standard Input Output. It has the information related to input/output functions.Here is the table that displays some of the functions in stdio.h in C language, Sr.No.Functions & Description1printf()It is used to print the strings, integer, character etc on the output screen.2scanf()It reads the character, string, integer etc from the keyboard.3getc()It reads the character from the file.4putc()It writes the character to the file.5fopen()It opens the file and all file handling functions are defined in stdio.h header file.6fclose()It closes the opened file.7remove()It deletes the file.8fflush()It flushes the file.Here is an example of stdio.h in C language, ... Read More

Advertisements