C-Program
Syntax of c
Last Updated on: 22nd Jan 2025 23:31:07 PM
Basic Syntax of C:
Key Components:
-
Header Files: These files contain pre-written code for common tasks. Think of them as libraries. For example,
<stdio.h>
(Standard Input/Output) provides functions likeprintf()
(for printing to the console) andscanf()
(for reading input). You include them using#include <filename.h>
. -
Semicolon (;): In C, every statement (a complete instruction) must end with a semicolon. It's like a period at the end of a sentence in English. For example:
int x = 10; // This is a statement
printf("Hello\n"); // Another statement
-
Case Sensitivity: C is case-sensitive. This means
myVariable
,MyVariable
, andmyvariable
are treated as three different variables. This is a crucial point to remember to avoid errors. -
Comments: Comments are used to explain your code. The compiler ignores them. They are essential for making your code readable and understandable, both for yourself and others.
- Single-line comments: Use
//
to comment out a single line. Everything after//
on that line is ignored. -
int age = 30; // This line declares an integer variable named age
- Multi-line comments: Use
/*
to begin a multi-line comment and*/
to end it. Everything between these markers is ignored, even if it spans multiple lines. -
/* This is a multi-line comment. */
Structure of a C Program :
-
#include <stdio.h> // Includes standard library for input/output int main() { // Entry point of the program // Code goes here return 0; // Indicates successful execution }
Example Program: Displaying Variables
-
#include <stdio.h> int main() { int age = 25; // Declaring an integer variable float salary = 50000.5; // Declaring a float variable printf("Age: %d\n", age); // %d is used for integers printf("Salary: %.2f\n", salary); // %.2f is used for floating-point numbers return 0; }