In C Programming language, there are many rules so to avoid different types of errors. One of such rule is not able to declare variable names with auto, long, etc. This is all because these are keywords. Let us check all keywords in C language.
What are Keywords?
Keywords are predefined or reserved words that have special meanings to the compiler. These are part of the syntax and cannot be used as identifiers in the program. A list of keywords in C or reserved words in the C programming language are mentioned below:
auto
| break
| case
| char
| const
| continue
| default
| do
|
double
| else
| enum
| extern
| float
| for
| goto
| if
|
int
| long
| register
| return
| short
| signed
| sizeof
| static
|
struct
| switch
| typedef
| union
| unsigned
| void
| volatile
| while
|
auto
auto is the default storage class variable that is declared inside a function or a block. auto variables can only be accessed within the function/block they are declared. By default, auto variables have garbage values assigned to them. Automatic variables are also called local variables as they are local to a function.
auto int num;
Here num is the variable of the storage class auto and its type is int. Below is the C program to demonstrate the auto keyword:
C
// C program to demonstrate
// auto keyword
#include <stdio.h>
int printvalue()
{
auto int a = 10;
printf("%d", a);
}
// Driver code
int main()
{
printvalue();
return 0;
}
break and continue
The break statement is used to terminate the innermost loop. It generally terminates a loop or a break statement. The continue statement skips to the next iteration of the loop. Below is the C program to demonstrate break and continue in C:
C
// C program to show use
// of break and continue
#include <stdio.h>
// Driver code
int main()
{
for (int i = 1; i <= 10; i++)
{
if (i == 2)
{
continue;
}
if (i == 6)
{
break;
}
printf("%d ", i);
}
return 0;
}
switch, case, and default
The switch statement in C is used as an alternate to the if-else ladder statement. For a single variable i.e, switch variable it allows us to execute multiple operations for different possible values of a single variable.
switch(Expression)
{
case '1': // operation 1
break;
case:'2': // operation 2
break;
default: // default statement to be executed
}
Below is the C program to demonstrate the switch case statement:
C
// C program to demonstrate
// switch case statement
#include <stdio.h>
// Driver code
int main() {
int i = 4;
switch (i) {
case 1:
printf("Case 1\n");break;
case 2:
printf("Case 2\n");break;
case 3:
printf("Case 3\n");break;
case 4:
printf("Case 4\n");break;
default:
printf("Default\n");break;
}
}
Note: it is best to add a break statement after every case so that switch statement doesn’t continue checking the remaining cases.
char
char keyword in C is used to declare a character variable in the C programming language.
char x = 'D';
Below is the C program to demonstrate the char keyword:
C
// C program to demonstrate
// char keyword
#include <stdio.h>
// Driver code
int main() {
char c = 'a';
printf("%c", c);
return 0;
}
const
The const keyword defines a variable who’s value cannot be changed.
const int num = 10;
Below is the C program to demonstrate the const keyword:
C
// C program to demonstrate
// const keyword
#include <stdio.h>
// Driver code
int main() {
const int a = 11;
a = a + 2;
printf("%d", a);
return 0;
}
This code will produce an error because the integer a was defined as a constant and it’s value was later on changed.
Output:
error: assignment of read-only variable 'a'
a = a + 2;
do
The do statement is used to declare a do-while loop. A do-while loop is a loop that executes once, and then checks it’s condition to see if it should continue through the loop. After the first iteration, it will continue to execute the code while the condition is true.
Below is the C program to demonstrate a do-while loop.
C
// C program to demonstrate
// do-while keyword
#include <stdio.h>
// Driver code
int main()
{
int i = 1;
do {
printf("%d ", i);
i++;
} while(i <= 5);
return 0;
}
double and float
The doubles and floats are datatypes used to declare decimal type variables. They are similar, but doubles have 15 decimal digits, and floats only have 7.
Example:
float marks = 97.5;
double num;
Below is the C program to demonstrate double float keyword:
C
// C program to demonstrate
// double float keyword
#include <stdio.h>
// Driver code
int main() {
float f = 0.3;
double d = 10.67;
printf("Float value: %f\n", f);
printf("Double value: %f\n", d);
return 0;
}
OutputFloat value: 0.300000
Double value: 10.670000
if-else
The if-else statement is used to make decisions, where if a condition is true, then it will execute a block of code; if it isn’t true (else), then it will execute a different block of code.
if(marks == 97) {
// if marks are 97 then will execute this block of code
}
else {
// else it will execute this block of code
}Below is the C program to demonstrate an if-else statement:
C
// C program to demonstrate
// if-else keyword
#include <stdio.h>
// Driver code
int main()
{
int a = 10;
if(a < 11)
{
printf("A is less than 11");
}
else
{
printf("A is equal to or "
"greater than 11");
}
return 0;
}
enum
The enum keyword is used to declare an enum (short for enumeration). An enum is a user-defined datatype, which holds a list of user-defined integer constants. By default, the value of each constant is it’s index (starting at zero), though this can be changed. You can declare an object of an enum and can set it’s value to one of the constants you declared before. Here is an example of how an enum might be used:
C
// An example program to
// demonstrate working of
// enum in C
#include<stdio.h>
// enum declaration:
enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};
// Driver code
int main()
{
//object of the enum (week), called day
enum week day;
day = Wed;
printf("%d", day);
return 0;
}
extern
The extern keyword is used to declare a variable or a function that has an external linkage outside of the file declaration.
C
#include <stdio.h>
extern int a;
int main(){
printf("%d", a);
return 0;
}
for
The “for” keyword is used to declare a for-loop. A for-loop is a loop that is specified to run a certain amount of times.
Below is the C program to demonstrate a for-loop:
C
// C program to demonstrate
// for keyword
#include <stdio.h>
// Driver code
int main()
{
for (int i = 0; i < 5; i++)
{
printf("%d ", i);
}
return 0;
}
goto
The goto statement is used to transfer the control of the program to the given label. It is used to jump from anywhere to anywhere within a function.
Example:
goto label;
// code
label:
Below is the C program to demonstrate the goto keyword:
C
// C program demonstrate
// goto keyword
#include <stdio.h>
// Function to print numbers
// from 1 to 10
void printNumbers() {
int n = 1;
label:
printf("%d ", n);
n++;
if (n <= 10) goto label;
}
// Driver code
int main(){
printNumbers();
return 0;
}
Output1 2 3 4 5 6 7 8 9 10
int
int keyword is used in a type declaration to give a variable an integer type. In C, the integer variable must have a range of at least -32768 to +32767.
Example:
int x = 10;
Below is the C program to show the int keyword:
C
// C program to demonstrate
// int keyword
#include <stdio.h>
void sum() {
int a = 10, b = 20;
int sum;
sum = a + b;
printf("%d", sum);
}
// Driver code
int main() {
sum();
return 0;
}
short, long, signed, and unsigned
Different data types also have different ranges up to which they can store numbers. These ranges may vary from compiler to compiler. Below is a list of ranges along with the memory requirement and format specifiers on the 32-bit GCC compiler.
Data Type
| Memory (bytes)
| Range
| Format Specifier
|
|---|
short int
|
2
| -32,768 to 32,767
| %hd
|
|---|
unsigned short int
|
2
| 0 to 65,535
| %hu
|
|---|
unsigned int
|
4
| 0 to 4,294,967,295
| %u
|
|---|
long int
|
4
| -2,147,483,648 to 2,147,483,647
| %ld
|
|---|
unsigned long int
|
4
| 0 to 4,294,967,295
| %lu
|
|---|
long long int
|
8
| -(2^63) to (2^63)-1
| %lld
|
|---|
unsigned long long int
|
8
| 0 to 18,446,744,073,709,551,615
| %llu
|
|---|
signed char
|
1
| -128 to 127
| %c
|
|---|
unsigned char
|
1
| 0 to 255
| %c
|
|---|
long double
|
16
| 3.4E-4932 to 1.1E+4932
| %Lf
|
|---|
Below is the C program to demonstrate the short, long, signed, and unsigned keywords:
C
// C program to demonstrate
// short, long, signed,
// and unsigned keyword
#include <stdio.h>
// Driver code
int main() {
// short integer
short int a = 12345;
// signed integer
signed int b = -34;
// unsigned integer
unsigned int c = 12;
// L or l is used for
// long int in C.
long int d = 99998L;
printf("Integer value with a short int data: %hd", a);
printf("\nInteger value with a signed int data: %d", b);
printf("\nInteger value with an unsigned int data: %u", c);
printf("\nInteger value with a long int data: %ld", d);
return 0;
}
OutputInteger value with a short int data: 12345
Integer value with a signed int data: -34
Integer value with an unsigned int data: 12
Integer value with a long int data: 99998
return
The return statement returns a value to where the function was called.
Example:
return x;
Below is the C program to demonstrate the return keyword:
C
// C program to demonstrate
// return keyword
#include <stdio.h>
int sum(int x, int y) {
int sum;
sum = x + y;
return sum;
}
// Driver code
int main() {
int num1 = 10;
int num2 = 20;
printf("Sum: %d",
sum(num1, num2));
return 0;
}
sizeof
sizeof is a keyword that gets the size of an expression, (variables, arrays, pointers, etc.) in bytes.
Example:
sizeof(char);
sizeof(int);
sizeof(float); in bytes.
Below is the C program to demonstrate sizeof keyword:
C
// C program to demonsstrate
// sizeof keyword
#include <stdio.h>
// Driver code
int main() {
int x = 10;
printf("%d", sizeof(x));
return 0;
}
register
Register variables tell the compiler to store variables in the CPU register instead of memory. Frequently used variables are kept in the CPU registers for faster access.
Example:
register char c = 's';
static
The static keyword is used to create static variables. A static variable is not limited by a scope and can be used throughout the program. It’s value is preserved even after it’s scope.
For Example:
static int num;
struct
The struct keyword in C programming language is used to declare a structure. A structure is a list of variables, (they can be of different data types), which are grouped together under one data type.
For Example:
struct Geek {
char name[50];
int num;
double var;
};Below is the C program for the struct keyword:
C
// C program to demonstrate
// struct keyword
#include <stdio.h>
#include <string.h>
struct Books {
char title[50];
char author[50];
};
// Driver code
int main( ) {
// Declare Book1 of type Book
struct Books book1;
// book 1 specification
strcpy(book1.title, "C++ Programming");
strcpy(book1.author, "Bjarne Stroustrup");
// Print book details
printf("Book 1 title : %s\n", book1.title);
printf("Book 1 author : %s\n", book1.author);
return 0;
}
OutputBook 1 title : C++ Programming
Book 1 author : Bjarne Stroustrup
typedef
The typedef keyword in C programming language is used to define a data type with a new name in the program. typedef keyword is used to make our code more readable.
For Example:
typedef long num
In this example we have changed the datatype name of “long” to “num”.
union
The union is a user-defined data type. All data members which are declared under the union keyword share the same memory location.
Example:
union GeekforGeeks {
int x;
char s;
} obj;Below is the C program for the union keyword:
C
#include <stdio.h>
union student {
int age;
char marks;
} s;
// Driver code
int main() {
s.age = 15;
s.marks = 56;
printf("age = %d", s.age);
printf("\nmarks = %d", s.marks);
}
Outputage = 56
marks = 56
void
The void keyword means nothing i.e, NULL value. When the function return type is used as the void, the keyword void specifies that it has no return value.
Example:
void fun() {
// program
}volatile
The volatile keyword is used to create volatile objects. Objects which are declared volatile are omitted from optimization as their values can be changed by code outside the scope of the current code at any point in time.
For Example:
const volatile marks = 98;
marks are declared constant so they can’t be changed by the program. But hardware can change it as they are volatile objects.
while
The while keyword is used to declare a while loop that runs till the given condition is true.
Example:
C++
#include <stdio.h>
int main() {
int i = 0;
// While loop that execute till i is less than 3
while (i < 3) {
printf("Hi\n");
i++;
}
return 0;
}
Conclusion
In this article, the points we learned about the keywords are mentioned below:
- Keywords are Reserved words in C with certain meanings.
- We can’t use keywords as any element’s name.
- There are 32 keywords in C all having unique meanings.
Keywords in C – FAQs
What are keywords in C?
Keywords in C are reserved words that have certain meanings and cannot be declare as any element’s name. For example: for is used for declaring loop and it can’t be declared as an element’s name.
How many keywords are there in the C language?
There are 32 keywords in the C language.
What is the sizeof keyword in C?
Sizeof is a keyword that gets the size of an expression, (variables, arrays, pointers, etc.) in bytes.
What is the default keyword in C?
The default keyword in C is used to specify the default case for the switch statement.
What is volatile in c used for?
Volatile keywords in C are used for volatile objects.
Difference between keywords and identifiers.
Keywords are reserved words that have some meaning whereas identifiers are the name-generated names for any variable, struct, class, object, or function in C.
Similar Reads
C Programming Language Tutorial
C is a general-purpose programming language referred as the "mother of all programming languages" because it influenced many modern programming languages like C++, Java, Python and Go. C is an excellent choice for beginners as it provides a strong foundation in programming concepts. In this C tutori
5 min read
C Basics
C Language Introduction
C is a procedural programming language initially developed by Dennis Ritchie in the year 1972 at Bell Laboratories of AT&T Labs. It was mainly developed as a system programming language to write the UNIX operating system. The main features of the C language include: General Purpose and PortableP
6 min read
Features of C Programming Language
C is a procedural programming language. It was initially developed by Dennis Ritchie in the year 1972. It was mainly developed as a system programming language to write an operating system. The main features of C language include low-level access to memory, a simple set of keywords, and a clean styl
3 min read
C Programming Language Standard
Introduction:The C programming language has several standard versions, with the most commonly used ones being C89/C90, C99, C11, and C18. C89/C90 (ANSI C or ISO C) was the first standardized version of the language, released in 1989 and 1990, respectively. This standard introduced many of the featur
6 min read
C Hello World Program
The âHello Worldâ program is the first step towards learning any programming language. It is also one of the simplest programs that is used to introduce aspiring programmers to the programming language. It typically outputs the text "Hello, World!" to the console screen. C Program to Print "Hello Wo
1 min read
Compiling a C Program: Behind the Scenes
The compilation is the process of converting the source code of the C language into machine code. As C is a mid-level language, it needs a compiler to convert it into an executable code so that the program can be run on our machine. The C program goes through the following phases during compilation:
4 min read
C Comments
The comments in C are human-readable explanations or notes in the source code of a C program. A comment makes the program easier to read and understand. These are the statements that are not executed by the compiler or an interpreter. Example: [GFGTABS] C #include <stdio.h> int main() { // Thi
3 min read
Tokens in C
In C programming, tokens are the smallest units in a program that have meaningful representations. Tokens are the building blocks of a C program, and they are recognized by the C compiler to form valid expressions and statements. Tokens can be classified into various categories, each with specific r
4 min read
Keywords in C
In C Programming language, there are many rules so to avoid different types of errors. One of such rule is not able to declare variable names with auto, long, etc. This is all because these are keywords. Let us check all keywords in C language. What are Keywords?Keywords are predefined or reserved w
13 min read
C Variables and Constants
C Variables
In C, variable is a name given to the memory location that helps us to store some form of data and retrieves it when required. It allows us to refer to memory location without having to memorize the memory address. A variable name can be used in expressions as a substitute in place of the value it s
3 min read
Constants in C
In C programming, constants are read-only values that cannot be modified during the execution of a program. These constants can be of various types, such as integer, floating-point, string, or character constants. They are initialized with the declaration and remain same till the end of the program.
3 min read
Const Qualifier in C
The qualifier const can be applied to the declaration of any variable to specify that its value will not be changed (which depends upon where const variables are stored, we may change the value of the const variable by using a pointer). The result is implementation-defined if an attempt is made to c
6 min read
Different ways to declare variable as constant in C
There are many different ways to make the variable as constant in C. Some of the popular ones are: Using const KeywordUsing MacrosUsing enum Keyword1. Using const KeywordThe const keyword specifies that a variable or object value is constant and can't be modified at the compilation time. Syntaxconst
2 min read
Scope rules in C
The scope of a variable in C is the block or the region in the program where a variable is declared, defined, and used. Outside this region, we cannot access the variable, and it is treated as an undeclared identifier. The scope is the area under which a variable is visible.The scope of an identifie
6 min read
Internal Linkage and External Linkage in C
In C, linkage is a concept that describes how names/identifiers can or cannot refer to the same entity throughout the whole program or a single translation unit. The above sounds similar to scope, but it is not so. To understand what the above means, let us dig deeper into the compilation process. B
5 min read
Global Variables in C
Prerequisite: Variables in C In a programming language, each variable has a particular scope attached to them. The scope is either local or global. This article will go through global variables, their advantages, and their properties. The Declaration of a global variable is very similar to that of a
3 min read
C Data Types
Data Types in C
Each variable in C has an associated data type. It specifies the type of data that the variable can store like integer, character, floating, double, etc. Each data type requires different amounts of memory and has some specific operations which can be performed over it. The data types in C can be cl
7 min read
Literals in C
In C, Literals are the constant values that are assigned to the variables. Literals represent fixed values that cannot be modified. Literals contain memory but they do not have references as variables. Generally, both terms, constants, and literals are used interchangeably. For example, âconst int =
4 min read
Escape Sequence in C
The escape sequence in C is the characters or the sequence of characters that can be used inside the string literal. The purpose of the escape sequence is to represent the characters that cannot be used normally using the keyboard. Some escape sequence characters are the part of ASCII charset but so
5 min read
bool in C
The bool in C is a fundamental data type in most that can hold one of two values: true or false. It is used to represent logical values and is commonly used in programming to control the flow of execution in decision-making statements such as if-else statements, while loops, and for loops. In this a
6 min read
Integer Promotions in C
Some data types like char , short int take less number of bytes than int, these data types are automatically promoted to int or unsigned int when an operation is performed on them. This is called integer promotion. For example no arithmetic calculation happens on smaller types like char, short and e
2 min read
Character Arithmetic in C
As already known character range is between -128 to 127 or 0 to 255. This point has to be kept in mind while doing character arithmetic. What is Character Arithmetic?Character arithmetic is used to implement arithmetic operations like addition, subtraction, multiplication, and division on characters
2 min read
Type Conversion in C
In C, type conversion refers to the process of converting one data type to another. It can be done automatically by the compiler or manually by the programmer. The type conversion is only performed to those data types where conversion is possible. Let's take a look at an example: [GFGTABS] C #includ
4 min read
C Input/Output
Basic Input and Output in C
In C programming, input and output operations refer to reading data from external sources and writing data to external destinations outside the program. C provides a standard set of functions to handle input from the user and output to the screen or to files. These functions are part of the standard
2 min read
Format Specifiers in C
The format specifier in C is used to tell the compiler about the type of data to be printed or scanned in input and output operations. They always start with a % symbol and are used in the formatted string in functions like printf(), scanf, sprintf(), etc. The C language provides a number of format
6 min read
printf in C
In C language, printf() function is used to print formatted output to the standard output stdout (which is generally the console screen). The printf function is the most used output function in C and it allows formatting the output in many ways. Example: [GFGTABS] C #include <stdio.h> int main
6 min read
scanf in C
In C, scanf is a function that stands for Scan Formatted String. It is the most used function to read data from stdin (standard input stream i.e. usually keyboard) and stores the result into the given arguments. It can accept character, string, and numeric data from the user using standard input. It
4 min read
Scansets in C
scanf family functions support scanset specifiers which are represented by %[]. Inside scanset, we can specify single character or range of characters. While processing scanset, scanf will process only those characters which are part of scanset. We can define scanset by putting characters inside squ
2 min read
Formatted and Unformatted Input/Output functions in C with Examples
This article focuses on discussing the following topics in detail- Formatted I/O Functions.Unformatted I/O Functions.Formatted I/O Functions vs Unformatted I/O Functions.Formatted I/O Functions Formatted I/O functions are used to take various inputs from the user and display multiple outputs to the
9 min read
C Operators
Operators in C
In C language, operators are symbols that represent operations to be performed on one or more operands. They are the basic components of the C programming. In this article, we will learn about all the built-in operators in C with examples. What is a C Operator?An operator in C can be defined as the
14 min read
Arithmetic Operators in C
Arithmetic operators are the type of operators used to perform basic math operations like addition, subtraction, and multiplication. Let's take a look at an example: [GFGTABS] C #include <stdio.h> int main() { // Calculate the area of the triangle int sum = 10 + 20; printf("%d", sum)
5 min read
Unary Operators in C
In C programming, unary operators are operators that operate on a single operand. These operators are used to perform operations such as negation, incrementing or decrementing a variable, or checking the size of a variable. They provide a way to modify or manipulate the value of a single variable in
6 min read
Relational Operators in C
In C, relational operators are the symbols that are used for comparison between two values to understand the type of relationship a pair of numbers shares. The result that we get after the relational operation is a boolean value, that tells whether the comparison is true or false. Relational operato
4 min read
Bitwise Operators in C
In C, the following 6 operators are bitwise operators (also known as bit operators as they work at the bit-level). They are used to perform bitwise operations in C. The & (bitwise AND) in C takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if bot
7 min read
C Logical Operators
Logical operators in C are used to combine multiple conditions/constraints. Logical Operators returns either 0 or 1, it depends on whether the expression result is true or false. In C programming for decision-making, we use logical operators. We have 3 logical operators in the C language: Logical AN
6 min read
Assignment Operators in C
In C, assignment operators are used to assign values to variables. The left operand is the variable and the right operand is the value being assigned. The value on the right must match the data type of the variable otherwise, the compiler will raise an error. Let's take a look at an example: [GFGTAB
5 min read
Increment and Decrement Operators in C
The increment ( ++ ) and decrement ( -- ) operators in C are unary operators for incrementing and decrementing the numeric values by 1 respectively. The incrementation and decrementation are one of the most frequently used operations in programming for looping, array traversal, pointer arithmetic, a
4 min read
Conditional or Ternary Operator (?:) in C
The conditional operator in C is kind of similar to the if-else statement as it follows the same algorithm as of if-else statement but the conditional operator takes less space and helps to write the if-else statements in the shortest way possible. It is also known as the ternary operator in C as it
3 min read
sizeof operator in C
Sizeof is a much-used operator in the C. It is a compile-time unary operator which can be used to compute the size of its operand. The result of sizeof is of the unsigned integral type which is usually denoted by size_t. sizeof can be applied to any data type, including primitive types such as integ
3 min read
Operator Precedence and Associativity in C
Operator precedence and associativity are rules that decide the order in which parts of an expression are calculated. Precedence tells us which operators should be evaluated first, while associativity determines the direction (left to right or right to left) in which operators with the same preceden
8 min read
C Control Statements Decision-Making
Decision Making in C (if , if..else, Nested if, if-else-if )
The conditional statements (also known as decision control structures) such as if, if else, switch, etc. are used for decision-making purposes in C programs. They are also known as Decision-Making Statements and are used to evaluate one or more conditions and make the decision whether to execute a s
11 min read
C - if Statement
The if in C is the simplest decision-making statement. It consists of the test condition and a block of code that is executed if and only if the given condition is true. Otherwise, it is skipped from execution. Let's take a look at an example: [GFGTABS] C #include <stdio.h> int main() { int n
4 min read
C if else Statement
The if else in C is an extension of the if statement which not only allows the program to execute one block of code if a condition is true, but also a different block if the condition is false. This enables making decisions with two possible outcomes. Let's take a look at an example: [GFGTABS] C #in
3 min read
C if else if ladder
In C, if else if ladder is an extension of if else statement used to test a series of conditions sequentially, executing the code for the first true condition. A condition is checked only if all previous ones are false. Once a condition is true, its code block executes, and the ladder ends. Example:
3 min read
Switch Statement in C
In C, switch statement is a control flow structure that allows you to execute one of many code blocks based on the value of an expression. It is often used in place of if-else ladder when there are multiple conditional codes. Example: [GFGTABS] C #include <stdio.h> int main() { // Switch varia
6 min read
Using Range in switch Case in C
You all are familiar with switch case in C, but did you know you can use a range of numbers instead of a single number or character in the case statement? Range in switch case can be useful when we want to run the same set of statements for a range of numbers so that we do not have to write cases se
2 min read
C - Loops
Loops in programming are used to repeat a block of code until the specified condition is met. A loop statement allows programmers to execute a statement or group of statements multiple times without repetition of code. [GFGTABS] C // C program to illustrate need of loops #include <stdio.h> int
6 min read
C for Loop
In C programming, the for loop is used to repeatedly execute a block of code as many times as instructed. It uses a variable (loop variable) whose value is used to decide the number of repetitions. It is generally used when we know how many times we want to repeat the code. Let's take a look at an e
4 min read
while Loop in C
The while loop in C allows a block of code to be executed repeatedly as long as a given condition remains true. It is often used when we want to repeat a block of code till some condition is satisfied. Let's take a look at an example: [GFGTABS] C #include <stdio.h> int main() { int i = 1; // C
5 min read
do...while Loop in C
The do...while loop is a type of loop in C that executes a block of code until the given condition is satisfied. The feature of do while loops is that unlike the while loop, which checks the condition before executing the loop, the do...while loop ensures that the code inside the loop is executed at
4 min read
For vs. While
In C, loops are the fundamental part of language that are used to repeat a block of code multiple times. The two most commonly used loops are the for loop and the while loop. Although they achieve the same result, their structure, use cases, and flexibility differ. The below table highlights some pr
4 min read
Continue Statement in C
The continue statement in C is a jump statement used to skip the current iteration of a loop and continue with the next iteration. It is used inside loops (for, while, or do-while) along with the conditional statements to bypass the remaining statements in the current iteration and move on to next i
4 min read
Break Statement in C
The break in C is a loop control statement that breaks out of the loop when encountered. It can be used inside loops or switch statements to bring the control out of the block. The break statement can only break out of a single loop at a time. Let's take a look at an example: [GFGTABS] C #include
5 min read
goto Statement in C
The goto statement in C allows the program to jump to some part of the code, giving you more control over its execution. While it can be useful in certain situations, like error handling or exiting complex loops, it's generally not recommended because it can make the code harder to read and maintain
4 min read
C Functions
C Functions
A function in C is a set of statements that when called perform some specific tasks. It is the basic building block of a C program that provides modularity and code reusability. The programming statements of a function are enclosed within { } braces, having certain meanings and performing certain op
10 min read
User-Defined Function in C
A user-defined function is a type of function in C language that is defined by the user himself to perform some specific task. It provides code reusability and modularity to our program. User-defined functions are different from built-in functions as their working is specified by the user and no hea
6 min read
Parameter Passing Techniques in C
In C, there are different ways in which parameter data can be passed into and out of methods and functions. Let us assume that a function B() is called from another function A(). In this case, A is called the "caller function" and B is called the "called function or callee function". Also, the argum
5 min read
Function Prototype in C
In C, a function prototype is a statement that tells the compiler about the functionâs name, its return type, numbers, and data types of its parameters. Using this information, the compiler cross-checks function parameters and their data type with function definition and function call. For example,
6 min read
How can I return multiple values from a function?
In C programming, a function can return only one value directly. However, C also provides several indirect methods in to return multiple values from a function. In this article, we will learn the different ways to return multiple values from a function in C. The most straightforward method to return
3 min read
main Function in C
The main function is the entry point of a C program. It is a user-defined function where the execution of a program starts. Every C program must contain, and its return value typically indicates the success or failure of the program. In this article, we will learn more about the main function in C.
5 min read
Implicit Return Type int in C
In C, every function has a return type that indicates the type of value it will return, and it is defined at the time of function declaration or definition. But in C language, it is possible to define functions without mentioning the return type and by default, int is implicitly assumed that the ret
2 min read
Callbacks in C
A callback is any executable code that is passed as an argument to another code, which is expected to call back (execute) the argument at a given time. In simple terms, a callback is the process of passing a function (executable code) to anther function as argument and then it is called by the passe
4 min read
Nested functions in C
Some programmer thinks that defining a function inside an another function is known as "nested function". But the reality is that it is not a nested function, it is treated as lexical scoping. Lexical scoping is not valid in C because the compiler cant reach/find the correct memory location of the i
2 min read
Variadic Functions in C
In C, variadic functions are functions that can take a variable number of arguments. This feature is useful when the number of arguments for a function is unknown. It takes one fixed argument and then any number of arguments can be passed. Let's take a look at an example: [GFGTABS] C #include <st
5 min read
_Noreturn function specifier in C
In C, the _Noreturn specifier is used to indicate that a function does not return a value. It tells the compiler that the function will either exit the program or enter an infinite loop, so it will never return control to the calling function. This helps the compiler to optimize code and issue warni
2 min read
Predefined Identifier __func__ in C
Before we start discussing __func__, let us write some code snippets and anticipate the output: C/C++ Code // C program to demonstrate working of a // Predefined Identifier __func__ #include <stdio.h> int main() { // %s indicates that the program will read strings printf("%s", __func
2 min read
C Library math.h Functions
The math.h header defines various C mathematical functions and one macro. All the functions available in this library take double as an argument and return double as the result. Let us discuss some important C math functions one by one. C Math Functions1. double ceil (double x) The C library functio
6 min read
C Arrays & Strings
C Arrays
Array in C is one of the most used data structures in C programming. It is a simple and fast way of storing multiple values under a single name. In this article, we will study the different aspects of array in C language such as array declaration, definition, initialization, types of arrays, array s
15+ min read
Properties of Array in C
An array in C is a fixed-size homogeneous collection of elements stored at a contiguous memory location. It is a derived data type in C that can store elements of different data types such as int, char, struct, etc. It is one of the most popular data types widely used by programmers to solve differe
8 min read
Multidimensional Arrays in C - 2D and 3D Arrays
Prerequisite: Arrays in C A multi-dimensional array can be defined as an array that has more than one dimension. Having more than one dimension means that it can grow in multiple directions. Some popular multidimensional arrays are 2D arrays and 3D arrays. In this article, we will learn about multid
10 min read
Initialization of Multidimensional Array in C
In C, multidimensional arrays are the arrays that contain more than one dimensions. These arrays are useful when we need to store data in a table or matrix-like structure. In this article, we will learn the different methods to initialize a multidimensional array in C. The easiest method for initial
4 min read
Pass Array to Functions in C
Passing an array to a function allows the function to directly access and modify the original array. In this article, we will learn how to pass arrays to functions in C. In C, arrays are always passed to function as pointers. They cannot be passed by value because of the array decay due to which, wh
3 min read
How to pass a 2D array as a parameter in C?
A 2D array is essentially an array of arrays, where each element of the main array holds another array. In this article, we will see how to pass a 2D array to a function. The simplest and most common method to pass 2D array to a function is by specifying the parameter as 2D array with row size and c
3 min read
What are the data types for which it is not possible to create an array?
In C, an array is a collection of variables of the same data type, stored in contiguous memory locations. Arrays can store data of primitive types like integers, characters, and floats, as well as user-defined types like structures. However, there are certain data types for which arrays cannot be di
2 min read
How to pass an array by value in C ?
In C programming, arrays are always passed as pointers to the function. There are no direct ways to pass the array by value. However, there is trick that allows you to simulate the passing of array by value by enclosing it inside a structure and then passing that structure by value. This will also p
2 min read
Strings in C
A String in C programming is a sequence of characters terminated with a null character '\0'. The C String is stored as an array of characters. The difference between a character array and a C string is that the string in C is terminated with a unique character '\0'. C String Declaration SyntaxDeclar
8 min read
Array of Strings in C
In C, an array of strings is a 2D array where each row contains a sequence of characters terminated by a '\0' NULL character (strings). It is used to store multiple strings in a single array. Let's take a look at an example: [GFGTABS] C #include <stdio.h> int main() { // Creating array of stri
3 min read
What is the difference between single quoted and double quoted declaration of char array?
In C programming, the way we declare and initialize a char array can differ based on whether we want to use a sequence of characters and strings. They are basically same with difference only of a '\0' NULL character. Double quotes automatically include the null terminator, making the array a string
2 min read
C String Functions
The C string functions are built-in functions that can be used for various operations and manipulations on strings. These string functions make it easier to perform tasks such as string copy, concatenation, comparison, length, etc. The <string.h> header file contains these string functions. Th
6 min read
C Pointers
C Pointers
A pointer is a variable that stores the memory address of another variable. Instead of holding a direct value, it holds the address where the value is stored in memory. There are 2 important operators that we will use in pointers concepts i.e. Dereferencing operator(*) used to declare pointer variab
10 min read
Pointer Arithmetics in C with Examples
Pointer Arithmetic is the set of valid arithmetic operations that can be performed on pointers. The pointer variables store the memory address of another variable. It doesn't store any value. Hence, there are only a few operations that are allowed to perform on Pointers in C language. The C pointer
10 min read
C - Pointer to Pointer (Double Pointer)
In C, double pointers are those pointers which stores the address of another pointer. The first pointer is used to store the address of the variable, and the second pointer is used to store the address of the first pointer. That is why they are also known as a pointer to pointer. Let's take a look a
5 min read
Function Pointer in C
In C, a function pointer is a type of pointer that stores the address of a function, allowing functions to be passed as arguments and invoked dynamically. It is useful in techniques such as callback functions, event-driven programs, and polymorphism (a concept where a function or operator behaves di
6 min read
How to Declare a Pointer to a Function?
A pointer to a function is similar to a pointer to a variable. However, instead of pointing to a variable, it points to the address of a function. This allows the function to be called indirectly, which is useful in situations like callback functions or event-driven programming. In this article, we
2 min read
Pointer to an Array | Array Pointer
A pointer to an array is a pointer that points to the whole array instead of the first element of the array. It considers the whole array as a single unit instead of it being a collection of given elements. Consider the following example: [GFGTABS] C #include<stdio.h> int main() { int arr[5] =
5 min read
Difference between constant pointer, pointers to constant, and constant pointers to constants
In this article, we will discuss the differences between constant pointer, pointers to constant & constant pointers to constants. Pointers are the variables that hold the address of some other variables, constants, or functions. There are several ways to qualify pointers using const. Pointers to
3 min read
Pointer vs Array in C
Most of the time, pointer and array accesses can be treated as acting the same, the major exceptions being: 1. the sizeof operator sizeof(array) returns the amount of memory used by all elements in the array sizeof(pointer) only returns the amount of memory used by the pointer variable itself 2. the
1 min read
Dangling, Void , Null and Wild Pointers in C
In C programming pointers are used to manipulate memory addresses, to store the address of some variable or memory location. But certain situations and characteristics related to pointers become challenging in terms of memory safety and program behavior these include Dangling (when pointing to deall
6 min read
Near, Far and Huge Pointers in C
In older times, the intel processors had 16-bit registers, but the address bus was 20-bits wide. Due to this, CPU registers were not able to hold the entire address at once. As a solution, the memory was divided into segments of 64 kB size, and the near pointers, far pointers, and huge pointers were
4 min read
restrict Keyword in C
The restrict keyword is a type qualifier that was introduced in the C99 standard. It is used to tell the compiler that a pointer is the only reference or access point to the memory it points to, allowing the compiler to make optimizations based on that information. Let's take a look at an example: [
3 min read