Starter Tutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.
Home » Programming » C Programming » C Programming Keywords List and their Functions
Suryateja Pericherla Categories: C Programming. 2 Comments on C Programming Keywords List and their Functions
C Programming Keywords List
Join Our Newsletter - Tips, Contests and Other Updates
Email
Name

Introduction to Keywords in C

Keywords in C are reserved words that have a specific meaning and purpose within the programming language. They are predefined by the C compiler and cannot be used for variables, functions, or identifiers.

 

Understanding these keywords is essential for writing syntactically correct C programs. They form the foundation of C syntax and help define the structure and behavior of the code.

 

List of Keywords in C

Keywords are reserved words that form the foundation of the language’s syntax and structure. Understanding these keywords is essential for writing correct and efficient C programs.

 

The common keywords in C are:

  • alignas – Type specifier used to set the memory alignment of a variable. (added in C23)
  • alignof – An operator that returns the alignment requirement (in bytes) of a specific type. (added in C23)
  • auto – Storage class specifier indicating automatic storage duration.
  • break – Terminates the nearest enclosing loop or switch statement.
  • bool – Data type representing boolean values. (added in C23)
  • case – Defines a branch in a switch statement.
  • char – Data type representing a single character.
  • const – Declares a variable as constant, preventing modification.
  • constexpr – Allows variables to be defined as constant expressions. (added in C23)
  • continue – Skips the remaining code in the current iteration of a loop and proceeds to the next iteration.
  • default – Specifies the default case in a switch statement.
  • do – Starts a do-while loop.
  • double – Data type for double-precision floating-point numbers.
  • else – Specifies the block of code to execute if the if condition is false.
  • enum – Declares an enumeration type.
  • extern – Specifies external linkage for variables or functions.
  • false – Boolean false value. (added in C23)
  • float – Data type for single-precision floating-point numbers.
  • for – Starts a for loop.
  • goto – Transfers control to another part of the program.
  • if – Executes a block of code if a specified condition is true.
  • inline – Suggests to the compiler to inline the function. (added in C99)
  • int – Data type for integer values.
  • long – Data type for long integer values.
  • nullptr – A dedicated type-safe null pointer constant. (added in C23)
  • register – Indicates a variable should be stored in a register for quick access.
  • return – Exits a function and optionally returns a value.
  • restrict – Hints to the compiler that the pointer is the sole means of accessing the object it points to. (added in C99)
  • short – Data type for short integer values.
  • signed – Specifies a signed data type (default for int).
  • sizeof – Operator to determine the size of a data type or variable.
  • static – Declares a variable with static storage duration.
  • static_assert – Compile-time assertions. (added in C23)
  • struct – Defines a structure data type.
  • switch – Executes different code based on the value of an expression.
  • thread_local – A storage class specifier used to define variables where each thread has its own private instance of the data. (added in C23)
  • typedef – Creates a new name (alias) for a data type.
  • typeof – Used to deduce the type of an expression at compile time. (added in C23)
  • typeof_unqual – Used to deduce the type of an expression at compile time. (added in C23)
  • true – Boolean true value. (added in C23)
  • union – Defines a union data type.
  • unsigned – Specifies an unsigned data type.
  • void – Indicates that a function does not return a value or a pointer to no type.
  • volatile – Indicates that a variable may change unexpectedly.
  • while – Starts a while loop.
  • _Alignas – Type specifier used to set the memory alignment of a variable. (added in C11)
  • _Alignof – An operator that returns the alignment requirement (in bytes) of a specific type. (added in C11)
  • _Atomic – To define atomic objects, ensuring thread-safe operations. (added in C11)
  • _BitInt – Supports bit-precise integer types of a specific width. (added in C23)
  • _Bool – Boolean data type. (added in C99)
  • _Complex – Complex number type. (added in C99)
  • _Decimal32 – Decimal floating-point arithmetic. (added in C23)
  • _Decimal64 – Decimal floating-point arithmetic. (added in C23)
  • _Decimal128 – Decimal floating-point arithmetic. (added in C23)
  • _Generic – Enables type-generic programming. (added in C11)
  • _Imaginary – Imaginary number type. (added in C99)
  • _Noreturn – To declare that a function will never return to its caller. (added in C11)
  • _Static_assert – Provides compile-time assertions. (added in C11)
  • _Thread_local – A storage class specifier used to define variables where each thread has its own private instance of the data. (added in C11)

 

These keywords are integral to writing syntactically correct and meaningful C programs. Familiarity with them helps programmers understand the structure, control flow, and data management within C language applications.

 

Data Type Keywords in C

In the C programming language, data types are essential as they define the type of data that a variable can hold. The keywords associated with data types are reserved identifiers that specify the nature and size of data stored in variables. Understanding these keywords is fundamental for efficient programming in C.

 

Basic Data Type Keywords

  • int: Used to declare integer variables that store whole numbers, typically 4 bytes on most systems.
  • float: Declares variables that hold single-precision floating-point numbers, suitable for decimal values.
  • double: Used for double-precision floating-point numbers, offering more precision than float.
  • char: Represents character data, typically 1 byte, and stores individual characters.

 

Modifiers for Data Types

Modifiers can be added to basic data types to alter their size and range:

  • signed: Specifies that the number can be positive, negative, or zero (default for int).
  • unsigned: Specifies that only non-negative numbers are stored, effectively doubling the maximum value.
  • short: Smaller integer type, often 2 bytes.
  • long: Larger integer type, commonly 8 bytes on modern systems.

 

Derived Data Types Keywords

  • void: Represents the absence of a data type. Used for functions that do not return a value and for generic pointers.

 

Other Data Type Keywords

  • bool: Represents boolean values (true or false). Available when stdbool.h is included.
  • size_t: An unsigned integer type used for sizes and counts, defined in stddef.h.

 

Mastering the data type keywords in C allows developers to write clear, efficient, and type-safe code. Proper use of these keywords ensures correct memory allocation and data manipulation, forming the backbone of robust C programming.

 

Control Keywords in C

Control keywords in C are essential for managing the flow of a program. They help in making decisions, repeating actions, and controlling the execution path of programs. Understanding these keywords is fundamental for writing efficient and logical C programs.

 

Different control keywords in C are:

  • if
  • else
  • switch
  • case
  • default
  • while
  • do
  • for
  • break
  • continue
  • return
  • goto

 

Conditional Statements

The if statement is used to execute a block of code based on a condition:

if(condition)
{
	// code to execute if condition is true 
}

 

Optionally, the else branch can be added:

if(condition)
{    
	// code if true 
} 
else 
{    
	// code if false 
}

 

Switch Case for Multiple Conditions

The switch statement provides a way to execute different parts of code based on the value of a variable:

switch(variable) 
{    
	case value1:        
		// code        
		break;    
	case value2:        
		// code        
		break;    
	default:        
		// default code 
}

 

Loops in C

The primary looping constructs are:

  • while: Repeats as long as the condition is true.
  • do-while: Executes at least once, then repeats if the condition is true.
  • for: Repeats for a specified number of iterations.

 

The syntax of for loop is as shown below:

for(initialization; condition; increment) 
{    
	// code 
}

 

Flow Control Keywords

  • break: Exits from loops or switch statements.
  • continue: Skips the current iteration and proceeds to the next.
  • return: Exits from the current function and optionally returns a value.
  • goto: Transfers control to a labeled statement within the same function, generally discouraged for structured programming.

 

Mastering control keywords in C enables programmers to write logical, efficient, and maintainable code. Using these keywords properly allows for complex decision-making and repetitive tasks, forming the backbone of procedural programming in C.

 

Storage Class Specifier Keywords in C

In the C programming language, storage class specifiers are used to define the scope, visibility, and lifetime of variables and functions. They provide the compiler with information about how variables should be stored and accessed during program execution. The primary storage class specifier keywords in C are auto, register, static, and extern.

 

Auto Storage Class

The auto keyword indicates that a variable has automatic storage duration, meaning it is created when the block is entered and destroyed when it is exited. It is the default storage class for local variables within functions. Explicitly using auto is optional because variables declared inside functions are automatically considered auto unless specified otherwise.

 

In C23, the meaning of auto was updated to allow for type inference in object definitions, similar to C++.

 

Register Storage Class

The register keyword hints to the compiler that the variable will be heavily used, suggesting it should be stored in a CPU register for faster access. However, modern compilers often ignore this hint. Variables declared with register must be of types that can fit into a register, and they cannot be used with the address-of operator (&).

 

Static Storage Class

The static keyword has different effects based on its context:

  • Within functions, a static variable preserves its value between function calls and is initialized only once.
  • Outside functions, a static variable or function has internal linkage, limiting its scope to the current translation unit.

 

Extern Storage Class

The extern keyword is used to declare a variable or function that is defined elsewhere, typically in another source file. It extends the visibility of variables across multiple files, enabling variable sharing in a program.

 

Structural Keywords in C

Structural keywords in C are fundamental to writing well-organized and efficient code. They help in defining data types, controlling execution flow, and managing program structure. Some of the most important structural keywords include:

  • struct: Used to define a composite data type that groups variables of different types under a single name.
  • if: Introduces conditional statements to perform different actions based on boolean expressions.
  • else: Provides an alternative action when the condition in an if statement evaluates to false.
  • switch: Implements multi-way branching based on the value of an expression.
  • case: Defines individual branches within a switch statement.
  • default: Specifies the action when none of the case conditions are met in a switch statement.
  • while: Implements repetitive execution of a block of statements as long as a condition is true.
  • do: Executes a statement or block at least once, then repeats as long as the condition holds true.
  • for: Facilitates looping with initialization, condition, and increment/decrement in a compact form.
  • break: Exits the nearest enclosing loop or switch statement.
  • continue: Skips the current iteration of a loop and proceeds to the next cycle.
  • return: Exits from a function and optionally returns a value to the caller.
  • typedef: Creates new data type names (aliases) for existing types.
  • enum: Defines an enumeration, a user-defined type consisting of named integer constants.

 

Role of Structural Keywords

These keywords form the backbone of procedural programming in C. They enable developers to define complex data structures, implement decision-making processes, and control the flow of execution efficiently. Proper understanding and use of these keywords lead to clean, maintainable, and robust code.

 

Mastering the structural keywords in C is essential for writing effective programs. They provide the necessary tools to organize data, control program flow, and create logical structures, making C a powerful language for system and application development.

 

Return and Sizeof Keywords in C

The C programming language uses keywords to perform specific operations and define language structure. Among these, return and size (though ‘size’ is not a standalone keyword, ‘sizeof’ is a keyword/operator) are essential for function control flow and memory management respectively.

 

Return Keyword in C

The return keyword is used within a function to send a value back to the function caller. It terminates the execution of the function and specifies the value that should be returned.

  • It enhances the modularity and reusability of code by allowing functions to compute and provide values.
  • A function declared with a return type must include a return statement returning a value of that type.

 

Example to demonstrate the use of return keyword is given below:

int add(int a, int b) 
{    
	return a + b; // returns the sum 
} 
void displayMessage() 
{    
	printf("Hello, World!"); // No return needed for void functions 
}

 

Sizeof Keyword in C

C provides the sizeof operator to determine the size in bytes of variables, data types, or expressions. This operator is essential for dynamic memory allocation, buffer management, and portability across different systems.

 

Example:

int size_int = sizeof(int); 
int size_double = sizeof(double); 
int size_char = sizeof(char);

 

Using sizeof Effectively

The sizeof operator helps in writing portable code that adapts to different system architectures. For example, to allocate memory dynamically:

int *ptr = malloc(n * sizeof(int));

 

Here, sizeof(int) ensures the correct amount of memory is allocated regardless of platform differences.

 

In C, return is a vital keyword for function control flow, enabling functions to output values. The sizeof operator allows programmers to query the size of data types and variables, facilitating portable and safe memory operations. Mastery of these keywords and operators is fundamental for effective C programming.

 

Keywords Introduced in Newer C Versions

Programming in C has evolved significantly over the years, with newer standards introducing a variety of keywords to enhance functionality, safety, and efficiency. These updates aim to provide developers with more tools to write robust and portable code. Below are some of the key keywords introduced in the more recent versions of the C language.

 

_Bool and _Bool Keyword

The _Bool keyword was introduced in the C99 standard. It provides a boolean data type that can hold only two values: true or false. Since _Bool is a keyword, C programmers can declare boolean variables directly without relying on macros or other means.

_Bool flag = 1; // true 
if(flag) 
{    
	// do something 
}

 

_Complex and _Imaginary Keywords

To support complex number arithmetic, C99 introduced the _Complex and _Imaginary keywords. These allow developers to work seamlessly with complex numbers, facilitating scientific and engineering applications.

double _Complex z = 1.0 + 2.0 * I; 
double _Imaginary y = 3.0 * I;

 

_Atomic Keyword

Modified in C11, the _Atomic keyword enables atomic operations, which are crucial for writing thread-safe code. It allows variables to be accessed and modified atomically, preventing data races in concurrent programming.

_Atomic int shared_var = 0; 
shared_var++;

 

Static assertions with _Static_assert

The _Static_assert keyword, introduced in C11, allows compile-time assertions. This helps catch errors early by verifying assumptions during compilation.

_Static_assert(sizeof(int) >= 4, "int must be at least 4 bytes");

 

The addition of these keywords in newer C standards significantly enhances the language’s capabilities, making it more expressive, safe, and suitable for modern software development. Staying updated with these keywords and their proper usage is essential for writing efficient and portable C code.

 

Frequently Asked Questions

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

2 Comments

You can follow any responses to this entry through the RSS 2.0 feed.

I really like how you’ve highlighted the newer C keywords like `_Atomic` and `_Static_assert`. These are super important for modern C programming, especially when dealing with multi-threading and type safety. It’s great to see how C evolves over time!

This comprehensive list of C programming keywords is a great resource for both beginners and intermediate programmers. I especially appreciated the breakdown of storage class specifiers and how they affect variable scope and lifetime — it really helps solidify understanding of memory management in C. The inclusion of newer C versions’ keywords like _Static_assert was also a nice touch, showing how the language has evolved over time.

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
Pinterest
Youtube
Instagram
Blogarama - Blog Directory