Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C Articles
Page 91 of 96
When to use extern in C/C++
In C, the extern keyword is used to declare a variable or function that is defined elsewhere in the program. It tells the compiler that the variable or function exists, but its actual definition (memory allocation) is in another file or later in the same file. Syntax // Variable declaration using extern extern datatype variable_name; // Function declaration using extern (optional) extern return_type function_name(parameters); Parameters: datatype − The data type of the variable (int, char, float, etc.) variable_name − Name of the variable to be declared return_type − Return type of the ...
Read MoreWhat is the correct way to use printf to print a size_t in C/C++?
In C programming, size_t is an unsigned integer type used to represent the size of objects and is commonly returned by functions like sizeof and strlen. To correctly print size_t variables, we should use the %zu format specifier instead of %d or other format specifiers. Syntax printf("%zu", size_t_variable); The %zu format specifier consists of − z − length modifier that specifies the argument corresponds to a size_t type u − conversion specifier for unsigned decimal integer Example 1: Basic size_t Printing This ...
Read MoreWhat is the difference between g++ and gcc?
The gcc and g++ are both GNU compiler collection tools, but they serve different purposes. gcc is primarily designed for C programs, while g++ is designed for C++ programs. Understanding their differences helps choose the right compiler for your project. Syntax gcc program.c -o executable_name g++ program.cpp -o executable_name gcc (GNU C Compiler) The gcc compiler is specifically designed to compile C programs. It treats files with .c extension as C source code and links against the standard C library by default. Example: Using gcc for C Program #include ...
Read MorePre-increment and Post-increment concept in C/C++?
Both pre-increment and post-increment are used to increase a variable's value by 1, but they behave differently in expressions. Pre-increment (++i) increases the value before it is used, while post-increment (i++) increases it after the current expression is evaluated. Syntax // Pre-increment ++variable_name; // Post-increment variable_name++; Key Differences: Pre-increment (++i) − Increments the value first, then uses the new value in the expression Post-increment (i++) − Uses the current value in the expression, then increments it Pre-Increment Operator (++x) The pre-increment operator increments the variable's value by ...
Read MoreSwapping two variable value without using third variable in C/C++
In C programming, swapping two variables without using a third variable can be accomplished using arithmetic operations. This technique saves memory and demonstrates clever use of mathematical operations. Syntax a = a + b; b = a - b; a = a - b; Method 1: Using Addition and Subtraction This approach uses arithmetic operations to swap values without requiring additional memory − #include int main() { int a = 10, b = 20; printf("Before swapping: a ...
Read MoreHow to print a variable name in C?
In C programming, you can print the name of a variable as a string using the stringizing operator (#) within a macro. This technique is useful for debugging and logging purposes where you want to display variable names along with their values. Syntax #define MACRO_NAME(variable) #variable The # operator converts the macro parameter into a string literal. Example Here's how to create a macro that prints variable names − #include #define VariableName(name) #name int main() { int age = 25; char ...
Read MoreC function to Swap strings
In C programming, swapping strings involves exchanging the contents of two string variables. This can be achieved through character-by-character swapping or by swapping string pointers. Syntax // Character-by-character swapping void swapStrings(char str1[], char str2[]); // Pointer swapping void swapPointers(char **str1, char **str2); Method 1: Character-by-Character Swapping This method swaps each character of the strings individually − #include #include int main() { char st1[] = "My 1st string"; char st2[] = "My 2nd string"; char swap; ...
Read MoreReturn values of printf() and scanf() in C
The printf() and scanf() functions are required for output and input respectively in C. Both of these functions are library functions and are defined in the stdio.h header file. Understanding their return values is crucial for error handling and program flow control. Syntax int printf(const char *format, ...); int scanf(const char *format, ...); The printf() Function Return Value The printf() function returns the number of characters that are printed successfully. If there is an error during output, it returns a negative value. Example #include int main() { ...
Read MoreReturn type of getchar(), fgetc() and getc() in C
In C programming, the character input functions getchar(), fgetc(), and getc() all return an int value, not a char. This is crucial because they need to return both valid character values (0-255) and the special value EOF (-1) to indicate end-of-file or error conditions. Syntax int getchar(void); int fgetc(FILE *stream); int getc(FILE *stream); Why int Return Type? All three functions return int instead of char because − EOF handling: EOF is typically -1, which cannot fit in an unsigned char range (0-255) Error detection: Distinguishes between actual character values and error conditions ...
Read Morepow() function in C
The pow() function in C is used to calculate the power of a number. It computes base raised to the power of exponent and returns the result as a double. This function is declared in the math.h header file. Syntax double pow(double base, double exponent); Parameters base − The base value whose power is to be calculated exponent − The power value (exponent) Return Value Returns the value of base raised to the power of exponent as a double. Example 1: Basic Usage Here is a simple example ...
Read More