diff --git a/pystencils/backends/cbackend.py b/pystencils/backends/cbackend.py
index 5560652513b817068a297ba6df39527280857601..ed6c3007571be4082d1f227081bea90610cd5ee5 100644
--- a/pystencils/backends/cbackend.py
+++ b/pystencils/backends/cbackend.py
@@ -26,7 +26,11 @@ __all__ = ['generate_c', 'CustomCodeNode', 'PrintNode', 'get_headers', 'CustomSy
 KERNCRAFT_NO_TERNARY_MODE = False
 
 
-def generate_c(ast_node: Node, signature_only: bool = False, dialect='c', custom_backend=None) -> str:
+def generate_c(ast_node: Node,
+               signature_only: bool = False,
+               dialect='c',
+               custom_backend=None,
+               with_globals=True) -> str:
     """Prints an abstract syntax tree node as C or CUDA code.
 
     This function does not need to distinguish between C, C++ or CUDA code, it just prints 'C-like' code as encoded
@@ -65,9 +69,10 @@ def generate_c(ast_node: Node, signature_only: bool = False, dialect='c', custom
         raise ValueError("Unknown dialect: " + str(dialect))
     code = printer(ast_node)
     if not signature_only and isinstance(ast_node, KernelFunction):
-        code = "\n" + code
-        for declaration in global_declarations:
-            code = printer(declaration) + "\n" + code
+        if with_globals and global_declarations:
+            code = "\n" + code
+            for declaration in global_declarations:
+                code = printer(declaration) + "\n" + code
 
     return code