C Basic Syntax - GeeksforGeeks (2024)

Last Updated : 16 Jun, 2023

Comments

Improve

C is a procedural programming language. It was developed by Dennis Ritchie at the Bell Laboratories in the year 1972. Despite being old C is a very popular language among programmers. It is a very fast language compared to other languages like Python, Java, etc.

Below is the basic syntax structure of the C program:

C Basic Syntax - GeeksforGeeks (1)

The basic syntax of the C program consists of the header, main() function, variable declaration, body, and return type of the program.

  • The header is the first line in the C program with extension .h which contains macro definitions and C functions.
  • Programs must contain the main() function because execution in C programming starts from the main().
  • Variable declaration in C is done inside the main function and can be used in the body anywhere but before the main, we can also declare variables which are known as Global variables.
  • In the body of the function, we perform operations required inside the function like printing, sum, average, sorting, searching, etc.
  • The last part of the C program is the return statement which refers to returning values of the program. If the return type is void then there will be no return statement.

C

// Basic Syntax of C Program

#include <stdio.h>

// main function

int main()

{

// body

printf("Hi! This is a basic C program.");

// return statement

return 0;

}

Output

Hi! This is a basic C program.

Tokens

A token in C programming is either a keyword, an identifier, a constant, a string literal, or a symbol. Let’s get a good understanding of tokens through a print statement in C language.

printf("GeeksforGeeks\n");

Individual tokens from the above syntax are:

printf("GeeksforGeeks\n");

Semicolons

In C programming Semicolon is used to show the termination of Instruction. It is also called a statement terminator since every single statement should be ended with a semicolon. Semicolons are used to end statements in C.

The Semicolon tells the compiler that the current statement has been terminated. If any statement in the Program ends without a semicolon, then the program will not compile and will generate an error message.

C

// C program to demonstrate use of Semicolon

#include <stdio.h>

int main() { printf("GeeksforGeeks") return 0; }

Error

./253df686-61b0-495f-98fe-46dfeb318172.c: In function 'main':./253df686-61b0-495f-98fe-46dfeb318172.c:7:5: error: expected ';' before 'return' return 0; ^

Preprocessor Directives

In C language, a program should begin with preprocessor directives since they contain multiple files that contain specific functions. Preprocessors in C are used to process our source code before compilation.

There are 4 main types of Preprocessor directives in C:

  1. Macros
  2. File inclusion
  3. Conditional Compilation
  4. Other directives

While executing a program in C multiple steps are involved as mentioned below:

C Basic Syntax - GeeksforGeeks (2)

Identifiers in C

In C programming, the identifier is used to identify a variable, function, or any other user-defined data type. C programming language does not allow special characters such as $, @, or % within the identifier.

C is a case-sensitive programming language which means that “geeksforgeeks” and “Geeksforgeeks” are treated as two different identifiers in C. Identifiers should begin with a letter Uppercase letters (A to Z), lowercase letters (a-z), digits (0-9) or underscores ( _ ).

Some examples of identifiers in C are:

geeksGeekgeek12_geek
g_f_gG_f_2_geek89geeksforgeeks
GEEKSFORGEEKSGEEKS_for_9081gfg_69g23gf9

C Keywords

In the C programming language, keywords are reserved words that have special meanings. These reserved words can’t be used as variables or constants or any other identifier name. C contains a total of 32 keywords that are reserved and have special meanings.

Keywords in C programming are mentioned below:

intlongshortsigned
unsignedvoidcharbreak
registerstructureclassvolatile
whileforswitchtypedef
unionstaticautoreturn
caseconstcontinuedefault
dodoubleelseif
enumexternfloatgoto

Comments in C

In C programming Comments are used to make any program more readable and understandable. Comments are generally used by programmers to add explanations or descriptive text in the code without any interference from the compiler or the programming structure.

Comments are not programming statements and they are ignored by the compiler. We can’t have comments inside comments since they will interfere within themselves.

In C language there are two types of programming comments:

  • Single-line Comments: Single-line comments begin with // and they are only used for a single line and end in the next line.
  • Multiline Comments: Multi-line comments start with /* and end with */. They can be a single line or multiline.

Example

C

// C Program to demonstrate the comments

#include <stdio.h>

int main()

{

// This is a single line comment

/*

This is a multiline comment

used in this program

*/

printf("GeeksforGeeks");

return 0;

}

Output

GeeksforGeeks

In the above C program, comments are ignored by the compiler and they can be read only by the programmer.

Whitespaces in C

In C programming lines containing white spaces, blank lines, and comments are ignored by the compiler. Whitespace in C is used to describe blanks, newline characters, comments, and tabs.

Whitespace is used to separate parts of a statement from another and it helps the compiler to distinguish the keywords, identifiers, and elements in a statement. It allows us to format our code in a way that makes it easier to understand by programmers and others. In C we are free to use the whitespaces to increase user readability.

Example of Whitespace in C:

int a;// whitespace used to increase readablity and to distinguish elements.string s = "GeeksforGeeks";

Functions

In C programming Function is a set of statements that performs specific computations. Functions help us to reduce our code redundancy. Instead of writing multiple lines of code again and again we use functions to reduce code redundancy.

Functions in C provide abstractions. In C “main” is also a function that has its own return type defined in the program. This function serves as the starting point for program execution.

Syntax

return_type function_name(parameter_list) { // Function body (code goes here)}

Example

int mul(int Full_marks,int Full_marks2);

Below is the representation of the above C function:

C Basic Syntax - GeeksforGeeks (3)



`; tags.map((tag)=>{ let tag_url = `videos/${getTermType(tag['term_id__term_type'])}/${tag['term_id__slug']}/`; tagContent+=``+ tag['term_id__term_name'] +``; }); tagContent+=`
C Basic Syntax - GeeksforGeeks (2024)

FAQs

What is the basic syntax of C? ›

The basic syntax of the C program consists of the header, main() function, variable declaration, body, and return type of the program. The header is the first line in the C program with extension . h which contains macro definitions and C functions.

What is basic syntax? ›

Syntax is the set of rules that define what the various combinations of symbols mean. This tells the computer how to read the code. Syntax refers to a concept in writing code dealing with a very specific set of words and a very specific order to those words when we give the computer instructions.

Can I learn C in a week? ›

Learning C programming within one week can be challenging, but it is possible to get a basic understanding of the language and its concepts in that time frame. Here are a few steps you can take to get started: Start with the basics: Learn about data types, variables, operators, control structures, and functions.

Is C syntax hard? ›

Simple language to grasp. Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

How to remember syntax in C? ›

Let's explore some effective strategies for memorizing code:
  1. Understand the Concepts. ...
  2. Break Down Code into Logical Chunks. ...
  3. Repetition and Practice. ...
  4. Create Mnemonic Devices. ...
  5. Utilize Visualization Techniques. ...
  6. Apply the Memorized Code. ...
  7. Leverage Code Documentation and Comments. ...
  8. Collaborate and Discuss with Peers.
Jun 14, 2024

Which are the 32 keywords in C? ›

There are a total of 32 keywords in the language of C:
autobreakchar
doubleelseextern
intlongreturn
structswitchunion
Jun 14, 2024

How to write syntax in C? ›

The main syntactic elements are:
  1. C's only reserved word in this program: int.
  2. Function names: main, printf.
  3. Delimiters for the function main.
  4. An end-of-statement symbol, the semicolon.
  5. Parentheses for enclosing method parameters (arguments).
  6. A string literal, Hello World!.
  7. Braces that delimit the body of main.

What is syntax for dummies? ›

Syntax in English is the arrangement of words and phrases in a specific order. If you change the position of even one word, it's possible to change the meaning of the entire sentence.

How to learn C perfectly? ›

The best way to learn C programming is to start by reading C programming books or tutorials online. Go through beginner tutorials to grasp the basics like data types, loops, functions, and arrays. Make sure to code alongside the tutorials and get hands-on practice with simple programs.

Why is C so easy to learn? ›

C program syntax is easy to learn and read; this makes debugging code more accessible and faster. C programs are relatively short compared to other languages, which reduces the time needed to complete them. C is a powerful programming language that enables developers to create sophisticated software systems.

Is C harder than Python? ›

Python is easier than C to learn. But C helps to learn the fundamentals of programming while Python focuses on doing the job. Because Python is made in C doesn't mean you need to learn it. It is supposed to be an opposite and make a fast learning environment, unlike C.

Is C or Java harder? ›

It's a general consensus that Java is easier to learn because its syntax is closer to natural language than C.

Which is easy C or SQL? ›

I agree with all of the other answers here that C is the harder language to learn, however, this is a fairly simplistic question. The language of SQL can be and usually is used in C and every other programming language on the planet. So SQL is a language that most programmers should know the basics of.

What is syntax structure in C? ›

Syntax to Define a Structure in C

Similar to a union, a structure also starts with a keyword. structName: This is the name of the structure which is specified after the keyword struct. data_Type: The data type indicates the type of the data members of the structure.

What is the syntax of function in C? ›

A function declaration usually contains the function name , return type , and the parameter types. The following is the syntax for defining a function in C: return_type function_name(parameter_list); Here, return_type is the data type of the value that the function returns.

What is the basic structure of the code in C? ›

To conclude, the basic structure of C program can be divided into six sections, namely - Documentation, Link, Definition, Global Declaration, Main() Function, and Subprograms. The main() function is compulsory to include in every C program, whereas the rest are optional.

What is the basic syntax of string in C? ›

C String Declaration Syntax

Below is the basic syntax for declaring a string. char string_name[size]; In the above syntax string_name is any name given to the string variable and size is used to define the length of the string, i.e the number of characters strings will store.

Top Articles
Financial planning basics: How to create a financial plan
Financial Planning: A Step-by-Step Guide - NerdWallet
Funny Roblox Id Codes 2023
Golden Abyss - Chapter 5 - Lunar_Angel
Www.paystubportal.com/7-11 Login
Joi Databas
DPhil Research - List of thesis titles
Shs Games 1V1 Lol
Evil Dead Rise Showtimes Near Massena Movieplex
Steamy Afternoon With Handsome Fernando
Which aspects are important in sales |#1 Prospection
Detroit Lions 50 50
18443168434
Newgate Honda
Zürich Stadion Letzigrund detailed interactive seating plan with seat & row numbers | Sitzplan Saalplan with Sitzplatz & Reihen Nummerierung
Grace Caroline Deepfake
978-0137606801
Nwi Arrests Lake County
Justified Official Series Trailer
London Ups Store
Committees Of Correspondence | Encyclopedia.com
Pizza Hut In Dinuba
Jinx Chapter 24: Release Date, Spoilers & Where To Read - OtakuKart
How Much You Should Be Tipping For Beauty Services - American Beauty Institute
Free Online Games on CrazyGames | Play Now!
Mccain Agportal
Amih Stocktwits
Fort Mccoy Fire Map
Uta Kinesiology Advising
Kcwi Tv Schedule
What Time Does Walmart Auto Center Open
Nesb Routing Number
Olivia Maeday
Random Bibleizer
10 Best Places to Go and Things to Know for a Trip to the Hickory M...
Black Lion Backpack And Glider Voucher
Gopher Carts Pensacola Beach
Duke University Transcript Request
Lincoln Financial Field, section 110, row 4, home of Philadelphia Eagles, Temple Owls, page 1
Jambus - Definition, Beispiele, Merkmale, Wirkung
Ark Unlock All Skins Command
Craigslist Red Wing Mn
D3 Boards
Jail View Sumter
Nancy Pazelt Obituary
Birmingham City Schools Clever Login
Thotsbook Com
Funkin' on the Heights
Vci Classified Paducah
Www Pig11 Net
Ty Glass Sentenced
Latest Posts
Article information

Author: Rob Wisoky

Last Updated:

Views: 6611

Rating: 4.8 / 5 (68 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Rob Wisoky

Birthday: 1994-09-30

Address: 5789 Michel Vista, West Domenic, OR 80464-9452

Phone: +97313824072371

Job: Education Orchestrator

Hobby: Lockpicking, Crocheting, Baton twirling, Video gaming, Jogging, Whittling, Model building

Introduction: My name is Rob Wisoky, I am a smiling, helpful, encouraging, zealous, energetic, faithful, fantastic person who loves writing and wants to share my knowledge and understanding with you.