Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

C Syntax

Here is a basic syntax for a C program:

#include <stdio.h> // header file
int main() {
   // main function
   printf("Hello, World!"); // output statement
   return 0; // exit status
}

Output:

Hello, World!

Here’s a breakdown of the syntax:

include – This is a preprocessor directive that includes the standard input/output library in the program.

int main() – This is the main function where the program execution starts.

{ and } – These curly braces enclose the statements that are executed as part of the main function.

printf(“Hello, World!”); – This is a function call that prints “Hello, World!” to the console.

return 0; – This statement exits the program with a status of 0, indicating successful execution.


C syntax include:

1. Structure of a C Program:

  • A C program consists of functions.
  • The main function, main(), is the entry point of the program.

2. Comments:

  • Comments are used to add explanatory text to the code.
  • Single-line comments start with //.
  • Multi-line comments start with /* and end with */.

3. Data Types:

  • C has built-in data types, such as int, float, double, char, etc.
  • You can define custom data types using struct, union, or enum.

4. Variables:

  • Variables are used to store data.
  • They need to be declared with a data type before use.
  • Syntax: ;

5. Constants:

  • Constants are fixed values that cannot be modified during program execution.
  • They can be defined using #define or const.
  • Syntax with #define: #define
  • Syntax with const: const = ;

6. Input and Output:

  • The printf() function is used for output.
  • Syntax: printf(“”, )
  • The scanf() function is used for input.
  • Syntax: scanf(“”, &)

7. Control Flow:

  • Conditional statements are used for decision making.

The if statement:

if (<condition>) {
    // code to execute if condition is true
} else {
    // code to execute if condition is false
}

The switch statement:

switch (<expression>) {
    case <value1>:
        // code to execute if expression matches value1
        break;
    case <value2>:
        // code to execute if expression matches value2
        break;
    default:
        // code to execute if expression does not match any value
}

8. Loops:

  • Loops are used for repetitive execution.

The while loop:

while (<condition>) {
    // code to execute repeatedly
}

The for loop:

for (<initialization>; <condition>; <increment>) {
    // code to execute repeatedly
}

The do-while loop:

do {
    // code to execute repeatedly
} while (<condition>);

9. Functions:

  • Functions are reusable blocks of code.
  • They have a return type, a name, optional parameters, and a body.
  • Syntax:<return_type> <function_name>(<parameters>) { <function_body> }

10. Arrays:

  • Arrays store multiple values of the same data type.
  • Syntax: <data_type> <array_name>[<size>];

11. Pointers:

  • Pointers store memory addresses.
  • They are declared using the * symbol.
  • Syntax: <data_type> *<pointer_name>;

Practice Problems on Syntax:

Problem 1:

Write a C program that calculates the factorial of a given number. The program should prompt the user to enter a positive integer and display its factorial.

#include <stdio.h>

int main() {
    int num, factorial = 1, i;
    
    printf("Enter a positive integer: ");
    scanf("%d", &num);
    
    for (i = 1; i <= num; i++) {
        factorial *= i;
    }
    
    printf("The factorial of %d is: %d\n", num, factorial);
    
    return 0;
}

Explanation: In this program, we declare three variables of type int: num, factorial, and i. The user is prompted to enter a positive integer using scanf(). A for loop is used to iterate from 1 to num and calculate the factorial by multiplying the current value of factorial with the loop variable i. Finally, the factorial is displayed using printf().

Output:

Enter a positive integer: 4
The factorial of 4 is: 24

Problem 2:

Write a C program that reads a character from the user and determines whether it is an uppercase letter, lowercase letter, or a digit.

#include <stdio.h>

int main() {
    char ch;
    
    printf("Enter a character: ");
    scanf(" %c", &ch);
    
    if (ch >= 'A' && ch <= 'Z') {
        printf("Uppercase letter\n");
    } else if (ch >= 'a' && ch <= 'z') {
        printf("Lowercase letter\n");
    } else if (ch >= '0' && ch <= '9') {
        printf("Digit\n");
    } else {
        printf("Other character\n");
    }
    
    return 0;
}

Explanation: In this program, we declare a variable ch of type char. The user is prompted to enter a character using scanf(). The program then uses multiple if statements to check the range of the character and determines whether it is an uppercase letter, lowercase letter, digit, or any other character. The appropriate message is displayed using printf().

Output:

Enter a character: j
Lowercase letter

Problem 3:

Write a C program that prints the Fibonacci series up to a given number. The program should prompt the user to enter a positive integer and display the Fibonacci series.

#include <stdio.h>
int main() {
    int num, prev = 0, current = 1, next;
    
    printf("Enter a positive integer: ");
    scanf("%d", &num);
    
    printf("Fibonacci series up to %d: %d, %d, ", num, prev, current);
    
    while (prev + current <= num) {
        next = prev + current;
        printf("%d, ", next);
        prev = current;
        current = next;
    }
    
    printf("\n");
    
    return 0;
}

Explanation: In this program, we declare variables of type int: num, prev, current, and next. The user is prompted to enter a positive integer using scanf(). The program prints the initial two numbers of the Fibonacci series, which are 0 and 1. Then, using a while loop, it calculates the next Fibonacci number by adding the previous two numbers and updates the variables accordingly. The loop continues until the next Fibonacci number is less than or equal to the given number. Finally, the Fibonacci series is displayed using printf().

Output:

Enter a positive integer: 5
Fibonacci series up to 5: 0, 1, 1, 2, 3, 5,