C Overview and Basic Syntax

    3 Votes


To learn any new programming language, for example C, C++, Java, etc., most commonly used approach is to write a basic HelloWorld program first and then learn the concepts from that. Let's follow the same approach here and first write a C program and learn about basic syntax of C programming.


HelloWorld in C:

#include<stdio.h>
int main()
{
    printf("Hello world!");
    return 0;
}

How to Run the HelloWorld C Program:

To compile and run the above C program, you've two options
  • You can either set up a local environment in your computer as explained here.
  • Or you can make use of online services like IdeOne. For learning purposes, this is the better option.
The above program will print 'Hello world!' as output.

Let's analyze the program line by line.
  • The first line is called a pre-processor directive.
    In this program, pre-processor directive '#include' informs the compiler to include the header file 'stdio.h' to the HelloWorld program. By doing so, the standard C functions like 'printf' are included in the program.
  • Next line, 'int main()' defines the main function, which will be invoked by the runtime when you run the program.
  • The lines written inside '{...}' is called the definition of main function.
  • Next line 'printf("Hello world!");' invokes a funtion 'printf' defined inside the header file 'stdio.h'. This prints the string 'Hello world!' in the output.
  • Next line 'return 0;' stop execution of the main function and return value '0' to the runtime.
  • Note that each statement in your program has to be terminated with a semicolon(;). This marks the end of the statement.

Character Sets Supported by C Programs:

C uses alphabets (upper case 'A' to 'Z' or lower case 'a' to 'z'), digits ('0' to '9') and some certain special characters shown in the figure below to construct basic elements of C program (constants, variables, expressions, operators etc.)
C Character Set
C Character Set

Identifiers and Keywords

Identifiers are the names by which we can distinguish different program elements (such as variables, constants, functions). The identifiers consist of letters (uppercase or lowercase) and digits in any possible combination as long as it follows one rule of identifiers.

The rule states that every identifier must have a letter as its first character; no digits are allowed in the first character. So, while “first” is a valid identifier, “1st” is not. Also, no identifier must contain any special character except “_” (underscore). Given below, a list of examples of valid and invalid identifiers with a reason why they are invalid.

1'st Invalid (First character must be letter )
first Valid
total_area Valid
phone-no Invalid ( ‘-‘ not allowed )
national flag Invalid ( blank space not allowed )
f1car Valid

Also Read - Top C Interview Questions and Answers for Freshers & Advanced C Interview Questions and Answers

There is another restriction for identifiers. Some reserved words can't be used as identifiers. These reserved words are predefined by C language and have predefined meanings and can be used for their intended purposes. They are called keywords. The list of keywords is given 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      

Also Read - Top Embedded C Interview Questions and Answers & C++ Interview Questions and Answers


C Keywords

All keywords are lowercase. So if you have an urge to use these keywords as variables, you can change any letter in the keywords to its uppercase character. But not using them as variable name is considered better programming practice.

Related Articles

Data Types in C Language
C Qualifiers - Constant and Volatile type Qualifier
C Type Casting with examples - float to int / int to char / int to long
C Constants and Literals
C Operators - Arithmetic, Assignment, Relational and Logical
C Operators - Bitwise Operators and Conditional Operators
C Operator Precedence and Associativity Table with Examples
C Input and Output Statements
C Format Specifiers for printf and scanf
C Decision Making Control Structures - 'if', 'if - else', nested 'if', 'switch'
Loops in C - for, while and do...while Loops
C 'break', 'continue' and 'goto' Statements to Control Program Flow
C Preprocessor Directives and Macros
C Functions Tutorial with Examples
C Arrays - Dynamic, 2d and Multidimensional with Examples
C Strings - Internal Working and Functions with example
C Pointers basics explained with examples
Structures in C Programming with Example
Unions in c programming with example
C Storage Class Specifiers - auto, register, static and extern

Popular Videos

How to speak to people

How to speak so that people want to listen.

Got a tip or Question?
Let us know